jinglixixi 发表于 2022-2-17 19:48

【国民技术N32G457评测】基于串口通讯的MP3语音模块及串口屏控制

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">前面介绍过以串口驱动ML组件,其实借助开发板所提供的多个串口可大大增强开发板的功能,如用串口控制MP3语音模块,以串口控制串口屏实现界面显示等,提供串口与模块模块的配合简直能够使其具有多媒体的部分功能。</span></span></span></span></span></p>

<p align="left" style="text-align:left">1.MP3<span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">语音模块控制</span></span></span></span></span></span></p>

<p align="left" style="text-align:left">MP3<span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">语音模块是一种通过串口接口控制其部分指定音乐、调节音量及控制播放启停的作用。</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-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">为便于控制,可将相应的指令存放到数组中并通过修改相应位置的参数来起作用。</span></span></span></span></span></p>

<p><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">以播放指定音乐和调节音量为例,其指令数组的定义为:</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">uint8_t cmd3 = {0X7E, 0xFF, 0x06, 0X03, 0x00, 0x00, 0x01, 0xFE, 0xF7, 0XEF};&nbsp; // play index</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">uint8_t cmd6 = {0X7E, 0xFF, 0x06, 0X06, 0x00, 0x00, 0x10, 0xFE, 0xE5, 0XEF};&nbsp; // set&nbsp; Volume</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">播放指定音乐的函数为:</span></span></span></span></span></p>

<pre>
<code class="language-cpp">void playn(uint16_t index) 
{
      int i;
      unsigned char checksum = 0;
      cmd3 = (unsigned char)(index &gt;&gt; 8);
      cmd3 = (unsigned char)(index);
      for (i=2; i&lt;8; i++)
      {
            checksum += cmd3;
      }
      cmd3 = (unsigned char)~checksum;
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">发送控制指令的函数为:</span></span></span></span></span></p>

<pre>
<code class="language-cpp">void Uart_Send(void)
{
  uint8_t i;
  for(i=0;i&lt;10;i++)
  {
      USART_SendData(USARTx,cmd3);
      while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
  }
}</code></pre>

<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)
{
    uint16_t n;
    RCC_Configuration();
    GPIO_Configuration();
    USART_InitStructure.BaudRate           = 9600;
    USART_InitStructure.WordLength         = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode              = USART_MODE_RX | USART_MODE_TX;
    USART_Init(USARTx, &amp;USART_InitStructure);
    USART_Enable(USARTx, ENABLE);
    //setVolume(18);
    //Uart_Send();
    keyInit(GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
    SysTick_Delay_Ms(100);
    N=1;
    while (1)
    {
           playn(n);
           Uart_Send();
           while(GPIO_ReadInputDataBit(GPIOA, GPIO_PIN_4)==1);
           if(n&lt;20) n++;
           else
           n=1;
          SysTick_Delay_Ms(200);
    }
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">经程序的编译和下载,每按下一次</span>K1<span style="font-family:宋体">键即可切换一次播放的音乐,其指令的发送见图</span>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">MP3<span style="font-family:宋体">语音模块的工作状态如图</span>2<span style="font-family:宋体">和图</span>3<span style="font-family:宋体">所示。</span></span></span></p>

<p align="left" style="text-align:left">图1 控制指令的测试</p>

<p align="left" style="text-align:left">图2模块处于停止状态</p>

<p align="left" style="text-align:left">图3 模块处于播放状态</p>

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

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span lang="EN-US" style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">2.</span></span></span><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">串口屏的控制</span></span></span></span></span></span></p>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">串口屏是一种通过串口通信来更新显示内容和接受触摸操作指令的显示器件,它与开发板的连接形式见图4所示。</span></span></span></span></span></span></p>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">值得指出的是,似乎开发板的电源功率相对较小,并不能直接供串口显示,这里是借用AB32VG1开发板的电源来为串口屏供电。</span></span></span></span></span></span></p>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">图</span>4 <span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">串口屏连接</span></span></span></span></span></span></p>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">与MP3语音模块的控制相同,这里也是将指令存放于数组中,并通过指定位置的数据修改来实现显示内容的更新。</span></span></span></span></span></span></p>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">以更新变量t8的显示内容为例,其指令为:t8.txt=&quot;123.6&quot;,并以3个0XFF为指令结束标志。</span></span></span></span></span></span></p>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">为此,其指令数组为:</span></span></span></span></span></span></p>

<p style="text-align:justify">uint8_t cmd_t&nbsp; = {0X74, 0x38, 0x2E, 0X74, 0x78, 0x74, 0x3D, 0x22, 0x31, 0X32, 0X33,0x2E, 0x36, 0x22, 0xFF, 0XFF, 0XFF};</p>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">相应的指令发送函数为:</span></span></span></span></span></span></p>

<pre>
<code class="language-cpp">void Uart_Send(void)
{
 uint8_t i;
 for(i=0;i&lt;17;i++)
 {
       USART_SendData(USARTx,cmd_t);
       while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET);
 }
}</code></pre>

<p align="left" style="text-align:left"><span style="font-size:10.5pt"><span style="background:white"><span style="font-family:Calibri,sans-serif"><span style="font-size:9.5pt"><span style="font-family:&quot;微软雅黑&quot;,&quot;sans-serif&quot;"><span style="color:#626262">实现串口屏显示内容更新的主程序则为:</span></span></span></span></span></span></p>

<pre>
<code class="language-cpp">int main(void)
{
    uint16_t n;
    RCC_Configuration();
    GPIO_Configuration();
    USART_InitStructure.BaudRate          = 9600;
    USART_InitStructure.WordLength        = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode              = USART_MODE_RX | USART_MODE_TX;
    USART_Init(USARTx, &amp;USART_InitStructure);
    USART_Enable(USARTx, ENABLE);
    Uart_Send();
    while (1) ;
}</code></pre>

<p>&nbsp;</p>
页: [1]
查看完整版本: 【国民技术N32G457评测】基于串口通讯的MP3语音模块及串口屏控制