How to use 51 single-chip microcomputer to realize serial communication

The STC51 microcontroller generally has one serial port, and some have two serial ports. The serial port is generally used for downloading programs and serial port communication. Serial communication is particularly suitable for controlling equipment, so the computer of the industrial computer generally has a serial port.

The serial port pins of the 51 microcontroller are the P3.0 pin and the P3.1 pin, which are the RXD and TXD pins respectively. Serial communication can be used for single-chip communication with other chips or between single-chip computers or between single-chip computers and computers. Serial communication is generally a cross-connect. The standard serial port has 9 pins. In general, we only use RXD TXD GND, and the remaining pins are communication control pins, which were used to control telephones in the past, but are basically useless now. So we only keep RXD and TXD on our 51 MCU.

The protocol of serial communication is very simple. If the serial port on the microcontroller is not enough, you can also use any two pins to simulate serial communication.

How to use 51 single-chip microcomputer to realize serial communication

In general, we use an 8-bit serial port protocol with a start bit. The baud rate is commonly 9600, in addition to 1200 2400 4800 115200 and so on.

The procedure to open the serial port is as follows:

TMOD=0x20;

TH1=0xFD;

TL1=0xFD;

SCON=0X50;

TR1=1;

The program involves the configuration of 5 registers.

TMOD is the timer control register

TH1 and TL1 are the initial value registers of timer 1

SCON is the serial port control register

TR1 is bit4 in the TCON register because this register can be bit-manipulated

The program for sending data through the serial port is as follows:

SBUF=55; // send data 55

while(!TI);

TI=0;

The program involves 2 registers

SBUF is the serial port data register

TI is bit1 in the SCON register, bit manipulation

The program of serial port receiving data is as follows:

if(RI)

{

recebuf=SBUF;

RI=0;

}

The program involves 2 registers

SBUF is the serial port data register

RI is bit0 in the SCON register, bit manipulation

SBUF is both a transmit data register and a receive data register.

You can encapsulate the above functional modules into functions, and call them directly when using them.

Solar Panel


Mono Solar Panel, Poly Solar Panel, HJT, BIPV, Double Glass, Small Solar Panel, Solar Cell Panel

Wuxi Sunket New Energy Technology Co.,Ltd , https://www.sunketsolar.com