最后一个传感器LSM6DUO
6轴加速度计和陀螺仪传感器。
原理图,IIC地址是0xD6:
寄存器:
这个传感器的寄存器很多,打那是其中使用的比较少。
下面例举一些常用的。
WHO-AM_I寄存器:
控制寄存器1:
设置频率,这个需要设置,我设置的是0x10.
控制寄存器2,也需要设置,我设置的是0x12.
温度输出寄存器,没有具体的说明,不知道采集的温度怎样换算
加速度传感器输出寄存器:
陀螺仪输出传感器:
代码:
初始化:
//初始化传感器LSM6DSO
if(0 == Set_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_CTRL1_XL, 0x10))
{
send_str((unsigned char *)"IF_CTRL set successful\r\n");
}
else
{
send_str((unsigned char *)"IF_CTRL set fail\r\n");
}
if(0 == Set_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_CTRL2_G, 0x12))
{
send_str((unsigned char *)"IF_CTRL set successful\r\n");
}
else
{
send_str((unsigned char *)"IF_CTRL set fail\r\n");
}
寄存器操作和串口发送数据:
//LSM6DSO写
static int IIC_WrDat_LSM6DSO(uint8_t IIC_Add, uint8_t IIC_Reg, uint8_t IIC_Data)
{
uint8_t ret_suc = 1;
IIC_Start();
IIC_Send_Byte(IIC_Add & 0xfe);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Send_Byte(IIC_Reg); //write data
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Send_Byte(IIC_Data);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Stop();
return 0;
}
//LSM6DSO读
static int IIC_Read_LSM6DSO(uint8_t IIC_Add, uint8_t IIC_Reg)
{
int ret;
uint8_t ret_suc = 1;
IIC_Start();
IIC_Send_Byte(IIC_Add & 0xfe);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Send_Byte(IIC_Reg);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Start();
IIC_Send_Byte(IIC_Add | 0x01);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
ret=IIC_Read_Byte(0);
IIC_Stop();
return ret;
}
//LSM6DSO读2个寄存器
static int IIC_Read2_LSM6DSO(uint8_t IIC_Add, uint8_t IIC_Reg)
{
int ret;
uint8_t ret_suc = 1;
IIC_Start();
IIC_Send_Byte(IIC_Add & 0xfe);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Send_Byte(IIC_Reg);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
IIC_Start();
IIC_Send_Byte(IIC_Add | 0x01);
ret_suc = IIC_Wait_Ack();
if(ret_suc == 1) {IIC_Stop();return -1;}
ret=IIC_Read_Byte(1);
ret=(IIC_Read_Byte(0)<<8)|ret;
IIC_Stop();
return ret;
}
//设置寄存器
int Set_Reg_LSM6DSO(uint8_t add, uint8_t reg, uint8_t dat)
{
int ret_succ = -1;
uint8_t i = 20u; //失败之后尝试的次数
do{
if(0 == IIC_WrDat_LSM6DSO(add, reg, dat))
{
ret_succ = 0; //设置成功
break;
}
else
{
i--; //获取失败,再次尝试
}
}while(i>0u);
return ret_succ;
}
//读取寄存器
int Get_Reg_LSM6DSO(uint8_t add, uint8_t reg)
{
uint8_t i = 20u;
int ret_get = -1;
do{
ret_get = IIC_Read_LSM6DSO(add, reg);
if(-1 != ret_get)
{
break; //获取数据成功
}
else
{
i--; //获取失败,再次尝试
}
}while(i>0u);
return ret_get;
}
//读取我是谁
int Get_WHO_AM_I_LSM6DSO(void)
{
return Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_WHO_AM_I);
}
//获取状态寄存器
int Get_Status_LSM6DSO(void)
{
return Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_CFG_STATUS);
}
float Get_Temp_LSM6DSO(void)
{
return (Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_TEMP_H)<<8)|Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_TEMP_L);
}
short Get_X_G_LSM6DSO(void)
{
return ((Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_X_H_G)<<8)|Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_X_L_G));
}
short Get_Y_G_LSM6DSO(void)
{
return ((Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Y_H_G)<<8)|Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Y_L_G));
}
short Get_Z_G_LSM6DSO(void)
{
return ((Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Z_H_G)<<8)|Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Z_L_G));
}
short Get_X_A_LSM6DSO(void)
{
return ((Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_X_H_A)<<8)|Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_X_L_A));
}
short Get_Y_A_LSM6DSO(void)
{
return ((Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Y_H_A)<<8)|Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Y_L_A));
}
short Get_Z_A_LSM6DSO(void)
{
return ((Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Z_H_A)<<8)|Get_Reg_LSM6DSO(LSM6DSO_ADDR_W_1, LSM6DSO_OUT_Z_L_A));
}
//发送数据到串口
void Send_Uart_LSM6DSO(void)
{
char str[35] = {0};
send_str((uint8_t *)"<-----------START--------->\r\n");
sprintf(str,"G_X:%d | G_Y:%d | G_Z:%d\r\n", Get_X_G_LSM6DSO(),Get_Y_G_LSM6DSO(),Get_Z_G_LSM6DSO());
send_str((uint8_t *)str);
sprintf(str,"A_X:%d | A_Y:%d | A_Z:%d\r\n", Get_X_A_LSM6DSO(),Get_Y_A_LSM6DSO(),Get_Z_A_LSM6DSO());
send_str((uint8_t *)str);
sprintf(str,"Temp:%.2f\r\n", Get_Temp_LSM6DSO());
send_str((uint8_t *)str);
sprintf(str,"Who_am_i:%X\r\n",Get_WHO_AM_I_LSM6DSO());
send_str((uint8_t *)str);
send_str((uint8_t *)"<------------END---------->\r\n\n");
}
宏定义:
#define LSM6DSO_FUNC_CFG_ACCESS (0x01U)
#define LSM6DSO_PIN_CTRLs (0x02U)
#define LSM6DSO_COUNTER_BDR_REG1 (0x0BU)
#define LSM6DSO_COUNTER_BDR_REG2 (0x0CU)
#define LSM6DSO_WHO_AM_I (0x0FU)
#define LSM6DSO_CTRL1_XL (0x10U)
#define LSM6DSO_CTRL2_G (0x11U)
#define LSM6DSO_CTRL3_C (0x12U)
#define LSM6DSO_CTRL4_C (0x13U)
#define LSM6DSO_CTRL5_C (0x14U)
#define LSM6DSO_CTRL6_G (0x15U)
#define LSM6DSO_CTRL7_G (0x16U)
#define LSM6DSO_CTRL8_XL (0x17U)
#define LSM6DSO_CTRL9_XL (0x18U)
#define LSM6DSO_CTRL10_C (0x19U)
#define LSM6DSO_CFG_STATUS (0x1EU)
#define LSM6DSO_TEMP_L (0x20U)
#define LSM6DSO_TEMP_H (0x21U)
#define LSM6DSO_OUT_X_L_G (0x22U)
#define LSM6DSO_OUT_X_H_G (0x23U)
#define LSM6DSO_OUT_Y_L_G (0x24U)
#define LSM6DSO_OUT_Y_H_G (0x25U)
#define LSM6DSO_OUT_Z_L_G (0x26U)
#define LSM6DSO_OUT_Z_H_G (0x27U)
#define LSM6DSO_OUT_X_L_A (0x28U)
#define LSM6DSO_OUT_X_H_A (0x29U)
#define LSM6DSO_OUT_Y_L_A (0x2AU)
#define LSM6DSO_OUT_Y_H_A (0x2BU)
#define LSM6DSO_OUT_Z_L_A (0x2CU)
#define LSM6DSO_OUT_Z_H_A (0x2DU)
/*IIC slave address write and read*/
/*SD0 connect to power supply*/
#define LSM6DSO_ADDR_W_1 0xD6U
#define LSM6DSO_ADDR_R_1 0xD7U
数据输出效果: