' MLX90614_08M2.Bas
' (c)2011 by MR Burnette for all rights not reserved by PH Anderson
' Modified for M2 chips by Armp - Thanks Peter and Ray 1/12/13
'
' 20M2 - Straight Drop In Replacement for 20X2,
' 08M2 - Connect 20X2 socket Pin 11 to Pin 17, Pin 13 to Pin 18,
' 196 Bytes
'
' Modified by JlangholzJ and Armp for use in 18m2+
' Also modified for MLX90615
' 18M2+ - Stright drop in
'
#picaxe 18m2
#No_Data
SYMBOL Lo = B0 ' B1-B0 overmapped by W0
SYMBOL Hi = B1
SYMBOL Temperature= W0
SYMBOL PEC = B2
SYMBOL RamLocation= B3 ' Could be a Constant in this example
SYMBOL X = B4
SYMBOL Tenths = B4
SYMBOL N = B5
SYMBOL CRC8 = B6
SYMBOL LOG_RATE = 1000 'Used as a pre-def for the logging rate (HZ)
SYMBOL PAUSE_TIME = 1000/LOG_RATE
SYMBOL SlaveAdr = B7
SlaveAdr = $01 * 2 'shift left by a bit to allow read/write bit.
sertxd("Beginning data:",CR,LF)
Initialize:
' Return to this section on error condition on I2C bus CRC
Hi2cSetup I2CMaster, SlaveAdr , I2CSlow, I2CByte
Again: ' RAM location $07 is the object temperature in Kevin * 50
RamLocation = $27
GoSub ReadRAM
If Temperature = 0 Then Error
'Temperature = Temperature+2/5 'Use for Kelvin - integer divide by 5 with Rounding
Temperature = Temperature - 12767 *3 /5 *3 /5 'to return Temp as xxx.xF - Armp
Tenths=Temperature//10 : Temperature=Temperature/10
Sertxd ("Temp: ", #Temperature,".",#Tenths,CR,LF)
Sertxd ("Addr: ", #SlaveAdr,CR,LF)
Pause LOG_RATE ' Results in approx 1 reading per second 900
' MLX90614 requires 865ms to settle
GoTo Again
ReadRAM:
Hi2cin RamLocation, (Lo, Hi, PEC)
CRC8 = $00
X = SlaveAdr : GoSub CalcCRC8
X = RamLocation : GoSub CalcCRC8
X = SlaveAdr + 1 : GoSub CalcCRC8
X = Lo : GoSub CalcCRC8
X = Hi : GoSub CalcCRC8
If CRC8 <> PEC Then
Temperature = 0 ' Return Zero K if CRC Error
Endif
Return
CalcCRC8:
X = X ^ CRC8
For N = 0 to 7
If X > 127 Then
X = X * 2
X = X ^ $07
Else
X = X * 2
Endif
Next
CRC8 = X
Return
Error:
Sertxd("ADDR ", #SlaveAdr, " Not valid", CR,LF)
SlaveAdr = SlaveAdr + 1 'inc slave addr and re-init
Goto Initialize
END