测量LPS22HH气压和温湿度通过串口打印
/* Includes ------------------------------------------------------------------*/
#include "lps22hh_reg.h"
#include "main.h"
#include <string.h>
#include <stdio.h>
//#define MKI109V2
#define NUCLEO_G474RE
#define TX_BUF_DIM 1000
/* Private variables ---------------------------------------------------------*/
static axis1bit32_t data_raw_pressure;
static axis1bit16_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];
extern I2C_HandleTypeDef hi2c1;
extern UART_HandleTypeDef hlpuart1;
/* Extern variables ----------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*
* Replace the functions "platform_write" and "platform_read" with your
* platform specific read and write function.
* This example use an STM32 evaluation board and CubeMX tool.
* In this case the "*handle" variable is usefull in order to select the
* correct interface but the usage uf "*handle" is not mandatory.
*/
static int32_t platform_write(void *handle, uint8_t Reg, uint8_t *Bufp,
uint16_t len)
{
if (handle == &hi2c1)
{
HAL_I2C_Mem_Write(handle, LPS22HH_I2C_ADD_H, Reg,
I2C_MEMADD_SIZE_8BIT, Bufp, len, 1000);
}
#ifdef MKI109V2
else if (handle == &hspi2)
{
HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &Reg, 1, 1000);
HAL_SPI_Transmit(handle, Bufp, len, 1000);
HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_SET);
}
else if (handle == &hspi1)
{
HAL_GPIO_WritePin(CS_SPI1_GPIO_Port, CS_SPI1_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &Reg, 1, 1000);
HAL_SPI_Transmit(handle, Bufp, len, 1000);
HAL_GPIO_WritePin(CS_SPI1_GPIO_Port, CS_SPI1_Pin, GPIO_PIN_SET);
}
#endif
return 0;
}
static int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp,
uint16_t len)
{
if (handle == &hi2c1)
{
HAL_I2C_Mem_Read(handle, LPS22HH_I2C_ADD_H, Reg,
I2C_MEMADD_SIZE_8BIT, Bufp, len, 1000);
}
#ifdef MKI109V2
else if (handle == &hspi2)
{
Reg |= 0x80;
HAL_GPIO_WritePin(CS_DEV_GPIO_Port, CS_DEV_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &Reg, 1, 1000);
HAL_SPI_Receive(handle, Bufp, len, 1000);
HAL_GPIO_WritePin(CS_DEV_GPIO_Port, CS_DEV_Pin, GPIO_PIN_SET);
}
else
{
Reg |= 0x80;
HAL_GPIO_WritePin(CS_RF_GPIO_Port, CS_RF_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &Reg, 1, 1000);
HAL_SPI_Receive(handle, Bufp, len, 1000);
HAL_GPIO_WritePin(CS_RF_GPIO_Port, CS_RF_Pin, GPIO_PIN_SET);
}
#endif
return 0;
}
/*
* Function to print messages
*/
void tx_com( uint8_t *tx_buffer, uint16_t len )
{
#ifdef NUCLEO_G474RE
HAL_UART_Transmit( &hlpuart1, tx_buffer, len, 1000 );
#endif
#ifdef MKI109V2
CDC_Transmit_FS( tx_buffer, len );
#endif
}
/* Main Example --------------------------------------------------------------*/
void example_main(void)
{
/*
* Initialize mems driver interface
*/
lps22hh_ctx_t dev_ctx;
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &hi2c1;
/*
* 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);
/*
* Read samples in polling mode (no int)
*/
while(1)
{
/*
* Read output only if new value is available
*/
lps22hh_reg_t reg;
lps22hh_read_reg(&dev_ctx, LPS22HH_STATUS, (uint8_t *)®, 1);
if (reg.status.p_da)
{
memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));
lps22hh_pressure_raw_get(&dev_ctx, data_raw_pressure.u8bit);
pressure_hPa = lps22hh_from_lsb_to_hpa( data_raw_pressure.i32bit);
sprintf((char*)tx_buffer, "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.u8bit, 0x00, sizeof(int16_t));
lps22hh_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
temperature_degC = lps22hh_from_lsb_to_celsius( data_raw_temperature.i16bit );
sprintf((char*)tx_buffer, "temperature [degC]:%6.2f\r\n", temperature_degC );
tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );
}
}
}
工程文件:
stm32g474 lps22hh.rar
(2.57 MB, 下载次数: 64)
此内容由EEWORLD论坛网友littleshrimp原创,如需转载或用于商业用途需征得作者同意并注明出处