of 30
8/19/2019 Chapter 5 Timer
1/30
Chapter 5Chapter 5
PIC18 TimersPIC18 Timers
1
8/19/2019 Chapter 5 Timer
2/30
TimersTimers
• Counter is a register that can be loaded with aCounter is a register that can be loaded with a
binary number (count) which can bebinary number (count) which can be
incremented per clock cycle.incremented per clock cycle.
• e can use this counter to measure time.e can use this counter to measure time.• Time is calculated as !ollows"Time is calculated as !ollows"
– #ind di!!erence between beginning count and last#ind di!!erence between beginning count and last
countcount
– $ultiply the count di!!erence by the clock period$ultiply the count di!!erence by the clock period
2
8/19/2019 Chapter 5 Timer
3/30
Counters !or %&ternal %'entsCounters !or %&ternal %'ents
• The register can also be used as aThe register can also be used as a
counter !or e&ternal e'ents by replacingcounter !or e&ternal e'ents by replacing
the clock with a signal !rom an e'ent.the clock with a signal !rom an e'ent.
• hen a signal !rom an e'ent arri'es thehen a signal !rom an e'ent arri'es thecount in the register is incremented (orcount in the register is incremented (or
decremented) thus the total number o!decremented) thus the total number o!
e'ents can be counted.e'ents can be counted.
3
8/19/2019 Chapter 5 Timer
4/30
Timer *pplicationsTimer *pplications
• Time delayTime delay
• Pulse wa'e generationPulse wa'e generation
• #re+uency measurement#re+uency measurement
• Timer as an e'ent counter Timer as an e'ent counter
4
8/19/2019 Chapter 5 Timer
5/30
PIC18 TimersPIC18 Timers
• The PIC18 microcontroller has multipleThe PIC18 microcontroller has multipletimerstimers
• Timers are di'ided into two groups" 8,bit andTimers are di'ided into two groups" 8,bit and1-,bit1-,bit
• abeled as Timer/ to Timer0abeled as Timer/ to Timer0
– Timer/ can be set up as an 8,bit or 1-,bitTimer/ can be set up as an 8,bit or 1-,bittimer.timer.
– Timer1 is a 1-,bit timer.Timer1 is a 1-,bit timer. – Timer0 is an 8,bit timer.Timer0 is an 8,bit timer.
• %ach timer associated with its pecial%ach timer associated with its pecial#unction 2egister (#2)" T/C34,T0C34#unction 2egister (#2)" T/C34,T0C34
5
8/19/2019 Chapter 5 Timer
6/30
Timer/Timer/
• Can be set up as an 8,bit or 1-,bit timer Can be set up as an 8,bit or 1-,bit timer – Parameters are set up by bits in T/C34 registerParameters are set up by bits in T/C34 register
– as eight options o! pre,scale 'alues (6it0,6it/)as eight options o! pre,scale 'alues (6it0,6it/)
– Can run on internal clock source (instruction cycle)Can run on internal clock source (instruction cycle)or e&ternal clock connected to pin 2*7T/C91or e&ternal clock connected to pin 2*7T/C91
– :enerates an interrupt or sets a !lag when it:enerates an interrupt or sets a !lag when it
o'er!lows !rom ##o'er!lows !rom ##55 to //to //55 in the 8,bit mode andin the 8,bit mode and
!rom ####!rom ####55
to ////to ////55
in the 1-,bit modein the 1-,bit mode
– Can be set up on either rising edge or !alling edgeCan be set up on either rising edge or !alling edge
(6it7) when an e&ternal clock is used(6it7) when an e&ternal clock is used
6
8/19/2019 Chapter 5 Timer
7/30
T/C34 (Timer/ Control) 2egister T/C34 (Timer/ Control) 2egister
• T$2/34 ; T/C34" 3n (1) or 3!! (/)T$2/34 ; T/C34" 3n (1) or 3!! (/)
• T/86IT ; T/C34" 8,bit (1) or 1-,bit (/)T/86IT ; T/C34" 8,bit (1) or 1-,bit (/)
• T/C ; T/C34" ?se %&ternal (1) orT/C ; T/C34" ?se %&ternal (1) or
Internal (/) clockInternal (/) clock
• T/% ; T/C34" Increment on , (1) orT/% ; T/C34" Increment on , (1) or
, (/) transition, (/) transition
• P* ; T/C34" ?se (/) or Ao not use (1)P* ; T/C34" ?se (/) or Ao not use (1)prescalingprescaling
• T/P0"T/P/ ; T/C34" peci!y
prescaler !actor prescaler !actor 7
8/19/2019 Chapter 5 Timer
8/30
T/C34 (Timer/ Control) 2egister T/C34 (Timer/ Control) 2egister
8
8/19/2019 Chapter 5 Timer
9/30
Internal Clock $ode (T/C ; /)Internal Clock $ode (T/C ; /)
• Clock source is the instruction cycleClock source is the instruction cycle
clockclock (#(#31C31C 7) 7)
• The Timer/ Interrupt #lag (The Timer/ Interrupt #lag (T$2/I#T$2/I# ;;
I4TC34) is raised when counterI4TC34) is raised when counter
rollo'er !rom ma& 'alue (#### in 1-,bitrollo'er !rom ma& 'alue (#### in 1-,bit
mode ## in 8,bit mode) to min 'aluemode ## in 8,bit mode) to min 'alue
(//// in 1-,bit mode // in 8,bit mode)(//// in 1-,bit mode // in 8,bit mode)• ?sed as a timer to create delay?sed as a timer to create delay
9
8/19/2019 Chapter 5 Timer
10/30
Create delay using Timer/ 1-,bit $odeCreate delay using Timer/ 1-,bit $ode
• Characteristics o! 1-,bit modeCharacteristics o! 1-,bit mode
– 1-,bit timer B allows 'alues o! //// to ####1-,bit timer B allows 'alues o! //// to ####
to be loaded into registers T$2/ andto be loaded into registers T$2/ and
T$2/T$2/ – The timer must be started by setting theThe timer must be started by setting the
T$2/34 (i.e. bs! T/C34 T$2/34)T$2/34 (i.e. bs! T/C34 T$2/34)
–The timer counts up a!ter being started.The timer counts up a!ter being started.
hen it rolls o'er !rom #### to //// ithen it rolls o'er !rom #### to //// it
sets the interrupt !lag T$2/I#.sets the interrupt !lag T$2/I#.
10
8/19/2019 Chapter 5 Timer
11/30
teps to create delayteps to create delay
1.1. et T/86IT ; T/C34 ; / (use 1-,bit mode)et T/86IT ; T/C34 ; / (use 1-,bit mode)0.0. oad register T$2/ !ollowed by T$2/ withoad register T$2/ !ollowed by T$2/ with
initial count 'aluesinitial count 'alues
@.@. tart the timer"tart the timer" bsf T0CON, TMR0ON bsf T0CON, TMR0ON
7.7. 9eep monitoring T$2/I# to see whether it is9eep monitoring T$2/I# to see whether it israised. :et out o! loop i! highraised. :et out o! loop i! high
5.5. top the timer"top the timer" bcf T0CON, TMR0ON bcf T0CON, TMR0ON
-.-. Clear T$2/I# !or ne&t roundClear T$2/I# !or ne&t round
=.=. 2epeat !rom tep 02epeat !rom tep 0
11
8/19/2019 Chapter 5 Timer
12/30
%&le%&le
Create a s+uare wa'e o! 5/ duty cycle onCreate a s+uare wa'e o! 5/ duty cycle onP32T6 bit.P32T6 bit.
bcf TRISB, 5 bcf TRISB, 5 ; Set PB5 as output; Set PB5 as output
movlw B ‘00001000 movlw B ‘00001000 ; 1!"b#t, #$t cl%, $o p&escale&; 1!"b#t, #$t cl%, $o p&escale&
movwf T0CON movwf T0CON
'e&e movlw 0())'e&e movlw 0()) ; TMR0' * 0(++; TMR0' * 0(++ movwf TMR0' movwf TMR0'
movlw 0() movlw 0() ; TMR0- * 0(..; TMR0- * 0(..
movwf TMR0- movwf TMR0-
bcf INTCON, TMR0I) bcf INTCON, TMR0I) ; Clea& t#me& #$te&&upt fla/; Clea& t#me& #$te&&upt fla/
bt/ PORTB, 5 bt/ PORTB, 5
bsf T0CON, TMR0ON bsf T0CON, TMR0ON ; sta&t T#me& 0; sta&t T#me& 0
/a#$ btfss INTCON, TMR0I) /a#$ btfss INTCON, TMR0I)
b&a /a#$ b&a /a#$
bcf TOCON, TMR0ON bcf TOCON, TMR0ON
b&a 'e&e b&a 'e&e ; loa TMR0' a$ TMR0- a/a#$; loa TMR0' a$ TMR0- a/a#$
12
8/19/2019 Chapter 5 Timer
13/30
Time Aelay CalculationTime Aelay Calculation
• #osc ; 7$D#osc ; 7$D
• Instruction Cycle ; 1Instruction Cycle ; 1EEss
• Instruction Cycle re+uired to reach ####Instruction Cycle re+uired to reach ####
; ####,###0 ; 1@ (in decimal); ####,###0 ; 1@ (in decimal)
• 4eed one more instruction cycle to4eed one more instruction cycle to
rollo'er to //// where T$2/I# isrollo'er to //// where T$2/I# is
triggered.triggered.
• Total time delay generated by Timer/;Total time delay generated by Timer/;
17 & 117 & 1EEs ; 17s ; 17EEss13
8/19/2019 Chapter 5 Timer
14/30
#ormula to Compute Time Aelay#ormula to Compute Time Aelay
• In e& total time delay generated byIn e& total time delay generated by
Timer/ ; (#### B FFGG H 1) & 1Timer/ ; (#### B FFGG H 1) & 1EEss
where FF and GG are the initial 'alues o!where FF and GG are the initial 'alues o!
T$2/ and T$2/ respecti'ely.T$2/ and T$2/ respecti'ely.
• In decimalIn decimal con'ert FFGG into itscon'ert FFGG into its
decimal e+ui'alence 44444 !irst anddecimal e+ui'alence 44444 !irst and
then use" (-55@- B 44444)then use" (-55@- B 44444) & 1& 1EEss
14
8/19/2019 Chapter 5 Timer
15/30
?se o! Prescaler ?se o! Prescaler
• ongest delay that can be produced isongest delay that can be produced is-5.5@- ms using a 1-,bit timer. hat i! we-5.5@- ms using a 1-,bit timer. hat i! we
want longer delaywant longer delay
• Prescaler can be used to increase period o!Prescaler can be used to increase period o!internal clock by a !actor o! 0internal clock by a !actor o! 044 where 4 ; where 4 ;
1 ... 8 and speci!ied by T/P0"T/P/ ;1 ... 8 and speci!ied by T/P0"T/P/ ;
T/C34.
• e.g. hen 4 ; - the period o! the clock ise.g. hen 4 ; - the period o! the clock is
increased by a !actor o! -7increased by a !actor o! -7 -7-7EEs. $a&s. $a&
delay ; -55@- & -7delay ; -55@- & -7 EEs ; 7.1J7 ss ; 7.1J7 s
15
8/19/2019 Chapter 5 Timer
16/30
%&les%&les
Initial T$2/ Prescaler Time Aelay (Es)
#C87
1 (no prescaler) 8J0
0 1=87
7 @5-8
8 =1@-
1- 170=0@0 08577
-7 5=/88
108 1171=-
*ssume #osc ; 7$D and Instruction Cycle ; 1$D*ssume #osc ; 7$D and Instruction Cycle ; 1$D(#### , #C87 H 1) & 1(#### , #C87 H 1) & 1Es ; 8J0 Es
16
8/19/2019 Chapter 5 Timer
17/30
Create delay using Timer/ 8,bit $odeCreate delay using Timer/ 8,bit $ode
17
8-bit Mode
16-bit Mode
8/19/2019 Chapter 5 Timer
18/30
Timer/ as counter Timer/ as counter
• I! dri'en by an e&ternal clock (T/C ; 1)I! dri'en by an e&ternal clock (T/C ; 1)Timer/ will increment either on e'eryTimer/ will increment either on e'ery
rising (T/% ; /) or !alling (T/% ; 1)rising (T/% ; /) or !alling (T/% ; 1)
edge o! pinedge o! pin 2*7T/C9I2*7T/C9I..
18
8/19/2019 Chapter 5 Timer
19/30
%&le%&le
*ssuming clock pulses are !ed into Pin T/C9I*ssuming clock pulses are !ed into Pin T/C9I(2*7) use Timer/ as a 8,bit counter o! pulses and(2*7) use Timer/ as a 8,bit counter o! pulses anddisplay the state o! T$2/ count on P32T6display the state o! T$2/ count on P32T6
bsf TRIS, R2; PORT32 set as #$put bsf TRIS, R2; PORT32 set as #$put
cl&f TRISB; PORTB set as outputcl&f TRISB; PORTB set as output
movlw 0(!4; T#me&0, 4"b#t, e(t cl%, $o p&escale movlw 0(!4; T#me&0, 4"b#t, e(t cl%, $o p&escale
movwf T0CON movwf T0CON
'R movlw 0(00'R movlw 0(00
movwf TMR0- movwf TMR0-
bcf INTCON, TMR0I) bcf INTCON, TMR0I)
bsf T0CON, TMR0ON bsf T0CON, TMR0ON
6IN movff TMR0-, PORTB 6IN movff TMR0-, PORTB
btfss INTCON, TMR0I) btfss INTCON, TMR0I)
b&a 6IN b&a 6IN
bcf T0CON, TMR0ON bcf T0CON, TMR0ON
/oto 'R/oto 'R 19
8/19/2019 Chapter 5 Timer
20/30
Timer1Timer1
Timer/ Timer18-bit or 16-bit timer orcounter.
16-bit timer or counter.
Supports prescaling factors of
1, 2, 4, 8, 16, 32, 64, 128,26.
Supports prescaling factors of
1, 2, 4 and 8.
!imer" counts cloc# pulsesfed into $%4
!imer1 counts cloc# pulsesfed into $&"
'(en counter rolls o)er,!M$"*+ in *!& S+$ israised
'(en counter rolls o)er,!M$1*+ in *$1 S+$ israised
20
8/19/2019 Chapter 5 Timer
21/30
ab 7 Task 0" #re+uency Counter ab 7 Task 0" #re+uency Counter
Kariable #re+uency :enerator" 3utput !re+uencyKariable #re+uency :enerator" 3utput !re+uencydepends on the chargingdischarging time o! C1depends on the chargingdischarging time o! C1which is controlled by 'arying the resistor 'aluewhich is controlled by 'arying the resistor 'alue
20 through turning a knob on the circuit board20 through turning a knob on the circuit board 21
8/19/2019 Chapter 5 Timer
22/30
ab 7 Task 0ab 7 Task 0"" #re+uency Counter #re+uency Counter
*lgorithm*lgorithm•et up Timer / to generate a 1,second delayet up Timer / to generate a 1,second delay•?se Timer 1 as a counter to count the rising?se Timer 1 as a counter to count the risingedges o! an unknown signal (!ed to 2C/)edges o! an unknown signal (!ed to 2C/)
within the secondwithin the second•tart Timer/ and Timer1tart Timer/ and Timer1•ait !or I# !lag !or Timer/ to raiseait !or I# !lag !or Timer/ to raise•top Timer/ and Timer1top Timer/ and Timer1•T$21"T$21 would then be the !re+uencyT$21"T$21 would then be the !re+uency
o! the signal.o! the signal.
22
8/19/2019 Chapter 5 Timer
23/30
Timer0Timer0
• Can only be used as a timer to create timeCan only be used as a timer to create timedelay B 4o e&ternal clock source can !eddelay B 4o e&ternal clock source can !edin.in.
• Prescaler and Postscaler to lengthenPrescaler and Postscaler to lengthendelays.delays.
23
8/19/2019 Chapter 5 Timer
24/30
8/19/2019 Chapter 5 Timer
25/30
T$20 and P20 #2T$20 and P20 #2
• T$20" 8,bit register o! Timer0T$20" 8,bit register o! Timer0• P20 (Period 2egister) is set to be a !i&edP20 (Period 2egister) is set to be a !i&ed
'alue.'alue.
• The input period can be lengthened byThe input period can be lengthened byselecting the prescaling !actor.selecting the prescaling !actor.
• hen T$20 ;; P20 %L signal will resethen T$20 ;; P20 %L signal will resetT$20 to /.T$20 to /.
• The output o! the comparator is di'ided byThe output o! the comparator is di'ided bypostscaling !actor.postscaling !actor. – i.e. I! postscaler ; 1"1- the %L signal will needi.e. I! postscaler ; 1"1- the %L signal will need
to be set 1- times be!ore T$20I# is set.to be set 1- times be!ore T$20I# is set.25
8/19/2019 Chapter 5 Timer
26/30
%&le 1" Timer 0%&le 1" Timer 0
• rite a program to turn on pin P32T6.7 whenrite a program to turn on pin P32T6.7 whenT$20 reaches the 'alue o! 1// (decimal).T$20 reaches the 'alue o! 1// (decimal).
bcf bcf TRISB, 2; ma%e PORTB32 as outputTRISB, 2; ma%e PORTB32 as output bcf bcf PORTB, 2; tu&$ off PORTB32PORTB, 2; tu&$ off PORTB32 movlw 0(00 movlw 0(00
movwf TCON; $o p&escale a$ postscale movwf TCON; $o p&escale a$ postscale movlw 0(00 movlw 0(00 movwf TMR movwf TMR movlw 7‘100 movlw 7‘100 movwf PR movwf PR bcf bcf PIR1, TMRI)PIR1, TMRI)
bsf bsf TCON, TMRONTCON, TMRON /a#$8 /a#$8 btfss PIR1, TMRI) btfss PIR1, TMRI) b&a b&a /a#$ /a#$ bsf bsf PORTB, 2PORTB, 2 bcf bcf TCON, TMRONTCON, TMRON
'e&e8'e&e8 b&a b&a 'e&e'e&e
26
8/19/2019 Chapter 5 Timer
27/30
%&le 0" Timer 0%&le 0" Timer 0
• ?se the prescaler and postscaler to create the?se the prescaler and postscaler to create thelongest possible delay by Timer0.longest possible delay by Timer0.
• Total delay ; (055H1) & 1- & 1- & 1Total delay ; (055H1) & 1- & 1- & 1EEs ; -5.08mss ; -5.08ms
bcf bcf TRISB, 2TRISB, 2
bcf bcf PORTB, 2PORTB, 2 movlw B‘01111011 movlw B‘01111011 movwf TCON; p&escale * 181!, postscale * 181! movwf TCON; p&escale * 181!, postscale * 181! movlw 0(00 movlw 0(00 movwf TMR movwf TMR movlw 7‘55 movlw 7‘55 movwf PR movwf PR
bcf bcf PIR1, TMRI)PIR1, TMRI) bsf bsf TCON, TMRONTCON, TMRON
/a#$8 /a#$8 btfss PIR1, TMRI) btfss PIR1, TMRI) b&a b&a /a#$ /a#$ bsf bsf PORTB, 2PORTB, 2 bcf bcf TCON, TMRONTCON, TMRON
'e&e8'e&e8 b&a b&a 'e&e'e&e
27
8/19/2019 Chapter 5 Timer
28/30
Timer / Timer 1 and Timer 0Timer / Timer 1 and Timer 0
28
Timer/ Timer1 Timer0
8-bit or 16-bit timer orcounter.
16-bit timer or counter. 8-bit timer. &annot beused as counter
Supports prescaling
factors of 1, 2, 4, 8, 16,32, 64, 128, 26.
Supports prescaling
factors of 1, 2, 4 and 8.
Supports prescaling
and postscalingfactors.
!imer" counts cloc#pulses fed into$!%.4
!imer1 counts cloc#pulses fed into$!&."
&annot be used ascounter
'(en counter rollso)er, !M$"*+ in*!& S+$ is raised
'(en counter rollso)er, !M$1*+ in *$1S+$ is raised
'(en counter rollso)er, !M$2*+ in *$1S+$ is raised
8/19/2019 Chapter 5 Timer
29/30
Time Aelay %+uationTime Aelay %+uation
• Time delay are e&pressed in instructionTime delay are e&pressed in instructioncycles.cycles.
• Timer/1 1-,bit modeTimer/1 1-,bit mode
– Con'ert the initial 'alue o! T$2& into itsCon'ert the initial 'alue o! T$2& into itsdecimal e+ui'alence 44444 !irst.decimal e+ui'alence 44444 !irst.
– Time Aelay ; (-55@- B 44444)Time Aelay ; (-55@- B 44444) & Prescaler & Prescaler
•Timer/ 8,bit modeTimer/ 8,bit mode – Time Aelay ; (05- B 444) & Prescaler Time Aelay ; (05- B 444) & Prescaler
• Timer 0Timer 0
– Time Aelay ; (P20H1) & Prescaler & Postscaler Time Aelay ; (P20H1) & Prescaler & Postscaler 29
8/19/2019 Chapter 5 Timer
30/30
Fou should be able to ... Fou should be able to ...
• ist the PIC18 timers and theirist the PIC18 timers and theirassociated registers.associated registers.
• Aescribe 'arious modes o! the PIC18Aescribe 'arious modes o! the PIC18
timer.timer.
• Program the PIC18 timers to generateProgram the PIC18 timers to generate
time delaystime delays
30