Vous êtes sur la page 1sur 18

8051

Timers
Timers/Event Counters
Timer is a counter register that can be loaded
with a number.
Two types of counters 8-bit and 16-bit are
commonly used.
Once started, it changes the count on every
clock.
At the final count it sets a flag or generates an
interrupt.
Types of Counters
Up Counter Counts Up until it reaches the final
count FFFFH (or FFH in a 8-bit counter) and
rolls over to 0000 (or 00)

Down Count: Counts down until it reaches 0000

Continuous Counter: Counts continuously can
not be stopped or started
8051 Timers
8051 has two timers
Timer0 (T0) and Timer1 (T1)
They can operate as 16-bit or 8-bit timers
Multiple modes: TMOD Register (or SFR)
Controlled by: TCON Register
Clock Source: System Clock/12
SFR Name Description SFR Address
TH0
Timer 0 High
Byte
8Ch
TL0
Timer 0 Low
Byte
8Ah
TH1
Timer 1 High
Byte
8Dh
TL1
Timer 1 Low
Byte
8Bh
TCON Timer Control 88h
TMOD Timer Mode 89h
8051: Timer Registers
Timer0 and Timer1


Bit Name Explanation of Function Timer
7 GATE1
When this bit is set the timer will only run when
INT1 (P3.3) is high. When this bit is clear the timer
will run regardless of the state of INT1.
1
6 C/T1
When this bit is set the timer will count events on T1
(P3.5). When this bit is clear the timer will be
incremented every machine cycle.
1
5
T1M1

Timer1 mode bit (see below)

1
4 T1M0 Timer1 mode bit (see below) 1
3 GATE0
When this bit is set the timer will only run when
INT0 (P3.2) is high. When this bit is clear the timer
will run regardless of the state of INT0.
0
2 C/T0
When this bit is set the timer will count events on T0
(P3.4). When this bit is clear the timer will be
incremented every machine cycle.
0
1 T0M1 Timer0 mode bit (see below) 0
0 T0M0 Timer0 mode bit (see below) 0
TMOD (89h) SFR
TxM1 TxM0 Timer Mode
Description of
Mode
0 0 0 13-bit Timer.
0 1 1 16-bit Timer
1 0 2 8-bit auto-reload
1 1 3 Split timer mode
Timer Modes:
Bit Name Bit Address Explanaation Timer
7
TF1 8Fh
Timer 1 Overflow. This
bit is set by the
microcontroller when
Timer 1 overflows.
1
6
TR1 8Eh
Timer 1 Run. When this
bit is set Timer 1 is
turned on. When this bit
is clear Timer 1 is off.
1
5
TF0 8Dh
Timer 0 Overflow. This
bit is set by the
microcontroller when
Timer 0 overflows.
0
4
TR0 8Ch
Timer 0 Run. When this
bit is set Timer 0 is
turned on. When this bit
is clear Timer 0 is off.
0
TCON (88h) SFR: Timer Control

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
TF1
Timer1
Flag
TR1
Timer1
On/Off
TF0
Timer0
Flag
TR0
Timer0
On/Off
TCON Timer Control

Instructions for Timer Control Bits
Timer0:
SETB TR0 Or SETB TCON.4
CLR TR0 Or CLR TCON.4
SETB TF0 Or SETB TCON.5
CLR TF0 Or CLR TCON.5

Timer1: TR1 -Bit6 and TF1 Bit7
Steps in Setting Up A Timer:
1. Select a timer and mode: TMOD SFR
2. Load a count: Low Count and High Count in
Selected Timer (TR0 or TR1)
3. Start Counter: TCON Bit TR0 and TR1
4. Monitor Flag: TCON Bit TF1 and TF0
5. Stop Timer: TCON = Bit TR0 and TR1
6. Clear Flag: TCON = Bit TF1 and TF0
7. Go Back
Programming in C
#include <reg51.h>
void Delay (void);
sbit P1Bit5 = P1^5;
void main (void)
{
while (1)
{ P1Bit5 = ~ P1Bit5;
Delay ( );
}
}
void Delay (void)
{
TMOD = 0 x 01; //Timer0 Mode 1

TL0 = 0 x 18; //Load Timer0 Low
TH0 = 0 x FC; //Load Timer0 High

TR0 = 1; // Turn on Timer0

while (TF0 ==0); //Wait here until TF0 is set

TR0 = 0; //Turn off Timer0
TF0 = 0; //Reset flag
}
Timer as Event Counter
To set up Timer0 and Timer1 as counters the
following steps are necessary:
In TMOD Register set:
Bit6 C/T = 1 for Timer1 T1= P3.5 is set up as input
Bit2 C/T = 1 for Timer0 T0=P3.4 is set up as input

P3.5 and P3.4 provides external event pulses