Firmware Test of Tilt Sense using BMI160

Dependencies:   mbed TI_ADS1220 ESP8266

Fork of GeoDynamics by GeoDynamics Hibrid Seismograph

Revision:
0:aa129e8d6912
Child:
1:268fc98970bb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 22 12:57:40 2016 +0000
@@ -0,0 +1,104 @@
+#include "mbed.h"
+
+
+/* defines the axis for acc */
+#define ACC_NOOF_AXIS       3
+#define GYR_NOOF_AXIS       3
+
+/* bmi160 slave address */
+#define BMI160_ADDR         ((0x68)<<1)
+
+#define M_PI           3.1415927
+ 
+Serial pc(USBTX, USBRX); // tx, rx
+
+I2C i2c(P2_3, P2_4);
+
+
+/* buffer to store acc samples */
+int16_t acc_sample_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+int16_t gyr_sample_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+
+double acc_result_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+double gyr_result_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+
+char i2c_reg_buffer[2] = {0};
+ 
+int main() {
+    pc.printf("Teste BMI160\n\r");
+    pc.printf("Configurando BMI160...\n\r");
+    wait_ms(250);
+    
+    /*Config Freq. I2C Bus*/
+    i2c.frequency(20000);
+    
+    /*Reset BMI160*/
+    i2c_reg_buffer[0] = 0x7E;
+    i2c_reg_buffer[1] = 0xB6;    
+    i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    wait_ms(200);
+    pc.printf("BMI160 Resetado\n\r");
+    
+    /*Habilita o Acelerometro*/
+    i2c_reg_buffer[0] = 0x7E;
+    i2c_reg_buffer[1] = 0x11; //PMU Normal   
+    i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    pc.printf("Acc Habilitado\n\r");
+    
+    /*Habilita o Giroscopio*/
+    i2c_reg_buffer[0] = 0x7E;
+    i2c_reg_buffer[1] = 0x15;  //PMU Normal 
+    i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    pc.printf("Gyr Habilitado\n\r");
+    
+    /*Config o Data Rate ACC em 100Hz*/
+    i2c_reg_buffer[0] = 0x40;
+    i2c_reg_buffer[1] = 0x28;    
+    i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    pc.printf("Data Rate ACC Selecionado a 100Hz\n\r");
+    
+    /*Config o Data Rate GYR em 100Hz*/
+    i2c_reg_buffer[0] = 0x42;
+    i2c_reg_buffer[1] = 0x28;    
+    i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    pc.printf("Data Rate GYR Selecionado a 100Hz\n\r");
+    
+    wait_ms(2000);
+    pc.printf("BMI160 Configurado\n\r");
+    
+        
+    while(1) {
+        
+        /*Le os Registradores do Acelerometro*/
+        i2c_reg_buffer[0] = 0x12;
+        i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true);
+        i2c.read(BMI160_ADDR, (char *)&acc_sample_buffer, sizeof(acc_sample_buffer), false);
+        
+        /*Le os Registradores do Giroscopio*/
+        i2c_reg_buffer[0] = 0x0C;
+        i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true);
+        i2c.read(BMI160_ADDR, (char *)&gyr_sample_buffer, sizeof(gyr_sample_buffer), false);
+        
+        /*Ajusta dados brutos Acelerometro e transforma m/s^2*/
+        acc_result_buffer[0] = (acc_sample_buffer[0]/16384.0)*9.80665;
+        acc_result_buffer[1] = (acc_sample_buffer[1]/16384.0)*9.80665;
+        acc_result_buffer[2] = (acc_sample_buffer[2]/16384.0)*9.80665;
+        
+        /*Ajusta dados Brutos do Giroscopio e transforma rad/s*/
+        gyr_result_buffer[0] = (gyr_sample_buffer[0]/16.4)*0.017453;
+        gyr_result_buffer[1] = (gyr_sample_buffer[1]/16.4)*0.017453;
+        gyr_result_buffer[2] = (gyr_sample_buffer[2]/16.4)*0.017453;
+                  
+                                 
+        
+        /*Imprime os dados ACC pre-formatados*/
+        pc.printf("AccXraw %f ", (acc_result_buffer[0]));
+        pc.printf("AccYraw %f ", (acc_result_buffer[1]));
+        pc.printf("AccZraw %f ", (acc_result_buffer[2]));
+        /*Imprime os dados GYR pre-formatados*/
+        pc.printf("GYRXraw %f ", (gyr_result_buffer[0]));
+        pc.printf("GYRYraw %f ", (gyr_result_buffer[1]));
+        pc.printf("GYRZraw %f\n\r", (gyr_result_buffer[2]));
+        wait_ms(100);
+    }
+}