单片机学习小组
直播中

崔映芬

7年用户 911经验值
私信 关注

怎样去编写lis2dh12驱动的程序代码呢

怎样去编写lis2dh12驱动的程序代码呢?

回帖(1)

李兵

2022-1-18 10:00:24
void lis2dh12_self_test(void)
{undefined
stmdev_ctx_t dev_ctx;
lis2dh12_reg_t reg;
uint8_t i, j;
/* Initialize mems driver interface /
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
// dev_ctx.handle = &SENSOR_BUS;
/ Wait boot time and initialize platform specific hardware /
platform_init();
/ Wait sensor boot time /
platform_delay(BOOT_TIME);
/ Check device ID */
// uint8_t value=0;
// for(uint8_t i=0;i<255;i++)
// {undefined
// I2C_dma_BlockRead_Sleep(i,LIS2DH12_WHO_AM_I,&value,1);
// platform_delay(1);
// if(valueLIS2DH12_ID)
// {
// value=0;
// }
// value=0;
// }
//验证传感器读写是否OK
uint8_t value=0xaa;
// I2C_dma_BlockWrite_Sleep(LIS2DH_I2C_ADDR2,LIS2DH12_INT1_THS,&value,1);
// platform_delay(1);
// value=0;
// I2C_dma_BlockRead_Sleep(LIS2DH_I2C_ADDR2,LIS2DH12_INT1_THS,&value,1);
// platform_delay(1);
// if(0xaavalue)
// {undefined
// value=0;//验证传感器读写是否OK
// }
value=0xFF;
// I2C_dma_BlockWrite_Sleep(LIS2DH_I2C_ADDR2,LIS2DH12_CTRL_REG3,&value,1);
platform_delay(1);
value=0;
I2C_dma_BlockRead_Sleep(LIS2DH_I2C_ADDR2,LIS2DH12_CTRL_REG3,&value,1);
platform_delay(1);
if(0xFF==value)
{undefined
value=0;//验证传感器读写是否OK
}
lis2dh12_device_id_get(&dev_ctx, ®.byte);


if (reg.byte != LIS2DH12_ID) {undefined
while (1) {undefined
/* manage here device not found */
}
}


/* Initialization of sensor */
lis2dh12_iic_write_byte(0x20, 0x37);        /* CTRL_REG1(20h): 关闭sensor,设置进入掉电模式 ODR 25HZ */


// lis2dh12_iic_write_byte(0x20, 0x57); /* CTRL_REG1(20h): 关闭sensor,设置进入低功耗模式 ODR 100HZ /
lis2dh12_iic_write_byte(0x21, 0x03); / CTRL_REG2(21h): IA1、IA2 开启高通滤波 bc */


lis2dh12_iic_write_byte(0x22, 0xc0);        /* CTRL_REG3(22h): 0x80 使能单击中断到INT_1 INT_2 */


// lis2dh12_iic_write_byte(0x22, 0xD0); /* CTRL_REG3(22h): 0x80 使能单击中断到INT_1 INT_2 /
lis2dh12_iic_write_byte(0x23, 0x88); / CTRL_REG4(23h): 使能快,数据更新,全量程+/-2G,非常精度模式 /
// lis2dh12_iic_write_byte(0x25, 0x00); / CTRL_REG6(25h): 高电平(上升沿)触发中断 */


/* INT1 翻转检测,中断*/                                                        //0x6a
lis2dh12_iic_write_byte(0x30, 0x7f);  /* INT1_CFG(30h): 使能,6D X/Y/Z任一超过阈值中断 */


// lis2dh12_iic_write_byte(0x30, 0x4f); /* INT1_CFG(30h): 使能,6D X/Y任一超过阈值中断 /
// lis2dh12_iic_write_byte(0x31, 0x20); / INT1_SRC(31h): 设置中断源 */


lis2dh12_iic_write_byte(0x32, 0x03);          /* INT1_THS(32h): 设置中断阀值 0x10: 16*2(FS)  0x20: 32*16(FS) */


// lis2dh12_iic_write_byte(0x33, 0x02); /* INT1_DURATION(33h): 1LSB=1/ODR 如果ODR=25HZ 那么1LSB=40ms 设置延时 1s,对应25->0x19 /
// lis2dh12_iic_write_byte(0x33, 0x03); / INT1_DURATION(33h): 1LSB=1/ODR 如果ODR=50HZ 那么1LSB=20ms 设置延时 1s,对应50->0x32 /
lis2dh12_iic_write_byte(0x33, 0x00); / INT1_DURATION(33h): 1LSB=1/ODR 如果ODR=100HZ 那么1LSB=10ms 设置延时 1s,对应100->0x64 */


// /* INT2 单击中断 /
lis2dh12_iic_write_byte(0x24, 0x01); / CTRL_REG5(24h): /
// lis2dh12_iic_write_byte(0x25, 0xa0); / CTRL_REG6(25h): Click interrupt on INT2 pin /
//
// lis2dh12_iic_write_byte(0x38, 0x15); / CLICK_CFG (38h): 单击识别中断使能 /
lis2dh12_iic_write_byte(0x39, 0x10);
// lis2dh12_iic_write_byte(0x3a, 0x7f); / CLICK_THS (3Ah): 单击阀值 /
// lis2dh12_iic_write_byte(0x3b, 0xff); / TIME_LIMIT (3Bh): 时间限制窗口6 ODR 1LSB=1/ODR 1LSB=1/100HZ,10ms,设置延时1s,对应100—>0x64*/
// lis2dh12_iic_write_byte(0x3c, 0xff); /* TIME_LATENCY (3Ch): 中断电平持续时间1 ODR=10ms /
// lis2dh12_iic_write_byte(0x3d, 0x01); / TIME_WINDOW (3Dh): 单击时间窗口 */


/* Start sensor */
1
// lis2dh12_iic_write_byte(0x20, 0x37);
lis2dh12_iic_write_byte(0x20, 0x5f); /* CTRL_REG1(20h): Start sensor at ODR 100Hz Low-power mode */


}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分