|
| 1 | +/* |
| 2 | + MAX6675.cpp - Library for reading temperature from a MAX6675. |
| 3 | +
|
| 4 | + This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. |
| 5 | + http://creativecommons.org/licenses/by-sa/3.0/ |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <MAX6675.h> |
| 9 | + |
| 10 | +MAX6675::MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units) |
| 11 | +{ |
| 12 | + pinMode(CS_pin, OUTPUT); |
| 13 | + pinMode(SO_pin, INPUT); |
| 14 | + pinMode(SCK_pin, OUTPUT); |
| 15 | + |
| 16 | + digitalWrite(CS_pin, HIGH); |
| 17 | + |
| 18 | + _CS_pin = CS_pin; |
| 19 | + _SO_pin = SO_pin; |
| 20 | + _SCK_pin = SCK_pin; |
| 21 | + _units = units; |
| 22 | +} |
| 23 | + |
| 24 | +float MAX6675::read_temp() |
| 25 | +{ |
| 26 | + uint16_t value = 0; |
| 27 | + uint8_t error_tc = 0; |
| 28 | + float temp = 0.0; |
| 29 | + |
| 30 | + /* |
| 31 | + Initiate a temperature conversion. According to MAX's tech notes FAQ's |
| 32 | + for the chip, Line going high initiates a conversion, which means, we |
| 33 | + need to clock the chip low to high to initiate the conversion, then wait |
| 34 | + for the conversion to be complete before trying to read the data from |
| 35 | + the chip. |
| 36 | + */ |
| 37 | + digitalWrite(_CS_pin,LOW); |
| 38 | + delay(2); |
| 39 | + digitalWrite(_CS_pin,HIGH); |
| 40 | + delay(220); |
| 41 | + |
| 42 | + /* Read the chip and return the raw temperature value */ |
| 43 | + |
| 44 | + /* |
| 45 | + Bring CS pin low to allow us to read the data from |
| 46 | + the conversion process |
| 47 | + */ |
| 48 | + digitalWrite(_CS_pin,LOW); |
| 49 | + |
| 50 | + /* Cycle the clock for dummy bit 15 */ |
| 51 | + digitalWrite(_SCK_pin,HIGH); |
| 52 | + delay(1); |
| 53 | + digitalWrite(_SCK_pin,LOW); |
| 54 | + |
| 55 | + /* |
| 56 | + Read bits 14-3 from MAX6675 for the Temp. Loop for each bit reading |
| 57 | + the value and storing the final value in 'temp' |
| 58 | + */ |
| 59 | + for (int i=11; i>=0; i--) { |
| 60 | + digitalWrite(_SCK_pin,HIGH); |
| 61 | + value += digitalRead(_SO_pin) << i; |
| 62 | + digitalWrite(_SCK_pin,LOW); |
| 63 | + } |
| 64 | + |
| 65 | + /* Read the TC Input inp to check for TC Errors */ |
| 66 | + digitalWrite(_SCK_pin,HIGH); |
| 67 | + error_tc = digitalRead(_SO_pin); |
| 68 | + digitalWrite(_SCK_pin,LOW); |
| 69 | + |
| 70 | + /* |
| 71 | + Read the last two bits from the chip, faliure to do so will result |
| 72 | + in erratic readings from the chip. |
| 73 | + */ |
| 74 | + for (int i=1; i>=0; i--) { |
| 75 | + digitalWrite(_SCK_pin,HIGH); |
| 76 | + delay(1); |
| 77 | + digitalWrite(_SCK_pin,LOW); |
| 78 | + } |
| 79 | + |
| 80 | + // Disable Device |
| 81 | + digitalWrite(_CS_pin, HIGH); |
| 82 | + |
| 83 | + /* |
| 84 | + Keep in mind that the temp that was just read is on the digital scale |
| 85 | + from 0˚C to 1023.75˚C at a resolution of 2^12. We now need to convert |
| 86 | + to an actual readable temperature (this drove me nuts until I figured |
| 87 | + this out!). Now multiply by 0.25. I tried to avoid float math but |
| 88 | + it is tough to do a good conversion to ˚F. THe final value is converted |
| 89 | + to an int and returned at x10 power. |
| 90 | +
|
| 91 | + 2 = temp in deg F |
| 92 | + 1 = temp in deg C |
| 93 | + 0 = raw chip value 0-4095 |
| 94 | + */ |
| 95 | + if(_units == 2) { |
| 96 | + temp = (value*0.25) * 9.0/5.0 + 32.0; |
| 97 | + } else if(_units == 1) { |
| 98 | + temp = (value*0.25); |
| 99 | + } else { |
| 100 | + temp = value; |
| 101 | + } |
| 102 | + |
| 103 | + /* Output negative of CS_pin if there is a TC error, otherwise return 'temp' */ |
| 104 | + if(error_tc != 0) { |
| 105 | + return -_CS_pin; |
| 106 | + } else { |
| 107 | + return temp; |
| 108 | + } |
| 109 | +} |
0 commit comments