Student project, pay no heed.

Dependencies:   MLX90614 RPCInterface adc mbed

Fork of IR_temperature by jim hamblen

Committer:
villeah
Date:
Thu Jan 09 06:59:47 2014 +0000
Revision:
1:9cd48cdf8681
Ihan kakka.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
villeah 1:9cd48cdf8681 1 #include "mbed.h"
villeah 1:9cd48cdf8681 2 #include "mlx90614.h"
villeah 1:9cd48cdf8681 3 #include "rpc.h"
villeah 1:9cd48cdf8681 4 #include "adc.h"
villeah 1:9cd48cdf8681 5 #include "SerialRPCInterface.h"
villeah 1:9cd48cdf8681 6
villeah 1:9cd48cdf8681 7 #define SAMPLE_RATE 150000
villeah 1:9cd48cdf8681 8
villeah 1:9cd48cdf8681 9
villeah 1:9cd48cdf8681 10 ADC adc(SAMPLE_RATE, 1);
villeah 1:9cd48cdf8681 11 // the default address of the infrared sensor (7 bit address + 1 bit for read/write, 0xB4 for write)
villeah 1:9cd48cdf8681 12 char i2caddress = 0xB4;
villeah 1:9cd48cdf8681 13
villeah 1:9cd48cdf8681 14 AnalogIn ain(p20); //adc from pin 20
villeah 1:9cd48cdf8681 15 DigitalOut myled(LED1); //displays I2C wait
villeah 1:9cd48cdf8681 16 I2C i2c(p28,p27); //sda,scl
villeah 1:9cd48cdf8681 17 SerialRPCInterface RPC(USBTX, USBRX);
villeah 1:9cd48cdf8681 18
villeah 1:9cd48cdf8681 19 bool getTemp(float* temp_val){
villeah 1:9cd48cdf8681 20
villeah 1:9cd48cdf8681 21 char p1,p2,p3;
villeah 1:9cd48cdf8681 22 float temp_thermo;
villeah 1:9cd48cdf8681 23 bool ch;
villeah 1:9cd48cdf8681 24
villeah 1:9cd48cdf8681 25 i2c.stop(); //stop i2c if not ack
villeah 1:9cd48cdf8681 26 wait(0.01);
villeah 1:9cd48cdf8681 27 i2c.start(); //start I2C
villeah 1:9cd48cdf8681 28 ch=i2c.write(i2caddress); //device address with write condition
villeah 1:9cd48cdf8681 29
villeah 1:9cd48cdf8681 30 if(!ch){
villeah 1:9cd48cdf8681 31 return false;
villeah 1:9cd48cdf8681 32 } //No Ack, return False
villeah 1:9cd48cdf8681 33
villeah 1:9cd48cdf8681 34 ch=i2c.write(0x07); //device ram address where Tobj value is present
villeah 1:9cd48cdf8681 35
villeah 1:9cd48cdf8681 36 if(!ch){
villeah 1:9cd48cdf8681 37 return false;
villeah 1:9cd48cdf8681 38 }
villeah 1:9cd48cdf8681 39
villeah 1:9cd48cdf8681 40 i2c.start(); //repeat start
villeah 1:9cd48cdf8681 41 ch=i2c.write(i2caddress|0x01); //device address with read condition
villeah 1:9cd48cdf8681 42 if(!ch){
villeah 1:9cd48cdf8681 43 return false;
villeah 1:9cd48cdf8681 44 }
villeah 1:9cd48cdf8681 45
villeah 1:9cd48cdf8681 46 p1=i2c.read(1); //Tobj low byte
villeah 1:9cd48cdf8681 47 p2=i2c.read(1); //Tobj heigh byte
villeah 1:9cd48cdf8681 48 p3=i2c.read(0); //PEC
villeah 1:9cd48cdf8681 49
villeah 1:9cd48cdf8681 50 i2c.stop(); //stop condition
villeah 1:9cd48cdf8681 51
villeah 1:9cd48cdf8681 52
villeah 1:9cd48cdf8681 53 temp_thermo=((((p2&0x007f)<<8)+p1)*0.02)-0.01; //degree centigrate conversion
villeah 1:9cd48cdf8681 54 *temp_val=temp_thermo-273; //Convert kelvin to degree Celsius
villeah 1:9cd48cdf8681 55
villeah 1:9cd48cdf8681 56 return true; //load data successfully, return true
villeah 1:9cd48cdf8681 57 }
villeah 1:9cd48cdf8681 58
villeah 1:9cd48cdf8681 59
villeah 1:9cd48cdf8681 60 float IRTemp = 0; //temperature in degrees C
villeah 1:9cd48cdf8681 61 float AnalogTemp;
villeah 1:9cd48cdf8681 62 float IRTempTemp;
villeah 1:9cd48cdf8681 63
villeah 1:9cd48cdf8681 64 void anaali(char* input, char* output);
villeah 1:9cd48cdf8681 65 void digit(char* input, char* output);
villeah 1:9cd48cdf8681 66
villeah 1:9cd48cdf8681 67 RPCFunction rpc_anal(&anaali, "rpc_anal");
villeah 1:9cd48cdf8681 68 RPCFunction rpc_digi(&digit, "rpc_digi");
villeah 1:9cd48cdf8681 69
villeah 1:9cd48cdf8681 70
villeah 1:9cd48cdf8681 71 int main() {
villeah 1:9cd48cdf8681 72
villeah 1:9cd48cdf8681 73 wait(1);
villeah 1:9cd48cdf8681 74 while (1) {
villeah 1:9cd48cdf8681 75 myled=1;
villeah 1:9cd48cdf8681 76 if (getTemp(&IRTempTemp)) {
villeah 1:9cd48cdf8681 77 myled=0;
villeah 1:9cd48cdf8681 78 }
villeah 1:9cd48cdf8681 79 wait(0.5);
villeah 1:9cd48cdf8681 80 if(IRTempTemp < 150) //discard clearly wrong values from trying to read the sensor when it's not ready
villeah 1:9cd48cdf8681 81 IRTemp = IRTempTemp;
villeah 1:9cd48cdf8681 82 }
villeah 1:9cd48cdf8681 83 }
villeah 1:9cd48cdf8681 84
villeah 1:9cd48cdf8681 85 void anaali(char* input, char* output){
villeah 1:9cd48cdf8681 86 sprintf(output, "%f", (float)ain); //prints the analog sensor value to the output stream
villeah 1:9cd48cdf8681 87 }
villeah 1:9cd48cdf8681 88
villeah 1:9cd48cdf8681 89 void digit(char* input, char* output){
villeah 1:9cd48cdf8681 90 sprintf(output, "%f", IRTemp); //prints the digital sensor value to the output stream
villeah 1:9cd48cdf8681 91 }