(5) serial port

The serial port is a widely used communication interface. The serial port is low in cost, easy to use, and simple in communication lines, and can realize mutual communication between two devices.

The serial port of the single-chip microcomputer can make the single-chip microcomputer communicate with the single-chip microcomputer, the single-chip microcomputer and the computer, and the single-chip microcomputer and various modules, which greatly expands the application range of the single-chip microcomputer and enhances the hardware strength of the single-chip microcomputer system.

The 51 single-chip microcomputer has its own UART (Universal Asynchronous Receiver Transmitter, Universal Asynchronous Receiver Transmitter), which can realize the serial communication of the single-chip microcomputer.

Please add a picture description

Level standard: it is the expression of data 1 and data 0, and it is the correspondence between the artificially specified voltage and data in the transmission cable

There are three commonly used level standards for serial ports:

  1. TTL level: +5V means 1, 0V means 0
  2. RS232 level: -3 -15V means 1, +3 +15V means 0
  3. RS485 level: two-wire voltage difference +2 +6V means 1, -2 -6V means 0 (differential signal)

Please add a picture description

  • Full-duplex: Both parties in communication can transmit data to each other at the same time
  • Half-duplex: The communication parties can transmit data to each other, but must time-division multiplex a data line
  • Simplex: Communication can only be sent from one party to the other and cannot be reversed
  • Asynchronous: The two communicating parties agree on the communication rate
  • Synchronization: The communication parties rely on a clock line to agree on the communication rate
  • Bus: A data transmission line connecting various devices (similar to a road, which connects the residents on the roadside so that the residents can communicate with each other)

Please add a picture description

  • STC89C52 has 1 UART
  • The UART of STC89C52 has four working modes:
    • Mode 0: Synchronous Shift Register
    • Mode 1: 8-bit UART with variable baud rate (commonly used)
    • Mode 2: 9-bit UART with fixed baud rate
    • Mode 3: 9-bit UART with variable baud rate

Baud rate: The rate of serial communication (the interval between sending and receiving each data bit), that is, the interval time agreed upon when asynchronous, used for data synchronization

Bit rate: the interval time between sending and receiving each bit, used for data synchronization

Check digit: used for data validation

奇偶校验
8位数据格式:0110 0011
9位数据格式:0110 0011 1 // 有偶数(4)个1,所以第9位置1 

Stop bit: used for data frame interval

Please add a picture description

Please add a picture description

Please add a picture description

DISCOUNT

Please add a picture description

Please add a picture description

Please add a picture description

After sending, the hardware sets TI to 1, and must set TI to 0 after the interrupt

Please add a picture description

SM0/FE	SM1	SM2	REN	TB8	RB8	 TI  RI
  0		 1	0	0	0	0	0	0 // 使用8位UART波特率可变,禁止接收
SCON = 0x40;

SBUF send and receive buffer, no initialization setting required

Please add a picture description

Timer configuration:

Default use timer 1, 8-bit auto-reload mode (mode 2)

8-bit auto-reload mode: 16-bit timer/counter consists of two 8-bits (0~65535), the initial value needs to be set when interrupting, otherwise it will start counting from 0, this operation will consume a certain amount of time, so the accuracy is low . The 8-bit automatic reload is also composed of two 8-bits, but only one 8-bit is used for counting, and the other 8-bit is responsible for recording the initial value. When the interrupt is interrupted, the 8-bit recording the initial value will be automatically reloaded (Auto Reload) On the 8 bits of the count (CNT), the operation of manually setting the initial value is omitted, but the counting range is reduced to 0~255

void UART_Init(){
    
    
    SCON = 0x40;
    
    TMOD = 0x20; // 0010 0000 定时器1,模式2
    
    PCON |= 0x80; // SMOD=1 波特率加倍
	//TF0 = 0; // 标志位初始化,防止产生中断
	TR1 = 1; // 运行控制位,开始计数工作
    
	TH1 = 0xF4; // 给计数单元赋初值
	TL1 = 0xF4;
	
	ET1 = 0;// 溢出中断允许位=0,禁止溢出中断
	//EA = 1; // 总中断允许控制位
	//PT0 = 0; // 定时器0中断优先级控制位
    //进行串口时钟定时不需要中断,只需要计数后溢出,通过计数溢出速率即可得到波特率
}
void UART_SendByte(unsigned char Byte){
    
    
	SBUF = Byte; // 写操作
	while(TI == 0);
	TI = 0; // 手动恢复TI
}

The lower the baud rate, the more stable the communication and the smaller the error, you can also delay for a few milliseconds to buffer

Serial port receiving:

REN置1
SCON = 0101 0000 = 0x50;// 或位寻址REN = 1;
EA = 1; // 启动总中断
ES = 1; // 启动串口中断(发送/接收)
void UART_Rountime() interrupt 4 {
    
    
	if(RI == 1){
    
    
		// 串口接收SBUF
		RI = 0;
	}
}

Calculation of baud rate:

The baud rate is calculated by the T1 timer, and because the 8-bit auto-reload mode is used, there is no need to assign initial values ​​​​to TH1 and TL1 after each interrupt

TH1 = 0xF4; // 244
TL1 = 0xF4;
256(8位定时器计数上限) - 244 = 12 // 每计12个数就溢出一次,晶振12MHz,每隔1us计一次数,即每隔12us溢出一次
溢出频率 = 1 / 12us = 0.083333MHz

若SMOD=1则溢出频率直接÷16 = 0.0052083MHz // 双倍波特率
SMOD=0则溢出频率先÷2再÷16 = 0.0026042MHz

波特率 = 0.0052083MHz * 1000 * 1000 = 5208.3Hz
或0.0026042MHz * 1000 * 1000 = 2604.2Hz

Data display mode:

  • HEX mode/hexadecimal mode/binary mode: display in the form of raw data
  • Text Mode/Character Mode: Display in encoded form of raw data

In text mode, the data sent by the MCU is encoded according to ASCII and then displayed

Please add a picture description

Guess you like

Origin blog.csdn.net/Falling_Asteroid/article/details/130736724