[DemoCode下载] M451例程讲解之串口

[复制链接]
 楼主| zhuomuniao110 发表于 2018-8-24 11:32 | 显示全部楼层 |阅读模式
  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: 3 $
  5. * $Date: 15/09/02 10:03a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate how to set GPIO pin mode and use pin data input/output control.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include "stdio.h"
  12. #include "M451Series.h"


  13. #define PLL_CLOCK       72000000


  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/

  19.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  20.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  21.     /* Wait for HIRC clock ready */
  22.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  25.     /* Enable HXT clock (external XTAL 12MHz) */
  26.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  27.     /* Wait for HXT clock ready */
  28.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  29.     /* Set core clock as PLL_CLOCK from PLL */
  30.     CLK_SetCoreClock(PLL_CLOCK);

  31.     /* Enable UART module clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);

  33.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  34.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1));

  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* Init I/O Multi-function                                                                                 */
  37.     /*---------------------------------------------------------------------------------------------------------*/

  38.     /* Set PD multi-function pins for UART0 RXD(PD.6) and TXD(PD.1) */
  39.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD6MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  40.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD6MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  41. }

  42. void UART0_Init()
  43. {
  44.     /*---------------------------------------------------------------------------------------------------------*/
  45.     /* Init UART                                                                                               */
  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Reset UART module */
  48.     SYS_ResetModule(UART0_RST);

  49.     /* Configure UART0 and set UART0 baud rate */
  50.     UART_Open(UART0, 115200);
  51. }

  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /*  Main Function                                                                                          */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. int32_t main(void)
  56. {

  57.       uint8_t a[]={'a','b','c','\n'};
  58.      
  59.     /* Unlock protected registers */
  60.     SYS_UnlockReg();

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

  63.     /* Lock protected registers */
  64.     SYS_LockReg();

  65.     /* Init UART0 for printf */
  66.     UART0_Init();
  67.         
  68.       UART_Write(UART0,a,5);
  69.         
  70.         UART_READ(UART0);
  71.    
  72.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  73.     printf("UART test\n\r");

  74.     while(1);

  75. }


zhuotuzi 发表于 2018-8-24 14:08 | 显示全部楼层
666,我要好好研究一下了。
稳稳の幸福 发表于 2018-8-25 20:23 | 显示全部楼层
这种需要配合串口助手学习。
643757107 发表于 2018-8-26 21:10 | 显示全部楼层
串口映射也要学
734774645 发表于 2018-8-27 19:56 | 显示全部楼层
串口的调用很有意思。
yiyigirl2014 发表于 2018-8-28 10:05 | 显示全部楼层
受教了。。。。。。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

232

主题

3495

帖子

11

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