[DemoCode下载] 串口自动波特率的使用方法

[复制链接]
968|7
 楼主| mintspring 发表于 2023-11-15 22:31 | 显示全部楼层 |阅读模式
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to use auto baud rate detection function.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2017 Nuvoton Technology Corp. All rights reserved.
  8. *
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NuMicro.h"

  12. /*---------------------------------------------------------------------------------------------------------*/
  13. /* Global variables                                                                                        */
  14. /*---------------------------------------------------------------------------------------------------------*/

  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Define functions prototype                                                                              */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. void SYS_Init(void);
  19. void UART0_Init(void);
  20. void AutoBaudRate_Test(void);
  21. void AutoBaudRate_TxTest(void);
  22. void AutoBaudRate_RxTest(void);
  23. uint32_t GetUartBaudrate(UART_T *uart);

  24. void SYS_Init(void)
  25. {
  26.     /*---------------------------------------------------------------------------------------------------------*/
  27.     /* Init System Clock                                                                                       */
  28.     /*---------------------------------------------------------------------------------------------------------*/
  29.     /* Unlock protected registers */
  30.     SYS_UnlockReg();

  31.     /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
  32.     PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

  33.     /* Enable HIRC clock (Internal RC 48MHz) */
  34.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  35.     /* Wait for HIRC clock ready */
  36.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  37.     /* Select HCLK clock source as HIRC and HCLK source divider as 1 */
  38.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  39.     /* Set both PCLK0 and PCLK1 as HCLK */
  40.     CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV1 | CLK_PCLKDIV_APB1DIV_DIV1;

  41.     /* Select IP clock source */
  42.     /* Select UART0 clock source is HIRC and UART module clock divider as 1 */
  43.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));


  44.     /* Enable UART0 peripheral clock */
  45.     CLK_EnableModuleClock(UART0_MODULE);


  46.     /* Update System Core Clock */
  47.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  48.     SystemCoreClockUpdate();

  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     /* Init I/O Multi-function                                                                                 */
  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  53.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk))    |       \
  54.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  55.     /* Lock protected registers */
  56.     SYS_LockReg();

  57. }
  58. /*---------------------------------------------------------------------------------------------------------*/
  59. /* Init UART0                                                                                              */
  60. /*---------------------------------------------------------------------------------------------------------*/
  61. void UART0_Init(void)
  62. {
  63.     /* Reset UART0 */
  64.     SYS_ResetModule(UART0_RST);

  65.     /* Configure UART0 and set UART0 baud rate */
  66.     UART_Open(UART0, 115200);
  67. }

  68. /*---------------------------------------------------------------------------------------------------------*/
  69. /* MAIN function                                                                                           */
  70. /*---------------------------------------------------------------------------------------------------------*/

  71. int32_t main(void)
  72. {
  73.     /* Init System, IP clock and multi-function I/O */
  74.     SYS_Init();
  75.     /* Init UART0 for printf */
  76.     UART0_Init();

  77.     /*---------------------------------------------------------------------------------------------------------*/
  78.     /* SAMPLE CODE                                                                                             */
  79.     /*---------------------------------------------------------------------------------------------------------*/


  80.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  81.     printf("\nUART Sample Program\n");

  82.     /* UART auto baud rate sample function */
  83.     AutoBaudRate_Test();

  84.     printf("\nUART Sample Program End\n");

  85.     while(1);

  86. }

  87. /*---------------------------------------------------------------------------------------------------------*/
  88. /*  Auto Baud Rate Function Test                                                            */
  89. /*---------------------------------------------------------------------------------------------------------*/
  90. void AutoBaudRate_Test(void)
  91. {
  92.     uint32_t u32Item;

  93.     printf("\n");
  94.     printf("+-----------------------------------------------------------+\n");
  95.     printf("|     Pin Configure                                         |\n");
  96.     printf("+-----------------------------------------------------------+\n");
  97.     printf("|  ______                                            _____  |\n");
  98.     printf("| |      |                                          |     | |\n");
  99.     printf("| |Master|--UART0_TXD(PB.13) <==> UART0_RXD(PB.12)--|Slave| |\n");
  100.     printf("| |      |                                          |     | |\n");
  101.     printf("| |______|                                          |_____| |\n");
  102.     printf("|                                                           |\n");
  103.     printf("+-----------------------------------------------------------+\n");

  104.     printf("\n");
  105.     printf("+-----------------------------------------------------------+\n");
  106.     printf("|     Auto Baud Rate Function Test                          |\n");
  107.     printf("+-----------------------------------------------------------+\n");
  108.     printf("|  Description :                                            |\n");
  109.     printf("|    The sample code needs two boards. One is Master and    |\n");
  110.     printf("|    the other is slave.  Master will send input pattern    |\n");
  111.     printf("|    0x1 with different baud rate. It can check if Slave    |\n");
  112.     printf("|    calculates correct baud rate.                          |\n");
  113.     printf("+-----------------------------------------------------------+\n");
  114.     printf("|  Please select Master or Slave test                       |\n");
  115.     printf("|  [0] Master    [1] Slave                                  |\n");
  116.     printf("+-----------------------------------------------------------+\n");
  117.     u32Item = getchar();

  118.     if(u32Item == '0')
  119.         AutoBaudRate_TxTest();
  120.     else
  121.         AutoBaudRate_RxTest();

  122. }
  123. /*---------------------------------------------------------------------------------------------------------*/
  124. /*  Auto Baud Rate Function Tx Test                                                                        */
  125. /*---------------------------------------------------------------------------------------------------------*/
  126. void AutoBaudRate_TxTest(void)
  127. {
  128.     uint32_t u32Item;

  129.     do
  130.     {

  131.         printf("\n");
  132.         printf("+-----------------------------------------------------------+\n");
  133.         printf("|     Auto Baud Rate Function Test (Master)                 |\n");
  134.         printf("+-----------------------------------------------------------+\n");
  135.         printf("| [1] baud rate 38400 bps                                   |\n");
  136.         printf("| [2] baud rate 57600 bps                                   |\n");
  137.         printf("| [3] baud rate 115200 bps                                  |\n");
  138.         printf("|                                                           |\n");
  139.         printf("| Select baud rate and master will send 0x1 to slave ...    |\n");
  140.         printf("+-----------------------------------------------------------+\n");
  141.         printf("| Quit                                              - [ESC] |\n");
  142.         printf("+-----------------------------------------------------------+\n\n");
  143.         u32Item = getchar();
  144.         printf("%c\n", u32Item);

  145.         /* Set different baud rate */
  146.         switch(u32Item)
  147.         {
  148.         case '1':
  149.             UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 38400);
  150.             break;
  151.         case '2':
  152.             UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 57600);
  153.             break;
  154.         default:
  155.             UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);
  156.             break;
  157.         }

  158.         /* Send input pattern 0x1 for auto baud rate detection bit length is 1-bit */
  159.         UART_WRITE(UART0, 0x1);

  160.     }
  161.     while(u32Item != 27);

  162. }

  163. /*---------------------------------------------------------------------------------------------------------*/
  164. /*  Get UART Baud Rate Function                                                                            */
  165. /*---------------------------------------------------------------------------------------------------------*/
  166. uint32_t GetUartBaudrate(UART_T *uart)
  167. {
  168.     uint8_t u8UartClkSrcSel=0, u8UartClkDivNum=0;
  169.     uint32_t u32ClkTbl[] = {0ul, 0ul, 0ul, __HIRC, 0ul, __LIRC};
  170.     uint32_t u32Baud_Div;

  171.     /* Get UART clock source selection and UART clock divider number */
  172.     switch((uint32_t)uart)
  173.     {
  174.     case UART0_BASE:
  175.         u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART0SEL_Msk) >> CLK_CLKSEL1_UART0SEL_Pos;
  176.         u8UartClkDivNum = (CLK->CLKDIV0 & CLK_CLKDIV0_UART0DIV_Msk) >> CLK_CLKDIV0_UART0DIV_Pos;
  177.         break;
  178.     }

  179.     /* Get PCLK clock frequency if UART clock source selection is PCLK */
  180.     if (u8UartClkSrcSel == 4ul)
  181.     {
  182.         if (uart == (UART_T *)UART0)
  183.             u32ClkTbl[u8UartClkSrcSel] =  CLK_GetPCLK0Freq();
  184.     }

  185.     /* Get UART baud rate divider */
  186.     u32Baud_Div = (uart->BAUD & UART_BAUD_BRD_Msk) >> UART_BAUD_BRD_Pos;

  187.     /* Calculate UART baud rate if baud rate is set in MODE 0 */
  188.     if ((uart->BAUD & (UART_BAUD_BAUDM1_Msk | UART_BAUD_BAUDM0_Msk)) == UART_BAUD_MODE0)
  189.         return ((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1ul) / (u32Baud_Div + 2ul)) >> 4;

  190.     /* Calculate UART baud rate if baud rate is set in MODE 2 */
  191.     else if ((uart->BAUD & (UART_BAUD_BAUDM1_Msk | UART_BAUD_BAUDM0_Msk)) == UART_BAUD_MODE2)
  192.         return ((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1ul) / (u32Baud_Div + 2ul));

  193.     /* Calculate UART baud rate if baud rate is set in MODE 1 */
  194.     else if ((uart->BAUD & (UART_BAUD_BAUDM1_Msk | UART_BAUD_BAUDM0_Msk)) == UART_BAUD_BAUDM1_Msk)
  195.         return ((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1ul) / (u32Baud_Div + 2ul)) / (((uart->BAUD & UART_BAUD_EDIVM1_Msk) >> UART_BAUD_EDIVM1_Pos) + 1ul);

  196.     /* Unsupported baud rate setting */
  197.     else
  198.         return 0;
  199. }
  200. /*---------------------------------------------------------------------------------------------------------*/
  201. /*  Auto Baud Rate Function Rx Test                                                                        */
  202. /*---------------------------------------------------------------------------------------------------------*/
  203. void AutoBaudRate_RxTest(void)
  204. {
  205.     /* Enable auto baud rate detect function */
  206.     UART0->ALTCTL |= UART_ALTCTL_ABRDEN_Msk;

  207.     printf("\nreceiving input pattern... ");

  208.     /* Wait until auto baud rate detect finished or time-out */
  209.     while((UART0->ALTCTL & UART_ALTCTL_ABRIF_Msk) == 0);

  210.     if(UART0->FIFOSTS & UART_FIFOSTS_ABRDIF_Msk)
  211.     {
  212.         /* Clear auto baud rate detect finished flag */
  213.         UART0->FIFOSTS = UART_FIFOSTS_ABRDIF_Msk;
  214.         printf("Baud rate is %dbps.\n", GetUartBaudrate(UART0));
  215.     }
  216.     else if(UART0->FIFOSTS & UART_FIFOSTS_ABRDTOIF_Msk)
  217.     {
  218.         /* Clear auto baud rate detect time-out flag */
  219.         UART0->FIFOSTS = UART_FIFOSTS_ABRDTOIF_Msk;
  220.         printf("Time-out!\n");
  221.     }

  222. }




  223. /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/


tpgf 发表于 2024-1-6 15:10 | 显示全部楼层
首次通讯的话 应该是固定的波特率吧
nawu 发表于 2024-1-6 15:51 | 显示全部楼层
这个还是通过通讯进行更改  不能算是全自动
zljiu 发表于 2024-1-6 16:33 | 显示全部楼层
其实其他的通讯方式也应该可以这样做是吧
tfqi 发表于 2024-1-6 22:01 | 显示全部楼层
配置之后多长时间才会生效呢
gwsan 发表于 2024-1-6 22:33 | 显示全部楼层
相应的其他种类的数据接口也可以进行通讯速率自动调整
aoyi 发表于 2024-1-6 23:11 | 显示全部楼层
这个更改之后需要等待多长时间才会生效呢
AloneKaven 发表于 2024-1-10 08:53 来自手机 | 显示全部楼层
这个还要通信啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

303

主题

4972

帖子

24

粉丝
快速回复 在线客服 返回列表 返回顶部