- /*
- ******************************************************************************
- * [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的驱动和数据读取与显示