jinglixixi 发表于 2020-12-30 11:52

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

<div class='showpostmsg'> 本帖最后由 jinglixixi 于 2020-12-30 11:54 编辑

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">在气压传感器LPS27HHW的相关资料中提供相应例程,但没用提供相应的工程,因此要进行具体的使用还是需要加以探索的。</span></span></span></span></span></span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">前文我们已经介绍过该例程是基于STM32F411RE开发板的,并在这个开发板上完成串行通讯功能,也就是说就差LPS27HHW的驱动程序了。</span></span></span></span></span></span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">有了LPS27HHW驱动程序,我们就可以用串口将检查的气压值显示出来。</span></span></span></span></span></span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">LPS27HHW</span></span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">的例程资源分为3个部分,见图1所示。</span></span></span></span></span></span></span></span></p>

<p></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">图1&nbsp;&nbsp;例程资源</span></span></span></span></span></span></span></span></p>

<p>&nbsp;</p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">其中在driver的目录中存放LPS27HHW的2个驱动文件,即lps27hhw_reg.c和lps27hhw_reg.h,而在example目录则存放的是示例主程序read_data_simple.c。</span></span></span></span></span></span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">在使用时,相应将lps27hhw_reg.c放置在主程序main.c的目录下,见图2所示。</span></span></span></span></span></span></span></span></p>

<p></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">图2 &nbsp;lps27hhw_reg.c放置位置</span></span></span></span></span></span></span></span></p>

<p>&nbsp;</p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">而将lps27hhw_reg.c放置在主程序main.h的目录下,见图3所示。</span></span></span></span></span></span></span></span></p>

<p></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">图3 &nbsp;lps27hhw_reg.h放置位置</span></span></span></span></span></span></span></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">此时,在已有的串行通讯程序中应在文件头部添加#include &quot;lps27hhw_reg.h&quot;。</span></span></span></span></span></span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">而在主程序main.c中,应在主程序之前引入read_data_simple.c中的相关定义及相应的辅助函数,其内容为:</span></span></span></span></span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">typedef union{</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">&nbsp; int16_t i16bit;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">&nbsp; uint8_t u8bit;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">} axis1bit16_t;</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">typedef union{</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">&nbsp; int32_t i32bit;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">&nbsp; uint8_t u8bit;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">} axis1bit32_t;</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">#define CS_SPI2_GPIO_Port&nbsp;&nbsp; 0</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">#define CS_SPI2_Pin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">#define CS_SPI1_GPIO_Port&nbsp;&nbsp; 0</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">#define CS_SPI1_Pin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">#define TX_BUF_DIM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1000</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static axis1bit32_t data_raw_pressure;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static axis1bit16_t data_raw_temperature;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static float pressure_hPa;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static float temperature_degC;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static uint8_t whoamI, rst;</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static uint8_t tx_buffer;</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static int32_t platform_write(void *handle, uint8_t Reg, uint8_t *Bufp, uint16_t len)</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">static int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp,uint16_t len)</span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">void tx_com( uint8_t *tx_buffer, uint16_t len )</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="background:white"><span style="font-family:宋体"><span style="color:#a6a6a6">而在初始化部分之后,添加</span></span></span><span lang="EN-US" style="background:white"><span style="color:#a6a6a6">read_data_simple.c</span></span><span style="background:white"><span style="font-family:宋体"><span style="color:#a6a6a6">的主体内容。</span></span></span></span></span></p>

<pre>
<code class="language-cpp">    whoamI = 0;
    lps27hhw_device_id_get(&amp;dev_ctx, &amp;whoamI);
    if ( whoamI != LPS27HHW_ID )  while(1);
    //manage here device not found
    //  Restore default configuration
    lps27hhw_reset_set(&amp;dev_ctx, PROPERTY_ENABLE);
    do {
       lps27hhw_reset_get(&amp;dev_ctx, &amp;rst);
    } while (rst);
    //  Enable Block Data Update
    lps27hhw_block_data_update_set(&amp;dev_ctx, PROPERTY_ENABLE);
  // Set Output Data Rate
  lps27hhw_data_rate_set(&amp;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(&amp;dev_ctx, LPS27HHW_STATUS, (uint8_t *)&amp;reg, 1);
    if (reg.status.p_da)
    {
      memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));
      lps27hhw_pressure_raw_get(&amp;dev_ctx, data_raw_pressure.u8bit);
      pressure_hPa = lps27hhw_from_lsb_to_hpa( data_raw_pressure.i32bit);
      sprintf((char*)tx_buffer, "pressure :%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(&amp;dev_ctx, data_raw_temperature.u8bit);
      temperature_degC = lps27hhw_from_lsb_to_celsius( data_raw_temperature.i16bit );
      sprintf((char*)tx_buffer, "temperature :%6.2f\r\n", temperature_degC ); //
      tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );
    }
}</code></pre>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">完成编译后的结果如图</span></span></span><span lang="EN-US" style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;"><span style="color:#a6a6a6">4</span></span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">所示。</span></span></span></span></span></span></span></p>

<p></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">图</span></span></span><span lang="EN-US" style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;"><span style="color:#a6a6a6">4 </span></span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">编译结果</span></span></span></span></span></span></span></p>

<p>&nbsp;</p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">目前使用串行通讯可输出的内容如图</span></span></span><span lang="EN-US" style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;"><span style="color:#a6a6a6">5</span></span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">所示,已能符合结果输出的需要。后续的工作是弄清</span></span></span><span lang="EN-US" style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">LPS27HHW</span></span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#999999">例程所使用的I2C接口具体是连接STM32F411的那个引脚,以便进行连接测试。</span></span></span></span></span></span></span></span></p>

<p></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:21.0pt"><span style="font-family:宋体"><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">图</span></span></span><span lang="EN-US" style="font-size:10.5pt"><span style="background:white"><span style="font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;"><span style="color:#a6a6a6">5 </span></span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="color:#a6a6a6">串口输出形式</span></span></span></span></span></span></span></p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

littleshrimp 发表于 2020-12-30 16:08

<p>你的气压值正好1000~!</p>

jinglixixi 发表于 2020-12-30 17:39

littleshrimp 发表于 2020-12-30 16:08
你的气压值正好1000~!

<p>只是个效果测试</p>

w494143467 发表于 2020-12-30 19:28

<p>这应该测量出来的是大气压吧?</p>

jinglixixi 发表于 2020-12-30 21:48

w494143467 发表于 2020-12-30 19:28
这应该测量出来的是大气压吧?

<p>从资料看是气压和温度</p>
页: [1]
查看完整版本: 【ST MEMS防水气压传感器LPS27HHW测评】+例程的分析及使用