一:ISM330DHCX知识分享:
带可选满量程的3D加速度计:+2/+4/+8/+16g
带扩展可选满量程的3D陀螺仪:+125/±250/±500/±1000/±2000/±4000 dps
扩展温度范围从-40到+105°C嵌入式补偿,确保温度变化下的高稳定性
SPI/I2C串行接口
辅助SPI串行接口,用于陀螺仪和加速度计的数据输出(OIS及其他稳定应用)
六通道同步输出
传感器集线器功能,可高效从额外外部传感器收集数据;
嵌入式智能FIFO,最大支持9千字节
可编程有限状态机,用于处理加速度计、陀螺仪和外部传感器的数据
机器学**智能嵌入式功能与中断:倾斜检测、自由落体、唤醒、6D/4D方向、单击与双击
嵌入式计步器、步数检测器和计数器,用于医疗保健应用
模拟供电电压:1.71V至3.6V
嵌入式温度传感器陀螺仪和加速度计均内置自检功能高抗冲击性
符合ECOPACK、RoHS和“绿色”标准
二:代码:
2.1 初始化
stmdev_ctx_t dev_ctx;
/* Initialize mems driver interface */
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.mdelay = platform_delay;
dev_ctx.handle = &hspi3;
/* Init test platform */
platform_init();
/* Wait sensor boot time */
platform_delay(BOOT_TIME);
/* Check device ID */
ism330dhcx_device_id_get(&dev_ctx, &whoamI);
if (whoamI != ISM330DHCX_ID)
while (1);
/* Restore default configuration */
ism330dhcx_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
ism330dhcx_reset_get(&dev_ctx, &rst);
} while (rst);
/* Start device configuration. */
ism330dhcx_device_conf_set(&dev_ctx, PROPERTY_ENABLE);
/* Enable Block Data Update */
ism330dhcx_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* Set Output Data Rate */
ism330dhcx_xl_data_rate_set(&dev_ctx, ISM330DHCX_XL_ODR_12Hz5);
ism330dhcx_gy_data_rate_set(&dev_ctx, ISM330DHCX_GY_ODR_12Hz5);
/* Set full scale */
ism330dhcx_xl_full_scale_set(&dev_ctx, ISM330DHCX_2g);
ism330dhcx_gy_full_scale_set(&dev_ctx, ISM330DHCX_2000dps);
/* Configure filtering chain(No aux interface)
*
* Accelerometer - LPF1 + LPF2 path
*/
ism330dhcx_xl_hp_path_on_out_set(&dev_ctx, ISM330DHCX_LP_ODR_DIV_100);
ism330dhcx_xl_filter_lp2_set(&dev_ctx, PROPERTY_ENABLE);
2.2 数据读取
uint8_t reg;
/* Read output only if new xl value is available */
ism330dhcx_xl_flag_data_ready_get(&dev_ctx, ®);
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_0);
HAL_Delay(500);
if (reg) {
/* Read acceleration field data */
memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
ism330dhcx_acceleration_raw_get(&dev_ctx, data_raw_acceleration);
acceleration_mg[0] =
ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[0]);
acceleration_mg[1] =
ism330dhcx_from_fs2g_to_mg(data_raw_acceleration[1]);
acceleration_mg[2] =
ism330dhcx_from_fs2g_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));
}
HAL_Delay(500);
ism330dhcx_gy_flag_data_ready_get(&dev_ctx, ®);
if (reg) {
/* Read angular rate field data */
memset(data_raw_angular_rate, 0x00, 3 * sizeof(int16_t));
ism330dhcx_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate);
angular_rate_mdps[0] =
ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[0]);
angular_rate_mdps[1] =
ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[1]);
angular_rate_mdps[2] =
ism330dhcx_from_fs2000dps_to_mdps(data_raw_angular_rate[2]);
snprintf((char *)tx_buffer, sizeof(tx_buffer),
"Angular rate [mdps]:%4.2f\t%4.2f\t%4.2f\r\n",
angular_rate_mdps[0], angular_rate_mdps[1], angular_rate_mdps[2]);
tx_com(tx_buffer, strlen((char const *)tx_buffer));
}
ism330dhcx_temp_flag_data_ready_get(&dev_ctx, ®);
HAL_Delay(500);
if (reg) {
/* Read temperature data */
memset(&data_raw_temperature, 0x00, sizeof(int16_t));
ism330dhcx_temperature_raw_get(&dev_ctx, &data_raw_temperature);
temperature_degC = ism330dhcx_from_lsb_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));
}
三:实物验证:
