0% found this document useful (0 votes)
16 views31 pages

Timers and Counters Class1 1

The document discusses the operation and configuration of timers in 8051-based systems, detailing clock frequencies, timer modes, and registers. It provides calculations for timer frequencies and periods based on different crystal frequencies, as well as programming examples for setting up timers and generating delays. Additionally, it explains the functionality of timer/counter registers and the control mechanisms for starting and stopping timers.

Uploaded by

onionharisforcr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views31 pages

Timers and Counters Class1 1

The document discusses the operation and configuration of timers in 8051-based systems, detailing clock frequencies, timer modes, and registers. It provides calculations for timer frequencies and periods based on different crystal frequencies, as well as programming examples for setting up timers and generating delays. Additionally, it explains the functionality of timer/counter registers and the control mechanisms for starting and stopping timers.

Uploaded by

onionharisforcr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Find the timer’s clock frequency and its period for various 8051-

based systems, with the following crystal frequencies.


(a) 12 MHz (b) 16 MHz (c) 11.0592 MHz
Solution:

XTAL
oscillator
÷ 12

(a) 1/12 × 12 MHz = 1 MHz and T = 1/1 MHz = 1 s


(b) 1/12 × 16 MHz = 1.333 MHz and
T = 1/1.333 MHz = 0.75 s
(c) 1/12 × 11.0592 MHz = 921.6 KHz;
T = 1/921.6 KHz = 1.085 s 1
Timers /Counters

• The 8051 has 2 timers/counters: timer/counter 0 and


timer/counter 1. They can be used as
1. The timer is used as a time delay generator.
– The clock source is the internal crystal frequency of the 8051.
2. An event counter.
– External input from input pin to count the number of events
on registers.
– These clock pulses could represent the number of people
passing through an entrance, or the number of wheel rotations,
or any other event that can be converted to pulses.

2
Timer

• 8051 timers use 1/12 of XTAL frequency as the


input of timers, regardless of machine cycle.
• Because the input of timer is a regular, fixed-
periodic square wave, we can count the number of
pulses and calculate the time delay.
8051
XTAL
÷ 12 Timer
oscillator
P1 to
LCD
TH0
Set
TL0
Timer 0 3
Counter

• Count the number of events


• External input from Tx input pin (x=0 or 1).
• We use Tx to denote T0 or T1.
– External input from T0 input pin (P3.4) for Counter 0
– External input from T1 input pin (P3.5) for Counter 1
8051
TH0
P1 to
TL0
LCD
Vcc P3.4
a switch T0
4
Timer/Counter 0

timer input

XTAL
÷ 12
oscillator
C/T = 0

TH0 TL0
C/T = 1
counter input
T0 Pin
1:start
Pin 3.4
TR0 TF0
0:stop

Gate 1. monitor by JNB


2. interrupt
INT0 Pin
Pin 3.2
hardware control

5
Timer/Counter 1

timer input

XTAL
÷ 12
oscillator
C/T = 0

TH1 TL1
C/T = 1
counter input
T1 Pin
Pin 3.5 1:start
TR1 TF1
0:stop

Gate 1. monitor by JNB

INT1 Pin 2. interrupt


Pin 3.3
hardware control

6
Registers Used in Timer/Counter

• TH0, TL0 (Timer 0 registers)


• TH1, TL1 (Timer 1 registers)
• TMOD (Timer mode register)
• TCON (Timer control register)

7
Basic Registers of the Timer

• Both Timer 0 and Timer 1 are 16 bits wide.


– Each 16-bit timer can be accessed as two separate registers
of low byte and high byte.
– Timer 0: TH0 & TL0
• Timer 0 high byte, timer 0 low byte
– Timer 1: TH1 & TL1
• Timer 1 high byte, timer 1 low byte
– These registers stores
• the time delay as a timer
• the number of events as a counter

8
Timer Registers

TH0 TL0

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

Timer 0

TH1 TL1

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

Timer 1
9
TCON Register (1/2)

• Timer control register: TCON


– Upper nibble for timer/counter, lower nibble for interrupts
• TR (run control bit)
– TR0 for Timer/counter 0; TR1 for Timer/counter 1.
– TRx is set by programmer to turn timer/counter on/off.
• TRx=0: off (stop)
• TRx=1: on (start)

(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
10
TCON Register (2/2)

• TF (timer flag, control flag)


– TF0 for timer/counter 0; TF1 for timer/counter 1.
– TFx is like a carry. Originally, TFx=0. When TH-TL roll
over to 0000 from FFFFH, the TFx is set to 1.
• TFx=0 : not reach
• TFx=1: reach
• If we enable interrupt, TFx=1 will trigger ISR.

(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
11
Equivalent Instructions for the Timer
Control Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4

SETB TF0 = SETB TCON.5


CLR TF0 = CLR TCON.5
For timer 1
SETB TR1 = SETB TCON.6
CLR TR1 = CLR TCON.6

SETB TF1 = SETB TCON.7


CLR TF1 = CLR TCON.7

TCON: Timer/Counter Control Register


TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 12
TMOD Register

• Timer mode register: TMOD


MOV TMOD,#21H
– An 8-bit register
– Set the usage mode for two timers
• Set lower 4 bits for Timer 0 (Set to 0000 if not used)
• Set upper 4 bits for Timer 1 (Set to 0000 if not used)
– Not bit-addressable

(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
13
TMOD Register

GATE Gating control when set. Timer/counter is enabled only


while the INTx pin is high and the TRx control pin is set.
When cleared, the timer is enabled whenever the TRx
control bit is set.
C/T Timer or counter selected cleared for timer operation
(input from internal system clock). Set for counter
operation (input from Tx input pin).
M1 Mode bit 1
M0 Mode bit 0
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0 14
C/T (Clock/Timer)

• This bit is used to decide whether the timer is used as


a delay generator or an event counter.
• C/T = 0 : timer
• C/T = 1 : counter

15
Gate
• Every timer has a mean of starting and stopping.
– GATE=0
• Internal control
• The start and stop of the timer are controlled by software.
• Set/clear the TR0 (or TR1) for start/stop timer.
– GATE=1
• External control
• The hardware way of starting and stopping the timer by software
and an external source.
• Timer/counter is enabled only while the INT0 (or INT1) pin has an
1 to 0 transition and the TR0 (or TR1) control pin is set.
• INT0: P3.2, pin 12; INT1: P3.3, pin 13.

16
M1, M0

• M0 and M1 select the timer mode for timers 0 & 1.

M1 M0 Mode Operating Mode


0 0 0 13-bit timer mode
8-bit THx + 5-bit TLx (x= 0 or 1)
0 1 1 16-bit timer mode
8-bit THx + 8-bit TLx (x= 0 or 1)
1 0 2 8-bit auto reload
8-bit auto reload timer/counter;
THx holds a value which is to be reloaded into
TLx each time it overflows.
17
1 1 3 Split timer mode
Example 1
Indicate which mode and which timer are selected for each of the
following.
(a) MOV TMOD,#01H (b) MOV TMOD,#20H
(c) MOV TMOD,#12H
Solution: (MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0

timer 1 timer 0

(a) TMOD = 00000001, mode 1 of timer 0 is selected.


(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010
mode 2 of timer 0, and mode 1 of timer 1 are selected. 18
Example 2

Find the value for TMOD if we want to program timer 0 in mode 2,


use 8051 XTAL for the clock source, and use instructions to start
and stop the timer.
(MSB) (LSB)
Solution: GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0

TMOD= 0000 0010 Timer 1 is not used.


Timer 0, mode 2,
C/T = 0 to use XTAL clock source (timer)
gate = 0 to use internal (software)

start and stop method. 19


Timer Mode 1
In following, timer 0 is used as an example.
• 16-bit timer (TH0 and TL0)
• TH0-TL0 is incremented continuously when TR0 is set
to 1. And the 8051 stops to increment TH0-TL0 when
TR0 is cleared.
• The timer works with the internal system clock. In other
words, the timer counts up every 12 clocks from XTAL.
• When the timer (TH0-TL0) reaches its maximum of
FFFFH, it rolls over to 0000, and TF0 is raised.
• Programmer should check TF0 and stop the timer 0.

20
Mode 1 Programming

XTAL
oscillator ÷ 12 like MC for
89C51

C/T = 0

TH TL TF

Start timer
TF goes high overflow
TR when FFFF 0 flag
21
Steps of Mode 1 (1/3)

1. Chose mode 1 timer 0


– MOV TMOD,#01H
2. Set the original value to TH0 and TL0.
– MOV TH0,#0FFH
– MOV TL0,#0FCH
3. You had better to clear the flag to monitor: TF0=0.
– CLR TF0
4. Start the timer.
– SETB TR0

22
Steps of Mode 1 (2/3)
5. The 8051 starts to count up by incrementing the TH0-
TL0.
– TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H

TR0=1 TR0=0
Start timer TH0 TL0
Stop timer

FFFC FFFD FFFE FFFF 0000


Roll over

TF0=0 TF0=0 TF0=0 TF0=0 TF0=1

TF0 Monitor TF01 until TF0=1


23
Steps of Mode 1 (3/3)

6. When TH0-TL0 rolls over from FFFFH to 0000, the


8051 set TF0=1.
– TH0-TL0= FFFEH, FFFFH, 0000H (Now TF0=1)
7. Keep monitoring the timer flag (TF) to see if it is
raised.
– AGAIN: JNB TF0, AGAIN
8. Clear TR0 to stop the process.
– CLR TR0
9. Clear the TF flag for the next round.
– CLR TF0
24
Initial Count Values

• The initial count value = FFFC.


• The number of counts = FFFFH-FFFCH+1 = 4
– we add one to 3 because of the extra clock needed when it
rolls over from FFFF to 0 and raises the TF flag.
• The delay = 4 MCs for 89C51
• If MC=1.085 s, then the delay = 4.34 s

25
Timer Delay Calculation for XTAL =
11.0592 MHz

(a) in hex (b) in decimal

(FFFF – YYXX + 1) × Convert YYXX values of the


1.085 s where YYXX are TH, TL register to
TH, TL initial values decimal to get a NNNNN
respectively. decimal number, then
Notice that values YYXX are in (65536 – NNNNN) × 1.085
hex. s

26
Find Timer Values

• Assume XTAL = 11.0592 MHz .


• How to find the inter values needed for the TH, TL?
– Divide the desired time delay by 1.085 s.
20ms ÷ 1.085 s = 18433
– Perform 65536 –n, where n is the decimal value we got in
Step 1.
65536-18433=47103=B7FFH
– Convert the result of Step 2 to hex, where yyxx is the initial
hex value to be loaded into the timer’s registers.
– Set TH = yy and TL = xx.
TH=B7H, TL=FFH
27
Example 3
In the following program, we are creating a square wave of 50%
duty cycle (with equal portions high and low) on the P1.5 bit.
Timer 0 is used to generate the time delay.
Analyze the program.

;each loop is a half clock


MOV TMOD,#01 ;Timer 0,mode 1(16-bit)
HERE: MOV TL0,#0F2H ;Timer value = FFF2H
MOV TH0,#0FFH
P1.5
CPL P1.5
ACALL DELAY 50% 50%
SJMP HERE whole clock
28
Example 3
;generate delay using timer 0
DELAY:
SETB TR0 ;start the timer 0
AGAIN:JNB TF0,AGAIN
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET

FFF2 FFF3 FFF4 FFFF 0000

TF0 = 0 TF0 = 0 TF0 = 0 TF0 = 0 TF0 = 1


29
Solution:
In the above program notice the following steps.
1. TMOD = 0000 0001 is loaded.
2. FFF2H is loaded into TH0 – TL0.
3. P1.5 is toggled for the high and low portions of the pulse.
4. The DELAY subroutine using the timer is called.
5. In the DELAY subroutine, timer 0 is started by the “SETB TR0”
instruction.
6. Timer 0 counts up with the passing of each clock, which is
provided by the crystal oscillator.
As the timer counts up, it goes through the states of FFF3, FFF4,
FFF5, FFF6, FFF7, FFF8, FFF9, FFFA, FFFB, FFFC, FFFFD,
FFFE, FFFFH. One more clock rolls it to 0, raising the timer flag
(TF0 = 1). At that point, the JNB instruction falls through.
30
7. Timer 0 is stopped by the instruction “CLR TR0”. The
DELAY subroutine ends, and the process is repeated.
8. Remember to clear TF0 by the instruction “CLR TF0”.

Notice that to repeat the process, we must reload the TL and TH


registers, and start the timer again (in the main program).

31

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy