Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

8051 Timer and Counter

Timer / Counter
• Two 16 bit timer / counter
• Timer is used to generate time delay
• Counter is used to count the event happening
outside the MCU.
• Each 16 bit register can be accused as two
separate 8 bit register
• Low byte(8bit) & High byte(8 bit)=16 bit
Low byte & High byte

• Low byte register - TL0


• High byte register - TH0
• These TL0,TH0 can be accessed as other
registers.
• Ex:
– MOV TL0,#24h
– MOV R1,TH1

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

TH0 TL0
TMOD Register ( Byte accessible )
Timer -1 Timer -0
gate C/T M1 M0 gate C/T M1 M0

7 6 5 4 3 2 1 0

• TMOD is an 8 bit register


• Lower 4 bits for Timer -0
• Higher 4 bits for Timer -1
TMOD – Timer -1
1. Gate [Bit - 7]
 gate = 1 Timer 1 will run when INT1 (P3.3) is high
 gate = 0 Timer 1 will run regardless of the state of
INT1 pin.
2. C / T [Bit - 6]
 C/T =1, counter mode (counter -1)
 Timer 1 will count event on T1(P3.5)
 C/T = 0 , Timer mode
 Timer -1 will increment every machine cycle
Cont..,
3. M1 & M0 [Bit 5 & 4]
M1 M0
0 0
• Mode 0 – 13 bit timer/counter
0 1
• Mode 1 - 16 bit timer/counter
1 0
• Mode 2 - 8 bit timer/counter
1 1
• Mode 3 - Split timer mode
TMOD – Timer -0
1. Gate [Bit - 3]
 gate = 1 Timer 0 will run when INT0 (P3.2) is high
 gate = 0 Timer 0 will run regardless of the state of
INT0 pin.
2. C / T [Bit - 2]
 C/T =1, counter mode
 Timer 0 will count event on T1(P3.4)
 C/T = 0 , Timer mode
 Timer -0 will increment every machine cycle
Cont..,
3. M1 & M0 [Bit 1 & 0]
M1 M0
0 0
• Mode 0 – 13 bit timer/counter
0 1
• Mode 1 - 16 bit timer/counter
1 0
• Mode 2 - 8 bit timer/counter
1 1
• Mode 3 - Split timer mode
Example on Timer mode
• 16 bit timer-0 with internal control
0 0 0 0 0 0 0 1 TMOD= 01h

• 8 bit timer -1 with internal control


0 0 1 0 0 0 0 0 TMOD= 20h

• 16 bit counter with external control event


@P3.4
0 0 0 0 1 1 0 1 TMOD= 0Dh
TCON
• 8 bit timer control register( bit accessible )
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer Interrupt

• 0 1 0 0 0 0 0 0
• Setb TR1 ; TR1=1
• TF1 & TF0 ( timer flags)
– When timer rolls from all 1’s to 0’s TF1=TF0=1

– Then timer overflow flag will be cleared TF1=TF0=0


Cont..,
• TR1 & TR0
– TR1=TR0=1
– It will enable the corresponding timer to start
function
– TR1=TR0=0
– It will hault the timer doesn’t reset timer.

You might also like