[DemoCode下载] ML51的串口1例程修改建议

[复制链接]
 楼主| 643757107 发表于 2019-8-31 22:28 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2019 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Website: http://www.nuvoton.com
  8. //  E-Mail : MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************

  10. #include "ML51.h"

  11. /**
  12. * [url=home.php?mod=space&uid=247401]@brief[/url]       UART1 use as printf.
  13. * @param       None
  14. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  15. * [url=home.php?mod=space&uid=1543424]@Details[/url]     None
  16. */


  17. void main (void)
  18. {

  19. /**
  20. For UART1 P2.3 as TXD1 / P2.3 as RXD1 GPIO setting
  21. * include gpio.c in Common Setting for UART0
  22. */
  23.     MFP_P23_UART1_TXD;                              // UART0 TXD use P0.5
  24.     P23_QUASI_MODE;                                 // set P0.5 and P0.6 as Quasi mode for UART0 trasnfer
  25. /**
  26. For UART1 initial setting
  27. * include uart.c in Common Setting for UART0
  28. */
  29.     UART_Open(24000000,UART1,115200);              // Open UART0 use timer1 as baudrate generate and baud rate = 115200en(24000000,UART0_Timer1,115200);        // Open UART0 use timer1 as baudrate generate and baud rate = 115200
  30.     set_S1CON_TI_1;                                // printf must first set TI_1 =1;
  31.    
  32. /**
  33.   UART1 loop test
  34.   UART1 TXD1 send data received by RXD1 pin. Connect TXD1 pin and RXD1 pin check result.
  35. */
  36.     while(1)
  37.     {
  38.       printf ("\n hello world! " );
  39.       Timer2_Delay(24000000,128,1000,1000);        //Means timer base 24MHz, divider 128, delay time 100 ms (100000us)
  40.     }
  41. }
上面是官方的例子,在启用串口1作为printf的时候,使用了set_S1CON_TI_1; // printf must first set TI_1 =1;
非常难易理解,而在串口0的例子中没这么用,串口0的例子使用的是
ENABLE_UART0_PRINTF
于是查找头文件发现了问题。头文件中是有一条
ENABLE_UART1_PRINTF
的,然后使用宏替换的set_S1CON_TI_1
所以这里写例程序的程序员故意挖坑了。
 楼主| 643757107 发表于 2019-8-31 22:29 | 显示全部楼层
所以这个例子应该是
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2019 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Website: http://www.nuvoton.com
  8. //  E-Mail : MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************

  10. #include "ML51.h"

  11. /**
  12. * @brief       UART1 use as printf.
  13. * @param       None
  14. * @return      None
  15. * @details     None
  16. */


  17. void main (void)
  18. {

  19. /**
  20. For UART1 P2.3 as TXD1 / P2.3 as RXD1 GPIO setting
  21. * include gpio.c in Common Setting for UART0
  22. */
  23.     MFP_P23_UART1_TXD;                              // UART0 TXD use P0.5
  24.     P23_QUASI_MODE;                                 // set P0.5 and P0.6 as Quasi mode for UART0 trasnfer
  25. /**
  26. For UART1 initial setting
  27. * include uart.c in Common Setting for UART0
  28. */
  29.     UART_Open(24000000,UART1,115200);              // Open UART0 use timer1 as baudrate generate and baud rate = 115200en(24000000,UART0_Timer1,115200);        // Open UART0 use timer1 as baudrate generate and baud rate = 115200
  30.     ENABLE_UART1_PRINTF;                                // printf must first set TI_1 =1;
  31.    
  32. /**
  33.   UART1 loop test
  34.   UART1 TXD1 send data received by RXD1 pin. Connect TXD1 pin and RXD1 pin check result.
  35. */
  36.     while(1)
  37.     {
  38.       printf ("\n hello world! " );
  39.       Timer2_Delay(24000000,128,1000,1000);        //Means timer base 24MHz, divider 128, delay time 100 ms (100000us)
  40.     }
  41. }


 楼主| 643757107 发表于 2019-8-31 22:29 | 显示全部楼层
这么一修改就看着容易懂多了,不用再可以去研究具体寄存器怎么配。
 楼主| 643757107 发表于 2019-8-31 22:29 | 显示全部楼层
我认为C语言都应该如此,看起来更优雅。
734774645 发表于 2019-8-31 23:02 | 显示全部楼层
建议很好,我也去看看有没有什么发现。
 楼主| 643757107 发表于 2019-9-4 21:29 | 显示全部楼层
我就觉得吧,例子应该非常容易懂。
huahuagg 发表于 2019-9-5 15:07 | 显示全部楼层
建议不错,希望下次更新能修正。
21mengnan 发表于 2019-9-8 22:15 | 显示全部楼层
优秀啊,多谢分享经验。
598330983 发表于 2019-9-8 22:16 | 显示全部楼层
确实不错,设计库函数时候都想到了,做例子时候竟然故意漏掉了?
antusheng 发表于 2019-9-8 22:16 | 显示全部楼层
这个方法不错,就算是不统一的配置方式,也可以用这个方式统一起来。
wahahaheihei 发表于 2019-9-8 23:59 | 显示全部楼层
统一的配置方法比较好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

222

主题

3931

帖子

11

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

222

主题

3931

帖子

11

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