前言:STM32L4、BMI160
学习笔记
原理图
BMI160有两种通信模式,上电默认IIC,使用SPI模式需要CSB引脚检测到一个上升沿。在寄存器0x70中可设置spi_en位使接口永久设置成SPI。
cubeMX配置
在spi.c中添加
/* USER CODE BEGIN 1*/
uint8_t SPI_ReadWriteByte(uint8_t TxData)
{
uint8_t Rxdata;
HAL_SPI_TransmitReceive(&hspi1,&TxData,&Rxdata,1,1000);
return Rxdata;
}
/* USER CODE END 1*/
配置传感器
void BMI160_config(void)
{
// BMI160_Write_Reg(0x40,);
BMI160_Write_Reg(0x41,0x08);//acc_range 8g
BMI160_Write_Reg(0x42,0x26);//gyr_conf 25hz
BMI160_Write_Reg(0x43,0x03);//gyr_range 250
BMI160_Write_Reg(0x47,0xfe);//enable
BMI160_Write_Reg(0x7e,0x11);//acc normal mode
BMI160_Write_Reg(0x7e,0x15);//gry normal mode
CS_HIGH;
}
读写函数
//BMI160写
uint8_t BMI160_Write_Reg(uint8_t reg,uint8_t value)
{
uint8_t status;
CS_LOW;
status =SPI_ReadWriteByte(reg & 0x7f);
SPI_ReadWriteByte(value);
CS_HIGH;
return(status);
}
//BMI160读
uint8_t BMI160_Read_Reg(uint8_t reg)
{
uint8_t reg_val;
CS_LOW ;
SPI_ReadWriteByte(reg | 0x80);
reg_val=SPI_ReadWriteByte(0xFF);
CS_HIGH;
return(reg_val);
}
逻辑分析仪