[技术问答] 求助大侠M453的UART初始化

[复制链接]
2215|6
 楼主| Jack67 发表于 2017-1-22 11:46 | 显示全部楼层 |阅读模式
求助大侠帮忙看看初始化UART0串口有什么错误的地方,就收进不了终端。   
/* Set PD multi-function pins for UART0 RXD and TXD */
    SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
    SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
       
    /* Reset UART module */
    SYS_ResetModule(UART0_RST);

        UART_EnableInt(UART0, (UART_INTSTS_RDAINT_Msk | UART_INTSTS_RLSINT_Msk));
        UART0->INTEN |= UART_INTEN_RDAIEN_Msk;
    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
598330983 发表于 2017-1-22 16:56 | 显示全部楼层
建议楼主下载这个系列的库函数和例程,官方网站有。非常给力。
捉虫天师 发表于 2017-1-22 19:02 | 显示全部楼层
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 9 $
  5. * $Date: 15/09/02 10:04a $
  6. * @brief
  7. *           Show how to use auto baud rate detection function.
  8. *           This sample code needs to work with UART_AutoBaudRate_Slave.
  9. * @note
  10. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include "M451Series.h"

  15. #define PLLCTL_SETTING  CLK_PLLCTL_72MHz_HXT
  16. #define PLL_CLOCK       72000000

  17. #define RXBUFSIZE 1024


  18. /*---------------------------------------------------------------------------------------------------------*/
  19. /* Define functions prototype                                                                              */
  20. /*---------------------------------------------------------------------------------------------------------*/
  21. extern char GetChar(void);
  22. int32_t main(void);
  23. void AutoBaudRate_TestItem(void);
  24. void AutoBaudRate_TxTest(void);


  25. void SYS_Init(void)
  26. {

  27.     /*---------------------------------------------------------------------------------------------------------*/
  28.     /* Init System Clock                                                                                       */
  29.     /*---------------------------------------------------------------------------------------------------------*/

  30.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  31.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  32.     /* Wait for HIRC clock ready */
  33.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  36.     /* Enable HXT clock (external XTAL 12MHz) */
  37.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  38.     /* Wait for HXT clock ready */
  39.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  40.     /* Set core clock as PLL_CLOCK from PLL */
  41.     CLK_SetCoreClock(PLL_CLOCK);

  42.     /* Enable UART module clock */
  43.     CLK_EnableModuleClock(UART0_MODULE);
  44.     CLK_EnableModuleClock(UART1_MODULE);

  45.     /* Select UART module clock source as HIRC and UART module clock divider as 1 */
  46.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1));
  47.     CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1));

  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Init I/O Multi-function                                                                                 */
  50.     /*---------------------------------------------------------------------------------------------------------*/

  51.     /* Set PD multi-function pins for UART0 RXD(PD.0) and TXD(PD.1) */
  52.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  53.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  54.     /* Set PB multi-function pins for UART1 RXD(PB.2) and TXD(PB.3) */
  55.     SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk);
  56.     SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB2MFP_UART1_RXD | SYS_GPB_MFPL_PB3MFP_UART1_TXD);

  57. }

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

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

  68. void UART1_Init()
  69. {
  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Init UART                                                                                               */
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Reset UART1 module */
  74.     SYS_ResetModule(UART1_RST);

  75.     /* Configure UART1 and set UART1 Baudrate */
  76.     UART_Open(UART1, 115200);
  77. }

  78. /*---------------------------------------------------------------------------------------------------------*/
  79. /* MAIN function                                                                                           */
  80. /*---------------------------------------------------------------------------------------------------------*/

  81. int main(void)
  82. {

  83.     /* Unlock protected registers */
  84.     SYS_UnlockReg();

  85.     /* Init System, peripheral clock and multi-function I/O */
  86.     SYS_Init();

  87.     /* Lock protected registers */
  88.     SYS_LockReg();

  89.     /* Init UART0 for printf */
  90.     UART0_Init();

  91.     /* Init UART1 for testing */
  92.     UART1_Init();

  93.     /*---------------------------------------------------------------------------------------------------------*/
  94.     /* SAMPLE CODE                                                                                             */
  95.     /*---------------------------------------------------------------------------------------------------------*/

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

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

  98.     /* UART auto baud rate sample master function */
  99.     AutoBaudRate_TxTest();

  100.     while(1);

  101. }

  102. /*---------------------------------------------------------------------------------------------------------*/
  103. /*  Auto Baud Rate Function Test Menu                                                                      */
  104. /*---------------------------------------------------------------------------------------------------------*/
  105. void AutoBaudRate_TestItem()
  106. {
  107.     printf("\n");
  108.     printf("+-----------------------------------------------------------+\n");
  109.     printf("|     Auto Baud Rate Function Test (Master)                 |\n");
  110.     printf("+-----------------------------------------------------------+\n");
  111.     printf("| [1] baud rate 38400 bps                                   |\n");
  112.     printf("| [2] baud rate 57600 bps                                   |\n");
  113.     printf("| [3] baud rate 115200 bps                                  |\n");
  114.     printf("|                                                           |\n");
  115.     printf("| Select baud rate and master will send 0x1 to slave ...    |\n");
  116.     printf("+-----------------------------------------------------------+\n");
  117.     printf("| Quit                                              - [ESC] |\n");
  118.     printf("+-----------------------------------------------------------+\n\n");
  119. }

  120. /*---------------------------------------------------------------------------------------------------------*/
  121. /*  Auto Baud Rate Function Tx Test                                                                        */
  122. /*---------------------------------------------------------------------------------------------------------*/
  123. void AutoBaudRate_TxTest()
  124. {
  125.     uint32_t u32Item;

  126.     printf("\n");
  127.     printf("+-----------------------------------------------------------+\n");
  128.     printf("|     Pin Configure                                         |\n");
  129.     printf("+-----------------------------------------------------------+\n");
  130.     printf("|  ______                                            _____  |\n");
  131.     printf("| |      |                                          |     | |\n");
  132.     printf("| |Master|--UART1_TXD(PB.3)  <==>  UART1_RXD(PB.2)--|Slave| |\n");
  133.     printf("| |      |                                          |     | |\n");
  134.     printf("| |______|                                          |_____| |\n");
  135.     printf("|                                                           |\n");
  136.     printf("+-----------------------------------------------------------+\n");

  137.     printf("\n");
  138.     printf("+-----------------------------------------------------------+\n");
  139.     printf("|     Auto Baud Rate Function Test                          |\n");
  140.     printf("+-----------------------------------------------------------+\n");
  141.     printf("|  Description :                                            |\n");
  142.     printf("|    The sample code needs two boards. One is Master and    |\n");
  143.     printf("|    the other is slave.  Master will send input pattern    |\n");
  144.     printf("|    0x1 with different baud rate. It can check if Slave    |\n");
  145.     printf("|    calculates correct baud rate.                          |\n");
  146.     printf("+-----------------------------------------------------------+\n");

  147.     do
  148.     {
  149.         AutoBaudRate_TestItem();
  150.         u32Item = getchar();
  151.         printf("%c\n", u32Item);

  152.         /* Set different baud rate */
  153.         switch(u32Item)
  154.         {
  155.             case '1':
  156.                 UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 38400);
  157.                 break;
  158.             case '2':
  159.                 UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 57600);
  160.                 break;
  161.             default:
  162.                 UART1->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);
  163.                 break;
  164.         }

  165.         /* Send input pattern 0x1 for auto baud rate detection bit length is 1-bit */
  166.         UART_WRITE(UART1, 0x1);

  167.     }
  168.     while(u32Item != 27);

  169. }


玛尼玛尼哄 发表于 2017-1-22 19:23 | 显示全部楼层
下载官方例程的,用库函数写的,比较好明白。
天灵灵地灵灵 发表于 2017-1-23 16:32 | 显示全部楼层
春节了,楼主还在忙工作啊?
heisexingqisi 发表于 2017-1-25 15:52 | 显示全部楼层
找个开发板试试看,看看是哪儿的错误。
598330983 发表于 2017-1-25 21:35 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

10

帖子

0

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