当前位置:网站首页>(9) Serial port interrupt
(9) Serial port interrupt
2022-06-12 02:19:00 【I love notes】
In this section, we will recall the contents related to the serial port , Serial port in our 51 Although not many single-chip computers are used , Mainly used to download , But in the future, if you play other boards, you will use them very frequently , So it is necessary to understand the communication principle of serial port , And there are various serial port protocols , These protocols are encapsulated from the most basic serial port protocols , And we are 51 SCM only needs to understand the most basic serial port interrupt content .
1. About serial port
The first thing to understand is what serial communication is , To understand serial communication, we must first understand serial communication and parallel communication , Parallel communication means that our data bytes are sent simultaneously with multiple data lines , This transmission mode is only suitable for short distance transmission , This transmission mode is less used , And the cost of long-distance transmission is high , So you just need to understand . The serial mode is to transmit data bytes bit by bit on a transmission line one by one , All you need is a data line . Send yes , To convert parallel data into serial data and send it to the line , Reception time , Then the serial data is changed into parallel data .
The serial data transmission is also divided into two ways , Asynchronous serial communication and synchronous serial communication , Generally, synchronous serial mode is seldom used , Generally not used , It doesn't matter if you don't know . But must understand is the asynchronous serial communication way .

Asynchronous communication refers to the process that the transmitting and receiving devices of communication use their respective clocks to control the transmission and reception of data , In order to coordinate the sending and receiving of both parties , It is required that the clocks of the sending and receiving devices should be consistent as much as possible . Asynchronous communication is based on characters ( The frame formed ) Transfer for units , The gap between characters ( The time interval ) Is arbitrary , When the bits of each character are transmitted at a fixed time , That is, there may not be... Between characters “ Bit spacing ” The integer multiples of , But the distance between each bit in the same character is " Bit spacing " Integer multiple . A frame of character information for asynchronous communication consists of 4 Part of it is made up of , As shown in the figure above : Start bit , Data bits , Check bit and stop bit , As shown in the figure above , Generally, we don't need to use the check bit , So the check digit is not marked .
However, the serial communication occasionally uses check bits , The check digit is known by the name , That is to say, check whether there is any error in your frame data , Our verification is generally divided into parity , Code sum check and cyclic redundancy check , In our serial communication, we usually use parity check , Data bits follow 1 Bit is parity bit . Odd check , In the data 1 If the sum of the number of and the check bits is an odd number, it is an odd check , The opposite is even parity , When receiving characters , We pass the right 1 Verification of the number of , If you find 1 The number of is inconsistent , This means that an error occurred in the data transmission process . And there is another division of our serial communication that is simplex , Half duplex and full duplex . Simplex means that data can only be transmitted in one direction , Reverse transmission is not possible . Half duplex means that data can be transmitted in two directions , But not at the same time , Full duplex means that data can be transmitted in both directions at the same time .
2 Hardware
About baud rate , The rate of single chip microcomputer or computer in serial communication is expressed by baud rate , It is defined as the number of binary codes transmitted per second , namely 1 potter =1 position / second , The unit is bps. Calculation of baud rate , In serial communication , The sending and receiving parties shall have an agreement on the rate of sending or receiving data . Our computer can use the serial port debugging tool to set the parameters of our computer , And ours 51 SCM can only be set by programming . Through programming, the serial port of single chip microcomputer can be set to 4 Ways of working , The way 0 And way 2 The baud rate is fixed , And the way 1 And way 3 The baud rate is variable , By timer T1 The overflow rate of .
The way 0 Baud rate :
The way 1 Baud rate :
X(T1 Overflow rate )
The way 2 Baud rate :
X(
)
The way 3 Baud rate :
X(T1 Overflow rate )
In style ,
Is the crystal vibration frequency of the system , Usually it is 12Mhz or 11.0592Hz;SMOD yes PCON The highest bit of the register ; and T1 The overflow rate is T1 The frequency of spills . Just work it out every time T1 The time for each overflow of the timer T, that T The reciprocal of is its overflow rate , Like T1 in 50ms Overflow once , So its overflow rate is 20Hz, So why often 51 SCM selection 11.0592MHZ Crystal oscillator , In fact, the common baud rate can be calculated accurately .
Serial port communication common registers :
Power management register PCON( It can't be bit addressable )

The only thing related to our serial port is SMOD This one , When we calculate the baud rate .
Serial port control register SCON( Addressable addressing )


SM0,SMI- Working mode selection bit
SM2- Multi machine communication control bit , Used for communication of multiple single chip computers ,
Mainly used in 2 And way 3. When the receiver SM2=1 You can use the received RB8 To control whether to activate RI(RB8=0 Not active when RI, The received message is discarded ;RB8=1 The data received when entering SBUF, And activate RI, In turn, the data is transferred from SBUF Go to read ). When SM2=0 when , Whatever you receive RB8 by 0 and 1, Can make the received data enter SBUF, And activate RI( Is this time RB8 No control RI Activated functions ). By controlling SM2, It can realize multi computer communication . In ways 0 when ,SM2 Must be 0. In ways 1 when , If SM2=1, Then only when a valid stop bit is received ,RI Only place 1.
REN- Allows serial reception of bits . Set by software REN=1, Then start the serial port to receive data ; If the software is set to REN=0, It is forbidden to receive .
TB8- In ways 2 Or the way 3 in , Is the ninth bit of sending data , Its function can be specified by software . Can be used as parity bit of data , Or in multi machine communication , As an address frame / Flag bit of data frame . In ways 0 And way 1 in , This bit is not used .
RB8- In ways 2 Or the way 3 in , Is the ninth bit of received data , As parity bit or address frame / Flag bit of data frame . In ways 1 when , if SM2=0, be RB8 Is the received stop bit .
TI- Send interrupt flag bit . In ways 0 when , When serially sending the first 8 At the end of bit data , Or in other ways , Start of serial transmission stop bit , By internal hardware TI Set up 1, towards CPU Send interrupt request . In the interrupt service program , It must be cleared with software 0, Cancel this interruption request .
RI- Receive interrupt flag bit . In ways 0 when , When serially receiving the first 8 At the end of bit data , Or in other ways , In the middle of the serial receive stop bit , By internal hardware RI Set up 1, towards CPU Send interrupt request . It must also be in the interrupt service program , Clear it with software 0, Cancel this interruption request .
3. Software
The system crystal frequency of my single chip microcomputer is 12Mhz, There may be garbled code after sending many times , It's also normal , I use the way 1 Programming :
#include "reg52.h"
int flag = 0;
char a = 0;
void uart_init(){
PCON |= (1 << 7);
TMOD |= 0x02 << 4;
TH1 = 0xf3;
TL1 = 0xf3;
SM0 = 0;
SM1 = 1;
REN = 1;
EA = 1;
TR1 = 1;
ES = 1;
}
void main(){
uart_init();
while(1){
if(flag == 1){
flag = 0;
ES = 0;
SBUF = a;
while(!TI);
T1 = 0;
ES = 1;
}
}
}
void uart_interrupt() interrupt 4{
RI = 0;
a = SBUF;
flag = 1;
}边栏推荐
- Ue4\ue5 touch screen touch event: single finger and double finger
- 力扣编程题-解法汇总
- SwiftyJSON解析本地JSON文件
- 打包一个包含手表端应用的手机端APK应用—Ticwear
- Implementation scheme of iteration and combination pattern for general tree structure
- 力扣解法汇总433-最小基因变化
- Force deduction solution summary 713- subarray with product less than k
- Force deduction solution summary 396 rotation function
- Summary of force deduction method 417- Pacific Atlantic current problems
- Android HTML5 page load cache optimization
猜你喜欢

超图倾斜数据合并根节点后转3dtiles

Smartbi helps you solve the problem of losing high-value customers

CVPR2022 | iFS-RCNN:一种增量小样本实例分割器

BaseDexClassLoader那些事

Knowledge points of mall development
![[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin](/img/f9/332b206d5aca0ca6afc3fdf11a53c8.jpg)
[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin

Proxy and reflection (II)

How to stop anti-virus software from blocking a web page? Take gdata as an example

ozzanimation-基于sse的动作系统

MySQL table common operation mind map
随机推荐
Summary of force deduction solution 427- establishment of quadtree
What insurance products can you buy at the age of 60?
Summary of force deduction solution 436- finding the right interval
Graphical data analysis | business analysis and data mining
力扣解法汇总713- 乘积小于 K 的子数组
Force deduction solution summary 1728- cat and mouse II
The establishment and introduction of the announcement module of PHP development blog system
Ue4\ue5 touch screen touch event: single finger and double finger
What is SAP c/4hana Foundation
Don't miss it! Five large data visualization screens that HR must collect
力扣解法汇总868-二进制间距
Force deduction solution summary 449 serialization and deserialization binary search tree
Force deduction solution summary 905- array sorted by parity
Basedexclassloader
Research Report on Chinese psoriasis drug market evaluation and investment direction (2022 Edition)
力扣解法汇总875-爱吃香蕉的珂珂
Proxy and reflection (II)
高考完不要急着去打工了,打工以后有的是机会,不差这三个月
Start ticwatch2
Force deduction solution summary 388- longest absolute path of file