【安信可蓝牙开发板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:"微软雅黑","sans-serif""><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:"微软雅黑","sans-serif""><span style="color:#444444"><span style="letter-spacing:.75pt">1. </span></span></span></span><span style="background:white"><span style="font-family:"微软雅黑","sans-serif""><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:"微软雅黑","sans-serif""><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"> 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"> 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"> memset(&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"> 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"> 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"> 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"> 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"> 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"> 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"> 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:"微软雅黑","sans-serif""><span style="color:#444444"><span style="letter-spacing:.75pt">2.</span></span></span></span><span style="background:white"><span style="font-family:"微软雅黑","sans-serif""><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:"微软雅黑","sans-serif""><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"> HAL_WAIT_CONDITION_TIMEOUT((AP_UART0->LSR & 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"> AP_UART0->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"> HAL_WAIT_CONDITION_TIMEOUT((AP_UART0->LSR & 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"> 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:"微软雅黑","sans-serif""><span style="color:#444444"><span style="letter-spacing:.75pt">发送字节数据成功。</span></span></span></span></span></span></p>
<p class="MsoNoSpacing" style="text-align:justify"> </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:"微软雅黑","sans-serif""><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:"Calibri","sans-serif"">LOG</span></span></span><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:宋体">比较方便。</span></span></span></p>
<p>谢谢分享!</p>
页:
[1]