I2C&ThermalSensor

Dependents:   COG4050_blink COG4050_adxl35x_adxrs290 COG4050_adxl355_adxl357-ver2 COG4050_adxrs290_adxrs453 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define ADT7420 0x48
00004 
00005 #define RESET_REG 0x2F
00006 #define TEMP_REG 0x00
00007 #define ID_REG 0x0B
00008 
00009 
00010 DigitalOut led1(LED1);
00011 DigitalOut sensor(D8);
00012 
00013 I2C Myi2c(I2C_SDA, I2C_SCL);
00014 
00015 Serial pc(USBTX, USBRX);
00016 
00017 void ADT7420_reset(void);
00018 char ADT7420_GetID(void);
00019 int ADT7420_GetTemp(void);
00020 int ADT7420_RegDump(void);
00021 
00022 char ALLREG[16];
00023 
00024 // main() runs in its own thread in the OS
00025 int main() {
00026     float status;
00027     int res;
00028     sensor=1;
00029     //configure for UART
00030     pc.baud(115200);
00031     pc.printf("I2C ADT7420 Demo\n");
00032    
00033     //configure for I2C @100KHz
00034     //Myi2c.init();
00035     Myi2c.frequency(10000);
00036    
00037     //configure for ADT7420
00038     Myi2c.lock();
00039     pc.printf("[RESET ] ADT7420\n"); 
00040     ADT7420_reset();
00041     wait(0.5);
00042 
00043     pc.printf("[CHECK ID] ADT7420\n"); 
00044     ADT7420_GetID();
00045 
00046     
00047     while (true) {
00048         led1 = 0;
00049         
00050         pc.printf("[Get Temp] ADT7420\n"); 
00051         res = ADT7420_GetTemp();
00052         
00053         if(res & 0x1000)
00054         {
00055             status = (float)(res * 0.0625 * -1.0);
00056             
00057         }
00058         else
00059         {
00060             status = (float)(res * 0.0625);
00061         }
00062         
00063         pc.printf("Current Temp %.4f C\n", status);
00064             
00065         wait(0.5);
00066         led1 = 1;
00067         wait(0.5);
00068         
00069         ADT7420_RegDump();
00070     }
00071     
00072 }
00073 
00074 void ADT7420_reset(void)
00075 {
00076     char cmd_reset;
00077     cmd_reset = RESET_REG;
00078     Myi2c.write(ADT7420);
00079     if(Myi2c.write(0xFF))
00080     {
00081         pc.printf("Ack \n");
00082     }
00083     else
00084     {
00085         pc.printf("Nack \n");
00086     }
00087     
00088     Myi2c.write(ADT7420);
00089     if(Myi2c.write(cmd_reset))
00090     {
00091         pc.printf("Reset Done\n");
00092     }
00093     else
00094     {
00095         pc.printf("Reset Failure\n");
00096     }
00097 }
00098     
00099 char ADT7420_GetID(void)
00100 {
00101     char cmd_id, read_val;
00102     int sts;
00103     cmd_id=ID_REG;
00104     //Myi2c.write(ADT7420, &cmd_id, 1, 0);
00105     sts = Myi2c.read(ADT7420, &cmd_id, 1, 0);
00106     if(sts==0)
00107     {
00108         pc.printf("ID ACK\n");
00109     }
00110     else
00111     {
00112         pc.printf("ID NACK\n");
00113         pc.printf("Return Value STS %d \n", sts);
00114     }
00115     Myi2c.stop();
00116     pc.printf("ID REG %02x\n", cmd_id);
00117     return cmd_id;
00118 }
00119 
00120 int ADT7420_GetTemp(void)
00121 {
00122     char cmd_temp[1], read_val[2];
00123     int temp=0;
00124     cmd_temp[0]=TEMP_REG;
00125     Myi2c.start();
00126     Myi2c.write(ADT7420, cmd_temp, 2);
00127     wait(0.2);
00128     Myi2c.read(ADT7420, read_val, 3);
00129     pc.printf("Temp Upper %02x\n", read_val[0]);
00130     pc.printf("Temp Lower %02x\n", read_val[1]);
00131     
00132     temp = (int)((read_val[0] << 8) | read_val[1]);
00133     temp >>= 3;
00134     return temp;
00135 }
00136 
00137 int ADT7420_RegDump(void)
00138 {
00139     int idx, result, status;
00140     char reg[1], data[16];
00141     reg[0]=0x09;
00142     Myi2c.write(ADT7420,reg, 1, 0);
00143     wait(0.5);
00144     status = Myi2c.read(ADT7420, data, sizeof(data));
00145     
00146     //if(status > 10) {
00147 
00148         for(idx=0; idx < 16; idx++)
00149         {
00150             pc.printf("Reg ID %d : Reg Val %02x\n", idx+4, data[idx]); 
00151         }
00152     //}
00153     //else
00154     //{
00155     //    pc.printf("Return value %d\n", status);
00156     //}
00157     //return 0;
00158 }