Here we mainly talk about the application of baud rate and timer 2.
Generally speaking, our serial communication uses asynchronous serial communication, and the working mode is mode 1.
Method 1 is to send a complete signal of 10 bits. The start signal is low level, and the end signal is high level. The two lines of serial communication are usually in a high level state. Once there is data to be forwarded, the level is pulled low, and the communication chip immediately monitors the signal. In this way, data can be sent and received normally.
Generally speaking, we use timer 1 mode 2 (auto-reload mode) as the baud rate generator. Similarly, we abandon the interrupt of timer 1, because the baud rate will not be disturbed when it is generated (if timer 1 has an interrupt function, then the interrupt function will turn off the timer 1 interrupt, and the baud rate generator is in the off state). According to the documents given to us by STC, the function of timer 1 is more powerful than that of timer 2, so we prefer to use timer 1 as a normal interrupt timer. From the timer usage instructions, we can also understand the three functions of timer 2.
use:
1. Capture mode, to put it simply, is to detect the external pin jump time, record the value during the entire negative jump period, and then apply for an interrupt. We manually read the value to get the waveform width.
2. Automatic reload mode. Same as timer 01 mode 2, but this timer range can reach 2^16, and hardware reload is used here, so there is no delay effect. If we want to use it with strict requirements on accuracy, and are troubled by the fact that timer 01 can only be reloaded to 2^8=256, then this timer is indeed a very powerful choice.
3. Baud rate generator, this is the same as the baud rate generator of timer 1. Similarly, it also uses the automatic reload mode, but here it uses 16-bit automatic reload, which may be one of its advantages. The baud rate variation range is very wide.
About baud rate: baud rate is used to define the amount of data transmitted per second during serial communication, expressed in bps. For example, the baud rate we defined is 600, which means 600 binary bits are sent per second, and each character occupies ten binary bits, so we can send a total of 6 binary bits per second.
Regarding the baud rate selection: Generally speaking, we choose a lower baud rate for communication, because a high baud rate will produce a large error if it is in the 12MHZ crystal oscillator working mode. If it lasts for a long time, the data will not be able to match the clock and cause garbled communication. And because the high baud rate transmits a large amount of data, it is easy to cause serious interference when the line distance is extended, so it is still preferred to choose a low baud rate for communication.
About baud rate and timer 2: When timer 2 is used as a baud rate generator, it takes over the function of baud rate generator of timer 1 by setting the status bit. So at this time, we can set the timer to be used as a simple interrupt for our program. Then timer 2 is used as a special baud rate generator. At this time, we don't need to set the interrupt of timer 2, and after initialization, we don't need to care about the interrupt of timer 2. We only need to pay attention to the serial port interrupt, that is, the interrupt request generated at the moment when the communication level is pulled down. Similarly, the serial port interrupt is completely different from the timer 2 interrupt. Here we don't need to use the timer 2 interrupt. When timer 2 overflows, it will perform two functions, one is to generate a timer 2 interrupt, and the other is to send an overflow signal to the baud rate generator. When the generator receives this signal, it automatically performs clock calibration and generates a bps clock signal. So the baud rate generator comes from this. To put it simply, timer 2 is used to determine the waveform width of the clock signal. The higher the overflow rate, the denser the clock signal generated in the same time period. The data sent synchronizes the clock signal, and by the same token, more data can be sent.
From the above figure, we can see that at 600bps, the automatic reload value of T2 is FD8F.
From the timing diagram above, we can also clearly see that in the upper and lower computers, the clock frequency of the lower computer is specified by the program, while the clock frequency of the upper computer is set manually. We only need to know that the two clock frequencies must be completely consistent, otherwise data transmission and reception cannot be carried out normally.
(Send) When the send signal is pulled low, txd is also pulled low to indicate that data communication is to be carried out. At this time, the data sent is changed at each high level of the clock signal, and is stable when the clock signal level is stable. In this way, the data sent out will strictly have a fixed level wavelength and high level wavelength. When the host computer receives it, when it detects that the level of the receiving end is pulled low, it also synchronizes the clock signal, and synchronizes the high level of the clock signal to the first time the level of the data receiving end is pulled low, and then collects data during the low level of the clock signal. For example, the second low level of the clock signal indicates that the level state of the data end at this time indicates that the third bit of the data is waiting to be captured. Therefore, if the clock must be strictly consistent, this is the reason. When the whole process is completed, TI is set, indicating that the reception is completed, so we can keep monitoring the situation of TI. Similarly, remember to manually clear it when it is set.
(Receiving) Receiving is a little different. Although it is also carried out under clock synchronization, it captures the signal during the high level of shift. (The upper computer may also be the case. No more clear information has been found for the time being.) The conversion time of each bit is very short and the stabilization time is very long. Generally speaking, it captures the signal in the middle of the low level of the clock signal. When the whole process is completed, RI is set, indicating that the reception is completed, so we can keep monitoring the status of RI. Similarly, remember to manually clear it when it is set.
Let's verify the above theory through a small program:
1 #include 2 3 unsigned char flag, dat; 4 5 void Init(); 6 void SendData(); 7 void main() 8 { 9 //initialization 10 //infinite loop 11 Init(); 12 while (1)13 {14 if (flag==1)15 {16 SendData(); 17 }18 }19 }20 21 void Init()22 {23 //After all settings are completed, then enable interrupt24 25 //Timer 2 settings26 RCAP2L=0x8F;//load initial value27 RCAP2H=0xFD;28 T2CON=0x34;//Timer 2 settings29 //Serial port settings SCON30 SCON=0x50;//SM01=01;REN=131 //Turn on timer 2, serial port interrupt, es, serial port interrupt, et2, timer 232 RI=0x00;33 TI=0x00;34 IE=0x90;35 }36 37 void Uart_int() interrupt 4//external trigger38 {39 //disable interrupt40 //The timer is used to generate baud rate, so do not turn it off41 if (RI=1)//data is received at this time42 {43 RI=0;44 dat=SBUF;45 flag=1;46 }47 }48 49 void SendData()50 {51 ES=0;//disable serial port interrupt52 SBUF=dat;53 while (!TI);54 TI=0;55 ES=1;//enable serial port interrupt56 flag=0;57 }
About initialization: In initialization, the three things we need to do are to configure the timer interrupt, turn on or off the timer, and set the initial value.
You can see the following figure: The configuration is as follows, turn on the total interrupt EA, turn on the ES serial port interrupt, 0x90
The serial port interrupt mode is SM01=01, REn=1, which allows serial reception. If this is not enabled, there is no way to receive the data sent by the host computer.
Similarly, we need to initialize RI and TI at the beginning. Because I always make mistakes when I don’t initialize them at the beginning, it is safer to clear them to zero.
Timer 2 settings, RCAP is the 16-bit auto-reload value of the timer. T2CON = 0x34 is configured as follows:
TF2: Overflow clear
EXF2: External interrupt clear
RCLK, TCLK: seize the baud rate generator rights of timer 1.
EXEN2: External interrupt clear
TR2: Set timer starter
CT2: Select internal clock generator
CP/RL2: Whatever. We clear
void Init(){ //After all settings are completed, turn on the interrupt //Timer 2 setting RCAP2L=0x8F; //Load initial value RCAP2H=0xFD; T2CON=0x34; //Timer 2 setting //Serial port setting SCON SCON=0x50; //SM01=01; REN=1 //Turn on timer 2, serial port interrupt, es, serial port interrupt, et2, timer 2 RI=0x00; TI=0x00; IE=0x90;}
At this time we should set interrupt 4, that is, the interrupt processing sent by the host computer:
1 void Uart_int() interrupt 4 //External trigger 2 { 3 //The timer is used to generate the baud rate, so there is no need to turn it off 4 if (RI=1) //Data is received at this time 5 { 6 RI=0; 7 dat=SBUF; 8 flag=1; 9 } 10 }
First of all, because RI=1 only when the host computer has finished sending; at this time SBUF has stored the data sent by the host computer, we remove it and set the flag bit to indicate that the data has been received, please process it.
Handler:
1 void SendData()2 {3 ES=0;//disable serial port interrupt4 SBUF=dat;5 while (!TI);6 TI=0;7 ES=1;//enable serial port interrupt8 flag=0;9 }
The processing procedure is relatively simple, so I won’t go into details.
Finally, we use the serial communication assistant to check whether the data we sent can be received and returned.
The sending flow chart is as follows:
Summary: I personally feel that I only found out that my timer 1 was already used when I was halfway through writing the program, so I had no choice but to switch to other methods. Timer 2 is indeed very powerful, mainly because it is a relatively independent timer, which is very convenient when porting the program. Because you only need to modify the general interrupt, it is much more convenient. Therefore, if you need serial communication, timer 2 is the best choice.
Previous article:51 MCU timer and interrupt programming
Next article:Detailed explanation of MCS-51 interrupt-related registers, interrupt entry addresses and numbers
Recommended ReadingLatest update time:2024-11-16 11:46
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Ultra-small packaged IC chips VKD233DS and VKD233DR for wireless Bluetooth headsets
- PCB board level shielding design
- DSP Basics--Fixed-point Decimal Operations
- "Chat and Laugh" has opened another thread to discuss the issue of nuclear energy. I hope everyone can express their opinions.
- Types of EMI filter components and filters
- Thermostat and speed regulator circuit design for electronic equipment
- Use protues8.6 to simulate, SRF04 simulation fails, the code is correct,
- DM648 FVID_exchange failed
- Photoelectronic thermometer circuit diagram
- MicroPython - Python for microcontrollers