Skip to content

Instantly share code, notes, and snippets.

@naikrovek
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naikrovek/d871f8f8fc3c0de8250c to your computer and use it in GitHub Desktop.
Save naikrovek/d871f8f8fc3c0de8250c to your computer and use it in GitHub Desktop.
Rewrite of the important parts of the Adafruit MAX31855 library for the Spark.io platform
// these methods should be fully "functional" in that they
// work, they have no known "side effects", and that they
// will return the same outputs given the same inputs (unless
// the temperature at the thermocouple changes, of course.)
// in setup() add these lines:
// SPI.setBitOrder(MSBFIRST);
// SPI.setDataMode(SPI_MODE0);
// SPI.begin();
// then use these methods in your application.
int32_t readThermocouple() {
// thermocouple data is returned as a 32-bit MSB number, like:
// 00000001101001000001101010100000
//
// 00000001101001 14-bit thermocouple data. 2's compliment.
// 0 reserved
// 0 fault bit, if set, one of the three bits at the end will be set.
// 000110101010 12-bit internal temp reading. 2's compliment.
// 0 reserved
// 0 thermocouple is shorted to VCC
// 0 thermocouple is shorted to GND
// 0 thermocouple is open circuit
// the 14-bit thermocouple temp reading is the temperature in C multiplied by 4.
// Divide it by 4.0 to get quarter degree C precision, or just bitshift right by
// two to get whole numbers.
// the 12-bit internal temperature of the amplifier is the temperature of the
// MAX31855 multiplied by 16. Divide by 16.0 to get 16th degree C precision, or
// bitshift right by 4 if you only care about whole degrees C.
int32_t d = 0;
digitalWrite(A2, LOW);
for (int bit = 8; bit; bit >>= 1) { //transfer 4 bytes, magically.
d <<= 8;
d |= SPI.transfer(0xFF);
}
digitalWrite(A2, HIGH);
return d;
}
boolean isError(int thermocoupleReading) {
return thermocoupleReading >> 16 & 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment