Serial communication programming

1. Basic understanding of serial ports

The serial port interface is referred to as serial port, also known as serial communication interface or serial interface (commonly referred to as COM interface), which is an extended interface using serial communication. Serial interface refers to the sequential transmission of data bit by bit, and its characteristic is communication The line is simple, as long as a pair of transmission lines can realize two-way communication (you can directly use the telephone line as the transmission line), thus greatly reducing the cost, especially suitable for long-distance communication, but the transmission speed is slow

* is a way of in-line communication between devices

* Data is transmitted sequentially bit by bit

* Two-way communication, full duplex

*Transfer speed is relatively slow

2. About the level of the serial port

UART that is often heard: asynchronous serial refers to UART (Universal Asynchronous Receive/Transmitter), universal asynchronous reception/transmission, UART includes TTL level serial port and RS232 level serial port

 

 3. Serial communication

Serial connection mode:

*RxD: data input pin, data receiving, STC89 series corresponds to P3.0 port

*Txd: data sending pin, data sending, STC89 series corresponds to P3.1 port

*Wiring

 impression shaping

*The input/output data buffers are called SBUF, both use 99H address code, but they are two independent 8-bit registers

* The code is reflected as : want to receive data char data=SBUF, want to send data, SBUF=data.

 *Recall that UART is an asynchronous serial interface, and the two sides of the communication use different clock hands, because the hardware configurations of the two parties are different, but the communication speed needs to be agreed, which is called the baud rate . To force the baud rate of the single-chip microcomputer, we need to write code configuration (the parameters should be the same as the computer configuration).

4. Analysis of serial port programming registers (the following table text is extracted from STC official website www.STCMCU.com)

The pins corresponding to the hardware part of the STC89C51 serial port are P3.0RXD and P3.1TxD.

4.1 Serial port related registers

4.1.1 Serial port control registers SCON and PCON

Serial control register SCON, baud rate selection special function register PCON

 

 

 

 Some important positions are listed above, and the specific use should refer to the official manual.

Actual coding (practical application):

Send a character to the PC through the microcontroller

 Send a string to the PC through the MCU (add two more functions and optimize the main function)

The PC side lights up the LED through the serial port (non-interrupted)

The PC side lights up the LED through the serial port (interruption)

 How to handle character instructions

 

 

 

Guess you like

Origin blog.csdn.net/weixin_62599865/article/details/129963991