MLX90614 IR Thermometer
Tom P
Posts: 97
NEED SOME MATH HELP!
The 10 degree field of view IR thermometer is one very cool device!
I would like to convert the sample code to display Fahrenheit temp reading, but need BS2 math help.
***********************************************************************
'· MLX90614_Demo-Simple.BS2
'·· {$STAMP BS2}
'·· {$PBASIC 2.5}
Reset······· CON···· 1
Alarm······· CON···· 2
Sensor······ CON···· 3
baud········ CON··· 84
xslave······ CON··· $35··························· 'slave address
temperature· VAR··· Word
tempL······· VAR··· temperature.LOWBYTE
tempH······· VAR··· temperature.HIGHBYTE
pec········· VAR··· Byte
Kelvin······ VAR··· Word
KelvinDec··· VAR··· Word
Celsius····· VAR··· Word
CelsiusDec·· VAR··· Word
w··········· VAR··· Byte
x··········· VAR··· Byte
y··········· VAR··· Byte
z··········· VAR··· Byte
'
[noparse][[/noparse] EEPROM Data ]
PEC0·· DATA $00,$07,$0E,$09,$1C,$1B,$12,$15,$38,$3F,$36,$31,$24,$23,$2A,$2D
PEC1·· DATA $70,$77,$7E,$79,$6C,$6B,$62,$65,$48,$4F,$46,$41,$54,$53,$5A,$5D
PEC2·· DATA $E0,$E7,$EE,$E9,$FC,$FB,$F2,$F5,$D8,$DF,$D6,$D1,$C4,$C3,$CA,$CD
PEC3·· DATA $90,$97,$9E,$99,$8C,$8B,$82,$85,$A8,$AF,$A6,$A1,$B4,$B3,$BA,$BD
PEC4·· DATA $C7,$C0,$C9,$CE,$DB,$DC,$D5,$D2,$FF,$F8,$F1,$F6,$E3,$E4,$ED,$EA
PEC5·· DATA $B7,$B0,$B9,$BE,$AB,$AC,$A5,$A2,$8F,$88,$81,$86,$93,$94,$9D,$9A
PEC6·· DATA $27,$20,$29,$2E,$3B,$3C,$35,$32,$1F,$18,$11,$16,$03,$04,$0D,$0A
PEC7·· DATA $57,$50,$59,$5E,$4B,$4C,$45,$42,$6F,$68,$61,$66,$73,$74,$7D,$7A
PEC8·· DATA $89,$8E,$87,$80,$95,$92,$9B,$9C,$B1,$B6,$BF,$B8,$AD,$AA,$A3,$A4
PEC9·· DATA $F9,$FE,$F7,$F0,$E5,$E2,$EB,$EC,$C1,$C6,$CF,$C8,$DD,$DA,$D3,$D4
PEC10· DATA $69,$6E,$67,$60,$75,$72,$7B,$7C,$51,$56,$5F,$58,$4D,$4A,$43,$44
PEC11· DATA $19,$1E,$17,$10,$05,$02,$0B,$0C,$21,$26,$2F,$28,$3D,$3A,$33,$34
PEC12· DATA $4E,$49,$40,$47,$52,$55,$5C,$5B,$76,$71,$78,$7F,$6A,$6D,$64,$63
PEC13· DATA $3E,$39,$30,$37,$22,$25,$2C,$2B,$06,$01,$08,$0F,$1A,$1D,$14,$13
PEC14· DATA $AE,$A9,$A0,$A7,$B2,$B5,$BC,$BB,$96,$91,$98,$9F,$8A,$8D,$84,$83
PEC15· DATA $DE,$D9,$D0,$D7,$C2,$C5,$CC,$CB,$E6,$E1,$E8,$EF,$FA,$FD,$F4,$F3
Init:
· LOW Reset
· INPUT Reset
· DEBUG CLS,··································· ' setup report screen
··· "MLX90614 Infra Red Thermometer", CR,
··· "=============================", CR,CR,
··· "Kelvin.........······ ", CR,
··· "Celsius........······ ", CR,
··· "PEC test.......······ ", CR
getTemperature:
· SEROUT Sensor,baud,[noparse][[/noparse]0,"!TEMR",xslave,$07]
· SERIN Sensor,baud,1000,PECfail,[noparse][[/noparse]tempL,tempH,pec]
checkPEC:
· z=0
· y = xslave<<1+0
· GOSUB calculateCRC
· y = $07
· GOSUB calculateCRC
· y = xslave<<1+1
· GOSUB calculateCRC
· y = tempL
· GOSUB calculateCRC
· y = tempH
· GOSUB calculateCRC
· IF z<>pec THEN PECfail
· GOTO convertTemperatures
calculateCRC:
· w=z^y
· READ w,z
· RETURN
convertTemperatures:
· Kelvin = (temperature/100)*2
· KelvinDec = temperature*2
· IF (temperature*2) < 27315 THEN overWordsize
· Celsius = (temperature/100*2)-273
· CelsiusDec = (temperature*2)-27315
· GOTO displayTemperatures
overWordsize:
· Celsius = ((27315-(temperature*2))/100)
· CelsiusDec = (27315-(temperature*2))
· DEBUG CRSRXY, 23, 4,"-",DEC Celsius,".",DEC2 CelsiusDec, CLREOL
displayTemperatures:
· DEBUG CRSRXY, 23, 3,DEC Kelvin,".",DEC1 KelvinDec,CLREOL
· DEBUG CRSRXY, 23, 4,DEC Celsius,".",DEC2 CelsiusDec, CLREOL
· DEBUG CRSRXY, 23, 5,"Pass",CLREOL
· PAUSE 1000
· GOTO getTemperature
PECfail:
· DEBUG CRSRXY, 23, 3,CLREOL,CR
· DEBUG CRSRXY, 23, 4,CLREOL,CR
· DEBUG CRSRXY, 23, 5,"Fail",CLREOL,CR
· PAUSE 500
· GOTO getTemperature
The 10 degree field of view IR thermometer is one very cool device!
I would like to convert the sample code to display Fahrenheit temp reading, but need BS2 math help.
***********************************************************************
'· MLX90614_Demo-Simple.BS2
'·· {$STAMP BS2}
'·· {$PBASIC 2.5}
Reset······· CON···· 1
Alarm······· CON···· 2
Sensor······ CON···· 3
baud········ CON··· 84
xslave······ CON··· $35··························· 'slave address
temperature· VAR··· Word
tempL······· VAR··· temperature.LOWBYTE
tempH······· VAR··· temperature.HIGHBYTE
pec········· VAR··· Byte
Kelvin······ VAR··· Word
KelvinDec··· VAR··· Word
Celsius····· VAR··· Word
CelsiusDec·· VAR··· Word
w··········· VAR··· Byte
x··········· VAR··· Byte
y··········· VAR··· Byte
z··········· VAR··· Byte
'
[noparse][[/noparse] EEPROM Data ]
PEC0·· DATA $00,$07,$0E,$09,$1C,$1B,$12,$15,$38,$3F,$36,$31,$24,$23,$2A,$2D
PEC1·· DATA $70,$77,$7E,$79,$6C,$6B,$62,$65,$48,$4F,$46,$41,$54,$53,$5A,$5D
PEC2·· DATA $E0,$E7,$EE,$E9,$FC,$FB,$F2,$F5,$D8,$DF,$D6,$D1,$C4,$C3,$CA,$CD
PEC3·· DATA $90,$97,$9E,$99,$8C,$8B,$82,$85,$A8,$AF,$A6,$A1,$B4,$B3,$BA,$BD
PEC4·· DATA $C7,$C0,$C9,$CE,$DB,$DC,$D5,$D2,$FF,$F8,$F1,$F6,$E3,$E4,$ED,$EA
PEC5·· DATA $B7,$B0,$B9,$BE,$AB,$AC,$A5,$A2,$8F,$88,$81,$86,$93,$94,$9D,$9A
PEC6·· DATA $27,$20,$29,$2E,$3B,$3C,$35,$32,$1F,$18,$11,$16,$03,$04,$0D,$0A
PEC7·· DATA $57,$50,$59,$5E,$4B,$4C,$45,$42,$6F,$68,$61,$66,$73,$74,$7D,$7A
PEC8·· DATA $89,$8E,$87,$80,$95,$92,$9B,$9C,$B1,$B6,$BF,$B8,$AD,$AA,$A3,$A4
PEC9·· DATA $F9,$FE,$F7,$F0,$E5,$E2,$EB,$EC,$C1,$C6,$CF,$C8,$DD,$DA,$D3,$D4
PEC10· DATA $69,$6E,$67,$60,$75,$72,$7B,$7C,$51,$56,$5F,$58,$4D,$4A,$43,$44
PEC11· DATA $19,$1E,$17,$10,$05,$02,$0B,$0C,$21,$26,$2F,$28,$3D,$3A,$33,$34
PEC12· DATA $4E,$49,$40,$47,$52,$55,$5C,$5B,$76,$71,$78,$7F,$6A,$6D,$64,$63
PEC13· DATA $3E,$39,$30,$37,$22,$25,$2C,$2B,$06,$01,$08,$0F,$1A,$1D,$14,$13
PEC14· DATA $AE,$A9,$A0,$A7,$B2,$B5,$BC,$BB,$96,$91,$98,$9F,$8A,$8D,$84,$83
PEC15· DATA $DE,$D9,$D0,$D7,$C2,$C5,$CC,$CB,$E6,$E1,$E8,$EF,$FA,$FD,$F4,$F3
Init:
· LOW Reset
· INPUT Reset
· DEBUG CLS,··································· ' setup report screen
··· "MLX90614 Infra Red Thermometer", CR,
··· "=============================", CR,CR,
··· "Kelvin.........······ ", CR,
··· "Celsius........······ ", CR,
··· "PEC test.......······ ", CR
getTemperature:
· SEROUT Sensor,baud,[noparse][[/noparse]0,"!TEMR",xslave,$07]
· SERIN Sensor,baud,1000,PECfail,[noparse][[/noparse]tempL,tempH,pec]
checkPEC:
· z=0
· y = xslave<<1+0
· GOSUB calculateCRC
· y = $07
· GOSUB calculateCRC
· y = xslave<<1+1
· GOSUB calculateCRC
· y = tempL
· GOSUB calculateCRC
· y = tempH
· GOSUB calculateCRC
· IF z<>pec THEN PECfail
· GOTO convertTemperatures
calculateCRC:
· w=z^y
· READ w,z
· RETURN
convertTemperatures:
· Kelvin = (temperature/100)*2
· KelvinDec = temperature*2
· IF (temperature*2) < 27315 THEN overWordsize
· Celsius = (temperature/100*2)-273
· CelsiusDec = (temperature*2)-27315
· GOTO displayTemperatures
overWordsize:
· Celsius = ((27315-(temperature*2))/100)
· CelsiusDec = (27315-(temperature*2))
· DEBUG CRSRXY, 23, 4,"-",DEC Celsius,".",DEC2 CelsiusDec, CLREOL
displayTemperatures:
· DEBUG CRSRXY, 23, 3,DEC Kelvin,".",DEC1 KelvinDec,CLREOL
· DEBUG CRSRXY, 23, 4,DEC Celsius,".",DEC2 CelsiusDec, CLREOL
· DEBUG CRSRXY, 23, 5,"Pass",CLREOL
· PAUSE 1000
· GOTO getTemperature
PECfail:
· DEBUG CRSRXY, 23, 3,CLREOL,CR
· DEBUG CRSRXY, 23, 4,CLREOL,CR
· DEBUG CRSRXY, 23, 5,"Fail",CLREOL,CR
· PAUSE 500
· GOTO getTemperature
Comments
You can use the "*/", Multiply Middle operator to do the whole number and fraction multiplication as follows:
FDec·· VAR··· Word
FDeg = (CelsiusDec */ $01CD) + 32······· ' $01CD represents 1.8 * 256 in HEX
See the·PBasic documentation for details of the "*/", Multiply Middle operator.
Duffer
I'am still all confused, I added the lines below with #### as shown.
When I try to RUN program-- I see: {FahrnheitDeg} which is highlighted and says --ERROR "label missing"
I notice in the {convert temperature} section of the program there are two line for both Kelvin & Celsius?
Celsius = (temperature/100*2)-273
CelsiusDec = (temperature*2)-27315
How do I accomplish this for the Fahrnheit lines????
Please help!
Tom
******************************************************************************************
'MLX90614_Demo-Simple.BS2
'{$STAMP BS2}
'{$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
Reset CON 1
Alarm CON 2
Sensor CON 3
'
[noparse][[/noparse] Constants ]
baud CON 84
xslave CON $35 'slave address
'
[noparse][[/noparse] Variables ]
temperature VAR Word
tempL VAR temperature.LOWBYTE
tempH VAR temperature.HIGHBYTE
pec VAR Byte
Kelvin VAR Word
KelvinDec VAR Word
Celsius VAR Word
CelsiusDec VAR Word
w VAR Byte
x VAR Byte
y VAR Byte
z VAR Byte
Fahrnheit VAR Word '####
FahrnheitDec VAR Word '####
'
[noparse][[/noparse] EEPROM Data ]
PEC0 DATA $00,$07,$0E,$09,$1C,$1B,$12,$15,$38,$3F,$36,$31,$24,$23,$2A,$2D
PEC1 DATA $70,$77,$7E,$79,$6C,$6B,$62,$65,$48,$4F,$46,$41,$54,$53,$5A,$5D
PEC2 DATA $E0,$E7,$EE,$E9,$FC,$FB,$F2,$F5,$D8,$DF,$D6,$D1,$C4,$C3,$CA,$CD
PEC3 DATA $90,$97,$9E,$99,$8C,$8B,$82,$85,$A8,$AF,$A6,$A1,$B4,$B3,$BA,$BD
PEC4 DATA $C7,$C0,$C9,$CE,$DB,$DC,$D5,$D2,$FF,$F8,$F1,$F6,$E3,$E4,$ED,$EA
PEC5 DATA $B7,$B0,$B9,$BE,$AB,$AC,$A5,$A2,$8F,$88,$81,$86,$93,$94,$9D,$9A
PEC6 DATA $27,$20,$29,$2E,$3B,$3C,$35,$32,$1F,$18,$11,$16,$03,$04,$0D,$0A
PEC7 DATA $57,$50,$59,$5E,$4B,$4C,$45,$42,$6F,$68,$61,$66,$73,$74,$7D,$7A
PEC8 DATA $89,$8E,$87,$80,$95,$92,$9B,$9C,$B1,$B6,$BF,$B8,$AD,$AA,$A3,$A4
PEC9 DATA $F9,$FE,$F7,$F0,$E5,$E2,$EB,$EC,$C1,$C6,$CF,$C8,$DD,$DA,$D3,$D4
PEC10 DATA $69,$6E,$67,$60,$75,$72,$7B,$7C,$51,$56,$5F,$58,$4D,$4A,$43,$44
PEC11 DATA $19,$1E,$17,$10,$05,$02,$0B,$0C,$21,$26,$2F,$28,$3D,$3A,$33,$34
PEC12 DATA $4E,$49,$40,$47,$52,$55,$5C,$5B,$76,$71,$78,$7F,$6A,$6D,$64,$63
PEC13 DATA $3E,$39,$30,$37,$22,$25,$2C,$2B,$06,$01,$08,$0F,$1A,$1D,$14,$13
PEC14 DATA $AE,$A9,$A0,$A7,$B2,$B5,$BC,$BB,$96,$91,$98,$9F,$8A,$8D,$84,$83
PEC15 DATA $DE,$D9,$D0,$D7,$C2,$C5,$CC,$CB,$E6,$E1,$E8,$EF,$FA,$FD,$F4,$F3
'
[noparse][[/noparse] Initialization ]
Init:
LOW Reset
INPUT Reset
DEBUG CLS, ' setup report screen
"MLX90614 Infra Red Thermometer", CR,
"=============================", CR,CR,
"Kelvin......... ", CR,
"Celsius........ ", CR,
"Fahrnheit........ ", CR, '####
"PEC test....... ", CR
'
[noparse][[/noparse] Program Code ]
getTemperature:
SEROUT Sensor,baud,[noparse][[/noparse]0,"!TEMR",xslave,$07]
SERIN Sensor,baud,1000,PECfail,[noparse][[/noparse]tempL,tempH,pec]
checkPEC:
z=0
y = xslave<<1+0
GOSUB calculateCRC
y = $07
GOSUB calculateCRC
y = xslave<<1+1
GOSUB calculateCRC
y = tempL
GOSUB calculateCRC
y = tempH
GOSUB calculateCRC
IF z<>pec THEN PECfail
GOTO convertTemperatures
calculateCRC:
w=z^y
READ w,z
RETURN
convertTemperatures:
Kelvin = (temperature/100)*2
KelvinDec = temperature*2
IF (temperature*2) < 27315 THEN overWordsize
Celsius = (temperature/100*2)-273
CelsiusDec = (temperature*2)-27315
FahrnheitDeg = (CelsiusDec */ $01CD) + 32 ' $01CD represents 1.8 * 256 in HEX '#####
GOTO displayTemperatures
overWordsize:
Celsius = ((27315-(temperature*2))/100)
CelsiusDec = (27315-(temperature*2))
DEBUG CRSRXY, 23, 4,"-",DEC Celsius,".",DEC2 CelsiusDec, CLREOL
displayTemperatures:
DEBUG CRSRXY, 23, 3,DEC Kelvin,".",DEC1 KelvinDec,CLREOL
DEBUG CRSRXY, 23, 4,DEC Celsius,".",DEC2 CelsiusDec, CLREOL
DEBUG CRSRXY, 23, 5,DEC Fahrnheit,".",DEC2 FahrnheitDec, CLREOL '#####
DEBUG CRSRXY, 23, 5,"Pass",CLREOL
PAUSE 1000
GOTO getTemperature
PECfail:
DEBUG CRSRXY, 23, 3,CLREOL,CR
DEBUG CRSRXY, 23, 4,CLREOL,CR
DEBUG CRSRXY, 23, 5,"Fail",CLREOL,CR
PAUSE 500
GOTO getTemperature