Application and error correction of microcontroller timer

Publisher:EnchantedDreamLatest update time:2006-12-11 Source: 电源技术应用Keywords:MCS-51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1 Introduction
The timer is a very important part of the MCS-51 microcontroller. Since its application is related to other hardware of the microcontroller, there is a certain complexity [1]. The timer is the most effective method to solve certain types of complex problems in microcontroller applications, and it is widely used. With the improvement of timing requirements, the errors caused in the timing processing process need to be corrected [2]. This article discusses the use of MCS-51 microcontroller and error correction methods, and provides general algorithms and programs. 2. Timer working mode and mode setting MCS-51 microcontroller has two independent timers. Each timer has 4 working modes (0~3). Mode 0 is a 13-bit counting structure. The counter consists of all 8 bits of TH and It consists of the lower 5 bits of TL; Mode 1 is a 16-bit counting structure, and the counter is composed of all 8 bits of TH and TL, a total of 16 bits; Mode 2 is an 8-bit counting structure, and the counter is composed of 8 bits of TL. Different from other modes, When the timer overflows, the hardware automatically loads the value of TH into TL, and has an automatic loading function. In the first three working modes, the setting and use of the two timers are exactly the same, but in working mode 3, the two timers are very different. Only the timer 0 can work in mode 3, and the timer 1 can only work in mode 3. It can work in modes 0 to 2. In working mode 3, the timer 0 is split into two independent 8-bit counters TL0 and TH1. The control bits and pin signals of the timer 0 are all assigned to TL0. Therefore, TH0 can only be used as a simple timer, and its control bit occupies the control bit of timer 1. If timer 0 works in mode 3, timer 1 usually gives up its control bit. As a serial port baud rate generator [3]. The selection of the timer's working mode mainly depends on setting the value of the TMOD register. Its bits are defined as follows:  



  

  


order
B7
B6
B5
B4
B3
B2
B1
B0
bit sign
GATE
C/T
M1
M0
GATE
C/T
M1
M0

Among them, B0~B3 are used to control timer 0, and B4~B7 are used to control timer 1.
  
The GATE bit is a gate control bit, which mainly determines how the timer is started. If GATE=0, use the TR0 (TR1) bit to start/stop the timer. If GATE=1, use an external interrupt request signal (INT0 or INT1) to start the timer. device.
  
The C/T bit is the timing and counting selection bit. C/T=0 is the timing mode, and C/T=1 is the counting mode.
  M0 and M1 are the working mode selection bits.
  M1M0=00 mode 0M1M0=10 mode 2
  M1M0=01 mode 1M1M0=11 mode 3
  
selects the timer working mode. The corresponding number should be written to the register TMOD according to the above regulations, such as setting 0 channel timing. If the timer works in timing mode 0 and the 1-channel timer works in mode 1, the following instructions should be used to set it:
  
MOVTMOD, #10H

3 Timer processing method and calculation of initial counting value

  
The timer of the MCS-51 microcontroller uses increment counting method, in the timer working mode, after starting the timer, the CPU hardware will automatically add 1 to the corresponding counter every machine cycle until it exceeds the maximum value that the corresponding counter can represent, a timer overflow occurs, and the hardware automatically sets TF0 (TF1) , the user can determine whether the timing overflows by querying the TF0 (TF1) bit. If the timing interrupt is open at this time, the corresponding timing interrupt will be triggered. MCS-51 responds to the interrupt according to the corresponding interrupt address (the interrupt entry address of the 0 timing interrupt is 000B, channel 1 is 001B) execute the interrupt handler.
  
There are two basic ways of timing programming. One way is the query mode, which turns off the corresponding timed interrupt when starting the timer, and then cycles through TF0 (TF1) for corresponding processing; the other way is the interrupt mode, which opens the corresponding timer interrupt when the timer is started. , and write the timer overflow handler in the form of an interrupt handler and transfer to the timer overflow handler by placing a jump instruction at the corresponding interrupt entry address (000B, 001B).
  
After the timer working mode is selected, the timer overflow time only depends on the initial value of the counter. How to determine the initial value of the counter according to the application needs is also an important issue in timer application. Assume that the machine cycle of the microcontroller is K0, the selected timing mode counter is n bits, and the realized timing time length is T, then:
  
    (2n-x) K0=T(1)
  
  where x is the initial value of the counter, according to Formula 1 can calculate the initial value x as:

    x=2n-(T/K0)(2)

4 Timer programming steps and general procedures for each step
  
  Timer application programming is mainly divided into the following steps. 4.1 Step 1   Calculate   the initial

counting value
   The machine cycle k0=2μs or 1μs, then determine the number of counter digits n according to the selected working mode, substitute the obtained k0 and n into equation 2, find the initial counting value x, and divide the value of x into TH0 (TH1) and TL0 (TL1). 4.2 Step 2: Write the initialization program. The initialization program is related to the timing programming mode. The interrupt mode initialization program is as follows (taking the 0-channel timer as an example):   movTMOD, #n (specifies the timing working mode)   movTH0, #n1 (sets the low bit of the counter)   movTL1, #n2 (Set the counter high bit)   setBEA (Open interrupt)   setBET0 (Open timed interrupt 0)   setBTR0 (Start timing) The   query mode initialization program is as follows:   movTMOD, #n (Specify the timing working mode)   movTH0, #n1 (Set the counter low bit)   movTL1 , #n2 (set the high bit of the counter)   clrET0 (open timed interrupt 0)   setBTR0 (start timer)   where #n1 and #n2 are related to the initial counting value x. If mode 2 is used, #n1=#n2, all initialization procedures are Take channel 0 timer as an example. If you use channel 1 timer, change the subscript 1 of the register and control bit in the program to 0. 4.3 Step 3: Prepare the timing processing program.  The interrupt mode processing program is as follows:   org000B   1jmpb1   b1:clrEA   movTH0, #n1   movTH1, #n2   timing processing statement segment   reti   . Among them, the 0-way timer is 000B, if it is 1-way timer, it is 001B, if When the timer works in mode 2, there is no need to reassign TH0 and TH1.   The query processing program is as follows:   L1:jbcTF0,LO1   sjmpL1   LO1:movTH0,#n1   movTH1,#n2   Timing processing statement segment   sjmpL1 5 Timing error calibration Since the machine cycle of the microcontroller is 1μs~2μs, the timing error should generally be 0μs~20μs Within, for general applications, this error can be ignored, but for applications with higher accuracy requirements, this error must be corrected. The timing error is the time it takes to transfer to the execution of the timing processing statement segment after the timing overflow. This time is mainly caused by the instructions or hardware processes that must be executed before the timing overflow transfers to the timing processing statement segment. The simple principle of timing error calibration is: after the timing overflow response, stop the timer counting, read the current count value (which reflects the delay time of the timing response), and then take into account the execution time of the program segment to complete this task, as The correction factor corrects the timing initial value. The following program segment takes the interrupt processing method as an example:   clrEA   clrTR1   clrc   movA,#n0   clrTR0   subbA,TL0   subbA,#08   movTL0,A   movA,#n1   subbA,TH0   movTH0,A   setbTR0   timing processing statement segment   reti



  



  








  










  


















  

  















  
Since it takes 8 machine cycles to execute instructions from instruction clrTR0 (stop counting) to instruction setbTR0 (restart counting), this consumption should be taken into account. The above program reduces the timing error to within 1 machine cycle.
Keywords:MCS-51 Reference address:Application and error correction of microcontroller timer

Previous article:Design of time/displacement commutation controller based on microcontroller AT89C51
Next article:Application and error correction of microcontroller timer

Recommended ReadingLatest update time:2024-11-16 22:19

Timing watering circuit designed and made by single chip microcomputer
This timed watering device composed of the MCS-51 series single-chip microcomputer can water the flowers and plants on a regular basis, solving your worries when you go out. 1. Functions to be completed by the circuit The basic requirement for the circuit is to complete daily timed watering, taking into acc
[Microcontroller]
Notes on assembly operators based on the MCS-51 kernel
Data transfer instructions: MOV accesses internal data storage MOVX accesses external data storage MOVC accesses ROM storage XCH Whole byte exchange XCHD Nibble swap (lower four bits) PUSH Set the contents pointed to by the stack pointer register SP (push the word onto the stack) POP pops the word out of the stack; SW
[Microcontroller]
Design of data collection and information analysis system based on LabVIEW and MCS-51 microcontroller
1. LabVIEW part design 1.1VISA Introduction LabVIEW provides a powerful VISA library. VISA (Virtual Instrument Software Architecture) - virtual instrument software specification, is the general name for the standard I/O function library and related specifications used for instrument programming. The VISA library resid
[Test Measurement]
Design of data collection and information analysis system based on LabVIEW and MCS-51 microcontroller
Electrical Control Circuit Wiring Fault Diagnosis System Based on MCS-51 Single Chip Microcomputer
0 Introduction In the teaching of electrical control courses, regular training projects mainly include the installation, wiring and debugging of three-phase asynchronous motor inching control circuit, continuous operation control circuit, forward and reverse control circuit, forward start and reverse stop c
[Microcontroller]
Electrical Control Circuit Wiring Fault Diagnosis System Based on MCS-51 Single Chip Microcomputer
Interrupt sources and interrupt vectors of mcs-51 microcontroller
1. External interrupt 0, 1; caused by the level signals of pins /INT0, /INT1 respectively.  2. Timer/ Counter 0, 1; caused by the overflow of T0, T1 respectively.  3. Serial port sending, receiving; caused by sending a byte or receiving a byte.  There are 5 interrupt sources in total.  4. Timer/Counter 2; caused b
[Microcontroller]
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号