- /*
- ******************************************************************************
- * [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)
- {
- }
然后在main函数中,调用温度读取函数:
四、运行效果
以上,就是压力传感器LPS22HH的驱动和数据读取与显示