[DemoCode下载] N76E003的串口使用

[复制链接]
2744|8
 楼主| xixi2017 发表于 2018-7-29 15:55 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 UART-0 Mode1 demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"

  20. /******************************************************************************
  21. * FUNCTION_PURPOSE: Serial interrupt, echo received data.
  22. * FUNCTION_INPUTS : P0.7(RXD) serial input
  23. * FUNCTION_OUTPUTS: P0.6(TXD) serial output
  24. * Following setting in Common.c
  25. ******************************************************************************/
  26. #if 0
  27. //void InitialUART0_Timer1(UINT32 u32Baudrate)    //T1M = 1, SMOD = 1
  28. //{
  29. //                P06_Quasi_Mode;               
  30. //                P07_Quasi_Mode;
  31. //       
  32. //    SCON = 0x52;     //UART0 Mode1,REN=1,TI=1
  33. //    TMOD |= 0x20;    //Timer1 Mode1
  34. //   
  35. //    set_SMOD;        //UART0 Double Rate Enable
  36. //    set_T1M;
  37. //    clr_BRCK;        //Serial port 0 baud rate clock source = Timer1

  38. //
  39. //#ifdef FOSC_160000
  40. //    TH1 = 256 - (1000000/u32Baudrate+1);               /*16 MHz */
  41. //#endif           
  42. //    set_TR1;
  43. //}
  44. ////---------------------------------------------------------------
  45. //void InitialUART0_Timer3(UINT32 u32Baudrate) //use timer3 as Baudrate generator
  46. //{
  47. //                P06_Quasi_Mode;
  48. //                P07_Quasi_Mode;       
  49. //       
  50. //    SCON = 0x52;     //UART0 Mode1,REN=1,TI=1
  51. //    set_SMOD;        //UART0 Double Rate Enable
  52. //    T3CON &= 0xF8;   //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)
  53. //    set_BRCK;        //UART0 baud rate clock source = Timer3

  54. //#ifdef FOSC_160000
  55. //        RH3    = HIBYTE(65536 - (1000000/u32Baudrate)-1);                  /*16 MHz */
  56. //  RL3    = LOBYTE(65536 - (1000000/u32Baudrate)-1);                        /*16 MHz */
  57. //#endif
  58. //    set_TR3;         //Trigger Timer3
  59. //}
  60. #endif

  61. /*******************************************************************************
  62. * FUNCTION_PURPOSE: Main function
  63. ******************************************************************************/
  64. void main (void)
  65. {

  66. #if 0       
  67.     InitialUART0_Timer1(9600);           //UART0 Baudrate initial,T1M=0,SMOD=0
  68.     while(1)
  69.     Send_Data_To_UART0(0x55);
  70. #else
  71.                 InitialUART0_Timer3(115200);
  72.     while(1)
  73.     Send_Data_To_UART0(0x55);
  74. #endif
  75.                        
  76.        
  77.                
  78. }
  79.   


 楼主| xixi2017 发表于 2018-7-29 15:55 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 UART-1 Mode1 demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"

  20. #define BUFFER_SIZE                16
  21. UINT8  UART_BUFFER[BUFFER_SIZE],temp;
  22. UINT16 u16CNT=0,u16CNT1=0;
  23. bit riflag;

  24. /******************************************************************************
  25. * FUNCTION_PURPOSE: Serial port 1 interrupt, echo received data.
  26. * FUNCTION_INPUTS : P0.2(RXD) serial input
  27. * FUNCTION_OUTPUTS: P1.6(TXD) serial output
  28. * Following setting in Common.c
  29. ******************************************************************************/
  30. #if 0
  31. //void InitialUART1_Timer3(UINT32 u32Baudrate) //use timer3 as Baudrate generator
  32. //{
  33. //                P02_Quasi_Mode;                //Setting UART pin as Quasi mode for transmit
  34. //                P16_Quasi_Mode;                //Setting UART pin as Quasi mode for transmit
  35. //       
  36. //          SCON_1 = 0x50;           //UART1 Mode1,REN_1=1,TI_1=1
  37. //    T3CON = 0x08;           //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1
  38. //                clr_BRCK;
  39. //       
  40. //#ifdef FOSC_160000
  41. //                RH3    = HIBYTE(65536 - (1000000/u32Baudrate)-1);                  /*16 MHz */
  42. //                RL3    = LOBYTE(65536 - (1000000/u32Baudrate)-1);                        /*16 MHz */
  43. //#endif
  44. //#ifdef FOSC_166000
  45. //                RH3    = HIBYTE(65536 - (1037500/u32Baudrate));                          /*16.6 MHz */
  46. //                RL3    = LOBYTE(65536 - (1037500/u32Baudrate));                                /*16.6 MHz */
  47. //#endif
  48. //    set_TR3;         //Trigger Timer3
  49. //}
  50. #endif

  51. void SerialPort1_ISR(void) interrupt 15
  52. {
  53.     if (RI_1==1)
  54.     {                                       /* if reception occur */
  55.         clr_RI_1;                             /* clear reception flag for next reception */
  56.         UART_BUFFER[u16CNT] = SBUF_1;
  57.         u16CNT ++;
  58.                                 riflag =1;
  59.     }
  60.     if(TI_1==1)
  61.     {
  62.         clr_TI_1;                             /* if emission occur */
  63.     }
  64. }


  65. /****************************************************************************************************************
  66. * FUNCTION_PURPOSE: Main function
  67.   
  68. !!! N76E003 UART1 pin also occupied by debug pin,
  69. please remove Nu-link or not in debug mode to test UART1 function.

  70. External UART1 connect also disturb debug download

  71. ***************************************************************************************************************/
  72. void main (void)
  73. {
  74.         P12_PushPull_Mode;                // For I/O toggle display
  75.        
  76. #if 0       
  77. //for Simple use UART1 transmit out
  78.                 InitialUART1_Timer3(115200);               
  79.     while(1)
  80.     Send_Data_To_UART1(0x55);
  81.                
  82. #else                       
  83. // For interrupt setting check receive
  84.                 InitialUART1_Timer3(115200);
  85.                 set_ES_1;                                        //For interrupt enable
  86.                 set_EA;
  87.                
  88.                 while(1)
  89.                 {
  90.                         if (riflag)
  91.                         {
  92.                                         P12 = ~ P12;                        //Receive each byte P12 toggle, never work under debug mode
  93.                                         riflag = 0;
  94.                         }
  95.                 }

  96. #endif
  97.                        
  98.        
  99.                
  100. }
  101.   
xinxianshi 发表于 2018-7-29 18:56 | 显示全部楼层
这个芯片是2个串口还是3个,貌似都挺好用。
21mengnan 发表于 2018-7-29 19:27 | 显示全部楼层
好像只有0和1串口。
mintspring 发表于 2018-7-29 20:03 | 显示全部楼层
默认的code高亮不是很专业。
dongnanxibei 发表于 2019-4-9 23:14 | 显示全部楼层
串口有个地方需要修改的吗
wanduzi 发表于 2019-4-10 23:32 | 显示全部楼层
需要修改的那个好像是printf。
天灵灵地灵灵 发表于 2019-4-13 22:43 | 显示全部楼层
串口:printf发送,接收。中断。一般这三种用法。
yiy 发表于 2019-4-14 21:42 | 显示全部楼层
串口的手册要仔细研究,51串口不难。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

145

主题

2034

帖子

2

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