jinglixixi 发表于 2020-8-31 22:44

【ESK32-360测评】+ 串口通信及应用

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">1. <span style="font-family:宋体">串口通信</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">在</span>ESK32-360<span style="font-family:宋体">开发板上提供了串行通讯功能,其接口如图</span>1<span style="font-family:宋体">所示。</span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:12.0pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.4pt"> </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 style="font-family:宋体">图</span>1&nbsp; RS232<span style="font-family:宋体">接口</span></span></span></p>

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

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:12.0pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.4pt"> </span></span></span></span></span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:12.0pt"><span style="font-family:宋体"><span lang="EN-US" style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.4pt"> </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 style="font-family:宋体">图</span>2&nbsp; DB9<span style="font-family:宋体">的引脚连接</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="font-family:宋体">通过串行通讯的例程,我们可以学习串口通信的基本用法。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">例程的主程序如下:</span></span></span></p>

<pre>
<code class="language-cpp">uc8 gHelloString[] = "Hello, this is USART Tx/Rx Polling example. Please enter characters.....\r\n";
int main(void)
{
  HT32F_DVB_LEDInit(HT_LED1);
  UxART_Configuration();
  UxART_TxTest();
  while (1)
  {
    #if 0 // Blocking mode
    {
      UxART_RxTest_Block();         // Wait until get UxART data
    }
    #else // Non-Blocking mode
    {
      UxART_RxTest_NonBlock();     // Return if no UxART data, LED can toggle by "LED_Demo()".
    }
    #endif
    LED_Demo();                    //   LED1  闪烁
  }
}</code></pre>

<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="font-family:宋体">由程序分析可以看出,它预先定义了一个数组来存放字符串,在完成串口通信的基本配置后,就发送该字符串来验证输出功能,随后可通过串口向其发送信息,在接收后再原样反馈输出,其执行效果如图</span>3<span style="font-family:宋体">所示。</span></span></span></p>

<p><span style="font-size:12pt"><span style="background:white"><span style="line-height:12.0pt"><span style="font-family:宋体"> </span></span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>3 <span style="font-family:宋体">串口通信</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">2.<span style="font-family:宋体">基本应用</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">有了串行通讯后,我们用它可以做什么呢?</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">我们还是将它与</span>LCD<span style="font-family:宋体">屏显示结合起来,最简单的就是发送数据在</span>LCD<span style="font-family:宋体">屏上进行绘制,如动态地指定一个宽度,然后进行指定色彩的填充,见图</span>4<span style="font-family:宋体">和图</span>5<span style="font-family:宋体">所示。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> </span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>4 <span style="font-family:宋体">窄彩条</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></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>5<span style="font-family:宋体">宽彩条</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">3.<span style="font-family:宋体">通讯传输显示图片</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="font-family:宋体">有了前面简单的测试,我们就可以大胆地期待着通过传输数据来显示图片了,因为前面用数组来显示大的图片时会导致下载崩溃。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">首先以小图片来测试,成功通过!其效果如图</span>6<span style="font-family:宋体">所示。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> </span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>6 <span style="font-family:宋体">以传送数据来显示图片</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="font-family:宋体">传送数据的界面如图</span>7<span style="font-family:宋体">所示,其中&ldquo;</span>00 38<span style="font-family:宋体">&rdquo;是图片宽度,即</span>56<span style="font-family:宋体">个像素,而&ldquo;</span>00 44<span style="font-family:宋体">&rdquo;是图片的高度,即</span>68<span style="font-family:宋体">个像素。别小看这个图片,它可是占用了</span>7616<span style="font-family:宋体">个字节呀!</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> </span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>7 <span style="font-family:宋体">传送图片数据</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">那么把胆子再放大些呢?</span> <span style="font-family:宋体">能传送</span>153600<span style="font-family:宋体">个字节吗?它可是满屏图片的规格,</span>153600=240*320*2</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">(&nbsp; 320=<span style="color:red">0140</span>H&nbsp; 240=<span style="color:red">F0</span>H&nbsp; <span style="color:red">153600</span>=240*320*2&nbsp; )</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">首先用</span>Image2Lcd<span style="font-family:宋体">软件将一幅</span>240*320<span style="font-family:宋体">像素点的图片转换为二进制数据文件</span>BIN<span style="font-family:宋体">,其格式设置如图</span>8<span style="font-family:宋体">所示,然后进行串口传送,则效果如图</span>9<span style="font-family:宋体">所示,一样的成功!</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">当然在执行时有些吓人,首先是读入文件时串口调试器有点懵,像是被打晕,好一会没找到北;好容易在发送窗口有了数据,这次该轮到发送器晕了,也是一段时间没响应,近乎是要崩溃!还在还算顽强,终于挺过来了,屏幕上一丝丝彩线在游走,终于是一个圆满!</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></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>8 <span style="font-family:宋体">准备数据文件</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></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>9 <span style="font-family:宋体">图片显示</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="font-family:宋体">为防止偶然,又传第</span>2<span style="font-family:宋体">幅、第</span>3<span style="font-family:宋体">幅</span> ... ... <span style="font-family:宋体">完全抗击打!</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"> </span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>10 <span style="font-family:宋体">演示效果</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">最后请出我们的主角,功能程序:</span></span></span></p>

<pre>
<code class="language-cpp">int main(void)
{
    u32 err = 0, result;
    u16 uData,a,b;
    u32 xid = 0;
    u32 ImgAdds = 0;
    u32 yid = 0;
    u32 i = 0, j = 0, color = 0;
    u8  X_Location=0, Height;
    u16 Y_Location=0, Width;
    /* Initialize LEDs      */
    HT32F_DVB_LEDInit(HT_LED1);
    HT32F_DVB_LEDInit(HT_LED2);
    HT32F_DVB_LEDInit(HT_LED3);
    LCD_Init();
    LCD_Config();
    LCD_BackColorSet(Black);
    LCD_TextColorSet(Yellow);
    LCD_StringLineDisplay(Line3, "       Holtek       ");
    LCD_StringLineDisplay(Line4, "     HT32 Series    ");
    LCD_StringLineDisplay(Line5, "     LCD Example    ");
        
    UxART_Configuration();
    UxART_TxTest();
        
    for (i = 0; i &lt; 4; i++)
  {
         while (USART_GetFlagStatus(HTCFG_UART_PORT, USART_FLAG_RXDR) == RESET);
       uData = USART_ReceiveData(HTCFG_UART_PORT);
         Tx_Buffer=(u8)uData;
         UxART_TxSend(uData);
    }
        
    a=Tx_Buffer;
    b=Tx_Buffer;
    Width=a &lt;&lt; 8 | b;
    a=Tx_Buffer;
    b=Tx_Buffer;
    Height=a &lt;&lt; 8 | b;
        
    xid = X_Location;
    yid = Y_Location;
        
    LCD_StarterSet(xid, yid);
    for (i = 0; i &lt; Height; i++)
    {
     LCD_WriteRAMPrior();
     for (j = 0; j &lt; Width; j++)
     {
      while(USART_GetFlagStatus(HTCFG_UART_PORT, USART_FLAG_RXDR) == RESET);
      a = USART_ReceiveData(HTCFG_UART_PORT);
                           
      while(USART_GetFlagStatus(HTCFG_UART_PORT, USART_FLAG_RXDR) == RESET);
      b = USART_ReceiveData(HTCFG_UART_PORT);
      color = a &lt;&lt; 8 | b;
                           
      LCD_WriteRAM(color);
   }
   xid++;
   LCD_StarterSet(xid, yid);
 }
 while (1);
}</code></pre>

<p>&nbsp;</p>
页: [1]
查看完整版本: 【ESK32-360测评】+ 串口通信及应用