| 
  
| 一、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
 
 
 
   
 
 
 
 三、cubeMX配置
 由于与之前的温度传感器采用相同的I2C接口,因此使用之前的配置就可以了,不用再配置了。
 
 四、代码编写
 1、驱动
 关于传感器的驱动程序,ST官方提供了各种传感器的驱动程序,可以直接使用,完全无需重复造轮子。
 
   
 
 2、应用程序
 ST的官方也给出了一个应用程序的example,可以拿来稍做修改就能在自己项目中使用。
 
   修改后的应用程序如下:
 
 然后在main函数中,调用温度读取函数:/*
 ******************************************************************************
 * [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 shows how 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-MKI192V1
 * - NUCLEO_F401RE + X_NUCLEO_IKS01A3
 * - DISCOVERY_SPC584B + STEVAL-MKI192V1
 *
 * 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(supported)
 *
 * 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 "lps22hh_reg.h"
// #include "stm32f4xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include "i2c.h"
/* Private macro -------------------------------------------------------------*/
#define    BOOT_TIME        5 //ms
#define TX_BUF_DIM          1000
/* Private variables ---------------------------------------------------------*/
static uint32_t data_raw_pressure;
static int16_t data_raw_temperature;
static float pressure_hPa;
static float temperature_degC;
static uint8_t whoamI, rst;
static uint8_t tx_buffer[TX_BUF_DIM];
stmdev_ctx_t dev_ctx;
lps22hh_reg_t reg;
/* 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);
/* Main Example --------------------------------------------------------------*/
void lps22hh_read_data_polling(void)
{
  /* Read samples in polling mode (no int) */
    /* Read output only if new value is available */
    lps22hh_read_reg(&dev_ctx, LPS22HH_STATUS, (uint8_t *)®, 1);
    if (reg.status.p_da) {
    memset(&data_raw_pressure, 0x00, sizeof(uint32_t));
        lps22hh_pressure_raw_get(&dev_ctx, &data_raw_pressure);
        pressure_hPa = lps22hh_from_lsb_to_hpa( data_raw_pressure);
        sprintf((char *)tx_buffer, "lps22hh pressure [hPa]:%6.2f\r\n", pressure_hPa);
        tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
    }
    if (reg.status.t_da) {
        memset(&data_raw_temperature, 0x00, sizeof(int16_t));
        lps22hh_temperature_raw_get(&dev_ctx, &data_raw_temperature);
        temperature_degC = lps22hh_from_lsb_to_celsius(
                            data_raw_temperature );
        sprintf((char *)tx_buffer, "lps22hh temperature [degC]:%6.2f\r\n",
                temperature_degC );
        tx_com( tx_buffer, strlen( (char const *)tx_buffer ) );
    }
}
void lps22hh_init( void )
{
  /* Initialize mems driver interface */
  dev_ctx.write_reg = platform_write;
  dev_ctx.read_reg = platform_read;
  dev_ctx.mdelay = platform_delay;
  dev_ctx.handle = &SENSOR_BUS;
  /* Initialize platform specific hardware */
  platform_init();
  /* Wait sensor boot time */
  platform_delay(BOOT_TIME);
  /* Check device ID */
  whoamI = 0;
  lps22hh_device_id_get(&dev_ctx, &whoamI);
  if ( whoamI != LPS22HH_ID )
    while (1); /*manage here device not found */
  /* Restore default configuration */
  lps22hh_reset_set(&dev_ctx, PROPERTY_ENABLE);
  do {
    lps22hh_reset_get(&dev_ctx, &rst);
  } while (rst);
  /* Enable Block Data Update */
  lps22hh_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  /* Set Output Data Rate */
  lps22hh_data_rate_set(&dev_ctx, LPS22HH_10_Hz_LOW_NOISE);
}
/*
 * [url=home.php?mod=space&uid=247401]@brief[/url]  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)
{
  HAL_I2C_Mem_Write(handle, LPS22HH_I2C_ADD_H, 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)
{
  HAL_I2C_Mem_Read(handle, LPS22HH_I2C_ADD_H, 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)
{
}
   
 
 四、运行效果
 
   
 
 以上,就是压力传感器LPS22HH的驱动和数据读取与显示
 
 
 
 
 
 
 
 
 
 | 
 |