This article is the original csdn first release of the blogger. I hope it will be helpful to you after reading it. Please correct me if I have any shortcomings! Let’s communicate and learn together and make progress together!
> Posted by: @日月同光, symbiotic with me_SCM-CSDN Blog
>You are welcome to like the original blogger Sun and Moon Shine Together, and live with me? + follow + collect + comment?.
Column Series: CSDN-MCU Serial Communication Learning Series
>My motto is: “Try your best and be the best version of yourself!
If you want to reprint, please inform us in advance! ! !
Copyright statement: This article is an original article by the CSDN blogger “The sun and the moon shine together, live with me”, and is the only article on CSDN.
Table of Contents
1. Code element, baud rate
1.1 code element
1.2 baud rate
2. Data frame
2.1 Starting bit
2.2 Stop bit
2.3 Data-frame header
3. System design
3.1 Design requirements
3.2 System Principle
3.3 Hardware design
3.3.1 Serial port design
3.3.2LED circuit
3.4 Software design
3.4.1Send data
3.4.2 Serial port initialization
3.4.3 Receiving interrupt
3.4.4 Timer initialization
3.4.5 Timer interrupt module
Receive data physics module at 3.4.6
4. Results
4.1 Send two strings
4.2LED light is on
4.3LED light off
1. Code element, baud rate
1.1 code unit
Use symbols at the same time interval to represent a binary number. Generally speaking, symbol ≠ bit (1 binary number is equivalent to 1 bit, represented by 1 bit). For example, there are 4 states (0, 1, 2, 3 ), respectively represented by binary numbers, that is, 00, 01, 10, 11, so 1 symbol = 2 bits. Specially, if there are two states (0, 1), two binary numbers 0 and 1 can be used respectively. Indicates two states, at this time 1 symbol = 1 bit. In serial communication, data is sent one by one each time, so serial communication symbols = bits.
1.2 baud rate
The number of symbols sent per unit time is called the baud rate, and the unit is b/s.
We commonly use 9600b/s. The time required to send each bit is 1s/9600=104us.
2. Data Frame
2.1 starting bit
1 bit, indicating the beginning of a communication, synchronizing the receiver clock and informing the receiving end to start receiving data.
2.2 stop bit
1 bit, the end of a communication.
2.3 data-frame header
The serial communication frame header (also called the frame start flag) is a special character used to identify the start position of a serial communication frame. Its function is to allow the receiving end to correctly identify the starting position of the data frame so that the receiving end can correctly parse the entire data frame. The frame header is usually a fixed specific character or combination of characters.
3. System Design
3.1 Design Requirements
In this design, at the beginning, the microcontroller com1 sends Wait for Serial Communication Tset Start. and Please Send a string of data: these two strings to the virtual serial port com3, and then the virtual serial port com3 sends the data to the microcontroller com1, the microcontroller After receiving the data, it can be sent back to com3. The data sent is composed of frame header + data. The frame header is AA 55 AA 55. There are two situations of data. When the data is 01 02, the LED connected to P1^0 lights up. When the data is 02 01, connected to P1^0 The LED light goes out. When the data is wrong, the LED light does not respond (it cannot turn off if it is nearly on, and cannot turn on if it is nearly off). In addition, the virtual terminal can receive data sent by the microcontroller.
3.2 System Principle
Sending data through the serial port is not sent all at once, but one character/byte at a time. The baud rate is 9600b/s, and it takes 104us to send one bit. The timer can be set to 1ms. If the timer exceeds 5ms (the specified value, usually 3ms-8ms), it means that the data reception is completed, so the timer count variable can be defined. recv_timer_cnt, every time this variable + 1, the timing accumulates 1ms. If the variable value exceeds 5, the timing exceeds 5ms, then the reception is completed. Every time a piece of data is received, the timing count variable recv_timer_cnt is cleared to 0, and the received data is stored in the array recv_buf.
After the reception is completed, you need to determine whether the data is correct and parse the correct data. You can set a frame header variable recv_move_index to determine whether each data in the array recv_buf is correct. If there is an error, jump out of the judgment, add the frame header variable + 1, and continue. Make the judgment again until the correct string of data is judged or the end. When the correct string of data is judged, start analyzing the data (processing LED).
3.3 Hardware Design
3.3.1 Serial Port Design
3.3.2LED circuit
The LED lamp adopts common anode connection method, the left end is connected to the power supply, and the right end is connected to the resistor first and then to P1^0.
3.4 Software Design
3.4.1 Send data
void sendByte(unsigned char dat) //Send one frame of data function { SBUF=dat; while(!TI); TI=0; } void sendString(unsigned char *dat)//Send string function { while(*dat != '\0') { sendByte(*dat + + ); } }
3.4.2 Serial port initialization
void UartInit(void) //[email protected] { PCON & amp;= 0x7F; //The baud rate is not doubled SCON = 0x50; //8-bit data, variable baud rate TMOD &= 0x0F; //Set timer mode TMOD |= 0x20; //Set timer mode TL1 = 0xFD; //Set the timing initial value TH1 = 0xFD; //Set the timed reload value ET1 = 0; //Disable timer interrupt ES=1; //Serial port interrupt is turned on TR1 = 1; //Timer 1 starts timing }
3.4.3 Receive Interrupt
void ES_timers() interrupt 4 //Receive interrupt { if(RI) { RI=0; start_timer=1;//1.Open timer flag position 1 if(recv_cnt<MAX_REX_NUM) //Receive data within the specified character length range { recv_buf[recv_cnt]=SBUF; //2. Receive data recv_cnt + + ; } else { recv_cnt=MAX_REX_NUM; } recv_timer_cnt=0; //3. Clear the count to 0 every time a frame of data is received. } }
3.4.4 Timer Initialization
void Timer0_Init(void) //1 millisecond @11.0592MHz { TMOD &= 0xF0; //Set timer mode TMOD |= 0x01; //Set timer mode TL0 = 0x66; //Set the timing initial value TH0 = 0xFC; //Set the initial value of the timing TF0 = 0; //Clear the TF0 flag ET0=1; //Timer 0 interrupt is turned on TR0 = 1; //Timer 0 starts timing }
3.4.5 timer interrupt module
void T0_timer() interrupt 1 //Use 1ms to count to determine whether the reception is completed { TR0=0; if(start_timer == 1)//Software timer turns on { recv_timer_cnt + + ;//Count plus 1 if(recv_timer_cnt>MAX_timer_cnt) //The count value exceeds the specified range, indicating that the reception is completed { recv_timer_cnt=0; //Count reset to 0 \t\t recv_flag=1;//reception completion flag position 1 } } TL0 = 0x66; //Set the timing initial value TH0 = 0xFC; //Set the initial value of the timing TR0=1; }
Receive data management module at 3.4.6
void uart_service(unsigned char *buf) //Complete the judgment of the correctness of the reception and the corresponding analysis { unsigned char recv_move_index;//Definition of frame header variables if(recv_flag)//reception completes and starts parsing { start_timer=0;//Close the software timer after receiving recv_flag=0;//The reception completion flag is set to 0 for next reception sendString(buf);//Send the data in the receiving buffer while((recv_cnt>=6) & amp; & amp;(recv_move_index<=recv_cnt))//The data bytes are greater than or equal to 6, the frame header cannot exceed the data bytes { if((buf[recv_move_index + 0]==0xAA) & amp; & amp;(buf[recv_move_index + 1]==0x55) & amp; & amp;(buf[recv_move_index + 2]==0xAA) & amp; & amp;(buf[recv_move_index + 3]==0x55)) //Satisfy frame header AA 55 AA 55 { if((buf[recv_move_index + 4]==0x01) & amp; & amp;(buf[recv_move_index + 5]==0x02)) //The LED lights up when the data after the frame header is 01 02 { LED=0; } if((buf[recv_move_index + 4]==0x02) & amp; & amp;(buf[recv_move_index + 5]==0x01)) //When the data after the frame header is 02 01, the LED goes off { LED=1; } break;//Exit the while loop when the frame header and data are satisfied } recv_move_index + + ;//Move the frame header by 1 bit } recv_cnt=0; clr_recvbuffer(buf);//Clear buffer function } }
4. Result
4.1 Send two strings
4.2LED light on
4.3LED light off
The next article will focus on the communication program for real-time parsing of data frame headers through serial port interrupts. Dear readers, please stay tuned, the next article will be more exciting! ! !
If you don’t study for a day, you will have no good ideas. My name is no free food. If you like me, you can support me. The blogger’s name is @日月同光, symbiotic with me.
@日月同光,symbiosis with me_Basics of single-chip microcomputer, single-chip serial communication-CSDN blog@日月同惠,symbiosis with me. Good at basics of single-chip microcomputer, single-chip serial communication, and other aspects of knowledge,@日月同光,symbiosis with me Pay attention to stm32, c language, 51 single chip microcomputer, proteus, single chip microcomputer field.https://blog.csdn.net/LIN___IT?spm=1000.2115.3001.5343