一:IIS2DH芯片描述
IIS2DH是一款超低功耗高性能三轴线性加速度计,具有数字I2C/SPI串行接口标准输出。
IIS2DH具有用户可选的满量程:+2g/+4g/+8g/+16g,能够以1Hz至5.3kHz的输出数据速率测量加速度。
该设备可配置为通过两个独立的惯性唤醒自由落体事件以及设备自身的位置来生成中断信号。
自检功能允许用户在最终应用中检查传感器的功能。
IIS2DH采用小型薄型塑料焊盘网格阵列封装(LGA),并保证可在-40°C至+85°C的宽温度范围内工作。
二:软件代码
stmdev_ctx_t dev_ctx;
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
// dev_ctx.mdelay = platform_delay;
dev_ctx.handle = &SENSOR_BUS;
/* Initialize platform specific hardware */
platform_init();
/* Wait sensor boot time */
platform_delay(BOOT_TIME);
/* Check device ID */
iis2dh_device_id_get(&dev_ctx, &whoamI);
if (whoamI != IIS2DH_ID) {
while (1) {
/* manage here device not found */
}
}
/* Enable Block Data Update */
iis2dh_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* Set Output Data Rate to 1Hz */
iis2dh_data_rate_set(&dev_ctx, IIS2DH_ODR_1Hz);
/* Set full scale to 2g */
iis2dh_full_scale_set(&dev_ctx, IIS2DH_2g);
/* Enable temperature sensor */
iis2dh_temperature_meas_set(&dev_ctx, IIS2DH_TEMP_ENABLE);
/* Set device in continuous mode with 12 bit resol. */
iis2dh_operating_mode_set(&dev_ctx, IIS2DH_HR_12bit);
2.2 读取数据函数
iis2dh_reg_t reg;
/* Read output only if new value available */
iis2dh_xl_data_ready_get(&dev_ctx, ®.byte);
if (reg.byte) {
/* Read accelerometer data */
memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
iis2dh_acceleration_raw_get(&dev_ctx, data_raw_acceleration);
acceleration_mg[0] =
iis2dh_from_fs2_hr_to_mg(data_raw_acceleration[0]);
acceleration_mg[1] =
iis2dh_from_fs2_hr_to_mg(data_raw_acceleration[1]);
acceleration_mg[2] =
iis2dh_from_fs2_hr_to_mg(data_raw_acceleration[2]);
snprintf((char *)tx_buffer, sizeof(tx_buffer),
"Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
tx_com(tx_buffer, strlen((char const *)tx_buffer));
}
iis2dh_temp_data_ready_get(&dev_ctx, ®.byte);
if (reg.byte) {
/* Read temperature data */
memset(&data_raw_temperature, 0x00, sizeof(int16_t));
iis2dh_temperature_raw_get(&dev_ctx, &data_raw_temperature);
temperature_degC =
iis2dh_from_lsb_hr_to_celsius(data_raw_temperature);
snprintf((char *)tx_buffer, sizeof(tx_buffer),
"Temperature [degC]:%6.2f\r\n",
temperature_degC);
tx_com(tx_buffer, strlen((char const *)tx_buffer));
}
三:串口接收数据

|