[技术问答] mini58 MCU怎么实现串口输出数据

[复制链接]
 楼主| merlinihuang 发表于 2017-3-28 15:12 | 显示全部楼层 |阅读模式
mini58 MCU怎么实现串口输出数据,方便调试。硬件软件都该怎么实现!我是真一点都不懂!!!!!!多谢了!
yiyigirl2014 发表于 2017-3-28 17:29 | 显示全部楼层
你没有下载官方的BSP吗,里面有个文件夹都是例子,有串口的。
huangcunxiake 发表于 2017-3-28 22:35 | 显示全部楼层
应该和mini51用的例程是一样的吧,我看官方没有在mini58下弄这个
huangcunxiake 发表于 2017-3-28 22:36 | 显示全部楼层
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
  4. * $Revision: 7 $
  5. * $Date: 15/10/06 1:27p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Transmit and receive data from PC terminal through RS232 interface.
  7. *
  8. * @note
  9. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "Mini51Series.h"

  14. #include "uart.h"


  15. #define RXBUFSIZE 1024

  16. /*---------------------------------------------------------------------------------------------------------*/
  17. /* Global variables                                                                                        */
  18. /*---------------------------------------------------------------------------------------------------------*/
  19. uint8_t g_u8SendData[12] = {0};
  20. uint8_t g_u8RecData[RXBUFSIZE]  = {0};

  21. volatile uint32_t g_u32comRbytes = 0;
  22. volatile uint32_t g_u32comRhead  = 0;
  23. volatile uint32_t g_u32comRtail  = 0;
  24. volatile int32_t g_bWait         = TRUE;
  25. volatile int32_t g_i32pointer = 0;

  26. /*---------------------------------------------------------------------------------------------------------*/
  27. /* Define functions prototype                                                                              */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. int32_t main(void);
  30. void UART_TEST_HANDLE(void);
  31. void UART_FunctionTest(void);


  32. void SYS_Init(void)
  33. {
  34.     /*---------------------------------------------------------------------------------------------------------*/
  35.     /* Init System Clock                                                                                       */
  36.     /*---------------------------------------------------------------------------------------------------------*/
  37.     /* Unlock protected registers */
  38.     SYS_UnlockReg();

  39.     /* Set P5 multi-function pins for XTAL1 and XTAL2 */
  40.     SYS->P5_MFP &= ~(SYS_MFP_P50_Msk | SYS_MFP_P51_Msk);
  41.     SYS->P5_MFP |= (SYS_MFP_P50_XTAL1 | SYS_MFP_P51_XTAL2);

  42.     /* Enable External XTAL (4~24 MHz) */
  43.     CLK->PWRCON &= ~CLK_PWRCON_XTLCLK_EN_Msk;
  44.     CLK->PWRCON |= (0x1 << CLK_PWRCON_XTLCLK_EN_Pos); // XTAL12M (HXT) Enabled

  45.     /* Waiting for 12MHz clock ready */
  46.     CLK_WaitClockReady( CLK_CLKSTATUS_XTL_STB_Msk);

  47.     /* Switch HCLK clock source to XTAL */
  48.     CLK->CLKSEL0 &= ~CLK_CLKSEL0_HCLK_S_Msk;
  49.     CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_XTAL;

  50.     /* Enable IP clock */
  51.     CLK->APBCLK |= CLK_APBCLK_UART_EN_Msk; // UART Clock Enable

  52.     /* Select IP clock source */
  53.     CLK->CLKSEL1 &= ~CLK_CLKSEL1_UART_S_Msk;
  54.     CLK->CLKSEL1 |= (0x0 << CLK_CLKSEL1_UART_S_Pos);// Clock source from external 12 MHz or 32 KHz crystal clock

  55.     /* Update System Core Clock */
  56.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  57.     SystemCoreClockUpdate();

  58.     /*---------------------------------------------------------------------------------------------------------*/
  59.     /* Init I/O Multi-function                                                                                 */
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Set P1 multi-function pins for UART1 RXD and TXD  */
  62.     SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  63.     SYS->P1_MFP |= (SYS_MFP_P12_RXD | SYS_MFP_P13_TXD);

  64.     /* Set P0 multi-function pins for UART1 RTS and CTS */
  65.     SYS->P0_MFP &= ~(SYS_MFP_P00_Msk | SYS_MFP_P01_Msk);
  66.     SYS->P0_MFP |= (SYS_MFP_P00_CTS | SYS_MFP_P01_RTS);

  67.     /* Lock protected registers */
  68.     SYS_LockReg();

  69. }

  70. void UART_Init()
  71. {
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Init UART                                                                                               */
  74.     /*---------------------------------------------------------------------------------------------------------*/
  75.     UART_Open(UART, 115200);
  76. }

  77. /*---------------------------------------------------------------------------------------------------------*/
  78. /* UART Test Sample                                                                                        */
  79. /* Test Item                                                                                               */
  80. /* It sends the received data to Hyper Terminal.                                                            */
  81. /*---------------------------------------------------------------------------------------------------------*/

  82. /*---------------------------------------------------------------------------------------------------------*/
  83. /* MAIN function                                                                                           */
  84. /*---------------------------------------------------------------------------------------------------------*/

  85. int main(void)
  86. {
  87.     /* Init System, IP clock and multi-function I/O */
  88.     SYS_Init();
  89.     /* Init UART for printf */
  90.     UART_Init();

  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* SAMPLE CODE                                                                                             */
  93.     /*---------------------------------------------------------------------------------------------------------*/

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

  95.     printf("+---------------------+\n");
  96.     printf("| UART function test  |\n");
  97.     printf("+---------------------+\n");

  98.     UART_FunctionTest();
  99. }

  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /* ISR to handle UART Channel 0 interrupt event                                                            */
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. void UART_IRQHandler(void)
  104. {
  105.     UART_TEST_HANDLE();
  106. }

  107. /*---------------------------------------------------------------------------------------------------------*/
  108. /* UART Callback function                                                                                  */
  109. /*---------------------------------------------------------------------------------------------------------*/
  110. void UART_TEST_HANDLE()
  111. {
  112.     uint8_t u8InChar=0xFF;
  113.     uint32_t u32IntSts= UART->ISR;

  114.     if(u32IntSts & UART_ISR_RDA_INT_Msk) {
  115.         printf("\nInput:");

  116.         /* Get all the input characters */
  117.         while(UART_IS_RX_READY(UART)) {
  118.             /* Get the character from UART Buffer */
  119.             u8InChar = UART_READ(UART);           /* Rx trigger level is 1 byte*/

  120.             printf("%c ", u8InChar);

  121.             if(u8InChar == '0') {
  122.                 g_bWait = FALSE;
  123.             }

  124.             /* Check if buffer full */
  125.             if(g_u32comRbytes < RXBUFSIZE) {
  126.                 /* Enqueue the character */
  127.                 g_u8RecData[g_u32comRtail] = u8InChar;
  128.                 g_u32comRtail = (g_u32comRtail == (RXBUFSIZE-1)) ? 0 : (g_u32comRtail+1);
  129.                 g_u32comRbytes++;
  130.             }
  131.         }
  132.         printf("\nTransmission Test:");
  133.     }

  134.     if(u32IntSts & UART_ISR_THRE_INT_Msk) {
  135.         uint16_t tmp;
  136.         tmp = g_u32comRtail;
  137.         if(g_u32comRhead != tmp) {
  138.             u8InChar = g_u8RecData[g_u32comRhead];
  139.             UART_WRITE(UART,u8InChar);
  140.             g_u32comRhead = (g_u32comRhead == (RXBUFSIZE-1)) ? 0 : (g_u32comRhead+1);
  141.             g_u32comRbytes--;
  142.         }
  143.     }
  144. }


  145. /*---------------------------------------------------------------------------------------------------------*/
  146. /*  UART Function Test                                                                                     */
  147. /*---------------------------------------------------------------------------------------------------------*/
  148. void UART_FunctionTest()
  149. {
  150.     printf("+-----------------------------------------------------------+\n");
  151.     printf("|  UART Function Test                                       |\n");
  152.     printf("+-----------------------------------------------------------+\n");
  153.     printf("|  Description :                                            |\n");
  154.     printf("|    The sample code will print input char on terminal      |\n");
  155.     printf("|    Please enter any to start     (Press '0' to exit)      |\n");
  156.     printf("+-----------------------------------------------------------+\n");

  157.     /*
  158.         Using a RS232 cable to connect UART and PC.
  159.         UART is set to debug port. UART is enable RDA and RLS interrupt.
  160.         When inputting char to terminal screen, RDA interrupt will happen and
  161.         UART will print the received char on screen.
  162.     */

  163.     /* Enable Interrupt and install the call back function */
  164.     UART_ENABLE_INT(UART, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk));
  165.     NVIC_EnableIRQ(UART_IRQn);
  166.     while(g_bWait);

  167.     /* Disable Interrupt */
  168.     UART_DISABLE_INT(UART, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk));
  169.     NVIC_DisableIRQ(UART_IRQn);
  170.     g_bWait =TRUE;
  171.     printf("\nUART Sample Demo End.\n");

  172. }

myxiaonia 发表于 2017-3-29 14:55 | 显示全部楼层
请问哪里有便宜的mini51系列开发板
dongnanxibei 发表于 2017-3-29 18:21 | 显示全部楼层
硬件找个官方的开发板看看,或者下载手册,直接看管脚分布图。
 楼主| merlinihuang 发表于 2017-3-29 19:17 | 显示全部楼层
zhuomuniao110 发表于 2017-3-30 14:01 | 显示全部楼层
开发板要去淘宝搜搜啊,万能的淘宝。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

0

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

1

主题

2

帖子

0

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