[STM32L4+] 【STEVAL-STWINKT1B测评】3、使用温/湿度传感器HTS221读取温度/湿度

[复制链接]
 楼主| xinmeng_wit 发表于 2024-8-19 21:14 | 显示全部楼层 |阅读模式
<

一、HTS221传感器介绍

STWINKT1B开发板板载了一个温湿度传感器HTS221。
本次使用HTS221传感器来测量温度和湿度。


HTS221 是一款用于测量相对湿度和温度的超紧凑型传感器。它包括一个传感元件和一个混合信号 ASIC,通过数字串行接口提供测量信息。传感元件由聚合物介电平面电容器结构组成,能够检测相对湿度变化,并采用专用的 ST 工艺制造。HTS221 采用小型顶孔帽基板栅格阵列 (HLGA) 封装,保证在 -40 °C 至 +120 °C 的温度范围内运行。

特性:
0 至 100% 相对湿度范围
电源电压:1.7 至 3.6 V
低功耗:2 μA @ 1 Hz ODR
可选 ODR 范围为 1 Hz 至 12.5 Hz
高 rH 灵敏度:0.004% rH/LSB
湿度精度:± 3.5% rH,20 至 +80% rH
温度精度:± 0.5 °C,15 至 +40 °C
嵌入式 16 位 ADC
16 位湿度和温度输出数据
SPI 和 I²C接口
工厂校准
微型 2 x 2 x 0.9 mm 封装
符合 ECOPACK 标准

二、接口确定

HTS221使用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 show the simplest way 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-MKI141V2
  25. * - NUCLEO_F401RE + STEVAL-MKI141V2
  26. * - DISCOVERY_SPC584B + STEVAL-MKI141V2
  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(N/A)
  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 "hts221_reg.h"

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


  69. /* Private macro -------------------------------------------------------------*/

  70. /* Private variables ---------------------------------------------------------*/
  71. static int16_t data_raw_humidity;
  72. static int16_t data_raw_temperature;
  73. static float humidity_perc;
  74. static float temperature_degC;
  75. static uint8_t whoamI;
  76. static uint8_t tx_buffer[1000];

  77. stmdev_ctx_t hts221_dev_ctx;
  78. /* Extern variables ----------------------------------------------------------*/

  79. /* Private functions ---------------------------------------------------------*/
  80. /*
  81. *   WARNING:
  82. *   Functions declare in this section are defined at the end of this file
  83. *   and are strictly related to the hardware platform used.
  84. *
  85. */
  86. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  87.                               uint16_t len);
  88. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  89.                              uint16_t len);
  90. static void tx_com(uint8_t *tx_buffer, uint16_t len);
  91. static void platform_delay(uint32_t ms);
  92. static void platform_init(void);

  93. /*
  94. *  Function used to apply coefficient
  95. */
  96. typedef struct {
  97.   float x0;
  98.   float y0;
  99.   float x1;
  100.   float y1;
  101. } lin_t;


  102. lin_t lin_hum;
  103. lin_t lin_temp;

  104. float linear_interpolation(lin_t *lin, int16_t x)
  105. {
  106.   return ((lin->y1 - lin->y0) * x + ((lin->x1 * lin->y0) -
  107.                                      (lin->x0 * lin->y1)))
  108.          / (lin->x1 - lin->x0);
  109. }
  110. /* Main Example --------------------------------------------------------------*/
  111. void hts221_read_data_polling(void)
  112. {
  113.   /* Read samples in polling mode */
  114.   /* Read output only if new value is available */
  115.   hts221_reg_t reg;
  116.   hts221_status_get(&hts221_dev_ctx, ®.status_reg);

  117.   if (reg.status_reg.h_da) {
  118.     /* Read humidity data */
  119.     memset(&data_raw_humidity, 0x00, sizeof(int16_t));
  120.     hts221_humidity_raw_get(&hts221_dev_ctx, &data_raw_humidity);
  121.     humidity_perc = linear_interpolation(&lin_hum, data_raw_humidity);

  122.     if (humidity_perc < 0) {
  123.       humidity_perc = 0;
  124.     }

  125.     if (humidity_perc > 100) {
  126.       humidity_perc = 100;
  127.     }

  128.     sprintf((char *)tx_buffer, "HTS221 Humidity [%%]:%3.2f\r\n", humidity_perc);
  129.     tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
  130.   }

  131.   if (reg.status_reg.t_da) {
  132.     /* Read temperature data */
  133.     memset(&data_raw_temperature, 0x00, sizeof(int16_t));
  134.     hts221_temperature_raw_get(&hts221_dev_ctx, &data_raw_temperature);
  135.     temperature_degC = linear_interpolation(&lin_temp,
  136.                                             data_raw_temperature);
  137.     sprintf((char *)tx_buffer, "HTS221 Temperature [degC]:%6.2f\r\n",
  138.             temperature_degC );
  139.     tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
  140.   }
  141. }

  142. void hts221_init( void )
  143. {
  144.   /* Initialize platform specific hardware */
  145.   platform_init();
  146.   /* Initialize mems driver interface */
  147.   hts221_dev_ctx.write_reg = platform_write;
  148.   hts221_dev_ctx.read_reg = platform_read;
  149.   hts221_dev_ctx.mdelay = platform_delay;
  150.   hts221_dev_ctx.handle = &SENSOR_BUS;
  151.   /* Check device ID */
  152.   whoamI = 0;
  153.   hts221_device_id_get(&hts221_dev_ctx, &whoamI);

  154.   if ( whoamI != HTS221_ID )
  155.     while (1); /*manage here device not found */

  156.   /* Read humidity calibration coefficient */

  157.   hts221_hum_adc_point_0_get(&hts221_dev_ctx, &lin_hum.x0);
  158.   hts221_hum_rh_point_0_get(&hts221_dev_ctx, &lin_hum.y0);
  159.   hts221_hum_adc_point_1_get(&hts221_dev_ctx, &lin_hum.x1);
  160.   hts221_hum_rh_point_1_get(&hts221_dev_ctx, &lin_hum.y1);
  161.   /* Read temperature calibration coefficient */
  162.   
  163.   hts221_temp_adc_point_0_get(&hts221_dev_ctx, &lin_temp.x0);
  164.   hts221_temp_deg_point_0_get(&hts221_dev_ctx, &lin_temp.y0);
  165.   hts221_temp_adc_point_1_get(&hts221_dev_ctx, &lin_temp.x1);
  166.   hts221_temp_deg_point_1_get(&hts221_dev_ctx, &lin_temp.y1);
  167.   /* Enable Block Data Update */
  168.   hts221_block_data_update_set(&hts221_dev_ctx, PROPERTY_ENABLE);
  169.   /* Set Output Data Rate */
  170.   hts221_data_rate_set(&hts221_dev_ctx, HTS221_ODR_1Hz);
  171.   /* Device power on */
  172.   hts221_power_on_set(&hts221_dev_ctx, PROPERTY_ENABLE);
  173. }

  174. /*
  175. * @brief  Write generic device register (platform dependent)
  176. *
  177. * @param  handle    customizable argument. In this examples is used in
  178. *                   order to select the correct sensor bus handler.
  179. * @param  reg       register to write
  180. * @param  bufp      pointer to data to write in register reg
  181. * @param  len       number of consecutive register to write
  182. *
  183. */
  184. static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
  185.                               uint16_t len)
  186. {
  187.   /* Write multiple command */
  188.   reg |= 0x80;
  189.   HAL_I2C_Mem_Write(handle, HTS221_I2C_ADDRESS, reg,
  190.                     I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);
  191.   return 0;
  192. }

  193. /*
  194. * @brief  Read generic device register (platform dependent)
  195. *
  196. * @param  handle    customizable argument. In this examples is used in
  197. *                   order to select the correct sensor bus handler.
  198. * @param  reg       register to read
  199. * @param  bufp      pointer to buffer that store the data read
  200. * @param  len       number of consecutive register to read
  201. *
  202. */
  203. static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
  204.                              uint16_t len)
  205. {
  206.   /* Read multiple command */
  207.   reg |= 0x80;
  208.   HAL_I2C_Mem_Read(handle, HTS221_I2C_ADDRESS, reg,
  209.                    I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
  210.   return 0;
  211. }

  212. /*
  213. * @brief  Write generic device register (platform dependent)
  214. *
  215. * @param  tx_buffer     buffer to transmit
  216. * @param  len           number of byte to send
  217. *
  218. */
  219. static void tx_com(uint8_t *tx_buffer, uint16_t len)
  220. {
  221.   HAL_UART_Transmit(&huart2, tx_buffer, len, 1000);
  222. }

  223. /*
  224. * @brief  platform specific delay (platform dependent)
  225. *
  226. * @param  ms        delay in ms
  227. *
  228. */
  229. static void platform_delay(uint32_t ms)
  230. {
  231.   HAL_Delay(ms);
  232. }

  233. /*
  234. * @brief  platform specific initialization (platform dependent)
  235. */
  236. static void platform_init(void)
  237. {

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

五、运行效果
5.png


以上,温湿度传感器HTS221的驱动和数据读取与显示
hhdesign2012 发表于 2024-8-23 07:07 | 显示全部楼层
g下载下来看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

70

主题

276

帖子

2

粉丝

70

主题

276

帖子

2

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