Test routine for interrupt receiving and sending of single-chip microcomputer C51 serial port (including the implementation of communication protocol)

Source: Internet
Author: User

Test routine for interrupt receiving and sending of single-chip microcomputer C51 serial port (including the implementation of communication protocol)

Author: Yan Jianwei http://www.gjwtech.com

Communication Protocol:1st bytes, MSB is 1, 1st bytes, 2nd bytes, MSB is 0, not the first byte, and so on ......, The last byte is the exclusive or checksum of the last seven digits of the first few bytes.

Test method: you can write the sending box of the serial port debugging assistant to, and select hexadecimal transmission. The receiving box is displayed in hexadecimal notation, if 95 10 20 25 is received every time a message is sent, the test is successful.


 

//This is a single-chip computer.Serial receive (Interrupt) and send routine, can be used to test 51MCU interrupt reception
//In addition, I don't think it is necessary to interrupt the sending because the program overhead is the same as the query sending.
//Programming:Yi Jianwei webmaster@gjwtech.com
//Technical home: http://www.gjwtech.com
//You can discuss this with me.

# Include<Reg51.h>
# Include<String. h>

# DefineInbuf_len 4//Data Length

Unsigned CharInbuf1 [inbuf_len];
Unsigned CharChecksum, count3;
Bit read_flag = 0;

VoidInit_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.0592 MHz
IE | = 0x90;// Enable serial interrupt
Tr1 = 1;// Timer 1 run
// Ti = 1;
}

//Send one character to the serial port
VoidSend_char_com (Unsigned CharCh)
{
Sbuf = CH;
While(Ti = 0 );
Ti = 0;
}

//Send a string to the serial port, strlenLength of this string
VoidSend_string_com (Unsigned Char* STR,Unsigned IntStrlen)
{
Unsigned IntK = 0;
Do
{
Send_char_com (* (STR + k ));
K ++;
}While(K <strlen );
}

//Serial receive interrupt function
VoidSerial () interrupt 4 using 3
{
If(RI)
{
Unsigned CharCh;
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 data received by the serial port reaches inbuf_lenAnd the verification is correct,
//Set the number of places
}
}
}
}

Main ()
{
Init_serialcomm ();//Initialize serial port
While(1)
{
If(Read_flag)//If the number marker is set, the read number is sent from the serial port.
{
Read_flag = 0;//Mark 0
Send_string_com (inbuf1, inbuf_len );
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.