一:IIS2mdc基础知识
IIS2MDC是一款高精度、超低功耗的三轴数字磁:传感器。
IIS2MDC具有高达+50高斯的磁场动态范围0
IIS2MDC包含一个支持标准、快速模式、快速模式增强和高速(100kHz、400kHz、1MHz和3.4MHz)以及SPI串行标准接口的I12C总线接口。
该设备可配置为生成中断信号以用于磁场检测。
IIS2MDC提供塑料扁平网格阵列封装(LGA),并保证在-40°C至+85°C的宽温度范围内工作。
二:硬件设计
IIS2MDC 通过标准的 I²C 总线与主控(MCU)通信。在配置前,需要确保硬件连接正确:
引脚连接:将传感器的 SDA 和 SCL 引脚分别连接到主控的对应 I²C 引脚,并必须通过上拉电阻(如 4.7kΩ)连接到电源,以保证总线空闲时为高电平 。CS 引脚需接高电平,以选择 I²C 模式 。
从设备地址:IIS2MDC 的 7 位 I²C 设备地址是 0x1E 。写操作为 0x3C (0x1E << 1),读操作为 0x3D (0x1E << 1 | 1) 。
三:软件代码:
3.1 初始化:
void iismdc_init( void )
{
/* Initialize mems driver interface */
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 */
whoamI = 0;
iis2mdc_device_id_get(&dev_ctx, &whoamI);
if ( whoamI != IIS2MDC_ID )
while (1); /*manage here device not found */
/* Restore default configuration */
iis2mdc_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
iis2mdc_reset_get(&dev_ctx, &rst);
} while (rst);
/* Enable Block Data Update */
iis2mdc_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* Set Output Data Rate */
iis2mdc_data_rate_set(&dev_ctx, IIS2MDC_ODR_10Hz);
/* Set / Reset sensor mode */
iis2mdc_set_rst_mode_set(&dev_ctx, IIS2MDC_SENS_OFF_CANC_EVERY_ODR);
/* Enable temperature compensation */
iis2mdc_offset_temp_comp_set(&dev_ctx, PROPERTY_ENABLE);
/* Set device in continuous mode */
iis2mdc_operating_mode_set(&dev_ctx, IIS2MDC_CONTINUOUS_MODE);
}
3.2 读取及其处理部分:
void iismdc_ReadData( void )
{
stmdev_ctx_t dev_ctx;
/* Read output only if new value is available */
iis2mdc_mag_data_ready_get(&dev_ctx, &drdy);
if (drdy) {
/* Read magnetic field data */
memset(data_raw_magnetic, 0x00, 3 * sizeof(int16_t));
iis2mdc_magnetic_raw_get(&dev_ctx, data_raw_magnetic);
magnetic_mG[0] = iis2mdc_from_lsb_to_mgauss( data_raw_magnetic[0]);
magnetic_mG[1] = iis2mdc_from_lsb_to_mgauss( data_raw_magnetic[1]);
magnetic_mG[2] = iis2mdc_from_lsb_to_mgauss( data_raw_magnetic[2]);
snprintf((char *)tx_buffer, sizeof(tx_buffer),
"Magnetic field [mG]:%4.2f\t%4.2f\t%4.2f\r\n",
magnetic_mG[0], magnetic_mG[1], magnetic_mG[2]);
tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
/* Read temperature data */
memset( &data_raw_temperature, 0x00, sizeof(int16_t) );
iis2mdc_temperature_raw_get( &dev_ctx, &data_raw_temperature );
temperature_degC = iis2mdc_from_lsb_to_celsius (
data_raw_temperature );
snprintf((char *)tx_buffer, sizeof(tx_buffer), "ST-iis2mdcTemperature [degC]:%6.2f\r\n",
temperature_degC );
tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
}
}
四:串口数据截图显示
