Single-Chip Microcomputer: Implementation of custom Serial Communication Protocol
Use 51 Single-Chip Microcomputer to complete a simple serial communication protocol.
Requirements:
A protocol frame is 8 bytes, with two bytes in the header: 0xFF and 0xFE;
3rd bytes represent the first data, and 4th bytes represent the second data (all positive numbers );
If the value of 5th bytes is 0, it indicates the sum of the two data, and 1 indicates the difference between the two data;
The value of 6th bytes is 0, indicating that the value is displayed in decimal format and 1 indicates that the value is displayed in hexadecimal format;
The last two bytes represent the end of the Protocol, with the values 0xFD and 0xFC respectively.
Display the two transmitted data and their calculation results on the digital tube respectively.
Note: If the difference is negative, the negative number should be displayed.
Idea: Use the serial port to interrupt, first receive 8 bytes to save the array, and then analyze and process the data.
See the code for detailed comments.
/* --------------------------------------------- Name: serial communication content: connect the serial port or usb to the computer, download the program, open the serial port debugging program, set the baud rate to 9600, the 11.0592 MHz non-parity crystal oscillator uses the same format for sending and receiving. For example, if both use the RST format and press reset to restart the program, you can see that the received RST sends any information in the sending area, the same message is returned in the receiving area, indicating that the serial port sending and receiving is correct ---------------------------------- */# include // contains the header file, which generally does not need to be changed. The header file contains the definition of the special function register sbit WEI = P2; sbit DUAN = P2 ^ 6; unsigned char code dofly_DuanMa [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,0 X7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71,0x40}; // display the field code value 0 ~ 9 unsigned char code dofly_WeiMa [] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f}; // the corresponding Digital tubes are lit, that is, the location code void delay (int t) {while (t --);}/* ---------------------------------------------- function declaration handler */void SendStr (unsigned char * s ); /* ---------------------------------------------- serial port initialization ------------------------------------------------ */void InitUART (void) {SCON = 0x50; // SCON: mode 1, 8-bit UART, enable to receive TMOD | = 0x20; // TMOD: timer 1, mode 2, 8-bit re-load TH1 = 0xFD; // TH1: Re-load value 9600 baud rate crystal oscillator 11.0592 MHz TR1 = 1; // TR1: timer 1 open EA = 1; // enable the total interrupt ES = 1; // enable the serial port interrupt} // parameter meaning: I is the number of digital tubes, n is the value to be displayed void Screen (int I, int n) {P0 = 0; DUAN = 1; DUAN = 0; P0 = dofly_WeiMa [I]; WEI = 1; WEI = 0; P0 = dofly_DuanMa [n]; DUAN = 1; DUAN = 0;}/* ---------------------------------------------- main function ------------- --------------------------------- */Int flag = 0; int num1, num2; int res; int judge [8]; int cnt = 0; int is_fd; int hex_mod; void main (void) {num1 = 0; num2 = 0; // res = 0; InitUART (); ES = 1; // enable the serial port to interrupt is_fd = 0; while (1) {if (judge [0] = 0xFF & judge [1] = 0xFE & judge [6] = 0xFD & judge [7] = 0xFC) {if (judge [5] = 0x01) {hex_mod = 16;} else {hex_mod = 10;} if (judge [2]/hex_mod> 0) screen (6, judge [2]/hex_mod); Screen (7, judge [2] % Hex_mod); if (judge [3]/hex_mod> 0) Screen (4, judge [3]/hex_mod); Screen (5, judge [3] % hex_mod ); if (judge [4] = 0x00) {res = judge [2] + judge [3]; is_fd = 0 ;} else {res = judge [2]-judge [3]; if (res <0) {is_fd = 1; res =-res;} else is_fd = 0 ;} if (res/hex_mod> 0) Screen (2, res/hex_mod); Screen (3, res % hex_mod); delay (10); if (is_fd) // determine the negative number {Screen () ;}}}/* ---------------------------------------------------- send a byte ------- --------------------------------------- */Void SendByte (unsigned char dat) {SBUF = dat; while (! TI); TI = 0;}/* producer sends a string ------------------------------------------------ */void SendStr (unsigned char * s) {while (* s! = '\ 0') // \ 0 indicates the end mark of the string. by checking whether the end of the string is {SendByte (* s); s ++ ;}} /* serial port interrupt program ------------------------------------------------ */void UART_SER (void) interrupt 4 // serial interrupt service program {unsigned char Temp; // defines the Temporary Variable if (RI) // identify the receipt interrupt generation {RI = 0; // The flag bit is cleared Temp = SBUF; // read the buffer value judge [cnt ++] = Temp; // The header is not satisfied or if (cnt = 8 | judge [0]! = 0xFF) cnt = 0; SBUF = Temp; // send the received value back to the computer.} if (TI) // if it is a sending flag, cleared TI = 0 ;}