打印
[应用相关]

ST MEMS防水气压传感器LPS27HHW测评】+例程的分析及使用

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

在气压传感器LPS27HHW的相关资料中提供相应例程,但没用提供相应的工程,因此要进行具体的使用还是需要加以探索的。

前文我们已经介绍过该例程是基于STM32F411RE开发板的,并在这个开发板上完成串行通讯功能,也就是说就差LPS27HHW的驱动程序了。

有了LPS27HHW驱动程序,我们就可以用串口将检查的气压值显示出来。

LPS27HHW的例程资源分为3个部分,见图1所示。


图1  例程资源


其中在driver的目录中存放LPS27HHW的2个驱动文件,即lps27hhw_reg.c和lps27hhw_reg.h,而在example目录则存放的是示例主程序read_data_simple.c。

在使用时,相应将lps27hhw_reg.c放置在主程序main.c的目录下,见图2所示。


图2  lps27hhw_reg.c放置位置


而将lps27hhw_reg.c放置在主程序main.h的目录下,见图3所示。


图3  lps27hhw_reg.h放置位置



此时,在已有的串行通讯程序中应在文件头部添加#include "lps27hhw_reg.h"。

而在主程序main.c中,应在主程序之前引入read_data_simple.c中的相关定义及相应的辅助函数,其内容为:

typedef union{

  int16_t i16bit;

  uint8_t u8bit[2];

} axis1bit16_t;


typedef union{

  int32_t i32bit;

  uint8_t u8bit[4];

} axis1bit32_t;


#define CS_SPI2_GPIO_Port   0

#define CS_SPI2_Pin         0

#define CS_SPI1_GPIO_Port   0

#define CS_SPI1_Pin         0


#define TX_BUF_DIM          1000


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];


static int32_t platform_write(void *handle, uint8_t Reg, uint8_t *Bufp, uint16_t len)

static int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp,uint16_t len)

void tx_com( uint8_t *tx_buffer, uint16_t len )


而在初始化部分之后,添加read_data_simple.c的主体内容。

  •     whoamI = 0;
  •     lps27hhw_device_id_get(&dev_ctx, &whoamI);
  •     if ( whoamI != LPS27HHW_ID )  while(1);
  •     //manage here device not found
  •     //  Restore default configuration
  •     lps27hhw_reset_set(&dev_ctx, PROPERTY_ENABLE);
  •     do {
  •        lps27hhw_reset_get(&dev_ctx, &rst);
  •     } while (rst);
  •     //  Enable Block Data Update
  •     lps27hhw_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  •     // Set Output Data Rate
  •     lps27hhw_data_rate_set(&dev_ctx, LPS27HHW_10_Hz_LOW_NOISE);
  •     // Read samples in polling mode (no int)
  •   while(1)
  •   {
  •     //  Read output only if new value is available
  •     lps27hhw_reg_t reg;
  •     lps27hhw_read_reg(&dev_ctx, LPS27HHW_STATUS, (uint8_t *)&reg, 1);
  •     if (reg.status.p_da)
  •     {
  •       memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));
  •       lps27hhw_pressure_raw_get(&dev_ctx, data_raw_pressure.u8bit);
  •       pressure_hPa = lps27hhw_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));
  •       lps27hhw_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
  •       temperature_degC = lps27hhw_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 ) );
  •     }
  •   }

完成编译后的结果如图4所示。


图4 编译结果


目前使用串行通讯可输出的内容如图5所示,已能符合结果输出的需要。后续的工作是弄清LPS27HHW例程所使用的I2C接口具体是连接STM32F411的那个引脚,以便进行连接测试。


图5 串口输出形式


使用特权

评论回复
沙发
晓伍| | 2021-2-1 09:59 | 只看该作者
这种传感器的原理是什么呢

使用特权

评论回复
板凳
磨砂| | 2021-2-1 10:00 | 只看该作者
检测精度高不高

使用特权

评论回复
地板
八层楼| | 2021-2-1 10:01 | 只看该作者
驱动程序有没有现成的 需要自己写吗

使用特权

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

本版积分规则

1883

主题

6437

帖子

8

粉丝