Log in

View Full Version : IR Temperature Sensor



TravisM
- 21st October 2012, 03:42
I developed the following code in PBP3. I am having trouble getting it to work. I would like to see the temperature data that gets stored in the AmbTemp and ObjTemp memory by the I2C command. The data in each of these memory locations are WORD length. I can't find a command in PBP3 that allows me to read these 16 bit data on two of the ports. Am I missing something? Also, can you tell me what is wrong with my code?

Thank you, TravisM

Temperature:

i2cread SDA, SCL, SAdef, LocAmbTemp, [AmbTemp] 'read amb temperature Ta & save in AmbTemp
gosub SwapAmbBytes: 'I2C reads the highest byte first; Go swap the bytes

i2cread SDA, SCL, SAdef, LocObjTemp, [ObjTemp] 'read object temperature (Tobj) & save in ObjTemp
gosub SwapObjBytes:

if AmbTemp >= ObjTemp then Temperature 'Object is not heating, continue to loop

if (ObjTemp - AmbTemp) >= ThresTemp then ObjHeating 'differance between Amb & Obj temperature has exceeded threshold temp


Reset: 'Reset time count and turn motion sensor off if (ObjTemp - AmbTemp) is less that threshold temp.
cnt = 1
low 6 'turns motion sensor off
goto Temperature


ObjHeating:
high 6 'obj is heating; turn motion detector on (by setting RB6 high) to check for motion in room .

for cnt = 1 to MaxTime 'measure time by counting # of loops of approx one sec each.

If portb.1 = 1 then Temperature 'motion occured in room, do a reset

i2cread SDA, SCL, SAdef, LocAmbTemp, [AmbTemp] 'read amb temperature Ta & save in AmbTemp
gosub SwapAmbBytes:

i2cread SDA, SCL, SAdef, LocObjTemp, [ObjTemp] 'read object temperature (Tobj) & save in ObjTemp
gosub SwapObjBytes:


if AmbTemp >= ObjTemp then Temperature 'Object is not heating, start new loop

if (ObjTemp - AmbTemp) < ThresTemp then Temperature 'differance between Amb & Obj temperature is less than threshold temp


next cnt

Alarm: 'object temperature has exceeded threshold temp for more than the MaxTime, so sound alarm
high portb.0
pause 2000
low portb.0
pause 15000
if portb.5 = 0 then Temperature
goto Alarm

end

SwapAmbBytes: 'swap the MSB and LSB of the amb and obj temperatures

swap AmbTemp.byte1, AmbTemp.byte0
return

SwapObjBytes:

swap ObjTemp.Byte1, ObjTemp.byte0
return

longpole001
- 21st October 2012, 17:34
i cant comment on the code much , but its a bit easier to read






Temperature:

i2cread SDA, SCL, SAdef, LocAmbTemp, [AmbTemp] 'read amb temperature Ta & save in AmbTemp
gosub SwapAmbBytes: 'I2C reads the highest byte first; Go swap the bytes

i2cread SDA, SCL, SAdef, LocObjTemp, [ObjTemp] 'read object temperature (Tobj) & save in ObjTemp
gosub SwapObjBytes:

if AmbTemp >= ObjTemp then Temperature 'Object is not heating, continue to loop
if (ObjTemp - AmbTemp) >= ThresTemp then ObjHeating 'differance between Amb & Obj temperature has exceeded threshold temp


Reset: 'Reset time count and turn motion sensor off if (ObjTemp - AmbTemp) is less that threshold temp.
cnt = 1
low 6 'turns motion sensor off
goto Temperature


ObjHeating:
high 6 'obj is heating; turn motion detector on (by setting RB6 high) to check for motion in room .

for cnt = 1 to MaxTime 'measure time by counting # of loops of approx one sec each.

If portb.1 = 1 then Temperature 'motion occured in room, do a reset

i2cread SDA, SCL, SAdef, LocAmbTemp, [AmbTemp] 'read amb temperature Ta & save in AmbTemp
gosub SwapAmbBytes:
i2cread SDA, SCL, SAdef, LocObjTemp, [ObjTemp] 'read object temperature (Tobj) & save in ObjTemp
gosub SwapObjBytes:

if AmbTemp >= ObjTemp then Temperature 'Object is not heating, start new loop

if (ObjTemp - AmbTemp) < ThresTemp then Temperature 'differance between Amb & Obj temperature is less than threshold temp

next cnt

Alarm: 'object temperature has exceeded threshold temp for more than the MaxTime, so sound alarm
high portb.0
pause 2000
low portb.0
pause 15000
if portb.5 = 0 then Temperature
goto Alarm

end

' ---------- Sub Routines ----------

SwapAmbBytes: 'swap the MSB and LSB of the amb and obj temperatures

swap AmbTemp.byte1, AmbTemp.byte0
return


SwapObjBytes:
swap ObjTemp.Byte1, ObjTemp.byte0
return

TravisM
- 22nd October 2012, 05:06
Thank you for reformatting my code. It is much easier to read, but probably still too difficult for someone else to diagnose my code.

My IR sensor uses an SMBus interface. Since the SMBus interface is very simular to my micro I2C interface, I think the two should be compatible. I can monitor the serial data stream from the IR sensor with a logic analyzer and see that the ambient and object temperature data is about what it should be. But the serial data stream includes other data than the temperature data. I would like to see what is being loaded into the AmbTemp and ObjTemp RAM locations. To do this, I need to be able to transfer the 16 bit data out of the AmbTemp, and ObjTemp RAM, locations onto two of the 8 bit ports to read the parallel data. I would appreciate any help.

Thank you, TravisM

longpole001
- 22nd October 2012, 06:45
any chance the pic u are using as eeprom

retepsnikrep
- 22nd October 2012, 09:03
Is this the Melexis sensor?

My code runs at 8mhz on 16f886 or 18F2620 IIRC




I2CClock var PORTC.3 'I2CClock Line
I2CData var PORTC.4 'I2CData Line

I2CAddr var BYTE 'I2C Device Address
I2CReg var BYTE 'I2C Device Register



Cal26: 'Melexis IR Temp Sensor Routine MLX90614 IR sensor

'The temperature in degrees Kelvin is then calculated as TempK = 0.02 * MelTemp Or TempC = 2 * MelTemp - 27315.

'I2CReg = $06 'Ram register for Ambient Temp
'I2CReg = $07 'Ram register for Object 1
'I2CReg = $08 'Ram register for Object 2

I2Caddr = 0
I2CRead I2CData, I2CClock, I2CAddr, I2CReg, [Result.lowbyte, Result.highbyte]
Result = ((Result * 2) - 27315) / 100 'Convert to Centigrade
LCDOUT #Result,32 'Display
Return 'Return to main program

longpole001
- 22nd October 2012, 09:20
have you tired just write the values of the variables that u like to check into the some free locations on the eeprom , then read them back using programmer , to confirm the values are what u expected

longpole001
- 22nd October 2012, 09:25
also if you keen to try there a few simulators that you can find on popular torrent downloads sites that support those pics , you may not get live data , but u can sim inputs patterns that your seeing and then look at the variables saved

TravisM
- 23rd October 2012, 00:51
Thanks for the reply. I am using the Melexis MLX90614 sensor and the PIC16F886 micro. I see in your sample code that you don't swap the low and high bytes in the result to get the 16 bit result in the standard format. Perhaps I will try not swaping my MSB & LSB. I have also discovered that I do not have a couple of the Special Function Registers setup properly. I will make those changes and post the results.

TravisM

eggman
- 23rd October 2012, 15:04
here's a thread about the melexis sensor: http://www.picbasic.co.uk/forum/archive/index.php/t-11782.html