base program for tilt measurement

Dependencies:   COG4050_ADT7420

Fork of COG4050_adxl355_adxl357 by ADI_CAC

Revision:
1:d3aeaa02781d
Parent:
0:74a0756399ff
Child:
2:14dc1ec57f3b
--- a/main.cpp	Tue Jun 05 13:03:07 2018 +0000
+++ b/main.cpp	Tue Aug 07 07:20:36 2018 +0000
@@ -1,97 +1,72 @@
 #include "mbed.h"
 
-#define ADT7420 0x48
-
-#define RESET_REG 0x2F
-#define TEMP_REG 0x00
-#define ID_REG 0x0B
+const static uint8_t _WRITE_REG_CMD = 0x0A; // write register
+const static uint8_t _READ_REG_CMD = 0x0B; // read register
+const static uint8_t _DUMMY_BYTE = 0xAA;
 
 
-DigitalOut led1(LED1);
-DigitalOut sensor(D8);
+SPI adxl362(SPI1_MOSI, SPI1_MISO, SPI1_SCLK);
+DigitalOut CS(SPI1_CS3);
 
-I2C Myi2c(I2C_SDA, I2C_SCL);
+void adxl362_reset(void);
+int adxl362_GetID(void);
+void adxl362_write_reg(uint8_t reg, uint8_t data);
+uint8_t adxl362_read_reg(uint8_t reg);
 
 Serial pc(USBTX, USBRX);
 
-void ADT7420_reset(void);
-int ADT7420_GetID(void);
-int ADT7420_GetTemp(void);
-
-// main() runs in its own thread in the OS
 int main() {
-    float status;
-    int res;
-    sensor=1;
-    //configure for UART
-    pc.baud(115200);
-    pc.printf("I2C ADT7420 Demo\n");
-   
-    //configure for I2C @400KHz
-    Myi2c.frequency(100000);
-   
-    //configure for ADT7420
+    pc.baud(9600);
+    pc.printf("SPI ADXL362 Demo\n");
+    CS = 1;
+    adxl362.lock();
     
-    pc.printf("[RESET ] ADT7420\n"); 
-    ADT7420_reset();
-    wait(0.5);
-
-    pc.printf("[CHECK ID] ADT7420\n"); 
-    ADT7420_GetID();
-
-    
-    while (true) {
-        led1 = 0;
-        
-        pc.printf("[Get Temp] ADT7420\n"); 
-        res = ADT7420_GetTemp();
-        
-        if(res & 0x1000)
-        {
-            status = (float)((res - 8192) / 16);
-        }
-        else
-        {
-            status = (float)(res / 16);
-        }
-        
-        pc.printf("Current Temp %.0f \n", status);
-            
-        wait(0.5);
-        led1 = 1;
-        wait(0.5);
+    //adxl362_reset();
+    wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
+    //adxl362.set_mode(ADXL362::MEASUREMENT);
+    uint8_t x,y,z; 
+    while(1) {
+        x=adxl362_GetID();
+        //y=adxl362.scany_u8();
+        //z=adxl362.scanz_u8();
+        //printf("x = %x y = %x z = %x\r\n",x,y,z);
+        printf("id = %x \r\n",x);
+        wait_ms(10);
     }
-    
 }
 
-void ADT7420_reset(void)
-{
-    char cmd_reset[1];
-    cmd_reset[0] = RESET_REG;
-    Myi2c.write(ADT7420, cmd_reset, 1);
-}
+void adxl362_reset(void)
+{   // format
+    adxl362.format(8,0);
+    adxl362.frequency(5e6);
     
-int 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;
+    adxl362_write_reg(0x1F, 0x52);
 }
+ 
+void adxl362_write_reg(uint8_t reg, uint8_t data)
+{   
+    adxl362.format(8, 0);
+    CS = 0;
+    adxl362.write(_WRITE_REG_CMD);
+    adxl362.write(static_cast<uint8_t>(reg));
+    adxl362.write(static_cast<uint8_t>(data));
+    CS = 1;
+}  
 
-int ADT7420_GetTemp(void)
+uint8_t adxl362_read_reg(uint8_t reg)
 {
-    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);
-    pc.printf("Temp Upper %02x\n", read_val[0]);
-    pc.printf("Temp Lower %02x\n", read_val[1]);
-    
-    temp = (int)((read_val[0] << 8) | read_val[1]);
-    temp >>= 3;
-    return temp;
-}
+    uint8_t ret_val;
+    CS = 0;
+    adxl362.format(8, 0);
+    adxl362.write(_READ_REG_CMD);
+    adxl362.write(reg);
+    ret_val = adxl362.write(_DUMMY_BYTE);
+    CS = 1;
+    return ret_val;
+} 
+int adxl362_GetID(void)
+{
+    uint8_t ret_val;    
+    ret_val = adxl362_read_reg(0x00);
+    return ret_val; 
+}
\ No newline at end of file