1-Wire Thermocouple Amplifier - MAX31855
anaxcontrol
Posts: 23
I'm using a MAX31855 on a BS2PX24. Its wired and works (confirmed 212 with boiling water and 32 with icebath). I cant seem to get past 650DegF (somewhere in that vicinity) before the values get erratic (jumps to 9000 etc). I believe this is because of the 16bit word size (-32767 to 32767). I don't need to read negative just 0-2500 Deg F. Anyone know how I can do this? My current code wont (below). Any assistance is appreciated as this is driving me nuts (searched everything I could find).
DO
LOW CS ' selelct SPI
SHIFTIN so, Clk, MSBPRE, [MAX_T\16] ' shift in the data, MSB first, MSBPRE for sign bit.
HIGH CS ' enough SPI xfr. Conversions occur in the background
IF MAX_T.BIT0 = 1 THEN Fault ' another way to capture all 3 fault conditons.
MAX_T = MAX_T >> 2 ' drop bits 0 and 1 to isolate the 14 bit result
MAX_T.BIT15 = MAX_T.BIT13 ' extend the sign to bits 14 and 15.
MAX_T.BIT14 = MAX_T.BIT13
MAX_T = MAX_T * 25 ' resolution is 0.25 degC per bit
' DEBUG CR,"degC=",REP "-"\MAX_T.BIT15, DEC ABS MAX_T/100,".",DEC2 ABS MAX_T ' display as sxxxx.xx +/-0.25 degC.
MAX_T = MAX_T + 4000 ' add 40 degC to make it positive.
F_temp = MAX_T ** 52429 + MAX_T - 4000 ' multiply * 1.8 and offset back to -40 degF.
' DEBUG CR,"degF=",REP "-"\F_temp.BIT15, DEC ABS F_temp/100,".",DEC2 ABS F_temp ' display as sxxxx.xx +/-0.45 degF.
SEROUT 0, LcdBaud, TX, [$A3, DEC ABS F_temp/100] ' display as sxxxx degF.
PAUSE 1000
LOOP
END
DO
LOW CS ' selelct SPI
SHIFTIN so, Clk, MSBPRE, [MAX_T\16] ' shift in the data, MSB first, MSBPRE for sign bit.
HIGH CS ' enough SPI xfr. Conversions occur in the background
IF MAX_T.BIT0 = 1 THEN Fault ' another way to capture all 3 fault conditons.
MAX_T = MAX_T >> 2 ' drop bits 0 and 1 to isolate the 14 bit result
MAX_T.BIT15 = MAX_T.BIT13 ' extend the sign to bits 14 and 15.
MAX_T.BIT14 = MAX_T.BIT13
MAX_T = MAX_T * 25 ' resolution is 0.25 degC per bit
' DEBUG CR,"degC=",REP "-"\MAX_T.BIT15, DEC ABS MAX_T/100,".",DEC2 ABS MAX_T ' display as sxxxx.xx +/-0.25 degC.
MAX_T = MAX_T + 4000 ' add 40 degC to make it positive.
F_temp = MAX_T ** 52429 + MAX_T - 4000 ' multiply * 1.8 and offset back to -40 degF.
' DEBUG CR,"degF=",REP "-"\F_temp.BIT15, DEC ABS F_temp/100,".",DEC2 ABS F_temp ' display as sxxxx.xx +/-0.45 degF.
SEROUT 0, LcdBaud, TX, [$A3, DEC ABS F_temp/100] ' display as sxxxx degF.
PAUSE 1000
LOOP
END
Comments
DO
LOW CS
SHIFTIN so, Clk, MSBPRE, [MAX_T\16]
HIGH CS
IF MAX_T.BIT0 = 1 THEN Fault
MAX_T = MAX_T >> 2
MAX_T.BIT15 = MAX_T.BIT13
MAX_T.BIT14 = MAX_T.BIT13
MAX_T = MAX_T * 25
MAX_T = MAX_T + 4000
F_temp = MAX_T ** 52429 + MAX_T - 4000
SEROUT 0, LcdBaud, TX, [$A3, DEC ABS F_temp/100]
PAUSE 1000
LOOP
END
Puts the radix point to the right of bit 0. Result is degrees C as an integer. As long as it is positive, everything is cool. You can then directly display it as degrees C or convert to F using the 9/5 yada yada. Remember that overflow can occur with intermediate results ( for example, after multiplying by nine and before dividing by five).
After everything is good with positive temperatures, you can worry about freezing.
This makes much more sense. I was going the wrong direction with MAX_T = MAX_T >>. question: how is the 11796 derived? Thank you for the fast response. I'm going to give it a test this morning!
You are always measuring positive degrees. If you can get this program to fail, then you have something other than overflow going on. Edit: If you cannot get this program to mis-behave, then you have working code to add to. endedit
Output blanks/zeros to blank the entire field immediately before outputting the the temperature. This was a common problem on serial terminals and thought it might be the case here.
You asked about the magic number, **11796.
That amounts to multiplication by 11796/65536 = 0.18. The Stamp divides by 65536 internally, using a 32 bit intermediate value. The ** operator is the Stampese way to multiply accurately by proper fractions. Actually, 11797 works better than 11796. There is a bit of roundoff error, always in the downwards direction. So 11797 will convert 0°C to 32°F, whereas 11796 rounds down to 31°F.
@Tom, I agree about simplifying to check the output of the sensor in a way that cannot overflow.
I also recommend checking the math. I know from many experiences how easy it is to get it wrong via basic misconceptions or by simple typos! I check the math by writing a loop that steps thru the range of input values that would be produced by the sensor from -40 to +1800 °C.