Created
May 12, 2016 15:16
-
-
Save geekman/bb0d9339fbdebf1b0d4d3379c10a3b68 to your computer and use it in GitHub Desktop.
MAX6675 thermocouple converter Arduino code sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MAX6675 thermocouple converter Arduino code sample | |
// | |
// 2016.05.12 darell tan | |
// | |
#include <SPI.h> | |
static const int CS_PIN = 13; | |
void setup() { | |
Serial.begin(115200); | |
SPI.begin(); | |
pinMode(CS_PIN, OUTPUT); | |
digitalWrite(CS_PIN, HIGH); | |
} | |
void loop() { | |
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)); | |
digitalWrite(CS_PIN, LOW); | |
// read 16 bits, MSB first | |
uint16_t data = 0; | |
data |= SPI.transfer(0) << 8; | |
data |= SPI.transfer(0) << 0; | |
SPI.endTransaction(); | |
digitalWrite(CS_PIN, HIGH); | |
// interpret value | |
if (data & 0x8000) { | |
Serial.println("sign not zero!"); | |
} else if (data & 0b100) { | |
Serial.println("not conn"); | |
} else { | |
uint16_t temp = (data & 0x7FF8) >> 3; | |
#if DEBUG | |
Serial.print("0x"); | |
Serial.print(data >> 8, HEX); | |
Serial.print(data & 0xFF, HEX); | |
Serial.print(" -- "); | |
#endif | |
// ADC is 12 bits (0-4095) | |
// temp is 0deg to 1023.75deg | |
// therefore each increment is 0.25 deg, or 1/4 deg | |
Serial.print(temp / 4); | |
Serial.print('.'); | |
Serial.print(25 * (temp % 4)); | |
Serial.println(); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment