关于LSI3DH的使用(转载)
https://blog.csdn.net/u012523921/article/details/107016750/
最近在调试lis3dh加速度计,网上一搜能找到很多资料,但是描述正确的,感觉不是很多,所以这里我来总结一下,也是在网友博客的基础上将正确的地方集中整理一下。
1. 首先说驱动,驱动网上流传的基本上就是一份,.h.c文件随便一搜就能下载到lis3dh的驱动,由于我的是公司电脑,文件是加密,就不上传驱动了,大家可以自行搜索其他的资源下载,驱动下载下来后,用户需要完成的就是底层SPI(这里我用的是SPI读写方法,IIC的没用过)的读写寄存器的函数,我把自己写的粘贴出来,大家参考一下,用的而是HAL函数,这里有一点需要说明一下,就是SPI的速率配置,开始我配置的6M,读取WHO_AM_I寄存器以及任何寄存器读出来都是0x88,检查很多地方都找不出原因来,后来偶然的情况下改成了3M,竟然读取ID成功了0x33, LIS3DH datasheet上写的最大支持spi速率是10MHz.看来实际支持不了这么大的速率。
另外,对于下载的驱动还有一点说明,要注意里面函数返回值的定义,原有驱动是:
typedef enum {
MEMS_SUCCESS = 0x01,
MEMS_ERROR = 0x00,
} status_t;
为了适应HAL库驱动,我给改成了:
typedef enum {
MEMS_SUCCESS = 0x00,
MEMS_ERROR = 0x01
} status_t;
另外驱动里面的每个函数返回值的判断是 :
if( !LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) )
return MEMS_ERROR;
我改成了:
if( LIS3DH_ReadReg(LIS3DH_FIFO_CTRL_REG, &value) ) //为了适应返回0就是成功,返回非0是失败。
return MEMS_ERROR;
-
/*注意:lis3dh波特率设置为3M可以读取正确,设置为8分频6M就不行*/
-
extern SPI_HandleTypeDef hspi2;
-
uint8_t LIS3DHInit(void)//SPI初始化
-
{
-
LIS3DH_CS_DISABLE; 取消片选
-
MX_SPI2_Init();
-
return HAL_OK;
-
}
-
/*******************************************************************************
-
* Function Name : LIS3DH_ReadReg
-
* Description : Generic Reading function. It must be fullfilled with either
-
* : I2C or SPI reading functions
-
* Input : Register Address
-
* Output : Data REad
-
* Return : None
-
*******************************************************************************/
-
uint8_t LIS3DH_ReadReg(uint8_t Reg, uint8_t* Data) {
-
-
uint8_t ret;
-
uint8_t txdata;
-
/* Start SPI transmission */
-
LIS3DH_CS_ENABLE;
-
/* Add read bit */
-
Reg |= 0x80;
-
/* Send address */
-
HAL_SPI_Transmit(&hspi2,&Reg,1,100);
-
/* Receive data */
-
txdata = LIS302DL_LIS3DSH_DUMMY_BYTE;
-
ret = HAL_SPI_TransmitReceive(&hspi2,&txdata,Data,1,100);
-
/* Stop SPI transmission */
-
LIS3DH_CS_DISABLE;
-
return ret;
-
}
-
-
-
/*******************************************************************************
-
* Function Name : LIS3DH_WriteReg
-
* Description : Generic Writing function. It must be fullfilled with either
-
* : I2C or SPI writing function
-
* Input : Register Address, Data to be written
-
* Output : None
-
* Return : None
-
*******************************************************************************/
-
uint8_t LIS3DH_WriteReg(uint8_t WriteAddr, uint8_t Data)
-
{
-
uint8_t txdata[2],ret;
-
txdata[0] = WriteAddr;
-
txdata[1] = Data;
-
/* Start SPI transmission */
-
LIS3DH_CS_ENABLE;
-
/* Send address */
-
ret = HAL_SPI_Transmit(&hspi2,txdata,2,100);
-
/* Stop SPI transmission */
-
LIS3DH_CS_DISABLE;
-
-
return ret;
-
}
-
uint8_t LIS3DH_MultiRead(uint8_t start_addr,uint8_t len,uint8_t *data)//连续读读寄存器
-
{
-
uint8_t ret,txdata;
-
-
/* Add read bit and autoincrement bit */
-
start_addr |= 0xC0;
-
txdata = start_addr;
-
/* Start SPI transmission */
-
LIS3DH_CS_ENABLE;
-
/* Send address */
-
HAL_SPI_Transmit(&hspi2,&txdata,1,100);
-
/* Receive data */
-
ret = HAL_SPI_TransmitReceive(&hspi2,data,data,len,1000);
-
/* Stop SPI transmission */
-
LIS3DH_CS_DISABLE;
-
-
return ret;
-
}
2. 初始化以及参数配置:
LIS3DHInit(); //1.中代码有这个函数的实现,主要是SPI初始化以及片选信号拉高
Reset_LIS3DH(); //复位各个寄存器
ret = gsensor_init(); //配置参数
if(ret != RT_EOK){
rt_kprintf("gsensor_init failed:%d.\r\n",ret);
ret = -RT_ERROR;
return ret;
}
gsensor_set_threshold(44,1);//中断阈值设置,这里主要设置产生中断的阈值,当加速度达到这个值后就会触发中断1.
下面给出各个函数的具体实现:
-
void Reset_LIS3DH(void)
-
{
-
LIS3DH_WriteReg(LIS3DH_TEMP_CFG_REG,0x00);
-
LIS3DH_WriteReg(LIS3DH_CTRL_REG1,0x07); //XYZ轴使能
-
LIS3DH_WriteReg(LIS3DH_CTRL_REG2,0x00);
-
LIS3DH_WriteReg(LIS3DH_CTRL_REG3,0x00);
-
LIS3DH_WriteReg(LIS3DH_CTRL_REG4,0x00);
-
LIS3DH_WriteReg(LIS3DH_CTRL_REG5,0x00);
-
LIS3DH_WriteReg(LIS3DH_CTRL_REG6,0x00);
-
LIS3DH_WriteReg(LIS3DH_REFERENCE_REG,0x00);
-
LIS3DH_WriteReg(LIS3DH_FIFO_CTRL_REG,0x00);
-
LIS3DH_WriteReg(LIS3DH_INT1_CFG,0x00);
-
LIS3DH_WriteReg(LIS3DH_INT1_THS,0x00);
-
LIS3DH_WriteReg(LIS3DH_INT1_DURATION,0x00);
-
LIS3DH_WriteReg(LIS3DH_CLICK_CFG,0x00);
-
LIS3DH_WriteReg(LIS3DH_CLICK_THS,0x00);
-
LIS3DH_WriteReg(LIS3DH_TIME_LIMIT,0x00);
-
LIS3DH_WriteReg(LIS3DH_TIME_LATENCY,0x00);
-
LIS3DH_WriteReg(LIS3DH_TIME_WINDOW,0x00);
-
}
-
int gsensor_init(void)
-
{
-
uint8_t response = 0;
-
response |= LIS3DH_SetODR(LIS3DH_ODR_100Hz);//设置数据输出频率
-
response = response << 1;
-
response |= LIS3DH_SetMode(LIS3DH_NORMAL);//设置正常模式
-
response = response << 1;
-
response |= LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);//设置量程为±2g, BIT6:DATA LSB
-
response = response << 1;
-
response |= LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);//使能三轴数据输出
-
response = response << 1;
-
return response;
-
}
-
uint8_t gsensor_set_threshold(uint8_t threshold_value,uint8_t interrupt_ID)
-
{
-
/*
-
1 LSb = 16 mg @ FS = ±2 g
-
1 LSb = 32 mg @ FS = ±4 g
-
1 LSb = 62 mg @ FS = ±8 g
-
1 LSb = 186 mg @ FS = ±16 g
-
*/
-
uint8_t response = 0;
-
if(interrupt_ID == 1)
-
{
-
LIS3DH_HPFAOI1Enable(MEMS_DISABLE);//High pass filter enabled for AOI function on interrupt 1,
-
response|= LIS3DH_SetIntConfiguration(LIS3DH_INT1_ZHIE_ENABLE|LIS3DH_INT1_ZLIE_ENABLE|
-
LIS3DH_INT1_YHIE_ENABLE|LIS3DH_INT1_YLIE_ENABLE|
-
LIS3DH_INT1_XHIE_ENABLE|LIS3DH_INT1_XLIE_ENABLE);//INT1_CFG中enable
-
response|= LIS3DH_SetInt6D4DConfiguration(LIS3DH_INT1_6D_ENABLE);
-
response|= LIS3DH_SetIntMode(LIS3DH_INT_MODE_6D_POSITION);
-
response|= LIS3DH_SetInt1Threshold(threshold_value);//16 * 16mg == 256mg
-
response|= LIS3DH_SetInt1Pin(LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE);
-
}
-
}
3. 数据采集及计算,有两种方法:
1)通过公式计算得到:
首先调用LIS3DH_GetAccAxesRaw()获得xyz三个方向的寄存器值,然后通过公式计算。参考文章是这篇:
https://blog.csdn.net/sinat_23338865/article/details/51612872
首先在2中参数配置里,配置了fullsacle满量程是±2g, LIS3DH是16bit的data output,其中有一bit是符号位,所以还剩15bit的数据值,2^15 = 32768,2g = 2000mg,精度为2g/2^15= 2000mg/32768 =0.061mg,所以可以计算三个方向的值为:
X = X * 2000 /32768; 单位:mg
Y= Y * 2000 /32768; 单位:mg
Z = Z * 2000 /32768; 单位:mg
2)通过移位的方式获得:
-
void LIS3DH_Get_AccRaw(int16_t* pdata)
-
{
-
uint8_t i = 0;
-
uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
-
int16_t symbol[3] = {0,0,0};
-
for(i=0;i<6;i++)
-
{
-
LIS3DH_ReadReg(LIS3DH_OUT_X_L+i, regValue+i);
-
}
-
/* Format the data. */
-
for(i=0;i<3;i++)
-
{
-
if(regValue[2*i+1] & 0x80) symbol[i] = 0xF000;
-
else symbol[i] = 0x0000;
-
}
-
pdata[0] =symbol[0] | ((( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] ) >> 4);
-
pdata[1] =symbol[1] | ((( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] ) >> 4);
-
pdata[2] =symbol[2] | ((( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] ) >> 4);
-
-
}
-
/*
-
*function:LIS3DH_Get_Sensitivity
-
*作用:读取满量程值:(00: +/- 2G; 01: +/- 4G; 10: +/- 8G; 11: +/- 16G)
-
*/
-
static void LIS3DH_Get_Sensitivity(uint8_t* sensitivity)
-
{
-
uint8_t fullscale = 0;
-
LIS3DH_ReadReg(LIS3DH_CTRL_REG4,&fullscale);
-
fullscale = (fullscale & 0x30) >> 4; //0x30:0011 0000
-
switch (fullscale)
-
{
-
case 0:*sensitivity = 1;break;
-
case 1:*sensitivity = 2;break;
-
case 2:*sensitivity = 4;break;
-
case 3:*sensitivity = 12;break;
-
default : break;
-
}
-
}
-
-
void LIS3DH_Get_AccValue(AxesRaw_t *pdata)
-
{
-
int16_t dataRaw[3];
-
uint8_t sensitivity = 0;
-
LIS3DH_Get_AccRaw(dataRaw);//获取3个方向的加速度值,
-
LIS3DH_Get_Sensitivity(&sensitivity);
-
pdata->AXIS_X = ( int32_t )( dataRaw[0] * sensitivity );
-
pdata->AXIS_Y = ( int32_t )( dataRaw[1] * sensitivity );
-
pdata->AXIS_Z = ( int32_t )( dataRaw[2] * sensitivity );
-
}
讲解一下移位方法的理解:最高位bit16是符号位,所以数据位是15bit=32768,那么1个值是多少个mg就是2000mg/32768=1/16mg,就是读取出来的寄存器值需要除以16,除以16就是右移4bit,(2^4 = 16). 相对第一种方法,这种方法算出来的更准确一些应该,因为没有小数的运算。
方法2参考文章:https://www.pianshen.com/article/2045248726/
后记,现在明白了这种移位的方法了,加速度值在寄存器中是以补码的形式存在的,上面LIS3DH_Get_AccRaw()是获取数据的补码的实现方法:
举例:regvalue[2] = 0xF0, regvalue[3] = 0xFB;
那么,if(regValue[2*i+1] & 0x80) symbol[i] = 0xF000; /*假如是负数,高4位保留(因为移位4位后,数据是0x0XXX的形式,补码需要数值前面符号位后面都是1,所以这里是0xF000),取补码,负数是以补码的形式输出*/
symbol[1] | ( ( ( ((int16_t)regValue[3]) << 8 ) + (int16_t)regValue[2] ) >> 4);执行后,结果是0xF000 | 0xFBF = 0xFFBF;
0xFFBF的二进制是 1111 1111 1011 1111
减一:1111 1111 1011 1110, 取反:0000 0000 0100 0001
它的原码是 0000 0000 0100 0001,换成10进制是65,加上负号就是-65,
4,测试结果:
-
void cmd_lis3dh_test(char *args){
-
rt_device_t lis3dh_dev;
-
int8_t ret;
-
AxesRaw_t acc,acc1;
-
int32_t x,y,z;
-
-
lis3dh_dev = n_device_find("lis3dh");//基于RTTthrea的方式实现的代码
-
if(lis3dh_dev == NULL){
-
cmd_printf("lis3dh not find.\r\n");
-
}
-
ret = n_device_open(lis3dh_dev,RT_DEVICE_FLAG_RDWR);
-
if(ret != HAL_OK)
-
cmd_printf("lis3dh open failed!.\r\n");
-
uint8_t lis_id;
-
n_device_control(lis3dh_dev,N_LIS3DH_CTRL_ID,&lis_id);
-
rt_kprintf("data= 0x%x\r\n",lis_id);
-
-
LIS3DH_GetAccAxesRaw(&acc);//方法1)
-
cmd_printf("LIS3DH read Acceleration adval, x:%d, y:%d, z:%d.\r\n",acc.AXIS_X,acc.AXIS_Y,acc.AXIS_Z);
-
-
acc.AXIS_X = acc.AXIS_X * 2000 / 32768;
-
acc.AXIS_Y = acc.AXIS_Y * 2000 / 32768;
-
acc.AXIS_Z = acc.AXIS_Z * 2000 / 32768;
-
cmd_printf("LIS3DH read Acceleration mg, x:%d, y:%d, z:%d.\r\n",acc.AXIS_X,acc.AXIS_Y,acc.AXIS_Z);
-
LIS3DH_Get_AccValue(&acc1); //方法2)
-
cmd_printf("LIS3DH read Acceleration g, x:%d, y:%d, z:%d.\r\n",acc1.AXIS_X,acc1.AXIS_Y,acc1.AXIS_Z);
-
cmd_printf("LIS3DH read INT1 Level: %d\r\n ",n_gpio_readpin(GPIOE,GPIO_PIN_13));//显示加速度计中断
-
-
n_device_close(lis3dh_dev);
-
}
5,测试结果:
0x33是lis3dh的ID。
adval是寄存器中的值; mg是方法1算出来的结果,应该是0.992g(第一行),g是方法2算出来的结果1.017g,单位标的不对。
看lis3dh知道,如果加速度计在水平放置时,在z轴方向上是1个g,x或y方向垂直放时,在x或y方向是1个g,其他两个方向应该是0,但是实际中不可能完全是0,会有微小的值存在。