int main(void) { /*==================[Inicializacion]==========================*/ uint8_t wbuf[2] = {0,0}; uint8_t rbuf[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //Se lo crea con 6 posiciones porque los registros que se procesan de los sensores son : /* ACCEL_XOUT_H ACCEL_XOUT_L ACCEL_YOUT_H ACCEL_YOUT_L ACCEL_ZOUT_H ACCEL_ZOUT_L TEMP_OUT_H TEMP_OUT_L GYRO_XOUT_H GYRO_XOUT_L GYRO_YOUT_H GYRO_YOUT_L GYRO_ZOUT_H GYRO_ZOUT_L */ uint16_t samples[7] = {0,0,0,0,0,0,0}; //cada posicion es de 16 bits, necesario para guardar //la parte low y high de las muestras de accel I2C_XFER_T xfer; initHardware(); /*==================[Configuracion del I2C_XFER_T]==========================*/ // Metodo de Escritura //Define el registro que se va a leer en la primera posicion de wbuf //wbuf[0] = MPU6050_RA_ACCEL_XOUT_H; //Parte high de la lectura en x del acelerometro //Como la lectura se realiza de forma secuencial, en la posicion 0 //del rbuf ira este dato y en la posicion 1 ira la correspondiente a //la posicion siguiente, osea ACCEL_XOUT_L (0x3C) MPU6050_wakeup(&xfer); //Lectura de PWR_MGMMT_1 2 (para verificar si se lo saco del sleep y de standby a los ejes) wbuf[0] = MPU6050_RA_PWR_MGMT_1; I2C_XFER_config(&xfer, rbuf, 2, MPU6050_I2C_SLAVE_ADDRESS, 0, wbuf, 1); //Configuracion de la 1era direccion desde la que se leeran los valores de los registros de los sensores wbuf[0]=MPU6050_RA_ACCEL_XOUT_H; wbuf[1]=0; while(1) { I2C_XFER_config(&xfer, rbuf, 14, MPU6050_I2C_SLAVE_ADDRESS, 0, wbuf, 1); Fill_Samples(&samples, &rbuf); } return 0; }
void registers_setup_MPU6050(void) { I2C_XFER_T xfer; uint8_t rbuf[1]={0}; uint8_t wbuf1[] = { MPU6050_RA_SELF_TEST_X,//first register (self_text_x) 0,0,0,0 //self test registers ,0 // 00000000 /sampling rate setting, 1kHz/(0+1)=1kHz ,1 // 00 000 001 / xx [extsync] [dlfp] / digital LPF setting, 1kHz-184Hz band Accel & 1kHz-188Hz band Gyro ,0 // 000 00 000 / [STx][y][z] [FS] 000 / gyro scale setting, 250deg/s ,0 // 000 00 000/ [STx][y][z] [FS] 000 / acc scale setting, 2g //MOT_THR queda igual como viene default, no lo uso // FIFO setting, hago que guarde en la fifo todos los bytes de los registros de gyro y accel ,78//0 111 1 000 [temp][gyro x][y][z] [accxyz] [slv2][1][0] / FIFO mask ,2 // 0 0 000 010 / - [FIFOen] [I2CMST][I2CDIS]- [FIFOreset][I2CMSTrst][SIGreset] / I2C Master Reset }; uint8_t wbuf2[] = {MPU6050_RA_USER_CTRL,0x40}; ///0 1 000 000 / - [FIFOen] [I2CMST][I2CDIS]- [FIFOreset][I2CMSTrst][SIGreset] / Enable FIFO I2C_XFER_config(&xfer, rbuf, 0, ADDRESS_MPU, 0, wbuf1, (int) sizeof(wbuf1)/sizeof(uint8_t)); I2C_XFER_config(&xfer, rbuf, 0, ADDRESS_MPU, 0, wbuf2, (int) sizeof(wbuf2)/sizeof(uint8_t)); }
/* * Se encarga de inicializar los registros de PWR_MGMT necesarios para habilitar los * sensores (acelerometro y giroscopo) en cada eje, para sus lecturas. * La configuracion en la que quedan seteados es la por defecto: * accelerometer (±2g) , gyroscope (±250°/sec). * * * xfer : Puntero a la estructura del tipo I2C_XFER_T necesaria para la utilizacion de Chip_I2C_MasterTransfer. * Chip_I2C_MasterTransfer : Funcion que resuelve la interaccion i2c en funcion de lo especificado en la estructura I2C_XFER_T */ void MPU6050_wakeup(I2C_XFER_T * xfer) { //Setea PWR_MGMT_1 y 2 en 0, el byte de cada uno uint8_t wbuf[3] = {MPU6050_RA_PWR_MGMT_1, 0, 0}; /*xfer->slaveAddr = MPU6050_DEVICE_ADDRESS; xfer->txBuff = wbuf; xfer->txSz = 3; xfer->rxSz = 0;*/ I2C_XFER_config(xfer, xfer->rxBuff, 0, MPU6050_I2C_SLAVE_ADDRESS, 0, wbuf, 3); }
/* * Esta funcion setea todos los registros que necesito para leer datos de la FIFO * Y FUNCIONA */ void reg_set_regs_inet(void) { I2C_XFER_T xfer;//CHECK uint8_t rbuf[]={0,0};//CHECK uint8_t * fifo_count_data = NULL; uint8_t wbuf[]={0};//CHECK uint16_t packet_count, fifo_count, i=0; // reset device, reset all registers, clear gyro and accelerometer bias registers I2C_WriteByte(ADDRESS_MPU, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device Delay(100); //El valor por defecto del registro luego del reset es el 0x40 //CHECK wbuf[0]=MPU6050_RA_PWR_MGMT_1; I2C_XFER_config(&xfer, rbuf, 2, ADDRESS_MPU, 0, wbuf, 1); // get stable time source // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001 I2C_WriteByte(ADDRESS_MPU, PWR_MGMT_1, 0x01); I2C_WriteByte(ADDRESS_MPU, PWR_MGMT_2, 0x00); Delay(200); // Configure device for bias calculation I2C_WriteByte(ADDRESS_MPU, INT_ENABLE, 0x00); // Disable all interrupts I2C_WriteByte(ADDRESS_MPU, FIFO_EN, 0x00); // Disable FIFO I2C_WriteByte(ADDRESS_MPU, PWR_MGMT_1, 0x00); // Turn on internal clock source I2C_WriteByte(ADDRESS_MPU, I2C_MST_CTRL, 0x00); // Disable I2C master I2C_WriteByte(ADDRESS_MPU, USER_CTRL, 0x00); // Disable FIFO and I2C master modes I2C_WriteByte(ADDRESS_MPU, USER_CTRL, 0x0C); // Reset FIFO and DMP Delay(15); // Configure MPU6050 gyro and accelerometer for bias calculation I2C_WriteByte(ADDRESS_MPU, CONFIG, 0x01); // Set low-pass filter to 188 Hz I2C_WriteByte(ADDRESS_MPU, SMPLRT_DIV, 0xFF); // Set sample rate to 1 kHz I2C_WriteByte(ADDRESS_MPU, GYRO_CONFIG, 0x00); // Set gyro full-scale to 250 degrees per second, maximum sensitivity I2C_WriteByte(ADDRESS_MPU, ACCEL_CONFIG, 0x00); // Set accelerometer full-scale to 2 g, maximum sensitivity // Configure FIFO to capture accelerometer and gyro data for bias calculation I2C_WriteByte(ADDRESS_MPU, USER_CTRL, 0x40); // Enable FIFO I2C_WriteByte(ADDRESS_MPU, FIFO_EN, 0xF8); // Enable gyro and accelerometer sensors for FIFO (max size 1024 bytes in MPU-6050) //Ahora puedo leer los datos que estan en la FIFO }