jinglixixi 发表于 2020-12-23 09:20

【ST MEMS防水气压传感器LPS27HHW测评】+基于STM32F411RE的串行通讯

<p>常言道:&ldquo;工欲善其事,必先利其器。&rdquo;</p>

<p>对于LPS27HHW气压传感器来讲,要想使其发挥作用,处理完成驱动外,还要解决必备的相关支持,例如要输出传感器的检测结果,就必须有显示器件做支持,</p>

<p>或是使用串行通讯。</p>

<p>对串行通讯来讲,是每个开发板都基本具备,因此就先解决串行通讯问题。</p>

<p>那以哪种开发板为基础呢?</p>

<p>因为从LPS27HHW的例程来看它是基于STM32F411RE开发板的,为便于尽早地实现LPS27HHW的驱动,故以STM32F411RE开发板为测试的基础。</p>

<p>当然,在使用前要先为开发板构建必要的开发环境,由于是选取KEIL为开发工具,因此还为其特地下载和安装了STM32F4xx_DFP.2.14.0的升级包。</p>

<p>这样就有了对STM32F411RE的芯片支持,见图1所示。</p>

<p>图1&nbsp; 实现芯片支持</p>

<p>&nbsp;</p>

<p>此外,还为其设置了相应的烧写算法,见图2所示。</p>

<p>图2 选取烧写算法</p>

<p>&nbsp;</p>

<p>实现程序通讯的主程序为:</p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="hljs language-cpp"><span class="hljs-keyword">int</span> main(<span class="hljs-keyword">void</span>)
{
<span class="hljs-comment">/* Configure the system clock to 100 MHz */</span>
SystemClock_Config();
<span class="hljs-comment">/* Initialize LED2 */</span>
LED_Init();
<span class="hljs-comment">/* Set LED2 Off */</span>
LED_Off();
<span class="hljs-comment">/* Initialize button in EXTI mode */</span>
UserButton_Init();
<span class="hljs-comment">/* Configure USARTx (USART IP configuration and related GPIO initialization) */</span>
Configure_USART();
<span class="hljs-comment">/* Infinite loop */</span>
<span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>)
{
    <span class="hljs-comment">/* Wait for user push button press to start transfer */</span>
    WaitForUserButtonPress();
    <span class="hljs-comment">/* transfer Tx buffer to PC application */</span>
    BufferTransfer();
}
}

</code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background: url(&quot;https://bbs.eeworld.com.cn/static/editor/plugins/widget/images/handle.png&quot;) rgba(220, 220, 220, 0.5); top: -15px; left: 0px; display: block;"><img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p>&nbsp;</p>

<p>相关的串行通讯函数为:</p>

<pre>
<code class="language-cpp">void WaitForUserButtonPress(void)
{
while (ubButtonPress == 0)
{
}
ubSend = 0;
}

void BufferTransfer(void)
{

/* Send characters one per one, until last char to be sent */
while (ubSend &lt; sizeof(aStringToSend))
{
#if (USE_TIMEOUT == 1)
    Timeout = USART_SEND_TIMEOUT_TXE_MS;
#endif /* USE_TIMEOUT */

    /* Wait for TXE flag to be raised */
    while (!LL_USART_IsActiveFlag_TXE(USARTx_INSTANCE))
    {
#if (USE_TIMEOUT == 1)
      /* Check Systick counter flag to decrement the time-out value */
      if (LL_SYSTICK_IsActiveCounterFlag())
      {
      if(Timeout-- == 0)
      {
          /* Time-out occurred. Set LED to blinking mode */
          LED_Blinking(LED_BLINK_SLOW);
      }
      }
#endif /* USE_TIMEOUT */
    }

    /* If last char to be sent, clear TC flag */
    if (ubSend == (sizeof(aStringToSend) - 1))
    {
      LL_USART_ClearFlag_TC(USARTx_INSTANCE);
    }

    /* Write character in Transmit Data register.
       TXE flag is cleared by writing data in DR register */
    LL_USART_TransmitData8(USARTx_INSTANCE, aStringToSend);
}

#if (USE_TIMEOUT == 1)
Timeout = USART_SEND_TIMEOUT_TC_MS;
#endif /* USE_TIMEOUT */

/* Wait for TC flag to be raised for last char */
while (!LL_USART_IsActiveFlag_TC(USARTx_INSTANCE))
{
#if (USE_TIMEOUT == 1)
    /* Check Systick counter flag to decrement the time-out value */
    if (LL_SYSTICK_IsActiveCounterFlag())
    {
      if(Timeout-- == 0)
      {
      /* Time-out occurred. Set LED to blinking mode */
      LED_Blinking(LED_BLINK_SLOW);
      }
    }
#endif /* USE_TIMEOUT */
}

ubButtonPress =0;
   
/* Turn LED2 On at end of transfer : Tx sequence completed successfully */
LED_On();
}
</code></pre>

<p>经程序的编译 下载,其结果如图3所示。</p>

<p>图3 完成下载</p>

<p>其运行效果如图4所示,有了它就不愁LPS27HHW的结果显示问题了。</p>

<p>图4&nbsp;运行效果</p>

freebsder 发表于 2020-12-23 18:31

<p>这么快就搞起来了,很期待看看这东西到底咋样。</p>

jinglixixi 发表于 2020-12-24 00:12

freebsder 发表于 2020-12-23 18:31
这么快就搞起来了,很期待看看这东西到底咋样。

<p>例程有问题编译通不过,可能会影响速度。</p>

okhxyyo 发表于 2020-12-28 10:40

<p><strong><a href="https://bbs.eeworld.com.cn/thread-1149042-1-1.html" target="_blank">免费评测:ST MEMS防水气压传感器LPS27HHW</a></strong></p>

<p>汇总贴:<a href="https://bbs.eeworld.com.cn/thread-1152577-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1152577-1-1.html</a></p>
页: [1]
查看完整版本: 【ST MEMS防水气压传感器LPS27HHW测评】+基于STM32F411RE的串行通讯