Timers & Counters
Timers & Counters
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
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
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