I've been asked for the code used to read the MAX6675 Thermocouple-to-Digital Converter used to measure the temperature in my ongoing SMD Reflow Oven project. The following is based on an Arduino (with a home built SPI helper library) but is fairly generic and should be applicable to most platforms.
| #define MAX6675_SELECT_PIN 8#define DESELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, HIGH)#define SELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, LOW)void setup(){ pinMode(MAX6675_SELECT_PIN, OUTPUT); DESELECT_MAX6675; setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK8); Serial.begin(19200);} |
| SELECT_MAX6675; delayMicroseconds(1); unsigned char highByte = send_spi(0); unsigned char lowByte = send_spi(0); DESELECT_MAX6675; |

| if (lowByte & (1<<2)) { Serial.print("Not connected "); Serial.print(highByte, HEX); Serial.print(" "); Serial.println(lowByte, HEX); } else { short value = (highByte << 5 | lowByte>>3); Serial.print(value/4); Serial.print("."); Serial.println(value%4 *25); } |
| #include <spi.h>// MAX6675 connected to pin D9#define MAX6675_SELECT_PIN 8#define DESELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, HIGH)#define SELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, LOW)void setup(){ pinMode(MAX6675_SELECT_PIN, OUTPUT); DESELECT_MAX6675; setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK8); Serial.begin(19200);}void loop(){ delay(500); // select the device, wait > 100nS, read two bytes, deselect SELECT_MAX6675; delayMicroseconds(1); unsigned char highByte = send_spi(0); unsigned char lowByte = send_spi(0); DESELECT_MAX6675; // if bit 3 is high thermocouple is unconnected if (lowByte & (1<<2)) { Serial.print("Not connected "); Serial.print(highByte, HEX); Serial.print(" "); Serial.println(lowByte, HEX); } else { // temperature value is in bits 6-0 of highByte and 7-2 of lowByte short value = (highByte << 5 | lowByte>>3); // 1 bit is 0.25 degree 'divide' by 4 to show degrees Serial.print(value/4); Serial.print("."); Serial.println(value%4 *25); }} |
| 欢迎光临 DIY编程器网 (http://www.diybcq.com/) | Powered by Discuz! X3.2 |