STC51 microcontrollers usually have one serial port, and some have two serial ports. The serial port is generally used for downloading programs and serial communication. Serial communication is particularly suitable for controlling equipment, so industrial computers generally have serial ports.
The serial port pins of the 51 single-chip microcomputer are P3.0 and P3.1, which are RXD and TXD pins respectively. Serial port communication can be used for communication between single-chip microcomputers and other chips, communication between single-chip microcomputers, or communication between single-chip microcomputers and computers. Serial port communication is generally cross-connected. The standard serial port has 9 pins. In general, we only use RXD TXD GND. The remaining pins are communication control pins, which were used to control telephones in the past, but are basically not used now. Therefore, only RXD and TXD are retained on our 51 single-chip microcomputer.
The serial communication protocol is very simple. If the serial ports on the microcontroller are not enough, you can also use any two pins to simulate serial communication.
Generally, we use an 8-bit serial port protocol with a start bit. The baud rate is usually 9600, and there are also 1200, 2400, 4800, 115200, etc.
The program 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 bit 4 in the TCON register, because this register can be bit operated
The program for sending data through the serial port is as follows:
SBUF=55; // Send data 55
while(!TI);
TI=0;
The program involves two registers
SBUF is the serial port data register
TI is bit 1 in the SCON register, bit operation
The program for receiving data through the serial port is as follows:
if(RI)
{
recebuf=SBUF;
RI=0;
}
The program involves two registers.
SBUF is the serial port data register.
RI is bit 0 in the SCON register.
SBUF is both a transmit data register and a receive data register.
The above functional modules can be encapsulated into functions and called directly when used.
Previous article:51 MCU Tutorial Lecture 6_Serial Port Communication
Next article:51 MCU Tutorial Lecture 5_C51 Program
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Please tell me the value of this color code diode
- Application of Silicon Carbide and Gallium Nitride Devices in High Frequency Power Supplies
- 【DIY Creative LED】PCB is ready
- EEWORLD University ---- IOT-ARM Architecture and Programming
- C2000 power-on process
- The 2020 National Seminar has concluded, and these STM32 dry goods are worth savoring!
- 28335 Transmit control signals between control boards
- As the Spring Festival holiday approaches, the forum has quietly launched a new feature (or a new page?) Let's see who discovers it first
- FPGA implementation of sliding average filtering algorithm and LZW compression algorithm.pdf
- Several commonly used anti-reverse connection protection circuits