Application of 51 MCU timer mode 2 and baud rate

Publisher:星光闪耀Latest update time:2020-02-04 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Application of 51 MCU timer mode 2 and baud rate

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.

[1] [2]
Reference address:Application of 51 MCU timer mode 2 and baud rate

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

Comparison of 51 MCU C and assembly instructions
1. Single instruction comparison: Byte Operations   Addition instruction Subtraction Instructions Multiplication Instructions Division Instructions Modulo instruction Bit Test Instructions _testbit_ (bit); Left shift instruction Variable = Variable 1; Right shift instruction Variable = Variabl
[Microcontroller]
Implementation of I2C bus protocol for 51 single chip microcomputer
//-----------------------Function declaration, variable definition------------------------------------------------------- #include reg51.h #include intrins.h sbit SDA=P1^0; // Make p1.0 port analog data port sbit SCL=P1^1; // Set the P1.1 port to the analog clock port #define NUM 10 // Depth of receive and sen
[Microcontroller]
C51 program for simulating RS232 serial communication with microcontroller IO port
This program has been used in one of my projects and is very stable. During the writing process, I referred to    the article http://www.51hei.com/mcu/1541.html on the 51hei website. Some subroutines are excerpted here. #include "reg52.h" #include "intrins.h"   #include "math.h"      #include "stdio.h" sbit BT_SND
[Microcontroller]
Based on 51 single chip microcomputer + LCD1602 display
LCD1602 Introduction LCD1602 related pins LCD1602 write command, write data related operation diagram According to the above timing diagram and timing parameter diagram, we can write the write command code and write data code Write command code Write Command void write_com(char command) { RS = 0;  RW = 0;
[Microcontroller]
Based on 51 single chip microcomputer + LCD1602 display
Interrupt control based on 8051 microcontroller
(1) An interrupt request is raised by the interrupt source, and the interrupt control enable control determines whether to respond to the interrupt. If the interrupt is allowed to be responded to, the CPU responds to the interrupt in the order of the set priority. If it is an interrupt of the same priority, respond to
[Microcontroller]
Design of Decimal Calculator System Based on AT89C51 Single Chip Microcomputer
This design is a decimal calculator system design based on the AT89C51 single-chip microcomputer. It can complete the keyboard input of the calculator, perform simple arithmetic operations of addition, subtraction, multiplication and division of 4-bit unsigned numbers, and display the results accordingly on the LED. I
[Microcontroller]
Design of Decimal Calculator System Based on AT89C51 Single Chip Microcomputer
【51 MCU】Timer/Counter
Changelog time Modifications 023-03-13 First draft completed TMOD is not bit-addressable, and can only be configured as a whole register. TCON is bit-addressable. It is used to control the start and stop of the timer, and to identify the overflow and
[Microcontroller]
【51 MCU】Timer/Counter
Doppler blood flow meter system based on 51 single chip microcomputer
Laser Doppler blood flowmeter is an instrument used to measure the blood flow perfusion flow and microcirculation state of tissues and micro-areas in microcirculation research. Using Doppler mobile component to measure blood cell flow is a new method of non-destructive, rapid, continuous and direct measurement of tiss
[Microcontroller]
Doppler blood flow meter system based on 51 single chip microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号