MLX90614 sensor is basically infrared tempararute sensor.It\'s works on I2c protocol

Dependencies:   mbed

Committer:
mitesh2patel
Date:
Wed May 04 05:50:35 2011 +0000
Revision:
0:5b140ed159a9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mitesh2patel 0:5b140ed159a9 1
mitesh2patel 0:5b140ed159a9 2 #include "mbed.h"
mitesh2patel 0:5b140ed159a9 3
mitesh2patel 0:5b140ed159a9 4 I2C i2c(p28,p27); //sda,scl
mitesh2patel 0:5b140ed159a9 5 Serial pc(USBTX,USBRX); //serial usb config
mitesh2patel 0:5b140ed159a9 6
mitesh2patel 0:5b140ed159a9 7 char a[5];
mitesh2patel 0:5b140ed159a9 8
mitesh2patel 0:5b140ed159a9 9 int main()
mitesh2patel 0:5b140ed159a9 10 {
mitesh2patel 0:5b140ed159a9 11 int p1,p2,p3; //to store adc high and low and PEC(packet error correction)
mitesh2patel 0:5b140ed159a9 12 int ch=0,temp_val; //to store temparature value
mitesh2patel 0:5b140ed159a9 13 i2c.frequency(20000); //I2C frequency 20000hz (20khz)
mitesh2patel 0:5b140ed159a9 14
mitesh2patel 0:5b140ed159a9 15
mitesh2patel 0:5b140ed159a9 16 while(1){
mitesh2patel 0:5b140ed159a9 17
mitesh2patel 0:5b140ed159a9 18 do{ //loop repeat if repeated start codition is not acknowledge
mitesh2patel 0:5b140ed159a9 19
mitesh2patel 0:5b140ed159a9 20 do{ //loop repeat if device ram address(reg address where the Tobj value present) condition is not acknowledge
mitesh2patel 0:5b140ed159a9 21
mitesh2patel 0:5b140ed159a9 22
mitesh2patel 0:5b140ed159a9 23
mitesh2patel 0:5b140ed159a9 24 do{ //loop repeat if device address acdition is not acknowledge
mitesh2patel 0:5b140ed159a9 25 pc.printf("add\t");
mitesh2patel 0:5b140ed159a9 26 i2c.stop(); //stop i2c if not ack
mitesh2patel 0:5b140ed159a9 27
mitesh2patel 0:5b140ed159a9 28 wait(0.2);
mitesh2patel 0:5b140ed159a9 29 i2c.start(); //start I2C
mitesh2patel 0:5b140ed159a9 30 ch=i2c.write(0x00); //device address of mlxIRtemprature sensorwith write condition
mitesh2patel 0:5b140ed159a9 31 } while(ch==0); //wait for ack
mitesh2patel 0:5b140ed159a9 32 pc.printf("ACK1\t");
mitesh2patel 0:5b140ed159a9 33
mitesh2patel 0:5b140ed159a9 34 ch=i2c.write(0x07); //device ram address where Tobj value is present
mitesh2patel 0:5b140ed159a9 35 } while(ch==0); //wait for ack
mitesh2patel 0:5b140ed159a9 36 pc.printf("ACK2\t");
mitesh2patel 0:5b140ed159a9 37
mitesh2patel 0:5b140ed159a9 38
mitesh2patel 0:5b140ed159a9 39
mitesh2patel 0:5b140ed159a9 40 i2c.start(); //repeat start
mitesh2patel 0:5b140ed159a9 41 ch=i2c.write(0x01); //device address with read condition
mitesh2patel 0:5b140ed159a9 42 }while(ch==0); //wait for ack
mitesh2patel 0:5b140ed159a9 43 pc.printf("ACK3\t");
mitesh2patel 0:5b140ed159a9 44 p1=i2c.read(1); //Tobj low byte
mitesh2patel 0:5b140ed159a9 45 p2=i2c.read(1); //Tobj heigh byte
mitesh2patel 0:5b140ed159a9 46 p3=i2c.read(0); //PEC
mitesh2patel 0:5b140ed159a9 47 i2c.stop(); //stop condition
mitesh2patel 0:5b140ed159a9 48
mitesh2patel 0:5b140ed159a9 49 //degree centigrate conversion
mitesh2patel 0:5b140ed159a9 50 temp_val=((((p2&0x007f)<<8)+p1)*0.02)-0.01;
mitesh2patel 0:5b140ed159a9 51 temp_val=temp_val-273;
mitesh2patel 0:5b140ed159a9 52
mitesh2patel 0:5b140ed159a9 53
mitesh2patel 0:5b140ed159a9 54
mitesh2patel 0:5b140ed159a9 55 wait(1.0);
mitesh2patel 0:5b140ed159a9 56
mitesh2patel 0:5b140ed159a9 57
mitesh2patel 0:5b140ed159a9 58 pc.printf("rx val1 is %d\n",p1);
mitesh2patel 0:5b140ed159a9 59 pc.printf("rx val2 is %d\n",p2);
mitesh2patel 0:5b140ed159a9 60 pc.printf("rx val3 is %d\n",p3);
mitesh2patel 0:5b140ed159a9 61 pc.printf("temp val is %d\n",temp_val);
mitesh2patel 0:5b140ed159a9 62
mitesh2patel 0:5b140ed159a9 63
mitesh2patel 0:5b140ed159a9 64 wait(2.0);
mitesh2patel 0:5b140ed159a9 65
mitesh2patel 0:5b140ed159a9 66
mitesh2patel 0:5b140ed159a9 67
mitesh2patel 0:5b140ed159a9 68 }
mitesh2patel 0:5b140ed159a9 69
mitesh2patel 0:5b140ed159a9 70
mitesh2patel 0:5b140ed159a9 71
mitesh2patel 0:5b140ed159a9 72 return 0;
mitesh2patel 0:5b140ed159a9 73 }