Mateo Morales / Mbed 2 deprecated Acelerometro_facil

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalIn boton(USER_BUTTON);
00004 Serial pc(SERIAL_TX, SERIAL_RX);
00005 I2C i2c(PB_9, PB_8 );
00006 
00007 const int addr = 0xD0;      // 7 bit I2C address
00008 char cmd[2];
00009 char read_buffer[14];
00010 
00011 int16_t acc_x = 0, acc_y = 0, acc_z = 0;
00012 int16_t gyr_x = 0, gyr_y = 0, gyr_z = 0;
00013 int16_t temp = 0;
00014 
00015 float ACC_SEN = 16384.0;    //Resolución ACC
00016 float GYR_SEN = 131.0;      //Resolución GYR
00017 float TEM_SEN = 340.0;      //Resolución temp
00018 
00019 float facc_x = 0, facc_y = 0, facc_z = 0;
00020 float fgyr_x = 0, fgyr_y = 0, fgyr_z = 0;
00021 float ftemp = 0;
00022 
00023 
00024 int cont=0;
00025 int i,j;
00026 int limite;
00027 int limite1;
00028 
00029 Timer tiempo;
00030 int val_tiempo;
00031 
00032 float memoria[501][8];
00033 unsigned int cont_m=1;
00034 
00035 DigitalOut myled(LED1);
00036 
00037 
00038 void lec_escritura (void);
00039 
00040 int main()
00041 {
00042 
00043     pc.baud(115200);
00044 
00045     pc.printf("Prueba MPU6050 \n\r");
00046 
00047     cmd[0] = 0x6B;
00048     cmd[1] = 0x00;
00049     i2c.write(addr, cmd, 2);                //Desactivar modo hibernación
00050 
00051     cmd[0] = 0x1B;
00052     cmd[1] = 0x00;
00053     i2c.write(addr, cmd, 2);                //gyro full scale 250 DPS
00054 
00055     cmd[0] = 0x1C;
00056     cmd[1] = 0x00;
00057     i2c.write(addr, cmd, 2);                //ACC fullsclae 2G
00058 
00059 
00060 //////////////////////////////////////////////////////////////////////
00061 // determinacion de tiempos con timer 
00062  
00063  /*
00064  while(1)
00065  {
00066  
00067  tiempo.reset();
00068  tiempo.start();
00069  lec_escritura();
00070  
00071  val_tiempo=tiempo.read_us();
00072  pc.printf("Tiempo en us es= %d  \n\r",val_tiempo);
00073 
00074  tiempo.reset();
00075 }
00076 */
00077 
00078 
00079 
00080 ////////////////////////////////////////////////////////////////////////////////
00081     //pc.printf("Inicializando \n");
00082     while(1) {
00083         if(boton==0 && cont==0) {
00084             cont_m=1;
00085             while(boton==0);
00086 
00087             for(cont_m=1; cont_m<=100; cont_m++) {
00088                 lec_escritura();
00089             }
00090             for(int i=1; i<=100; i++) {
00091 
00092                 pc.printf("Registro # %d  ACCx = %.2f ACCy = %.2f ACCz = %.2f  GYRx = %.2f GYRy = %.2f GYRz = %.2f  Temp = %.2f \n\r",i,memoria[i][1],memoria[i][2],memoria[i][3],memoria[i][4],memoria[i][5],memoria[i][6],memoria[i][7]);
00093             }
00094             cont=1;
00095         }
00096 
00097 
00098         if(boton==0 && cont==1) {
00099             cont_m=1;
00100             while(boton==0);
00101 
00102             for(cont_m=1; cont_m<=500; cont_m++) {
00103                 lec_escritura();
00104             }
00105             for(int i=1; i<=500; i++) {
00106 
00107                 pc.printf("Registro # %d  ACCx = %.2f ACCy = %.2f ACCz = %.2f  GYRx = %.2f GYRy = %.2f GYRz = %.2f  Temp = %.2f \n\r",i,memoria[i][1],memoria[i][2],memoria[i][3],memoria[i][4],memoria[i][5],memoria[i][6],memoria[i][7]);
00108             }
00109             cont=0;
00110         }
00111 
00112     }
00113 }
00114 
00115 void lec_escritura(void)
00116 
00117 {
00118 
00119     //wait_ms(10);
00120 
00121     cmd[0]=0x3B;
00122     i2c.write(addr, cmd, 1);            //Escritura del registro de inicio
00123     i2c.read(addr, read_buffer, 14);    //Lectura en rafaga de los valores de la MPU
00124 
00125     //.................Construcción de la medición de los valores ..................
00126     acc_x = read_buffer[0]<<8 | read_buffer[1];
00127     acc_y = read_buffer[2]<<8 | read_buffer[3];
00128     acc_z = read_buffer[4]<<8 | read_buffer[5];
00129     temp  = read_buffer[6]<<8 | read_buffer[7];
00130     gyr_x = read_buffer[8]<<8 | read_buffer[9];
00131     gyr_y = read_buffer[10]<<8 | read_buffer[11];
00132     gyr_z = read_buffer[12]<<8 | read_buffer[13];
00133 
00134 
00135     //pc.printf("ACCx = %i ACCy = %i ACCz = %i \n\r", acc_x, acc_y, acc_z);
00136 
00137     facc_x = acc_x/ACC_SEN;
00138     facc_y = acc_y/ACC_SEN;
00139     facc_z = acc_z/ACC_SEN;
00140 
00141     //pc.printf("ACCx = %.2f ACCy = %.2f ACCz = %.2f \n\r", facc_x, facc_y, facc_z);
00142 
00143     fgyr_x = gyr_x/GYR_SEN;
00144     fgyr_y = gyr_y/GYR_SEN;
00145     fgyr_z = gyr_z/GYR_SEN;
00146 
00147 
00148     ftemp= (temp - 521)/TEM_SEN;
00149 
00150     // pc.printf("Registro # %d  ACCx = %.2f ACCy = %.2f ACCz = %.2f  GYRx = %.2f GYRy = %.2f GYRz = %.2f  Temp = %.2f \n\r",i, facc_x, facc_y, facc_z, fgyr_x, fgyr_y, fgyr_z,ftemp);
00151 
00152     memoria[cont_m][1]=facc_x;
00153     memoria[cont_m][2]=facc_y;
00154     memoria[cont_m][3]=facc_z;
00155     memoria[cont_m][4]=fgyr_x;
00156     memoria[cont_m][5]=fgyr_y;
00157     memoria[cont_m][6]=fgyr_z;
00158     memoria[cont_m][7]=ftemp;
00159 
00160     myled = !myled;
00161 }