1, What is serial port (RS232 9-pin serial port)
Serial port is the basic external interface of most of our micro control units (MCU). Generally, the most basic function of serial port is debugging, and it can also be the interface of data communication (the amount of data is smaller).
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2, Serial communication principle
The communication mode of each data of a message is called serial communication, which can be serial port or other (74LS164 shift register).
According to the transmission mode, it can be divided into simplex, half duplex and full duplex, as shown in the figure:
Serial port transmission is shown in the figure below:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3, Introduction of serial port register
1. Serial port control register SCON
2. Power control register PCON
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4, Serial port sample code
1. Sending single data through serial port
/************************************************************************************** * *Realization phenomenon: serial port sends single data * *Note: baud rate is 4800 ***************************************************************************************/ #include "reg52.h" / / this file defines some special function registers of SCM typedef unsigned int u16; //Declare data types typedef unsigned char u8; void Delay(void)//Delay 500ms { unsigned char i,j; for(i=0;i<255;i++) //Cycle operation to achieve delay effect for(j=0;j<255;j++); for(i=0;i<255;i++) for(j=0;j<255;j++); for(i=0;i<255;i++) for(j=0;j<255;j++); } /******************************************************************************* * Function name: UsartInit() * Function function: set serial port * Input: None * Output: None *******************************************************************************/ void UsartInit() { SCON=0X50; //Set to working mode 1: 8 data bits TMOD=0X20; //Set the counter working mode 2 high four bits and low four bits PCON=0X80; //Double baud rate TH1=0XF3; //Set the initial value of the counter. Note that the baud rate is 4800. If the baud rate is 9600, modify TH1 and TL1 TL1=0XF3; } /******************************************************************************* * Function name: UsartSendByte() * Function function: send single byte through serial port * Input: None * Output: None *******************************************************************************/ void UsartSendByte(unsigned char byte) { SBUF=byte; //Buffer load bytes to send while(TI==0)//Wait for sending data to complete, TI flag will be set to 1 TI=0; //Clear send interrupt flag bit } /******************************************************************************* * Function name: main * Function function: main function * Input: None * Output: None *******************************************************************************/ void main() { unsigned char i = 0; UsartInit(); //Serial port initialization while(1) { UsartSendByte(i);//Serial port sends single byte data Delay(); i++; if(i>255) i=0; } }
2. Serial port receives and returns data
/************************************************************************************** * *Realization phenomenon: serial port receives and returns data * *Note: baud rate is 4800 ***************************************************************************************/ #include "reg52.h" / / this file defines some special function registers of SCM typedef unsigned int u16; //Declare data types typedef unsigned char u8; void Delay(void)//Delay 500ms { unsigned char i,j; for(i=0;i<255;i++) //Cycle operation to achieve delay effect for(j=0;j<255;j++); for(i=0;i<255;i++) for(j=0;j<255;j++); for(i=0;i<255;i++) for(j=0;j<255;j++); } void UsartInit() { SCON=0X50; //Set to working mode 1: 8 data bits TMOD=0X20; //Set the counter working mode 2 high four bits and low four bits PCON=0X80; //Double baud rate TH1=0XF3; //Set the initial value of the counter. Note that the baud rate is 4800. If the baud rate is 9600, modify TH1 and TL1 TL1=0XF3; ES=1; //Open receive interrupt EA=1; //Open total interrupt TR1=1; //Turn on counter } /******************************************************************************* * Function name: UsartSendByte() * Function function: send single byte through serial port *******************************************************************************/ void UsartSendByte(unsigned char byte) { SBUF=byte; //Buffer load bytes to send while(TI==0)//Wait for sending data to complete, TI flag will be set to 1 TI=0; //Clear send interrupt flag bit } /************************************************************ * *Input: pstr string * *Function: serial port printing string * ************************************************************/ void PrintfStr(char *pstr) { while(pstr && *pstr) { UsartSendByte(*pstr++); } } void main() { unsigned char i = 0; UsartInit(); // Serial port initialization while(1); } /******************************************************************************* * Function name: Usart() interrupt 4 * Function function: serial communication interrupt function *******************************************************************************/ void Usart() interrupt 4 //Serial port interrupt number { u8 receiveData; receiveData=SBUF; //Out of the received data receive interrupt flag bit RI = 0; //Clear receive interrupt flag bit UsartSendByte(receiveData);//Return received data //SBUF=receiveData; / / put the received data into the send register //while(!TI); / / wait for sending data to complete //TI=0; / / clear the send completion flag bit and send interrupt flag bit }