2022-04-18 08:51 PM
if(HAL_I2C_IsDeviceReady(&hi2c1,0xA6,3,100)==HAL_OK)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
HAL_Delay(500);
}
DATA_BUFF[0]=0x2D; //addr of POWER_CONTROL regiser
DATA_BUFF[1]=0x08; //set bit3 for measure mode 0000 1000
HAL_I2C_Master_Transmit(&hi2c1,0xA6,DATA_BUFF,2,100);
memset(DATA_BUFF,0,6);
DATA_BUFF[0]=0x31; //address of data_format reg
DATA_BUFF[1]=0x02; //set the device +/-8g range 0000 0010 //by puting range as 10
HAL_I2C_Master_Transmit(&hi2c1,0xA6, DATA_BUFF,2,100);
memset(DATA_BUFF,0,6);
//HAL_I2C_Master_Transmit(&hi2c1,0x32, (uint8_t *)20,2,100);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_I2C_Mem_Read(&hi2c1,0xA7,0x32,1,(uint8_t *)data_rec,6,100); //read 6 bits from 0x32
x = ((data_rec[1]<<8)|data_rec[0]);
y = ((data_rec[3]<<8)|data_rec[2]);
z = ((data_rec[5]<<8)|data_rec[4]);
HAL_Delay(100);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}