Tengo problemas con la lectura de datos de MPU6050 a la placa del núcleo STM32F4. La conexión es con I2C. El problema es que en la primera función IsDeviceReady, el dispositivo se reconoce y el LED se enciende, pero cuando configuro los datos en i2cBuf [1] = 0x08 y los envío, después de recibir esos datos, i2cBuf [1] se configura en 0x00 - así como se establece antes de recibir los datos. ¿Tal vez sabes cuál podría ser el problema? Gracias.
Código:
#include "stm32f4xx_hal.h"
#include <string.h>
void GPIO_Config(void);
void I2C_Config(void);
void UART_Config(void);
void DMA_Config(void);
#define mpu6050Address 0xD0
I2C_HandleTypeDef MBI2Chandle;
UART_HandleTypeDef MBUARThandle;
DMA_HandleTypeDef MBDMA_Uart2Handle;
//UART Test
char txData[40] = "Hello from STM\r\n";
char rxData[40];
//I2C variables
uint8_t i2cBuf[8];
int16_t ax,ay,az;
float Xaccel, Yaccel, Zaccel;
int main(void)
{
//HAL Initialise
HAL_Init();
//Config functions
GPIO_Config();
I2C_Config();
UART_Config();
DMA_Config();
//1. Scan the I2C adresses
for(uint8_t i=0; i<255; i++)
{
if(HAL_I2C_IsDeviceReady(&MBI2Chandle, i, 1, 10) == HAL_OK) //test for the slave address
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
break;
}
}
//2. I2C Write example
//a) Set accelerometer range (reg28)
i2cBuf[0] = 28; //Register address: Accelerometer config 1
i2cBuf[1] = 0x08; //Data to write, +-4g range
HAL_I2C_Master_Transmit(&MBI2Chandle, mpu6050Address, i2cBuf, 2, 10);
//3. I2C Read example
//Request to read from a register (reg 28)
i2cBuf[0] = 28; //Register address: Accelerometer config 1
HAL_I2C_Master_Transmit(&MBI2Chandle, mpu6050Address, i2cBuf, 1, 10);
//Read data
i2cBuf[1] = 0x00;
HAL_I2C_Master_Receive(&MBI2Chandle, mpu6050Address|0x01, &i2cBuf[1], 1, 10);
while(1)
{
/*
//4.Read accelerometer data
//Request to read from a register
i2cBuf[0] = 0x3B; //Register address: X_axis H
HAL_I2C_Master_Transmit(&MBI2Chandle, mpu6050Address, i2cBuf, 1, 10);
//Read data
i2cBuf[1] = 0x00;
HAL_I2C_Master_Receive(&MBI2Chandle, mpu6050Address|0x01, &i2cBuf[1], 6, 10);
ax= -(i2cBuf[1]<<8 | i2cBuf[2]);
ay= -(i2cBuf[3]<<8 | i2cBuf[4]);
az= -(i2cBuf[5]<<8 | i2cBuf[6]);
Xaccel = ax/8192.0;
Yaccel = ay/8192.0;
Zaccel = az/8192.0;
HAL_Delay(50);
*/
//UART Test
HAL_UART_Transmit(&MBUARThandle, (uint8_t *)txData, strlen(txData), 10);
HAL_Delay(500);
}
}
void GPIO_Config(void)
{
//Enable Ports clocks
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
//Init typeDef
GPIO_InitTypeDef MBPinInit;
//LED pins config
//PB7 BLUE LED
MBPinInit.Pin = GPIO_PIN_7;
MBPinInit.Mode = GPIO_MODE_OUTPUT_PP;
MBPinInit.Pull = GPIO_NOPULL;
MBPinInit.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &MBPinInit);
//I2C pins config
//I2C1_SCL PB8, I2C1_SDA PB9
MBPinInit.Pin = GPIO_PIN_8 | GPIO_PIN_9;
MBPinInit.Mode = GPIO_MODE_AF_OD;
MBPinInit.Pull = GPIO_PULLUP;
MBPinInit.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
MBPinInit.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &MBPinInit);
//Systick interrupt enable for HAL_Delay function
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
void UART_Config(void)
{
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
GPIO_InitTypeDef MBUartDef;
MBUartDef.Pin = GPIO_PIN_5 | GPIO_PIN_6;
MBUartDef.Mode = GPIO_MODE_AF_PP;
MBUartDef.Pull = GPIO_PULLUP;
MBUartDef.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
MBUartDef.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOD, &MBUartDef);
//UART Configuration
MBUARThandle.Instance = USART2;
MBUARThandle.Init.BaudRate = 115200;
MBUARThandle.Init.Mode = UART_MODE_TX_RX;
MBUARThandle.Init.WordLength = UART_WORDLENGTH_8B;
MBUARThandle.Init.StopBits = UART_STOPBITS_1;
MBUARThandle.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&MBUARThandle);
//System interrupt enable
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
void I2C_Config(void)
{
//Enable I2C peripheral clock
__HAL_RCC_I2C1_CLK_ENABLE();
MBI2Chandle.Instance = I2C1;
MBI2Chandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
MBI2Chandle.Init.ClockSpeed = 100000;
MBI2Chandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
MBI2Chandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
MBI2Chandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
MBI2Chandle.Init.GeneralCallMode = I2C_NOSTRETCH_DISABLED;
MBI2Chandle.Init.OwnAddress1 = 0;
MBI2Chandle.Init.OwnAddress2 = 0;
HAL_I2C_Init(&MBI2Chandle);
}
void SysTick_Handler(void)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
}
void DMA_Config(void)
{
__HAL_RCC_DMA1_CLK_ENABLE();
MBDMA_Uart2Handle.Instance = DMA1_Stream5; //pdf str 196
MBDMA_Uart2Handle.Init.Channel = DMA_CHANNEL_4;
MBDMA_Uart2Handle.Init.Direction = DMA_PERIPH_TO_MEMORY;
MBDMA_Uart2Handle.Init.PeriphInc = DMA_PINC_DISABLE;
MBDMA_Uart2Handle.Init.MemInc = DMA_MINC_ENABLE;
MBDMA_Uart2Handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
MBDMA_Uart2Handle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
MBDMA_Uart2Handle.Init.Mode = DMA_CIRCULAR;
MBDMA_Uart2Handle.Init.Priority = DMA_PRIORITY_LOW;
MBDMA_Uart2Handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HAL_DMA_Init(&MBDMA_Uart2Handle);
__HAL_LINKDMA(&MBUARThandle, hdmarx, MBDMA_Uart2Handle);
//Enable DMA1 Stream 5 interrupt
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
}
void DMA2_Stream5_IRQHandler(void)
{
HAL_DMA_IRQHandler(&MBDMA_Uart2Handle);
}