51 MCU serial communication uses interrupt mode

Publisher:朝霞暮雨Latest update time:2018-06-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. /*-------------------------------------------------  

  2. Function: Connect the serial port to the computer, download the program, turn on the power  

  3.         Open the serial port debugging assistant and set the baud rate to 2400 with no parity check.  

  4.         The crystal oscillator is 12MHz, and the format used for sending and receiving is the same.  

  5.         Character format, after setting correctly, the acceptance box can display:  

  6.         The UART test, please enter the information in the send area  

  7. ----------------------------------------*/  

  8.   

  9. #include //Include the header file. Generally, no modification is required. The header file contains the definition of special function registers.                          

  10.   

  11. /*------------------------------------------------  

  12.                    Function declaration  

  13. ----------------------------------------*/  

  14. void SendStr(unsigned char *s);  

  15.   

  16. /*------------------------------------------------  

  17.                     Serial port initialization  

  18. ----------------------------------------*/  

  19. void InitUART (void)  

  20. {  

  21.   

  22.     SCON = 0x50; // SCON: Mode 1, 8-bit UART, receive enabled    

  23.     TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reinstall  

  24.     //TH1 = 0xF3; // TH1: reload value 2400    

  25.       

  26.         TH1=0xfd;  

  27.       TL1=0xfd; //11.0592MHZ crystal oscillator 9600 baud rate corresponding to the initial value  

  28.       

  29.     TR1 = 1; // TR1: timer 1 is turned on                           

  30.     EA = 1; // Enable general interrupt  

  31.    // ES = 1; // Enable serial port interrupt  

  32. }                              

  33. /*------------------------------------------------  

  34.                     Main function  

  35. ----------------------------------------*/  

  36. void main (void)  

  37. {  

  38.   

  39. InitUART();  

  40.   

  41. SendStr("The UART test, please enter information in the send area");  

  42.   

  43. ES = 1; //Open serial port interrupt  

  44. while (1)                         

  45.     {  

  46.       

  47.     }  

  48. }  

  49.   

  50. /*------------------------------------------------  

  51.                     Send a byte  

  52. ----------------------------------------*/  

  53. void SendByte(unsigned char dat)  

  54. {  

  55.  SBUF = dat;  

  56.  while(!TI);  

  57.       TI = 0;  

  58. }  

  59. /*------------------------------------------------  

  60.                     Send a string  

  61. ----------------------------------------*/  

  62. void SendStr(unsigned char *s)  

  63. {  

  64.  while(*s!='\0')// \0 indicates the end of the string, by checking whether the string ends  

  65.   {  

  66.   SendByte(*s);  

  67.   s++;  

  68.   }  

  69. }  

  70. /*------------------------------------------------  

  71.                      Serial port interrupt program  

  72. ----------------------------------------*/  

  73. void UART_SER (void) interrupt 4 //Serial interrupt service routine  

  74. {  

  75.     unsigned char Temp; //define temporary variable   

  76.      

  77.    if(RI) //Judge whether a receive interrupt is generated  

  78.      {  

  79.       RI=0; // Flag cleared  

  80.       Temp=SBUF; //Read the value in the buffer  

  81.       P1=Temp; //Output the value to port P1 for observation  

  82.       SBUF=Temp; //Send the received value back to the computer  

  83.      }  

  84.    if(TI) //If it is a send flag, clear it  

  85.      TI=0;  

  86. }


Reference address:51 MCU serial communication uses interrupt mode

Previous article:51 MCU interrupt implementation process
Next article:Keil C51 redirect printf to serial port

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号