Timers & Counters

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

Timers & Counters

Timers
It Has two timers / counters which can be used as a timer or as a counter

Timers Counters
Timer 0 Timer 1
Can generate delay Count events
outside
Both are about 16 bit registers , but controller deals with 8 bit microcontrollers
So we deal with it separately
0 means timer 0 , L means least 8 bits ,H means
Highest 8 bits

We access them by
MOV TL0,#00H
MOV TH0,#40H now timer 0 has 4000H

This value is incremented by one each clock cycle


Timers
Timers consists of 3 things to control
Inputs
1- Timer registers (TL0,TH0,TL1,TH1)
2- TMOD Register
3- TR0,TR1 enable bin
outputs
4- TF0,TF1 flag
TMOD

Gate pin is set to one when we need to use external


control of timer and counter
(INTx pin must be set and TRx)

C/T’ is used to select how to use timer or counter ?


(timer =input from system clock ,count= input from tx pin)

M1 M0 is used to select mode of counting (how many bits


available to count and auto reload or manual reload)
Hardware Control timer counts
Sequence of control
1- Gate bit = 1 so input to or is zero
Must wait for external INT
2- Gate bit =0 so output of or is one ,so wait only
for TR to start timer
3- if C/T =0 ,source of counting is internal oscillator
C/T =1 ,source of counting is counter pin
How to do a timer for a specific delay
steps
1- Calculate Machine Cycle
2- Calculate #of counts that a timer should ticks and
add one for overflow
3- Calculate Initial Value = Final value that can a
register hold according to mode - # of counts
4- Configure Value of TMOD We want to design 20ms debounce delay function
5- Load Initial value to timer registers Suppose fcrystal = 12Mhz
6- do your program 1- Machine Cycle = 1 / (12MHz/12) = 1us
7- call delay function that start the timer via TRx 2- # of counts = 20ms / 1us = 20000
8- keep check TF flag tell it is one As timer 0 is 16 bit
9- stop the timer and clear the flag 3- initial value = (2^16 – 1 ) +1 - 20000 = 65536 – 20000 =45536
10- reload values again In hex B1E0 so TH0 = B1 ,TL0=E0
4-TMOD = 00000001 (least 4 bits > timer 0,gate =0,timer,mode 1)
Switch
ORG 00H
;Configuration ORG 00H
SWITCH BIT P1.0 ;switch connected to ground DELAY: SETB TR0 ;start timer
LED BIT P2.0
SETB SWITCH Check:JNB TF0,Check ;check end of counting
CLR LED CLR TR0 ;Stop the timer
;Configure Timer
MOV TMOD,#01H ;Timer 0 mode 1 CLR TF0 ;Clear the flag
AGAIN:MOV TL,#E0 ;20000 Ticks END
MOV TH,#B1
L1:JB SWITCH,L1 ;NO press stay here
ACALL DELAY ;A press wait for some time
JB AGAIN ; if it is one don’t set up led
SETB LED ; a press on switch on the led
SJMP AGAIN ;Again reload as mode 1 is not auto reload
;must be reloaded each time
END
RTC Clock
Real time clock means a clock that counts
seconds ,minutes and hours
Idea is that assign registers be incremented each 1s
period ,so we need a delay of 1s
Delay 1s Delay_1s: MOV R5,#20 ;repeat it 20 times
1- Machine Cycle = 1us AGAIN:MOV TH,#3C
2- #counts = 1s / 1us = 1,000,000 MOV TL,#B0
SETB TR1 ;start Timer1
Max capacity of timer 1 is 65536 ,If we design at
50000 ticks so we ant to repeat 50000
Here:JNB TF1,here ;check flag of finish
(1000000/50000= 20) CLR TR0 ;stop timer 1
So we create delay of 50000 ticks and repeat it 20
CLR TF0 ;clear flag
times DJNZ R5,AGAIN
3-Initial value = 65536 – 50000 = 15536 =3CB0
RET

TH = 3C ,TL=B0
RTC Clock
ORG 00H
CJNE R3,#59,loop ;R2!=60 so delay 1s
MOV TMOD,#00010000b ;enable timer 1 mode 1
JC inc_hours ; R2 > 59 ,increment hours
reset:MOV R2,#0 ;track seconds
SJMP LOOP
MOV R3,#0 ;track minutes
inc_hours:INC R4 ;if R2>59
MOV R4,#0 ;track hours
MOV R2,#0 ;make seconds zero
MOV R3,#0 ;make minutes zero
Loop:ACALL DELAY
CJNE R4,#23,loop ;check hours if exceed 24
INC R2 ;after each second inc R2
SJMP LOOP
CJNE R2,#59,loop ;R2!=60 so delay 1s
JC reset ;if hours =24 do reset
JC inc_min ; R2 > 59 ,increment minutes
SJMP LOOP ;hours <24 increment seconds
SJMP LOOP
inc_min:INC R3 ;if R2>59
MOV R2,#0 ;make seconds zero
CJNE R3,#59 ;check minutes if exceed 60
Mode 2 examples

If we use mode 2
Max ticks are (2^8 -1) + 1 = 256 ticks that is 256us
Min # ticks = 1 = 1us Frequency Initial value
So if we use mode 2
Min period = 1us x 2(on and off) = 2us max frequency = 1/2us = 500KHZ 250 KHZ 254
Max period = 256us x2 = 512us min frequency = 1.953125khz 2khz 6
Assume min be 2khz so period = 1/2khz = 500us so we need delay 250us that needs 250ticks

To generate 500KHZ > # of counts = 1 #initial value = 256 -1 = 255 TL =FF


Make it the most user can take frequency 255 that is the highest value in the register that can be
taken through keypad so we take 254 instead of 255 (that is 2 counts ,delay on = 2*1=2us,period =
4us ,freq = 250khz
Tell the user that our system generates (2-250khz)
How to calculate the initial value in
counter
Final value – initial value + 1 = Delay /Machine Cycle Delay
255 – x + 1 = / 1us
255 – x + 1 =
F required will make the user enter the freq in khz
255 – x + 1 =
F = 1 /2*Delay
255 – x + 1 = = =
X = 255 - + 1 to do it using 8050 1- Divide 250/f 2-mul by 2 3- CPL 4- INC
Steps in Timer mode 2
1- Calculate Machine Cycle Org 00h
2- Calculate #of counts that a timer should ticks ;configure inputs and outputs Delay: SETB TR0
Output BIT P2.0 Here:JNB TF0,here
and add one for overflow
Keypad EQU P1
3- Calculate Initial Value = Final value that can a CLR Output CLR TF0
register hold according to mode - # of counts MOV Keypad,#F0 RET
4- Configure Value of TMOD ;configure timer
MOV TMOD,#00000010b ;timer 0 mode 2 auto reload
5- Load Initial value to TH
ACALL read_freq ;now the value of freq in reg A
6- do your program MOV B,A
7- call delay function that start the timer via TRx MOV A,#250
8- keep check TF flag tell it is one DIV AB
MOV B,#2
9- stop the timer and clear the flag
MUL AB
CPL A
Assume pin p2.0 is the output and a INC A ; A = initial value
function read_freq that reads a MOV TH,A ;load initial in TH
AGAIN:SETB OUTPUT
frequency value from keypad (range 2-
ACALL DELAY
250khz) CLR OUTPUT
Keypad at port P1 ACALL DELAY
SJMP AGAIN
PWM
Now we calculated the initial value x that is
loaded into reg that controls the period of
frequency
Now we want to control how much on and how
much off ?
Example x = 100 so #of ticks = 255-100 + 1 = 156
156 is equal for on and off Example
If we configure duty cycle 25 -50 -75 If period is 10 ticks
X=5
25 % > initial value of on = x + (#ofticks /2) 75 % duty cycle
initial value of off = x - (#ofticks /2) Xon= 5 - ( (10-5) /2) = 5 – 2.5 = 2.5
50 % > initial value of on = x That allows (10-2.5) = 7.5 ticks for on and 2.5 for off
initial value of off = x Xof = 10 – 2.5 = 7.5 or 5 + 2.5 = 7.5
75 % > initial value of on = x – (#ofticks /2)
initial value of off = x + (#ofticks /2)
PWM
Org 00h Next_1: CJNE A,#’2’,next_2
25 % > initial value of on = x + (#ofticks /2) //pervious program AGAIN:MOV A,R4 ;# of ticks
initial value of off = x - (#ofticks /2) MOV TMOD,#00100000b ;t 1 mode 2 MOV B,#2
50 % > initial value of on = x ACALL read_freq DIV AB ;a=#ticks /2
MOV B,A
initial value of off = x MOV R5,A ;R5=#ticks /2
MOV A,#250
ADD A,R3
75 % > initial value of on = x – (#ofticks /2) DIV AB
MOV TH,A
MOV B,#2
initial value of off = x + (#ofticks /2) MUL AB SETB OUTPUT
Allow user to choose 1 for 50% duty cycle CPL A ACALL DELAY
INC A ; A = initial value MOV A,R3 ;x
2 for 25% duty cycle MOV R3,A ;Save original value in R3 CLR C
3 for 75%duty cycle CPL A ;A= 255-x
SUBB A,R5
INC A ;A now has number of ticks
MOV R4,A ;R4 ha number of ticks MOV TH,A
#assume that a function read_duty ACALL Read_duty CLEAR OUTPUT
CJNE A.#’1’,next_1 ACALL DELAY
Return this in a ;50 % duty cycle SJMP AGAIN
MOV A,R3 Next_2 :
MOV TH,A ;load initial in TH Same for next 2
AGAIN:SETB OUTPUT
ACALL DELAY
CLR OUTPUT
ACALL DELAY
SJMP Again
PWM by step 5%
To allow 5% duty cycle #step = (5/100) * #total number of ticks
User enters duty cycle in multiple of fives
Example #total number of ticks = 120 Example 65%
If duty > 50 %
#step = (1/20) * #total number of ticks = #total number of ticks /20 1- calculate step = #ticks/ 20 =6
2- we subtract it -50 (65-50=15)
As example = 120/20 =6 3- divide this number by your design precesion (15/5) =3 (number of
repeated add/sub)
So if we have initial value of x , 4- multiply step by the number from pervious step (3*6) = 18
5- for on subtract this value from x and for off add this value to x
Xon = X-6 (55%)
*******************************
Xoff = X+6 If duty cycle <50%
1- calculate step = #ticks/ 20 =6
**************************************************** 2- we subtract it -50 (25-50=-25) take the absolute (CPL + INC)
3- divide this number by your design precesion (25/5) =5 (number of
If we need 60 % repeated add/sub)
4- multiply step by the number from pervious step (5*6) = 30
X = X-12 5- for on add this value to x and for off SUPTRACT this value from x
65 %
X= X-18
Org 00h
//pervious program

PWM MOV TMOD,#00100010b ;t 1 mode 2


ACALL read_freq
MOV B,A
User enters duty cycle in multiple of fives MOV A,#250 Next_1: JC next_2 ;<50 % duty
Example 65% DIV AB ;>50%duty
If duty > 50 % MOV B,#2 AGAIN: CLR C
1- calculate step = #ticks/ 20 =6 MUL AB
SUBB A,#50 ;a = 65 -50 =15
2- we subtract it -50 (65-50=15) CPL A
3- divide this number by your design precesion (15/5) =3 (number of INC A ; A = initial value MOV B,#5
repeated add/sub) MOV R3,A ;Save original value in R3 DIV AB ;A=15/5 =3
4- multiply step by the number from pervious step (3*6) = 18 CPL A ;A= 255-x MOV B,R7 ;get the step
5- for on subtract this value from x and for off add this value to x INC A ;A now has half number of ticks MUL AB
******************************* MOV B,#2 MOV R6,A ;R6 = value to be added/sub
If duty cycle <50% MUL AB ; ;A now has total number of ticks MOV A,R3
1- calculate step = #ticks/ 20 =6 MOV B,#20
2- we subtract it -50 (25-50=-25) take the absolute (CPL + INC) CLR C
DIV AB ;A now has step =6
3- divide this number by your design precesion (25/5) =5 (number of MOV R7,A ;step now in R7 SUBB A,R6
repeated add/sub) ACALL Read_duty MOV TH,A
4- multiply step by the number from pervious step (5*6) = 30 CJNE A.#50,next_1 SETB OUTPUT
5- for on add this value to x and for off SUPTRACT this value from x ;50 % duty cycle ACALL DELAY
MOV A,R3 MOV A,R3 ;x
Allow user to enter duty cycle In multiple of five MOV TH,A ;load initial in TH ADD A,R6
AGAIN:SETB OUTPUT MOV TH,A
#assume that a function read_duty ACALL DELAY
Return this in a CLEAR OUTPUT
CLR OUTPUT ACALL DELAY
ACALL DELAY
SJMP AGAIN
SJMP Again
Next_2 :
Same for next 2
Counters
0000 0000 0000 0000
Crystal T1 (P3.5)
T0 (P3.4)
TIMER register TIMER register
incremented by internal incremented by external
oscillator of 8051 event outside 8051
Incremented by one each
positive edge of the clock
Timer
Counter

TL=1 TL=2
Frequency measure
ORG 00H
Mov TMOD,#0101 0001b ;
MOV TL1,#0
MOV TH1,#0 ;initialize counter by zero
SETB P3.5 ;configure T1 pin as input
AGAIN:SETB TR1 ;start the counter
BACK: ACALL Delay_1s ;call delay 1s by timer 0 Idea is to count #positive edges in 1second timer
MOV A,TL1 ;read number of counts in that period interval
MOV P2,A ;output this value Use timer 1 as counter mode in mode 1
JNB TF1,Back ;check counter overflow Use timer 0 as timer mode to calculate 1s ,use delay
CLR TR1 ;stop the counter 1 function delay_1s as in slide 8 but timer0
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it

You might also like