C51 Interrupt Receive Query Send

Publisher:温柔之风Latest update time:2017-11-27 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

C51 Interrupt Receive Query Send

  1. #include   

  2.   

  3. volatile unsigned char rx_data;  

  4. volatile bit          rx_flag = 0;  

  5.   

  6. void dalay_1ms(unsigned short n)  

  7. {  

  8.     unsigned short i,j;  

  9.     for(i=0; i

  10.     {  

  11.         for(j=60000; j>0; j--);  

  12.     }  

  13. }  

  14.   

  15.   

  16. void send_char(unsigned char ch)  

  17. {  

  18.     SBUF = ch;  

  19.     while(!IF);  

  20.     IF = 0;  

  21. }  

  22.   

  23. void send_string(unsigned char *str, unsigned char len)  

  24. {  

  25.     unsigned char i;  

  26.     for(i=0; i

  27.     {  

  28.         send_char(*(str+i));  

  29.     }  

  30. }  

  31.   

  32.   

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

  34. void main (void)  

  35. {  

  36. //    volatile unsigned char i,tmp;  

  37.     unsigned char *str = "Serial test";  

  38.     TMOD = 0x20; //T1 mode 2  

  39.     TH1 = 0xFD;    //Baud:9600bps@11.0592MHz  

  40.     TL1 = 0xFD;  

  41.     TR1 = 1; //Start timer 1  

  42.       

  43.     SCON = 0x50; //Serial port mode 1, 8-n-1, receiving is allowed  

  44.     REN = 1; //Enable serial port reception  

  45.     EA = 1; // Enable general interrupt  

  46.     ES = 1; //Turn on the serial port interrupt switch  

  47.   

  48.     send_string((unsigned char *)str, 11);  

  49.     while(1)  

  50.     {    

  51.         delay_1ms(1);  

  52.         if(rx_flag)  

  53.         {  

  54.             ES = 0; //The serial port interrupt must be turned off first, otherwise the PC will not receive data  

  55.             SBUF = rx_data;  

  56.             while(RI == 0);  

  57.             ES = 1; //Re-enable serial port interrupt  

  58.             RI = 0;  

  59.         }  

  60.     }  

  61. }  

  62. //----------------------------------------------  

  63. void serial(void) interrupt 4  

  64. {  

  65.     rx_data = SBUF;  

  66.     P1 = rx_data;  

  67.     rx_flag = 1;  

  68.     RI = 0;  

  69. }   

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


Keywords:C51 Reference address:C51 Interrupt Receive Query Send

Previous article:C51 queue mode interrupt receive query send
Next article:The essence of commonly used electronic password lock source program

Latest Microcontroller Articles
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号