Serial Port
Today we will introduce how to communicate with a computer through a single-chip microcomputer via a serial port.
1. Overview
Communication is essentially the transmission, exchange and processing of binary-coded letters, numbers, symbolic and digitized sound and image information through certain communication lines and devices, with the computer as the center. Since computer information is processed in bytes (8 bits) or words (1 or several bytes), it is more appropriate to use words or bytes as the transmission unit. Today we will focus on using serial ports for communication.
Serial communication means that each bit of the transmitted data is sent or received one by one in sequence. In a serial communication system, each bit of the transmitted data is only transmitted on one transmission line, so it is more economical when transmitting data over long distances. The disadvantage is that the transmission speed is slow.
2. Types of serial communication
Serial communication is divided into two modes: synchronous communication and asynchronous communication.
(1) Synchronous serial communication: It requires that the clock frequency of the sending device is consistent with the clock frequency of the receiving device, and the sending device first sends one (or several) synchronization characters, followed by a group of data. Once the receiving device detects the synchronization character that meets the requirements, it will continuously receive all the data in sequence. Advantages: Faster speed. Disadvantages: Higher requirements on the hardware structure of the device.
(2) Asynchronous serial communication: The clocks of the sending device and the receiving device are inconsistent. Data is transmitted in frames in a sequence of one character at a time. The frame format diagram is shown in Figure 2-1:
0 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 1
Start bit D0 D1 D2 D3 D4 D5 D6 Parity check
bit Stop bit
Seven-bit data
Figure 2-1 Asynchronous communication signal transmission format
Explanation: A start bit "0" at the beginning, followed by 5 to 8 data bits, with the low bit in front and the high bit in the back (7 data bits in the figure), followed by a parity check bit (can be omitted), and finally a stop bit "1" to indicate the end of the character.
Since it is difficult to keep the clock frequencies of the sending device and the receiving device strictly consistent for a long time in technical implementation, the serial asynchronous communication method is currently more commonly used.
3. Duplex transmission of data in serial port
In serial communication, the communication interface of the machine can only send or receive, which is a one-way transmission, called simplex transmission. If two machines can transmit in both directions, this method is called duplex transmission.
In duplex transmission, if receiving and sending cannot be performed at the same time, they can only be received and sent in time-sharing, this transmission is called half-duplex transmission. If both sending and receiving of the two machines can be performed at the same time, it is full-duplex transmission.
4. Serial communication rate
Baud rate is the regulation of data transmission rate in asynchronous communication of single-chip microcomputer, that is, the number of binary bits of information transmitted per second.
5. Serial interface
In essence, all serial interfaces are connected to the CPU in parallel data form and to the external logic unit in serial data form. Their basic function is to receive serial data from the external logic unit, convert it into parallel data and transmit it to the CPU; or send parallel data from the CPU, convert it into serial data and output it to the external logic unit.
The serial interface contains at least one receiver and one transmitter.
6. 51 Serial Interface
The 51 MCU has a powerful full-duplex serial interface circuit unit UART (Universal Asynchronous Receiver/Transmitter). When sending, data is sent from the TXD terminal, and when receiving, data is input from the RXD terminal. There are two buffers SBUF, one for sending buffer and the other for receiving buffer. The serial port has 4 working modes, and the baud rate can be set by software and generated by the timer/counter inside the chip. The serial port can trigger the interrupt system when receiving or sending data, which is very convenient to use.
6.1. Composition of the serial port
6.1.1. The serial port control register SCON has the following format:
D7 D6 D5 D4 D3 D2 D1 D0
SM0 SM1 SM2 REN TB8 RB8 TI RI
(1) SM0, SM1: serial port working mode control bits. For the specific working modes, see Table 6-1:
Table 6-1 Serial port working mode control
SM0 SM1 Working mode description Baud rate
0 0 Mode 0 Synchronous shift register fOSC/12
0 1 Mode 1 10-bit asynchronous transmission and reception controlled by timer
1 0 Mode 2 11-bit asynchronous transmission and reception fOSC/32 or fOSC/64
1 1 Mode 3 11-bit asynchronous transmission and reception controlled by timer
(2) SM2: multi-machine communication control bit (mode 2, 3)
1-> RI is set only when the 9th bit (RB8) is received as 1;
0-> RI is set when a character is received.
(3) REN: Serial port receive enable bit
1-> Enable serial port receive;
0-> Disable serial port receive.
(4) TB8: In mode 2 and mode 3, it is the 9th data bit sent, which can also be used as a parity bit.
(5) RB8: In mode 2 and mode 3, it is the 9th data bit received; in mode 1, it is the received stop bit.
(6) TI: Transmit interrupt flag
Set by hardware, must be cleared by software
(7) RI: Receive interrupt flag
Set by hardware, must be cleared by software
6.1.2. Power control register PCON:
Bit 7 SMOD of PCON is a selection bit related to the baud rate setting of the serial port.
D7 D6 D5 D4 D3 D2 D1 D0
SMOD
(1) SMOD: Serial port baud rate doubling bit
1-> Mode 1 and Mode 3, baud rate = Timer 1 overflow rate/16; Mode 2 baud rate = fOSC/32;
0-> Mode 1 and Mode 3, baud rate = Timer 1 overflow rate/32; Mode 2 baud rate = fOSC/64;
Normally, when using the serial port of a microcontroller, the selected crystal oscillator is relatively fixed, generally 6MHz, 12MHz and 11.0592MHz, often used for communication with a microcomputer; the selected baud rate is also relatively fixed. The commonly used baud rates of the serial port and the corresponding settings are shown in Table 6-2:
Table 6-2 Commonly used baud rates of the serial port
Serial port working mode baud rate fOSC = 6 MHz fOSC = 12 MHz fOSC = 11.0592 MHz
SMOD TMOD TH1 SMOD TMOD TH1 SMOD TMOD TH1
Mode 0 1M × × ×
Mode 2 375K 1 × ×
187.5K 1 × × 0 × ×
Mode 1
or
Mode 3 62.5K 1 20 FFH
19.2K 1 20 FDH
9.6K 0 20 FDH
4.8K 1 20 F3H 0 20 FAH
2.4K 1 20 F3H 0 20 F3H 0 20 F4H
1.2K 1 20 E6H 0 20 E6H 0 20 E8H
600 1 20 CCH 0 20 CCH 0 20 D0H
300 0 20 CCH 0 20 98H 0 20 A0H
137.5 1 20 1DH 0 20 1DH 0 20 2EH
110 0 20 72H 0 20 FEEBH 0 10 FEFFH
7. Example
MCU serial port sending/receiving program, every byte received is sent out immediately; after connecting to the microcomputer, the characters typed by the microcomputer are echoed on the screen.
0x00; TR1=1; while(1) { while(RI==0); RI=0; a=SBUF; SBUF=a; while(TI==0); TI=0; } } } 1. The code is as follows : /*************************************************************** * Title: Serial port
receiving and sending program (letters)
* Description:
* Copyright:
* Company:
* @author Enron
**********************************************************/
#include
#define uchar unsigned char
/**********************************************************
Main function
*******************************************************/
void main()
{
uchar a;
TMOD=0x20;
TL1=0xfd; // Using 11.0592MHz crystal oscillator, baud rate 9600bps
TH1=0xfd;
SCON=0xd8;
PCON=0x00;
TR1=1;
while(1)
{
while(RI==0);
RI=0;
a=SBUF;
SBUF=a;
while(TI==0);
TI=0;
}
}
2. The code is as follows: /*************************************************************** * Title: Serial port receiving and sending program (letters) * Description: * Copyright: * Company: * @author Enron **************************************************************/ #include
D7 D6 D5 D4 D3 D2 D1 D0
GATE T/C 1 M1 M0 GATE T/C 0 M1 M0
0 0 1 0 0 0 0 0
It can be seen that we use working mode 1 of timer 1, that is, timer 1 is an 8-bit timer/counter with automatic reset.
TH1 = 0xFd; //At 11.0592MHz, set the serial port baud rate to 9600, mode 3
TL1 = 0xFd;
serial port working mode baud rate fOSC = 6 MHz fOSC = 12 MHz fOSC = 11.0592 MHz
SMOD TMOD TH1 SMOD TMOD TH1 SMOD TMOD TH1
mode 0 1M × × ×
mode 2 375K 1 × ×
187.5K 1 × × 0 × ×
mode 1
or
mode 3 62.5K 1 20 FFH
19.2K 1 20 FDH
9.6K 0 20 FDH
4.8K 1 20 F3H 0 20 FAH
2.4K 1 20 F3H 0 20 F3H 0 20 F4H
1.2K 1 20 E6H 0 20 E6H 0 20 E8H
600 1 20 CCH 0 20 CCH 0 20 D0H
300 0 20 CCH 0 20 98H 0 20 A0H
137.5 1 20 1DH 0 20 1DH 0 20 2EH
110 0 20 72H 0 20 FEEBH 0 10 FEFFH
According to the table, at 11.0592MHZ, the baud rate of the serial port is 9.6K, which is 9600, so TH1 TL1 is set to 0xFD
SCON = 0xd8; //Serial port control
D7 D6 D5 D4 D3 D2 D1 D0
SM0 SM1 SM2 REN TB8 RB8 TI RI
1 1 0 1 1 0 0 0
Table 6-1 Serial port working mode control
SM0 SM1 Working mode description Baud rate
0 0 Mode 0 Synchronous shift register fOSC/12
0 1 Mode 1 10-bit asynchronous transmission and reception controlled by timer
1 0 Mode 2 11-bit asynchronous transmission and reception fOSC/32 or fOSC/64
1 1 Mode 3 11-bit asynchronous transmission and reception controlled by timer
That is to say, the serial port works in mode 3, and the 11-bit asynchronous transmission and reception baud rate is controlled by the timer. REN: 1-> Allow serial port reception. TB8: In mode 2 and mode 3, it is the 9th bit of data sent, and can also be used as a parity bit. RB8: In mode 2 and mode 3, it is the 9th bit of data received; in mode 1. TI: Transmit interrupt flag, set by hardware, must be cleared by software. RI: Receive interrupt flag, set by hardware, must be cleared by software.
PCON = 0x00;
D7 D6 D5 D4 D3 D2 D1 D0
SMOD
0 0 0 0 0 0 0 0
1->Mode 1 and Mode 3, baud rate = Timer 1 overflow rate / 16; Mode 2 baud rate = fOSC/32;
0->Mode 1 and Mode 3, baud rate = Timer 1 overflow rate / 32; Mode 2 baud rate = fOSC/64
TR1 = 1; //Start timer T1
, that is, start timerwhile
(1)
while(RI==0); //Serial port receives
RI = 0;
c = SBUF;
SBUF = c;
while(TI==0); //Serial port sends
TI=0;
while(RI==0); If RI is 0, wait (if not 0, execute the next sentence)
RI = 0; Executing this sentence means that RI is no longer 0 (reception is completed), here force RI to 0
c = SBUF; Receive character
SBUF = c; Send characterwhile
(TI==0); If TI is 0, wait, otherwise execute the next sentence
TI=0; Executing this sentence means that TI is not 0 (i.e., the sending is finished), and forcing TI to be 0
means judging whether RI is 0. When RI==0, it will keep looping and judging. When RI is not equal to 0, it will execute the following statement. RI=0 is reset. Then receive data SBUF is the place where serial port data is stored. When C=SBUF, the serial port data is received and the original data is sent out SBUF=C. While (TI==0) means that when TI is 0, it will keep waiting. When it is not 0, execute TI=0 to reset TI.
Compile the previous program and write it into the microcontroller, and connect the serial port line. Send characters to the microcontroller through the serial port debugging assistant, and you will find that the sent content is sent back as is.
Previous article:STC89C51 MCU running light program
Next article:Single chip microcomputer keil-c51 language multiplication and division and film taking out of range problem
- Popular Resources
- Popular amplifiers
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
- 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
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Free application: DCM product evaluation samples are waiting for you!
- I want a switching power supply chip model with silk screen printing
- No GD32? Replace STM32 with domestic MM32
- 【CC1352P Review】Summary
- Single TL431 constant current improved circuit
- Wifi6 video explanation
- Looking for: 3.7V lithium battery charging, boost, power, switch, SOP8 chip
- FPGA_100 Days Tour_ROM-based Arbitrary Waveform Generator
- Merry Christmas Eve
- MicroPython upgraded to v1.11