首先,LIS3DH的II2地址:
I2C Device Address 8 bit format if SA0=0 -> 0x31(30也可以) if SA0=1 -> 0x33
void Init_LIS3DH( void )
{
uint8_t temp = 0;
if( HAL_I2C_Mem_Read(&hi2c2, DEVADDR, WHO_AM_I, I2C_MEMADD_SIZE_8BIT, &temp, 1, 10) == HAL_OK )
{
if( temp == 0x33 )
{
temp = 0x2f;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, CTRL_REG1,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
temp = 0x3f;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, CTRL_REG2,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
temp = 0x40;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, CTRL_REG3,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
temp = 0xa0;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, CTRL_REG4,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
temp = 0x0a;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, CTRL_REG5,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
temp = 0x02;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, CTRL_REG6,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
temp = INACTIVITYVAL*10/625;;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, INT1_THS,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
temp = 0;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, INT1_DURATION,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
HAL_I2C_Mem_Read(&hi2c2, DEVADDR, REFERENCE, I2C_MEMADD_SIZE_8BIT, &temp, 1, 10);
temp = 0x2A;
HAL_I2C_Mem_Write(&hi2c2, DEVADDR, INT1_CFG,I2C_MEMADD_SIZE_8BIT, &temp,1,10);
// 清除中断信号,注意首次使用前一定要这样操作,不然后面的中断产生不了
if( (GPIOE->IDR & 0x01 ) == 0 )
{
HAL_I2C_Mem_Read(&hi2c2, DEVADDR, INT1_SRC, I2C_MEMADD_SIZE_8BIT, &temp, 1, 10);
}
}
}
}
上面的代码用到的寄存器不多,主要是 CTRL_REG1,CTRL_REG2,CTRL_REG3,CTRL_REG4,CTRL_REG5,INT1_THS,INT1_DURATION,REFERENCE,INT1_CFG,INT1_SRC,他们的作用分别:
CTRL_REG1 -- 设置工作模式 ,及数据更新频率
CTRL_REG2 -- 开启高通滤波器,这一点对运动检测很有必要,运动检测是检测信号的变化,直流信号不关注,所示开启开通滤波,关注交流信号
CTRL_REG3 -- 开启中断1
CTRL_REG4 -- 设置测量范围
CTRL_REG5 -- 锁存中断信号
INT1_THS -- 中断阈值设置
INT1_DURATION -- 中断检测时间间隔设置
REFERENCE -- 虚拟操作
INT1_CFG -- 中断1配置
INT1_SRC -- 中断状态读取,在配置时需先确清楚现有中断