[STM32L4+] 【STEVAL-STWINKT1B测评】2.驱动振动传感器(IIS3DWB)

[复制链接]
 楼主| 电子烂人 发表于 2024-8-20 09:24 | 显示全部楼层 |阅读模式
<
本帖最后由 电子烂人 于 2024-8-22 17:19 编辑

#申请原创# #申请开发板#这篇帖子试着驱动下传感器:
1.IIS3DWB
IS3DWB采用系统级封装,配备了具有低噪音以及超宽且平坦频率范围的三轴数字振动传感器。该器件具有高带宽、低噪音、高稳定性和可重复灵敏度,以及在扩展温度范围(可达+105℃)内的工作能力,特别适合工业应用中的振动监控。低功耗、高性能、还有数字输出和嵌入式数字功能(如FIFO和中断),这些特点非常适合电池供电的工业无线传感器节点。
IIS3DWB具有可选的满量程加速度范围(±2/±4/±8/±16 g),并且能够测量带宽最高达6 kHz的加速度项目,输出数据率为26.7 kHz。器件中集成了3 kB的先进先出(FIFO)缓冲器,以避免任何数据丢失,并限制主机处理器的干预。(来源:意法半导体官方)
这个传感器非常适合做工业电机的监测。
2.初始化配置
74266c06b8dad7af.png
1362366c06c11c8d11.png
由板卡的原理图得知,震动计链接在SPI3上面
6604266c702414ab64.png
并且需要配置PB2使能
SPI配置如图:
6274866c06ccb35d9f.png
3.移植中间件
XCUBE-MEMS中间件包配置如图:
8584766c06b4f51e18.png

1438966c702914057e.png
另外,要进行传输数据的话,这里使能一组USB虚拟串口:
9639966c0c015b2501.png
同时也在中间件包中选择虚拟串口:
5868366c0c0d78b41e.png

4.代码编写:
初始化生成代码之后,根目录会有几个驱动文件:
1551566c0c0ad57b6c.png
这些里面包含了传感器和USB的所有驱动,我们只需要用到部分即可;
找到IIS3DBW的GIT开源网站,里面会有polling代码供移植参考
6029866c0c153e860e.png
不过这份代码有些老,并不完全适配STWINKT1B这款板卡,需要我们做一些改动:
  1. void iis3dwb_read_data_polling()
  2. {
  3.   stmdev_ctx_t dev_ctx;
  4.   /* Initialize mems driver interface */
  5.   dev_ctx.write_reg = platform_write;
  6.   dev_ctx.read_reg = platform_read;
  7.   dev_ctx.mdelay = platform_delay;
  8.   dev_ctx.handle = &hspi3;
  9.   /* Init test platform */
  10. // platform_init();
  11.   /* Wait sensor boot time */
  12.   platform_delay(BOOT_TIME);
  13.   /* Check device ID */
  14.   iis3dwb_device_id_get(&dev_ctx, &whoamI);

  15.   if (whoamI != IIS3DWB_ID)
  16.     while (1);

  17.   /* Restore default configuration */
  18.   iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);

  19.   do {
  20.     iis3dwb_reset_get(&dev_ctx, &rst);
  21.   } while (rst);

  22.   /* Enable Block Data Update */
  23.   iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  24.   /* Set Output Data Rate */
  25.   iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);
  26.   /* Set full scale */
  27.   iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);
  28.   /* Configure filtering chain(No aux interface)
  29.    * Accelerometer low pass filter path
  30.    */
  31.   iis3dwb_xl_filt_path_on_out_set(&dev_ctx, IIS3DWB_LP_ODR_DIV_100);


  32.   /* Read samples in polling mode (no int) */
  33.   while (1) {
  34.     uint8_t reg;
  35.     /* Read output only if new xl value is available */
  36.     iis3dwb_xl_flag_data_ready_get(&dev_ctx, ®);

  37.     if (reg) {
  38.       /* Read acceleration field data */
  39.       memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
  40.       iis3dwb_acceleration_raw_get(&dev_ctx, data_raw_acceleration);
  41.       acceleration_mg[0] =
  42.         iis3dwb_from_fs2g_to_mg(data_raw_acceleration[0]);
  43.       acceleration_mg[1] =
  44.         iis3dwb_from_fs2g_to_mg(data_raw_acceleration[1]);
  45.       acceleration_mg[2] =
  46.         iis3dwb_from_fs2g_to_mg(data_raw_acceleration[2]);
  47.       //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]);
  48.       tx_com(tx_buffer, strlen((char const *)tx_buffer));
  49.     }

  50.     iis3dwb_temp_flag_data_ready_get(&dev_ctx, ®);

  51.     if (reg) {
  52.       /* Read temperature data */
  53.       memset(&data_raw_temperature, 0x00, sizeof(int16_t));
  54.       iis3dwb_temperature_raw_get(&dev_ctx, &data_raw_temperature);
  55.       temperature_degC = iis3dwb_from_lsb_to_celsius(data_raw_temperature);
  56.       //sprintf((char *)tx_buffer, "Temperature [degC]:%6.2f\r\n", temperature_degC);
  57.       tx_com(tx_buffer, strlen((char const *)tx_buffer));
  58.     }
  59.   }
  60. }

  61. /*
  62. * [url=home.php?mod=space&uid=247401]@brief[/url]  Write generic device register (platform dependent)
  63. *
  64. * @param  handle    customizable argument. In this examples is used in
  65. *                   order to select the correct sensor bus handler.
  66. * @param  reg       register to write
  67. * @param  bufp      pointer to data to write in register reg
  68. * @param  len       number of consecutive register to write
  69. *
  70. */
  71. int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  72.                               uint16_t len)
  73. {


  74.     HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_RESET);
  75.     HAL_SPI_Transmit(handle, ®, 1, 1000);
  76.     HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);
  77.     HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,GPIO_PIN_SET);
  78.   return 0;
  79. }

  80. /*
  81. * [url=home.php?mod=space&uid=247401]@brief[/url]  Read generic device register (platform dependent)
  82. *
  83. * @param  handle    customizable argument. In this examples is used in
  84. *                   order to select the correct sensor bus handler.
  85. * @param  reg       register to read
  86. * @param  bufp      pointer to buffer that store the data read
  87. * @param  len       number of consecutive register to read
  88. *
  89. */
  90. int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  91.                              uint16_t len)
  92. {

  93.     reg |= 0x80;
  94.     HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_RESET);
  95.     HAL_SPI_Transmit(handle, ®, 1, 1000);
  96.     HAL_SPI_Receive(handle, bufp, len, 1000);
  97.     HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_SET);

  98.   return 0;
  99. }

  100. /*
  101. * [url=home.php?mod=space&uid=247401]@brief[/url]  Send buffer to console (platform dependent)
  102. *
  103. * @param  tx_buffer     buffer to transmit
  104. * @param  len           number of byte to send
  105. *
  106. */
  107. void tx_com(uint8_t *tx_buffer, uint16_t len)
  108. {

  109.   CDC_Transmit_FS(tx_buffer, len);

  110. }

  111. /*
  112. * @brief  platform specific delay (platform dependent)
  113. *
  114. * @param  ms        delay in ms
  115. *
  116. */
  117. void platform_delay(uint32_t ms)
  118. {
  119.   HAL_Delay(ms);
  120. }

  121. /*
  122. * @brief  platform specific initialization (platform dependent)
  123. */
  124. void platform_init(void)
  125. {
  126. }
5.演示: 2278666c702226637b.png

(B站过审后再补充贴上)

1099166c06c45371ce.png
757966c06d01b611f.png
8280366c0c1826a17f.png
yangjiaxu 发表于 2024-9-20 10:16 | 显示全部楼层
s
三轴传感器不好调试的,主要是有些数据不知道是否正确,也不好验证
呐咯密密 发表于 2024-9-20 10:16 | 显示全部楼层
s
8969266ecdb02792a4.png
这三个值是三轴的数据吗

评论

对的,加速度计的XYZ三轴  发表于 2024-9-20 20:51
班杰明 发表于 2024-9-21 12:14 | 显示全部楼层
s
虚拟串口是不是不用配置波特率啊
hans00911 发表于 2025-2-12 16:09 | 显示全部楼层
s
测试结果的值应该不是静置的值吧
7639967ac55f9d8d19.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

16

主题

70

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部