MCU C51 serial port interrupt reception and transmission test routine (including communication protocol implementation)

Publisher:SereneJourneyLatest update time:2011-05-14 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Communication protocol: The first byte, MSB is 1, which is the first byte mark, the second byte, MSB is 0, which is the non-first byte mark, and so on... The last byte is the XOR checksum of the last 7 bits of the previous bytes.
Test method: You can write 95 10 20 25 in the sending box of the serial port debugging assistant, and select hexadecimal sending, and select hexadecimal display in the receiving box. If 95 10 20 25 is received every time it is sent, it means the test is successful.


//This is a microcontroller C51 serial port receiving (interrupt) and sending routine, which can be used to test the interrupt reception
//and query sending of the 51 microcontroller. In addition, I think it is not necessary to use interrupts for sending, because the program overhead is the same


#include
#include

#define INBUF_LEN 4 //data length

unsigned char inbuf1[INBUF_LEN];
unsigned char checksum,count3;
bit read_flag= 0 ;

void init_serialcomm( void )
{
SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80 ; //SMOD=1;
TH1 = 0xF4 ; //Baud:4800 fosc=11.0592MHz
IE |= 0x90 ; //Enable Serial Interrupt
TR1 = 1 ; // timer 1 run
// TI=1;
}

//Send a character to the serial port
void send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0 );
TI= 0 ;
}

//Send a string to the serial port, strlen is the length of the string void
send_string_com( unsigned char *str, unsigned int strlen)
{
unsigned int k= 0 ;
do
{
send_char_com(*(str + k));
k++;
} while (k < strlen);
} //Serial port receive interrupt function void serial () interrupt 4 using 3 { if (RI) { unsigned char ch; RI = 0 ; ch=SBUF; if (ch> 127 ) { count3= 0 ; inbuf1[count3]=ch; checksum= ch- 128 ; } else { count3++; inbuf1[count3]=ch; checksum ^= ch; if ( (count3==(INBUF_LEN- 1 )) && (!checksum) ) { read_flag= 1 ; //If the serial port receives data up to INBUF_LEN and the verification is correct, //set the data read flag } } } } main() { init_serialcomm(); //Initialize the serial portwhile ( 1 ) { if (read_flag) //If the data read flag is set, send the read number from the serial port { read_flag= 0 ; //Clear the data read flag send_string_com(inbuf1,INBUF_LEN); } } }












































Reference address:MCU C51 serial port interrupt reception and transmission test routine (including communication protocol implementation)

Previous article:Temperature and humidity automatic detection system composed of AT89C51 55
Next article:Design of computer key peripheral circuit based on AT89C51RC

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号