Documente Academic
Documente Profesional
Documente Cultură
Timers
Many microcontroller applications require the counting
of external events, such as the frequency of a pulse
train, or the generation of precise internal time delays
between computer actions. Both of these tasks can be
accomplished using software techniques, but software
loops for counting or timing keep the processor occupied
so that other, perhaps more important, functions are not
done.
To relieve the processor of this burden, two 16-bit
up counters, named T0 and T1, are provided for the
general use of the programmer.
Each counter may be programmed to count internal
clock pulses, acting as a timer, or programmed to count
external pulses as a counter.
Timers
The counters are divided into two 8-bit
registers called the timer low (TL0 ,TL1)
and high (TH0, TH1) bytes. All counter
action is controlled by bit states:
in the timer mode control register (TMOD),
the timer/counter control register (TCON) ,
and certain program instructions.
TCON
TCON
7
TF1
Bit
TR1
TF0
TR0
IE1
IT1
IE0
IT0
Symbol
Function
TF1
Timer 1 Overflow flag. Set when timer rolls from all ones to zero.
Cleared when processor vectors to execute interrupt service
routine located at program address 001Bh.
TR1
TF0
Timer 0 Overflow flag. Set when timer rolls from all ones to zero.
Cleared when processor vectors to execute interrupt service
routine located at program address 000Bh.
TR0
TCON
3
IE1
IT1
IE0
IT0
TMOD
TMOD
6
C/T
M1
M0
Gate
C/T
M1
M0
7
Gate
Timer 1
Bit
7/3
Timer 0
Symbol
Function
Gate
TMOD
6/2
5/1
4/0
C/T
M1
M0
Mod
M1
M0
Timer
12
C/T=0
Pin T1/0
TR1/0
Gate
Pin INT1/0
C/T=1
pulsuri intrare
Timer
If a counter is programmed to be a timer, it will count the
internal clock frequency of the oscillator divided by 12d.
As an example, if the crystal frequency is 6.0 megahertz,
then the timer clock will have a frequency of 500
kilohertz.
The resultant timer clock is gated to the timer by means
of the circuit.
In order for oscillator clock pulses to reach the timer, the
C I T bit in the TMOD register must be set to 0 (timer
operation). Bit TRX in the TCON register must be set to
1 (timer run), and the gate bit in the TMOD register must
be 0, or external pin INTX must be a 1.
In other words, the counter is configured as a timer, then
the timer pulses are gated to the counter by the run bit
and the gate bit or the external input bits INTX.
Timer Mode 0
Setting timer X mode bits to 00b in the TMOD register results in using
the THX register as an 8-bit counter and TLX as a 5-bit counter; the
pulse input is divided by 32d in TL so that TH counts the original
oscillator frequency reduced by a total 384d.
As an example:
the 6 megahertz oscillator frequency would result in a final frequency to
TH of 15625 hertz. The timer flag is set whenever THX goes from FFh
to 00h,or in .0164 seconds for a 6 megahertz crystal if THX starts at
00h.
Timer Mode 1
is similar to mode 0 except TLX is configured as a full 8-bit
counter when the mode bits are set to 01b in TMOD. The timer
flag would be set in .1311 seconds using a 6 megahertz crystal.
TLX
8 biti
THX
8biti
TFX
intrerupere
Timer Mode 2
Setting the mode bits to 10b in TMOD configures the timer to
use only the TLX counter as an 8-bit counter. THX is used to hold a
value that is loaded into TLX every time TLX overflows from FFh to 00h.
The timer flag is also set when TLX overflows.
This mode exhibits an auto-reload feature: TLX will count up from the
number in THX, overflow, and be initialized again with the contents of
THX. For example, placing 9Ch in THX will result in a delay of exactly
.0002 seconds before the overflow flag is set if a 6 megahertz crystal is
used
pulsuri intrare
TLX
8 biti
THX
8biti
TFX
intrerupere
Timer Mode 3
Timer Mode 3
pulsuri intrare
TL0
8 biti
f/12
TL0
8 biti
TR1
TF0
TF1
intrerupere
intrerupere
Counting
The only difference between counting and timing is the source of the clock
pulses to the counters. When used as a timer, the clock pulses are sourced
from the oscillator through the divide-by-12d circuit. When used as a counter,
pin T0 (P3.4) supplies pulses to counter 0, and pin T1 (P3.5) to counter 1.
The C/T bit in TMOD must be set to 1 to enable pulses from the TX pin to
reach the control circuit.
The input pulse on TX is sampled during P2 of state 5 every machine cycle. A
change on the input from high to low between samples will increment the
counter.
Each high and low state of the input pulse must thus be held constant
for at least one machine cycle to ensure reliable counting. Since this takes
24 pulses, the maximum input frequency that can be accurately counted is the
oscillator frequency divided by 24.
For our 6 megahertz crystal, the calculation yields a maximum external
frequency of 250 kilohertz.
Timer programming
Programarea n modul 1:
Modul 1 se caracterizeaz prin urmtoarele:
Este un timer pe 16 bii ceea ce inseamn c in regitrii TL respectiv
TH se poate ncrca orice numr cuprins ntre 0000 i FFFFh.
Dup ce regitrii sunt ncrcai cu valorile iniiale, timerul-ul poate fi
pornit. Acest lucru se realizeaz cu instruciunile : SETB TR0 pentru
timerul 0 respectiv SETB TR1 pentru timerul1.
Dup ce timerul este pornit acesta incepe s numere nainte pana
atinge valoarea FFFFh. Cnd trece de la valoarea FFFFh la
valoarea 0000h este setat bitul TF0 pentru timerul 0, respectiv TF1
pentru timerul1. Aceste flag-uri pot fi monitorizate. Avem
posibilitatea s oprim timerul cnd flag-ul de depire ia valoarea 1
(cu instruciuni de tipul CLR TR0 sau CLR TR1).
Procesul poate fi repetat prin ncrcarea registrilor TH i TL cu
valoarea iniial i TF adus la 0.
Programarea timerelor
Pentru a genera o ntrziere prin utilizarea timer-ului n
modul 1 trebuie parcuri urmtorii pai:
1. Se
ncrc
registrul
TMOD
cu
o
valoare
corespunztoare prin care se alege modul de
funcionare.
2. Se ncrc registrii TH, respectiv TL cu valoarea iniial.
3. Se pornete timerul;
4. Se monitorizeaz flag-ul de depire TF cu o
instruciune de tipul JNB TFx, target
5. Se oprete timerul;
6. Se terge flag-ul de depire;
7. Se merge napoi la pasul 2 i se rencrc regitrii TL i
TH cu valoarea corespunztoare.
Programarea timerelor
Exemplu
Urmtoarea secven de program genereaz un semnal
dreptunghiular cu un factor de umplere de 50% la pinul portului P1.3.
ntrzierea necesar este generat de timer-ul 0.
MOV TMOD,#01
; este selecionat modul 1 pentru timer-ul 0;
HERE: MOV TL0, #0F2h
; se ncrc valoarea iniial n registrul TL0;
MOV TH0, #0FFh ; se ncrc valoarea niial n registrul TH0;
CPL P1.3
; se complementeaz bitul P1.5;
ACALL DELAY
SJMP HERE
DELAY: SETB TR0
;se pornete timerul 0;
AGAIN: JNB TF0, AGAIN
; se monitorizeaz flag-ul de depire;
CLR TR0
CLR TF0
RET
Programarea n modul 2
Pentru programarea timerului n modul 2 se
parcurg urmtorii pai:
1.Se incrc registru TMOD cu o valoare
corespunztoare;
2.Se ncrc registrul TH cu valoarea iniial;
3.Se pornete timer-ul;
4.Se monitorizeaz flag-ul TF ;
5.e terge flag-ul TF;
6.Se merge napoi la pasul 4.
Programarea n modul 2
Exemplu
MOV TMOD, #20h
MOV TH1, #5
SETB TR1
Back: JNB TF1, Back
CPL P1.0
CLR TF1
SJMP Back
Exemplu
Se presupune c impusurile vin la pinul T1, timerul(numratorul 1 este
programat n modul 2. Starea registrului TL1 este livrat la pinii portului P2);
MOV TMOD, #01100000B
; numratorul 1 n modul 2, numra
; pulsurile externe
MOV TH1, 0
SETB P3.5
; Programeaz T1 ca intrare
AGAIN: SETB TR1
; pornete numrtorul
BACK: MOV A, TL1
; copiaz registrul TL1 n
;acumulator
MOV P2, A
; copiaz TL1 (via A) n latch-ul
;portului P2
JNB TF1, BACK
; monitorizeaz starea flag-ului TF1
CLR TR1
; oprete numrtorul;
CLR TF1
; terge flag-ul TF1
SJMP AGAIN
Programming in C
The C program toggles all the bits of port P1 continuously with some delay in
between. Use Timer 0, 16-bit mode to generate the delay.
#include <reg51.h>
void T0Delay(void);
void main(void){
while (1) {
P1=0x55;
T0Delay();
P1=0xAA;
T0Delay();
}
}
void T0Delay(){
TMOD=0x01;
TL0=0x00;
TH0=0x35;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
1/2500 Hz = 400 s
400 s /2 = 200 s
200 s / 1.085 s = 184
Assume that a 1-Hz external clock is being fed into pin T1 (P3.5).
Write a C program for counter 1 in mode 2 (8-bit auto reload) to count
up and display the state of the TL1 count on P1. Start the count at 0H.
Solution:
#include <reg51.h>
sbit Ti1=P3^5;
void main(void){
Ti1=1;
TMOD=0x60;
TH1=0;
while (1) {
do {
TR1=1;
P1=TL1;
}
while (TF1==0);
TR1=0;
TF1=0;
}
}