jinglixixi 发表于 2021-12-15 12:33

【安信可蓝牙开发板PB-02-Kit测评】串行通讯及使用

<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:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">串行通讯是一种主要的数据传送方式,也是在没有显示器件的情况下仅有的调试观察手段。</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="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">1. </span></span></span></span><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">串口通讯的初始化</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="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">要使用串口通信必须对串口进行初始化,其初始化的函数为:</span></span></span></span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">int hal_uart_init(uart_Cfg_t cfg)</span></span></span></p>

<p class="MsoNoSpacing" 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></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; if(m_uartCtx.enable)</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return PPlus_ERR_BUSY;</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; memset(&amp;m_uartCtx, 0, sizeof(m_uartCtx));</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; if(cfg.hw_fwctrl)</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return PPlus_ERR_NOT_SUPPORTED;</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; m_uartCtx.cfg = cfg;</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; uart_hw_config();</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; m_uartCtx.enable = TRUE;</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; hal_pwrmgr_register(MOD_UART, NULL, uart_hw_config);</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp; return PPlus_SUCCESS;</span></span></span></p>

<p class="MsoNoSpacing" 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></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="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">2.</span></span></span></span><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">字节数据发送</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="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">在串口通信中,最主要的函数是字节数据发送,它可以解决常规print函数所不能解决的问题,因为print函数所输出的内容通常是字符型的,而进行外设控制其指令多是二进制的指令,为此只有字节数据发送函数可以解决此类问题。</span></span></span></span></span></span></p>

<p class="MsoNoSpacing" 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></span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">int hal_uart_send_byte(unsigned char data)</span></span></span></p>

<p class="MsoNoSpacing" 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></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; HAL_WAIT_CONDITION_TIMEOUT((AP_UART0-&gt;LSR &amp; LSR_THRE), 10000);</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; AP_UART0-&gt;THR=data;</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; HAL_WAIT_CONDITION_TIMEOUT((AP_UART0-&gt;LSR &amp; LSR_TEMT), 10000);</span></span></span></p>

<p class="MsoNoSpacing" style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="background:white">&nbsp;&nbsp;&nbsp; return PPlus_SUCCESS;</span></span></span></p>

<p class="MsoNoSpacing" 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></span></span></p>

<p class="MsoNoSpacing" 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></span><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">发送字节数据成功。</span></span></span></span></span></span></p>

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

<p class="MsoNoSpacing" 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></span><span style="background:white"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#444444"><span style="letter-spacing:.75pt">发送字节数据的基础,要想用其控制MP3语音播放模块便也不难了,只需将控制指令存放到数组中,然后按序输出即可。</span></span></span></span></span></span></p>

<p><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:宋体">当然,在输出字符串类的信息时还是使用</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;">LOG</span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:宋体">比较方便。</span></span></span></p>

freebsder 发表于 2021-12-16 16:41

<p>谢谢分享!</p>
页: [1]
查看完整版本: 【安信可蓝牙开发板PB-02-Kit测评】串行通讯及使用