本帖最后由 电子烂人 于 2024-8-22 17:19 编辑
#申请原创# #申请开发板#这篇帖子试着驱动下传感器:
1.IIS3DWBIS3DWB采用系统级封装,配备了具有低噪音以及超宽且平坦频率范围的三轴数字振动传感器。该器件具有高带宽、低噪音、高稳定性和可重复灵敏度,以及在扩展温度范围(可达+105℃)内的工作能力,特别适合工业应用中的振动监控。低功耗、高性能、还有数字输出和嵌入式数字功能(如FIFO和中断),这些特点非常适合电池供电的工业无线传感器节点。 IIS3DWB具有可选的满量程加速度范围(±2/±4/±8/±16 g),并且能够测量带宽最高达6 kHz的加速度项目,输出数据率为26.7 kHz。器件中集成了3 kB的先进先出(FIFO)缓冲器,以避免任何数据丢失,并限制主机处理器的干预。(来源:意法半导体官方) 这个传感器非常适合做工业电机的监测。 2.初始化配置
由板卡的原理图得知,震动计链接在SPI3上面
并且需要配置PB2使能
SPI配置如图:
3.移植中间件
XCUBE-MEMS中间件包配置如图:
另外,要进行传输数据的话,这里使能一组USB虚拟串口:
同时也在中间件包中选择虚拟串口:
4.代码编写:
初始化生成代码之后,根目录会有几个驱动文件:
这些里面包含了传感器和USB的所有驱动,我们只需要用到部分即可;
找到IIS3DBW的GIT开源网站,里面会有polling代码供移植参考
不过这份代码有些老,并不完全适配STWINKT1B这款板卡,需要我们做一些改动:void iis3dwb_read_data_polling()
{
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 */
iis3dwb_device_id_get(&dev_ctx, &whoamI);
if (whoamI != IIS3DWB_ID)
while (1);
/* Restore default configuration */
iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
iis3dwb_reset_get(&dev_ctx, &rst);
} while (rst);
/* Enable Block Data Update */
iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* Set Output Data Rate */
iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);
/* Set full scale */
iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);
/* Configure filtering chain(No aux interface)
* Accelerometer low pass filter path
*/
iis3dwb_xl_filt_path_on_out_set(&dev_ctx, IIS3DWB_LP_ODR_DIV_100);
/* Read samples in polling mode (no int) */
while (1) {
uint8_t reg;
/* Read output only if new xl value is available */
iis3dwb_xl_flag_data_ready_get(&dev_ctx, ®);
if (reg) {
/* Read acceleration field data */
memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
iis3dwb_acceleration_raw_get(&dev_ctx, data_raw_acceleration);
acceleration_mg[0] =
iis3dwb_from_fs2g_to_mg(data_raw_acceleration[0]);
acceleration_mg[1] =
iis3dwb_from_fs2g_to_mg(data_raw_acceleration[1]);
acceleration_mg[2] =
iis3dwb_from_fs2g_to_mg(data_raw_acceleration[2]);
//sprintf((char *)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));
}
iis3dwb_temp_flag_data_ready_get(&dev_ctx, ®);
if (reg) {
/* Read temperature data */
memset(&data_raw_temperature, 0x00, sizeof(int16_t));
iis3dwb_temperature_raw_get(&dev_ctx, &data_raw_temperature);
temperature_degC = iis3dwb_from_lsb_to_celsius(data_raw_temperature);
//sprintf((char *)tx_buffer, "Temperature [degC]:%6.2f\r\n", temperature_degC);
tx_com(tx_buffer, strlen((char const *)tx_buffer));
}
}
}
/*
* [url=home.php?mod=space&uid=247401]@brief[/url] Write generic device register (platform dependent)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to write
* @param bufp pointer to data to write in register reg
* @param len number of consecutive register to write
*
*/
int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
uint16_t len)
{
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, ®, 1, 1000);
HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,GPIO_PIN_SET);
return 0;
}
/*
* [url=home.php?mod=space&uid=247401]@brief[/url] Read generic device register (platform dependent)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to read
* @param bufp pointer to buffer that store the data read
* @param len number of consecutive register to read
*
*/
int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
reg |= 0x80;
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, ®, 1, 1000);
HAL_SPI_Receive(handle, bufp, len, 1000);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_SET);
return 0;
}
/*
* [url=home.php?mod=space&uid=247401]@brief[/url] Send buffer to console (platform dependent)
*
* @param tx_buffer buffer to transmit
* @param len number of byte to send
*
*/
void tx_com(uint8_t *tx_buffer, uint16_t len)
{
CDC_Transmit_FS(tx_buffer, len);
}
/*
* @brief platform specific delay (platform dependent)
*
* @param ms delay in ms
*
*/
void platform_delay(uint32_t ms)
{
HAL_Delay(ms);
}
/*
* @brief platform specific initialization (platform dependent)
*/
void platform_init(void)
{
}
5.演示:
(B站过审后再补充贴上)
|