2018-07-14 11:46 AM
I've recently been trying to get readings from an LSM303DLHC chip using my F411RE board but nothing seems to work. I've been on this task for 2 days and I'm getting nada. The issue is actually receiving the data as apparently everything seems to be fine when transmitting data to the registers when initialising.
Every time the functions return HAL_OK but using openocd and debugging the code no sensible result for the linear accelerometer on the x axis seems to come out. It tends to be 0 regardless of the angle the chip is at. I'm completely stumped so any form of help would be grateful.
Cheers in advanced!
int main(void)
{
HAL_Init();
uint16_t addr_acc = 0x32;
SystemClock_Config();
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct;/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();I2C_HandleTypeDef imu;
imu.Instance = I2C1; imu.Init.ClockSpeed = 400000; imu.Init.DutyCycle = I2C_DUTYCYCLE_2; imu.Init.OwnAddress1 = 0; imu.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; imu.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; imu.Init.OwnAddress2 = 0; imu.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; imu.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);if (HAL_I2C_Init(&imu) != HAL_OK)
{ _Error_Handler(__FILE__, __LINE__); }unsigned char* buffer;
buffer[0] = 0x20; buffer[1] = 0x27; HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(&imu, addr_acc, buffer, 2, HAL_MAX_DELAY);buffer[0] = 0x23;
buffer[1] = 0x40;
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(&imu, addr_acc, buffer, 2, HAL_MAX_DELAY);
while (1) { unsigned char* reg;reg = 0x28;
status = HAL_I2C_Master_Transmit(&imu, addr_acc, reg, 1, HAL_MAX_DELAY);uint8_t* buff;
status = HAL_I2C_Master_Receive(&imu, addr_acc << 1,buff
, 2, HAL_MAX_DELAY);uint16_t x =
buff
[1] << 8 |buff
[0]; }}#f411re #lsm303dlhc2018-07-15 04:07 PM
this part is already marked :
Part StatusObsolete
I just put a LIS3M and 3D on my board...
the IIC interface uses 7 bit addressing:
try this: status = HAL_I2C_Master_Transmit(&imu, addr_acc << 1 , reg, 1, HAL_MAX_DELAY);