Example of calculation and counting program of timer/counter initialization value

Publisher:极地征服者Latest update time:2017-10-12 Source: eefocusKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Since the function of the timer/ counter is determined by software programming, it is usually necessary to initialize the timer/counter before using it so that it works according to the set function. The steps for initialization are generally as follows:

1. Determine the working mode (i.e. assign a value to TMOD);
2. Preset the initial value of timing or counting (the initial value can be directly written into TH0, TL0 or TH1, TL1);
3. Enable the interrupt of the timer/counter as needed (directly assign a value to the IE bit);
4. Start the timer/counter (if it is specified to be started by software, TR0 or TR1 can be set to "1"; if it is specified to be started by the external interrupt pin level, the start level needs to be added to the external pin. When the startup requirements are met, the timer starts counting or timing according to the specified working mode and initial value). 

The following is a specific method for determining the initial value of the timer/counter.
Because the number of counter bits is different in different working modes, the maximum count value is also different.


Now assume that the maximum count value is M, then the maximum value M under each mode is as follows:
Mode 0: M = 213 = 8 192


Method 1: M = 216 = 65 536
Method 2: M = 28 = 256 
Method 3: Timer 0 is divided into two 8-bit counters, so both M are 256. 


Because the timer/counter counts by "1" and generates an interrupt when the count overflows, the initial value X can be calculated as follows:
X = M - count value


The following example illustrates how to determine the initial value.
Example 1: Select T1 mode 0 for timing, and the output period of P1.1 is 1ms square wave, and the crystal oscillator fosc=6MHz.
Solution: According to the question, as long as P1.1 is inverted every 500us, a 1ms square wave can be obtained, so the timing time of T1 is 500us. Since the timing time is not long, mode 0 can be used. Then M1 M0=0; because it is a timer mode, C/T=0; T1 is started by software here, so GATE=0. T0 is not used, and the mode word can be set arbitrarily, as long as it does not enter mode 3, it is generally taken as 0, so TMOD=00H. After the system is reset, TMOD is 0, and TMOD does not need to be cleared to 0 again. Content from the home of single-chip microcomputers www.dpj100.com 
The following is the calculation of the initial value of the 500us timing T1:
machine cycle T=12/fosc=12/(6×106)Hz=2μs



Assume the initial value is X, then:
(1013-X)×2×10-6s=500×10-6s 
X=7942D=1111100000110B=1F06H

Because when used as a 13-bit counter, the upper 3 bits of TL1 are not used and should be filled with 0, and TH1 occupies the upper 8 bits, so the actual filling of X should be: 

X=111100000000110B=F806H


Result: TH1=F8H, TL1=06H 

The source program is as follows: 

ORG 2000H    

MOV TL1,#06H ; Initialize TL1

MOV TH1,#0F8H ; Initialize TH1 

SETB TR1 ; Start T1

LP1:JBC TF1,LP2; Check whether the count overflows?


AJMP LP1


LP2:MOV TL1,#06H ; Reset the initial value of the count 

MOV TH1,#0F8H


CPL P1.1 ; Output inversion 

AJMP LP1 ; Repeat cycle


Keywords:Timer Reference address:Example of calculation and counting program of timer/counter initialization value

Previous article:Principle of timing counter Calculation of counting period
Next article:MCS51 single chip counter working mode 0 timing time calculation and programming example

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

PIC microcontroller TIMER0 realizes the automatic control design of running water lamps
#include __CONFIG(0x3B31); unsignedcharnum=0,i=0; voidinit() { TRISB=0x00; T RISC =0x00; PORTB=0xFE; PORTC=0xFE; } voidT MR0_init () { OP TI ON=0x07;//0 has pull-up resistor 00001111:256 frequency division GIE=1; PEIE=1; T0IF=0; TMR0=250; T0IE=1; } voidinte rrupt TI MER0 () { T0IF=0; if(num==5) { num=0; PORTB=(POR
[Microcontroller]
PIC microcontroller TIMER0 realizes the automatic control design of running water lamps
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号