[STM32L4+] 【STEVAL-STWINKT1B测评】4、使用压力传感器LPS22HH测量并显示压力

[复制链接]
 楼主| xinmeng_wit 发表于 2024-8-22 21:43 | 显示全部楼层 |阅读模式
<
一、LPS22HH传感器介绍

LPS22HH是一款超紧凑的压阻式绝对压力传感器,可用作数字输出气压计。该器件包括一个传感元件和一个 IC 接口,该接口通过 I²C、MIPI、I3C 进行通信SM系列或从传感元件到应用的 SPI。
检测绝对压力的传感元件由一个悬浮膜组成,该膜采用意法半导体开发的专用工艺制造。
该LPS22HH采用全模、带孔 LGA 封装 (HLGA)。保证工作温度范围为-40 °C至+85 °C。 封装上有孔,以允许外部压力到达传感元件。
特性:


  • 260 至 1260 hPa 绝对压力范围
  • 电流消耗低至 4 μA
  • 绝对压力精度:0.5 hPa
  • 低压传感器噪声:0.65 Pa
  • 高性能 TCO:0.65 Pa/°C
  • 嵌入式温度补偿
  • 24位压力数据输出
  • ODR 从 1 Hz 到 200 Hz


二、接口确定


LPS22HH使用I2C接口与MCU连接,与上一篇的温度传感器使用同一路I2C接口,也就是PF0和PF1。
具体详见上一篇帖子:https://bbs.21ic.com/icview-3396498-1-1.html


1.png




三、cubeMX配置
由于与之前的温度传感器采用相同的I2C接口,因此使用之前的配置就可以了,不用再配置了。

四、代码编写
1、驱动
关于传感器的驱动程序,ST官方提供了各种传感器的驱动程序,可以直接使用,完全无需重复造轮子。
2.png


2、应用程序
ST的官方也给出了一个应用程序的example,可以拿来稍做修改就能在自己项目中使用。
3.png
修改后的应用程序如下:
  1. /*
  2. ******************************************************************************
  3. * [url=home.php?mod=space&uid=288409]@file[/url]    read_data_polling.c
  4. * [url=home.php?mod=space&uid=187600]@author[/url]  MEMS Software Solution Team
  5. * [url=home.php?mod=space&uid=247401]@brief[/url]   This file shows how to get data from sensor .
  6. *
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>© Copyright (c) 2020 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under BSD 3-Clause license,
  14. * the "License"; You may not use this file except in compliance with the
  15. * License. You may obtain a copy of the License at:
  16. *                        opensource.org/licenses/BSD-3-Clause
  17. *
  18. ******************************************************************************
  19. */

  20. /*
  21. * This example was developed using the following STMicroelectronics
  22. * evaluation boards:
  23. *
  24. * - STEVAL_MKI109V3 + STEVAL-MKI192V1
  25. * - NUCLEO_F401RE + X_NUCLEO_IKS01A3
  26. * - DISCOVERY_SPC584B + STEVAL-MKI192V1
  27. *
  28. * and STM32CubeMX tool with STM32CubeF4 MCU Package
  29. *
  30. * Used interfaces:
  31. *
  32. * STEVAL_MKI109V3    - Host side:   USB (Virtual COM)
  33. *                    - Sensor side: SPI(Default) / I2C(supported)
  34. *
  35. * NUCLEO_STM32F401RE - Host side: UART(COM) to USB bridge
  36. *                    - I2C(Default) / SPI(supported)
  37. *
  38. * DISCOVERY_SPC584B  - Host side: UART(COM) to USB bridge
  39. *                    - Sensor side: I2C(Default) / SPI(supported)
  40. *
  41. * If you need to run this example on a different hardware platform a
  42. * modification of the functions: `platform_write`, `platform_read`,
  43. * `tx_com` and 'platform_init' is required.
  44. *
  45. */

  46. /* STMicroelectronics evaluation boards definition
  47. *
  48. * Please uncomment ONLY the evaluation boards in use.
  49. * If a different hardware is used please comment all
  50. * following target board and redefine yours.
  51. */

  52. //#define STEVAL_MKI109V3  /* little endian */
  53. //#define NUCLEO_F401RE    /* little endian */
  54. //#define SPC584B_DIS      /* big endian */

  55. /* ATTENTION: By default the driver is little endian. If you need switch
  56. *            to big endian please see "Endianness definitions" in the
  57. *            header file of the driver (_reg.h).
  58. */

  59. /* NUCLEO_F401RE: Define communication interface */
  60. #define SENSOR_BUS hi2c2


  61. /* Includes ------------------------------------------------------------------*/
  62. #include <string.h>
  63. #include <stdio.h>
  64. #include "lps22hh_reg.h"

  65. // #include "stm32f4xx_hal.h"
  66. #include "usart.h"
  67. #include "gpio.h"
  68. #include "i2c.h"


  69. /* Private macro -------------------------------------------------------------*/
  70. #define    BOOT_TIME        5 //ms

  71. #define TX_BUF_DIM          1000

  72. /* Private variables ---------------------------------------------------------*/
  73. static uint32_t data_raw_pressure;
  74. static int16_t data_raw_temperature;
  75. static float pressure_hPa;
  76. static float temperature_degC;
  77. static uint8_t whoamI, rst;
  78. static uint8_t tx_buffer[TX_BUF_DIM];

  79. stmdev_ctx_t dev_ctx;
  80. lps22hh_reg_t reg;

  81. /* Extern variables ----------------------------------------------------------*/

  82. /* Private functions ---------------------------------------------------------*/

  83. /*
  84. *   WARNING:
  85. *   Functions declare in this section are defined at the end of this file
  86. *   and are strictly related to the hardware platform used.
  87. *
  88. */

  89. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  90.                               uint16_t len);
  91. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  92.                              uint16_t len);
  93. static void tx_com( uint8_t *tx_buffer, uint16_t len );
  94. static void platform_delay(uint32_t ms);
  95. static void platform_init(void);

  96. /* Main Example --------------------------------------------------------------*/

  97. void lps22hh_read_data_polling(void)
  98. {

  99.   /* Read samples in polling mode (no int) */
  100.     /* Read output only if new value is available */
  101.     lps22hh_read_reg(&dev_ctx, LPS22HH_STATUS, (uint8_t *)®, 1);

  102.     if (reg.status.p_da) {
  103.     memset(&data_raw_pressure, 0x00, sizeof(uint32_t));
  104.         lps22hh_pressure_raw_get(&dev_ctx, &data_raw_pressure);
  105.         pressure_hPa = lps22hh_from_lsb_to_hpa( data_raw_pressure);
  106.         sprintf((char *)tx_buffer, "lps22hh pressure [hPa]:%6.2f\r\n", pressure_hPa);
  107.         tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
  108.     }

  109.     if (reg.status.t_da) {
  110.         memset(&data_raw_temperature, 0x00, sizeof(int16_t));
  111.         lps22hh_temperature_raw_get(&dev_ctx, &data_raw_temperature);
  112.         temperature_degC = lps22hh_from_lsb_to_celsius(
  113.                             data_raw_temperature );
  114.         sprintf((char *)tx_buffer, "lps22hh temperature [degC]:%6.2f\r\n",
  115.                 temperature_degC );
  116.         tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
  117.     }
  118. }


  119. void lps22hh_init( void )
  120. {
  121.   /* Initialize mems driver interface */
  122.   dev_ctx.write_reg = platform_write;
  123.   dev_ctx.read_reg = platform_read;
  124.   dev_ctx.mdelay = platform_delay;
  125.   dev_ctx.handle = &SENSOR_BUS;
  126.   /* Initialize platform specific hardware */
  127.   platform_init();
  128.   /* Wait sensor boot time */
  129.   platform_delay(BOOT_TIME);
  130.   /* Check device ID */
  131.   whoamI = 0;
  132.   lps22hh_device_id_get(&dev_ctx, &whoamI);

  133.   if ( whoamI != LPS22HH_ID )
  134.     while (1); /*manage here device not found */

  135.   /* Restore default configuration */
  136.   lps22hh_reset_set(&dev_ctx, PROPERTY_ENABLE);

  137.   do {
  138.     lps22hh_reset_get(&dev_ctx, &rst);
  139.   } while (rst);

  140.   /* Enable Block Data Update */
  141.   lps22hh_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  142.   /* Set Output Data Rate */
  143.   lps22hh_data_rate_set(&dev_ctx, LPS22HH_10_Hz_LOW_NOISE);
  144. }

  145. /*
  146. * [url=home.php?mod=space&uid=247401]@brief[/url]  Write generic device register (platform dependent)
  147. *
  148. * @param  handle    customizable argument. In this examples is used in
  149. *                   order to select the correct sensor bus handler.
  150. * @param  reg       register to write
  151. * @param  bufp      pointer to data to write in register reg
  152. * @param  len       number of consecutive register to write
  153. *
  154. */
  155. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  156.                               uint16_t len)
  157. {
  158.   HAL_I2C_Mem_Write(handle, LPS22HH_I2C_ADD_H, reg,
  159.                     I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);
  160.   return 0;
  161. }

  162. /*
  163. * @brief  Read generic device register (platform dependent)
  164. *
  165. * @param  handle    customizable argument. In this examples is used in
  166. *                   order to select the correct sensor bus handler.
  167. * @param  reg       register to read
  168. * @param  bufp      pointer to buffer that store the data read
  169. * @param  len       number of consecutive register to read
  170. *
  171. */
  172. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  173.                              uint16_t len)
  174. {
  175.   HAL_I2C_Mem_Read(handle, LPS22HH_I2C_ADD_H, reg,
  176.                    I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
  177.   return 0;
  178. }

  179. /*
  180. * @brief  Write generic device register (platform dependent)
  181. *
  182. * @param  tx_buffer     buffer to transmit
  183. * @param  len           number of byte to send
  184. *
  185. */
  186. static void tx_com(uint8_t *tx_buffer, uint16_t len)
  187. {
  188.   HAL_UART_Transmit(&huart2, tx_buffer, len, 1000);
  189. }

  190. /*
  191. * @brief  platform specific delay (platform dependent)
  192. *
  193. * @param  ms        delay in ms
  194. *
  195. */
  196. static void platform_delay(uint32_t ms)
  197. {
  198.   HAL_Delay(ms);
  199. }

  200. /*
  201. * @brief  platform specific initialization (platform dependent)
  202. */
  203. static void platform_init(void)
  204. {

  205. }

然后在main函数中,调用温度读取函数:
4.png


四、运行效果
5.png


以上,就是压力传感器LPS22HH的驱动和数据读取与显示








梵蒂冈是神uy 发表于 2024-8-23 14:38 | 显示全部楼层
使用 I2C 接口连接 LPS22HH,并且它与温度传感器共享同一个 I2C 总线(PF0 和 PF1),因此你可以继续使用之前在 CubeMX 中的配置。
呐咯密密 发表于 2024-9-20 10:10 | 显示全部楼层
这个开发板的开发环境真不错,传感器的库都集成好了
yangjiaxu 发表于 2024-9-20 10:14 | 显示全部楼层
这个不错啊,传感器集成上了,这样方便一些工程师调试和应用了
Henryko 发表于 2024-9-21 12:09 | 显示全部楼层
这个官方提供的驱动在哪找啊?
 楼主| xinmeng_wit 发表于 2024-9-24 19:30 | 显示全部楼层
Henryko 发表于 2024-9-21 12:09
这个官方提供的驱动在哪找啊?

可以看我的温度传感器的那篇文章,里面有github的链接
更多更合适ii 发表于 2024-10-2 21:43 来自手机 | 显示全部楼层
LPS22HH使用I2C接口与MCU连接,与上一篇的温度传感器使用同一路I2C接口
25Frank 发表于 2025-7-7 09:48 | 显示全部楼层
您好请问,每次修改原例程的代码,主要是修改那些部分
 楼主| xinmeng_wit 发表于 2025-7-15 12:47 | 显示全部楼层
25Frank 发表于 2025-7-7 09:48
您好请问,每次修改原例程的代码,主要是修改那些部分

初始化部分单独拿出来,然后是数据处理,根据自己的需求来
您需要登录后才可以回帖 登录 | 注册

本版积分规则

70

主题

276

帖子

2

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