' MLX90614_05.Bas ' I2C protocol code (c)2011 by PH Anderson and used with permission ' Please visit www.phanderson.com ' (c)2011 by MR Burnette for all rights not reserved by PH Anderson ' ' Output of PICAXE is serial and intended for PC use ' Expands on _04 version by not reading the RAM $06 ' 255 bytes / 4096 ' PICAXE-20X2 directly connected to ' MLX90614ESF-AAA is I2C protocol IR sensor with 0.02 degree Kevin accuracy ' Refer to product note section 8.7.2 for how temperature is converted from RAM reading ' Term 11 SCL --------------- SCL ' Term 13 SDA --------------- SDA ' PICAXE Compiler directives #picaxe 20x2 #Terminal 2400 #No_Table #No_Data #freq m4 'Defined in code for Serial Output in SYMBOL table ' SYMBOL table - aliases SYMBOL Lo = B0 ' B1-B0 overmapped by W0 & bit0 - bit 7 SYMBOL Hi = B1 SYMBOL PEC = B2 ' B3-B2 overmapped by W1 & bit8 - bit15 SYMBOL Status = B3 SYMBOL ______b4 = B4 ' B5-B4 overmapped by W2 & bit16 - bit31 SYMBOL ______b5 = B5 SYMBOL ______b6 = B6 ' B7-B6 overmapped by W3 SYMBOL ______b7 = B7 SYMBOL ______b8 = B8 ' B9-B8 overmapped by W4 SYMBOL ______b9 = B9 SYMBOL SlaveAdr_2 = B10 ' B11-10 overmapped by W5 SYMBOL RamLocation= B11 SYMBOL X = B12 ' B13-B12 overmapped by W6 SYMBOL N = B13 SYMBOL CRC8 = B14 ' B15-B14 overmapped by W7 SYMBOL ______b15 = B15 SYMBOL ______b16 = B16 ' B17-B16 overmapped by W8 SYMBOL ______b17 = B17 SYMBOL ______b18 = B18 ' B19-B18 overmapped by W9 SYMBOL ______b19 = B19 SYMBOL Val = W10 ' W10 overwrites B20 - B21 SYMBOL TC_100 = W11 ' W11 overwrites B22 - B23 SYMBOL MLX90614ESF= W12 ' W12 overwrites B24 - B25 ' Note CLOCK & BAUD are related and must change together ' Default for the 20X2 chip is 8MHz SYMBOL CLOCK = m4 SYMBOL BAUD = N2400_4 SYMBOL Serial = A.0 ' Set frequency to CLOCK value since different than the 8MHz default setfreq CLOCK ' Send startup message to serial port serout Serial,BAUD,("Beginning data:",CR,LF) INITIALIZE: ' Initialize I2C interface for MLX90614 ' I2C code (c) by P.H. Anderson - Thanks Professor ' Return to this section on error condition on I2C bus CRC Hi2cSetup I2CMaster, 45, I2CSlow, I2CByte ' Setting values to 0 is only a formality as all PIC values are 0 on power-up ' General slave adr as only one device to read on IC2 bus SlaveAdr_2 = $00 * 2 Again: ' RAM location $06 is the chip die temperature ' MLX90614ESF is set for display use ' RamLocation = $06 ' GoSub ReadRAM ' Return Val (2 Byte word value) ' If Status = 0 Then Error 'Based on CRC algorithm ' MLX90614ESF = Val ' RAM location $07 is the object temperature in Kevin * 50 (0.02K resolution) RamLocation = $07 GoSub ReadRAM If Status = 0 Then Error Val = Val/5 ' integer divide by 5 to assist in smoothing ' If here then a Val word was returned for tObject and tAmbient is in var MLX90614ESF GoSub Display Pause 390 ' Pause 265 ' At 2400 BAUD, 265ms results in a approx 1 second per reading ' 865ms total is required for the MLX90614 to fully settle before next reading GoTo Again ReadRAM: Hi2cin [SlaveAdr_2], RamLocation, (Lo, Hi, PEC) Val = Hi Val = Val * 256 + Lo ' bitshift and combine 'SerTxD ("DB ", #Hi,"-",#Lo,"-",#Val, CR, LF) CRC8 = $00 X = SlaveAdr_2 : GoSub CalcCRC8 X = RamLocation : GoSub CalcCRC8 X = SlaveAdr_2 + 1 : GoSub CalcCRC8 X = Lo : GoSub CalcCRC8 X = Hi : GoSub CalcCRC8 X = PEC : GoSub CalcCRC8 ' If CRC8 = 0 Then Status = 1 ' True is good Else Status = 0 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 Display: ' Serout Serial, BAUD, (#MLX90614ESF,", ",#Val,CR,LF) Serout Serial, BAUD, (#Val,CR,LF) Return Error: If RamLocation = $06 then Serout Serial,BAUD,("T_A Error...", CR,LF) else Serout Serial,BAUD,("T_S Error...", CR,LF) endif Goto INITIALIZE END