打印
[STM32L4+]

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

[复制链接]
1496|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

一、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


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

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



2、应用程序
ST的官方也给出了一个应用程序的example,可以拿来稍做修改就能在自己项目中使用。


修改后的应用程序如下:
/*
******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url]    read_data_polling.c
* [url=home.php?mod=space&uid=187600]@author[/url]  MEMS Software Solution Team
* [url=home.php?mod=space&uid=247401]@brief[/url]   This file show the simplest way to get data from sensor.
*
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*                        opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/

/*
* This example was developed using the following STMicroelectronics
* evaluation boards:
*
* - STEVAL_MKI109V3 + STEVAL-MKI141V2
* - NUCLEO_F401RE + STEVAL-MKI141V2
* - DISCOVERY_SPC584B + STEVAL-MKI141V2
*
* and STM32CubeMX tool with STM32CubeF4 MCU Package
*
* Used interfaces:
*
* STEVAL_MKI109V3    - Host side:   USB (Virtual COM)
*                    - Sensor side: SPI(Default) / I2C(supported)
*
* NUCLEO_STM32F401RE - Host side: UART(COM) to USB bridge
*                    - I2C(Default) / SPI(N/A)
*
* DISCOVERY_SPC584B  - Host side: UART(COM) to USB bridge
*                    - Sensor side: I2C(Default) / SPI(supported)
*
* If you need to run this example on a different hardware platform a
* modification of the functions: `platform_write`, `platform_read`,
* `tx_com` and 'platform_init' is required.
*
*/

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

//#define STEVAL_MKI109V3  /* little endian */
//#define NUCLEO_F401RE    /* little endian */
//#define SPC584B_DIS      /* big endian */

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


/* NUCLEO_F401RE: Define communication interface */
#define SENSOR_BUS hi2c2

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include "hts221_reg.h"

// #include "stm32f4xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include "i2c.h"


/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
static int16_t data_raw_humidity;
static int16_t data_raw_temperature;
static float humidity_perc;
static float temperature_degC;
static uint8_t whoamI;
static uint8_t tx_buffer[1000];

stmdev_ctx_t hts221_dev_ctx;
/* Extern variables ----------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
/*
*   WARNING:
*   Functions declare in this section are defined at the end of this file
*   and are strictly related to the hardware platform used.
*
*/
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
                              uint16_t len);
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
                             uint16_t len);
static void tx_com(uint8_t *tx_buffer, uint16_t len);
static void platform_delay(uint32_t ms);
static void platform_init(void);

/*
*  Function used to apply coefficient
*/
typedef struct {
  float x0;
  float y0;
  float x1;
  float y1;
} lin_t;


lin_t lin_hum;
lin_t lin_temp;

float linear_interpolation(lin_t *lin, int16_t x)
{
  return ((lin->y1 - lin->y0) * x + ((lin->x1 * lin->y0) -
                                     (lin->x0 * lin->y1)))
         / (lin->x1 - lin->x0);
}
/* Main Example --------------------------------------------------------------*/
void hts221_read_data_polling(void)
{
  /* Read samples in polling mode */
  /* Read output only if new value is available */
  hts221_reg_t reg;
  hts221_status_get(&hts221_dev_ctx, ®.status_reg);

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

    if (humidity_perc < 0) {
      humidity_perc = 0;
    }

    if (humidity_perc > 100) {
      humidity_perc = 100;
    }

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

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

void hts221_init( void )
{
  /* Initialize platform specific hardware */
  platform_init();
  /* Initialize mems driver interface */
  hts221_dev_ctx.write_reg = platform_write;
  hts221_dev_ctx.read_reg = platform_read;
  hts221_dev_ctx.mdelay = platform_delay;
  hts221_dev_ctx.handle = &SENSOR_BUS;
  /* Check device ID */
  whoamI = 0;
  hts221_device_id_get(&hts221_dev_ctx, &whoamI);

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

  /* Read humidity calibration coefficient */

  hts221_hum_adc_point_0_get(&hts221_dev_ctx, &lin_hum.x0);
  hts221_hum_rh_point_0_get(&hts221_dev_ctx, &lin_hum.y0);
  hts221_hum_adc_point_1_get(&hts221_dev_ctx, &lin_hum.x1);
  hts221_hum_rh_point_1_get(&hts221_dev_ctx, &lin_hum.y1);
  /* Read temperature calibration coefficient */
  
  hts221_temp_adc_point_0_get(&hts221_dev_ctx, &lin_temp.x0);
  hts221_temp_deg_point_0_get(&hts221_dev_ctx, &lin_temp.y0);
  hts221_temp_adc_point_1_get(&hts221_dev_ctx, &lin_temp.x1);
  hts221_temp_deg_point_1_get(&hts221_dev_ctx, &lin_temp.y1);
  /* Enable Block Data Update */
  hts221_block_data_update_set(&hts221_dev_ctx, PROPERTY_ENABLE);
  /* Set Output Data Rate */
  hts221_data_rate_set(&hts221_dev_ctx, HTS221_ODR_1Hz);
  /* Device power on */
  hts221_power_on_set(&hts221_dev_ctx, PROPERTY_ENABLE);
}

/*
* @brief  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
*
*/
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
                              uint16_t len)
{
  /* Write multiple command */
  reg |= 0x80;
  HAL_I2C_Mem_Write(handle, HTS221_I2C_ADDRESS, reg,
                    I2C_MEMADD_SIZE_8BIT, (uint8_t*) bufp, len, 1000);
  return 0;
}

/*
* @brief  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
*
*/
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
                             uint16_t len)
{
  /* Read multiple command */
  reg |= 0x80;
  HAL_I2C_Mem_Read(handle, HTS221_I2C_ADDRESS, reg,
                   I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
  return 0;
}

/*
* @brief  Write generic device register (platform dependent)
*
* @param  tx_buffer     buffer to transmit
* @param  len           number of byte to send
*
*/
static void tx_com(uint8_t *tx_buffer, uint16_t len)
{
  HAL_UART_Transmit(&huart2, tx_buffer, len, 1000);
}

/*
* @brief  platform specific delay (platform dependent)
*
* @param  ms        delay in ms
*
*/
static void platform_delay(uint32_t ms)
{
  HAL_Delay(ms);
}

/*
* @brief  platform specific initialization (platform dependent)
*/
static void platform_init(void)
{

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


五、运行效果



以上,温湿度传感器HTS221的驱动和数据读取与显示

使用特权

评论回复
沙发
hhdesign2012| | 2024-8-23 07:07 | 只看该作者
g下载下来看看

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

68

主题

240

帖子

1

粉丝