I2C&ThermalSensor

Dependents:   COG4050_blink COG4050_adxl35x_adxrs290 COG4050_adxrs290_adxrs453 COG4050_adxl355_adxl357 ... more

Files at this revision

API Documentation at this revision

Comitter:
APS_Lab
Date:
Tue May 14 02:00:12 2019 +0000
Parent:
0:74a0756399ff
Commit message:
Latest 2019.05.14

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jun 05 13:03:07 2018 +0000
+++ b/main.cpp	Tue May 14 02:00:12 2019 +0000
@@ -15,8 +15,11 @@
 Serial pc(USBTX, USBRX);
 
 void ADT7420_reset(void);
-int ADT7420_GetID(void);
+char ADT7420_GetID(void);
 int ADT7420_GetTemp(void);
+int ADT7420_RegDump(void);
+
+char ALLREG[16];
 
 // main() runs in its own thread in the OS
 int main() {
@@ -27,11 +30,12 @@
     pc.baud(115200);
     pc.printf("I2C ADT7420 Demo\n");
    
-    //configure for I2C @400KHz
-    Myi2c.frequency(100000);
+    //configure for I2C @100KHz
+    //Myi2c.init();
+    Myi2c.frequency(10000);
    
     //configure for ADT7420
-    
+    Myi2c.lock();
     pc.printf("[RESET ] ADT7420\n"); 
     ADT7420_reset();
     wait(0.5);
@@ -48,37 +52,69 @@
         
         if(res & 0x1000)
         {
-            status = (float)((res - 8192) / 16);
+            status = (float)(res * 0.0625 * -1.0);
+            
         }
         else
         {
-            status = (float)(res / 16);
+            status = (float)(res * 0.0625);
         }
         
-        pc.printf("Current Temp %.0f \n", status);
+        pc.printf("Current Temp %.4f C\n", status);
             
         wait(0.5);
         led1 = 1;
         wait(0.5);
+        
+        ADT7420_RegDump();
     }
     
 }
 
 void ADT7420_reset(void)
 {
-    char cmd_reset[1];
-    cmd_reset[0] = RESET_REG;
-    Myi2c.write(ADT7420, cmd_reset, 1);
+    char cmd_reset;
+    cmd_reset = RESET_REG;
+    Myi2c.write(ADT7420);
+    if(Myi2c.write(0xFF))
+    {
+        pc.printf("Ack \n");
+    }
+    else
+    {
+        pc.printf("Nack \n");
+    }
+    
+    Myi2c.write(ADT7420);
+    if(Myi2c.write(cmd_reset))
+    {
+        pc.printf("Reset Done\n");
+    }
+    else
+    {
+        pc.printf("Reset Failure\n");
+    }
 }
     
-int ADT7420_GetID(void)
+char ADT7420_GetID(void)
 {
-    char cmd_id[1], read_val;
-    cmd_id[0]=ID_REG;
-    Myi2c.write(ADT7420, cmd_id, 1, 1);
-    Myi2c.read(ADT7420, &read_val, 1);
-    pc.printf("ID %02x\n", read_val);
-    return read_val;
+    char cmd_id, read_val;
+    int sts;
+    cmd_id=ID_REG;
+    //Myi2c.write(ADT7420, &cmd_id, 1, 0);
+    sts = Myi2c.read(ADT7420, &cmd_id, 1, 0);
+    if(sts==0)
+    {
+        pc.printf("ID ACK\n");
+    }
+    else
+    {
+        pc.printf("ID NACK\n");
+        pc.printf("Return Value STS %d \n", sts);
+    }
+    Myi2c.stop();
+    pc.printf("ID REG %02x\n", cmd_id);
+    return cmd_id;
 }
 
 int ADT7420_GetTemp(void)
@@ -86,8 +122,10 @@
     char cmd_temp[1], read_val[2];
     int temp=0;
     cmd_temp[0]=TEMP_REG;
-    Myi2c.write(ADT7420, cmd_temp, 1, 1);
-    Myi2c.read(ADT7420, read_val, 2);
+    Myi2c.start();
+    Myi2c.write(ADT7420, cmd_temp, 2);
+    wait(0.2);
+    Myi2c.read(ADT7420, read_val, 3);
     pc.printf("Temp Upper %02x\n", read_val[0]);
     pc.printf("Temp Lower %02x\n", read_val[1]);
     
@@ -95,3 +133,26 @@
     temp >>= 3;
     return temp;
 }
+
+int ADT7420_RegDump(void)
+{
+    int idx, result, status;
+    char reg[1], data[16];
+    reg[0]=0x09;
+    Myi2c.write(ADT7420,reg, 1, 0);
+    wait(0.5);
+    status = Myi2c.read(ADT7420, data, sizeof(data));
+    
+    //if(status > 10) {
+
+        for(idx=0; idx < 16; idx++)
+        {
+            pc.printf("Reg ID %d : Reg Val %02x\n", idx+4, data[idx]); 
+        }
+    //}
+    //else
+    //{
+    //    pc.printf("Return value %d\n", status);
+    //}
+    //return 0;
+}
\ No newline at end of file