0% found this document useful (0 votes)
52 views

UP102 Timer

The document discusses timer programming for the PIC18 microcontroller. It describes the functions and clock sources of Timers 0 and 1. It explains the registers associated with each timer for configuration and reading timer values. Examples are provided to generate delays, pulses, and count external events using the timers with different configurations.

Uploaded by

Mohammed Maher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

UP102 Timer

The document discusses timer programming for the PIC18 microcontroller. It describes the functions and clock sources of Timers 0 and 1. It explains the registers associated with each timer for configuration and reading timer values. Examples are provided to generate delays, pulses, and count external events using the timers with different configurations.

Uploaded by

Mohammed Maher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

PIC18 Timer Programming

g g

Hsiao-Lung
H i L Chan
Ch
Dept Electrical Engineering
Chang Gung University, Taiwan
chanhl@mail cgu edu tw
[email protected]
Functions of PIC18 timer

Functions of the timer


Generate a time delay
As a counter to count events happening
pp g outside the
microcontroller
Clock sources of the timer
Internal clock pulse
1/4th of the frequency of the crystal oscillator on the OSC1 and
OSC2 pins (Fosc/4)
Usually used to generate a time delay
External clock pulse
Usually used as a counter

2
Registers
g of the timer

Special
p function
registers (SFRs)

Timer1

Timer0

3
Timer0 registers
g

Timer0 can be used as an 88-bit


bit or 16-bit
16 bit timer
Can be assessed like any SFRs
MOVWF TMR0L
MOVFF TMR0H, PORTC

4
T0CON ((Timer0 control)) register
g

Set various timer operation mode

5
TMR0IF flag bit in INTCON (interrupt control
register)

T0CS=0

TMR0ON

6
Timer0 16-bit block diagram
g

Fosc/4
Sync with
TMR0 high byte
Set interrupt
internal TMR0L
flag TMR0IF
clock
T0CKI
pin T0SE

PSA Parallel operations


T0PS2:T0PS0 with TMR0L read or
write
T0CS
TMR0H

Should load TMR0H first, and then


TMR0L

7
Find the frequency of the square wave on the
PORTB 5 bit if XTAL = 10 MHz
PORTB.5
BCF TRISB, 5
MOVLW 0x08 ; Timer0, 16-bit, internal clock, no prescale
MOVWF T0CON
Clock period = 1 / (1/4 x 10 MHz) = 0.4 s
HERE MOVLW 0x76 Countingg clocks FFFFH 7634H + 1 = 35,276,
MOVWF TMR0H time delay = 35,276 x 0.4 s = 14.11 ms
MOVLW 0x34 frequency = 1/(14.11 ms x 2) = 35.434 Hz
MOVWF TMR0L
BCF INTCON, TMR0IF ; clear Timer0 interrupt flag
CALL DELAY
BTG PORTB,, 5
BRA HERE

DELAY BSF T0CON,, TMR0ON ; start Timer0


AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag
BRA AGAIN
BCF T0CON, TMR0ON ; stop Timer0
RETURN
8
Assume that XTAL = 10 MHz, modify previous
program for a period of 10 ms

A period of 10ms a time delay of 5 ms


XTAL = 10 MHz fosc = 2.5 MHz
Counter counts up every 0.4 s
Need 5 ms / 0.4 s = 12,500 clocks
Initial value for TMR0 = 65,535
65 535 12,500
12 500 + 1 = CF2CH
TMR0H = CF
TMR0L
0 = 2CC

9
Use prescaler to generate a large time delay
(assume that XTAL = 10 MHz)
BCF TRISB, 2
MOVLW 0x05 ; Timer0, 16-bit, internal clock, prescaler 64
MOVWF T0CON
HERE MOVLW 0x01 FFFFH 0108H + 1 = 65,272
MOVWF TMR0H 65,272 x 64 x 0.4 s = 1.671 sec
MOVLW 0x08
MOVWF TMR0L
BCF INTCON, TMR0IF ; clear Timer0 interrupt flag
CALL DELAY
BTG PORTB,, 2
BRA HERE

DELAY BSF T0CON,, TMR0ON ; start Timer0


AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag
BRA AGAIN
BCF T0CON, TMR0ON ; stop Timer0
RETURN
10
Largest
g time delayy that can be achieved

Set TMR0 to 0000 count from 0000 to FFFF


65,536 clocks
Set prescaler 256
Time delay = 4 s x 256 x 65,536 = 67,108,864 s

11
8-bit mode p
programming
g g of Timer0

Fosc/4
Sync with
internal TMR0
clock
T0CKI
pin
T0SE
Set interrupt
PSA flag TMR0IF
T0PS2:T0PS0
T0CS

12
Find the frequency of the square wave if XTAL = 10
MHz
BCF TRISB, 3
BCF INTCON TMR0IF
INTCON, ; clear Timer0 interrupt flag
MOVLW 0x48 ; Timer0, 8-bit, internal clock, no prescale
MOVWF T0CON
HERE MOVLW -D150
D150
MOVWF TMR0L
Delay subroutine = 150 x 0.4 s = 60 s
BSF PORTB, 3 T = high portion + low portion
CALL DELAY = 2 x 60 s + 60 s = 180 s
MOVWF TMR0L
CALL DELAY
BCF PORTB, 3
MOVWF TMR0L
CALL DELAY
BRA HERE
DELAY BSF T0CON, TMR0ON ; start Timer0
AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag
BRA AGAIN
BCF T0CON, TMR0ON ; stop Timer0
13
Counter p
programming
g g using
g Timer0

Set T0CS (Timer0 clock source) in T0CON to 1


Get pulses from T0CKI (Timer0 clock input): RA4
(
(PORTA.4))

14
Clock pulses are fed into T0CKI and a buzzer is connected to
PORTB 1 Sound the buzzer after counting 100 pulses
PORTB.1.
BCF TRISB, 1 ; RB1 as an output to a buzzer
BSF TRISA 4
TRISA, ; RA4 as an input for clock-in
clock in
MOVLW 0x68 ; Timer0, 8-bit, external clock, no prescale
MOVWF T0CON
MOVLW -D100
D100
MOVWF TMR0L
BSF T0CON, TMR0ON ; start Timer0
AGAIN BTFSS INTCON TMR0IF ; monitor
INTCON, it Timer0
Ti 0 interrupt
i t t flag
fl
BRA AGAIN
BCF T0CON, TMR0ON ; stop Timer0
OVER BTG PORTB, 1
CALL DELAY
GOTO OVER

15
Timer1

Only support 16-bit


16 bit mode

Set interrupt flag CCP Special Event Trigger


TMR1IF on overflow

TMR1H TMR1L

TMR1ON

T1SYNC

T1OSO/T1CKI
T1OSCEN

T1OSI

Fosc/4
T0PS1:T0PS0
TMR1CS

16
T1CON ((Timer1 control)) register
g

17
PIR1 ((interrupt
p control register
g 1))

18
Assume XTAL = 10 MHz, use Timer1 maximum
prescaler to generate a square wave of 50 Hz
BCF TRISB, 5
MOVLW 0x30 ; Timer1, 16-bit, internal clock, prescaler 1:8
MOVWF T1CON
HERE MOVLW 0xF3 FFFFH F3CBH + 1 = 3125
MOVWF TMR1H 3125 x 8 x 0.4 s = 10 ms
MOVLW 0xCB frequency = 1/(2x10ms) = 50 Hz
MOVWF TMR1L
BCF PIR1, TMR1IF ; clear Timer1 interrupt flag
CALL DELAY
BTG PORTB,, RB5
BRA HERE

DELAY BSF T1CON,, TMR1ON ; start Timer1


AGAIN BTFSS PIR1, TMR1IF ; monitor Timer1 interrupt flag
BRA AGAIN
BCF PIR1, TMR1ON ; stop Timer1
RETURN
19
Counter p
programming
g g using
g Timer1

Two options for the external clock source


The clock fed into T1CKI pin
The clock from a crystal
y connected to T1OSI and T1OSO
pins where a 32-kHz crystal is connected to
External clock source option (TMR1CS) is set to 1
Ti
Timer1
1 oscillator
ill t enable
bl bit (T1OSCEN) is
i sett to
t 1
SLEEP mode
SLEEP instruction shut down main crystal to save power
Timer1 is still enable for on-chip RTC (real-time clock)

20
Assume that a 1-Hz pulse is fed into T1CKI, display the
counter values on ports B and D.
D
BSF TRISC, RC0 ; RC0 as an input for clock-in
CLRF TRISB
CLRF TRISD
MOVLW 0x02 ; Timer1, 16-bit, external clock, no prescale
MOVWF T1CON
HERE MOVLW 0x0
MOVWF TMR1H
MOVLW 0 0
0x0
MOVWF TMR1L
BCF PIR1, TMR1IF
BSF T1CON, TMR1ON ; start Timer1
AGAIN MOVFF TMR1H, PORTD
MOVFF TMR1L, PORTB
BTFSS PIR1, TMR1IF ; monitor Timer1 flag
BRA AGAIN
BCF PIR1, TMR1ON ; stop Timer1
GOTO HERE

21
Timer2 block diagram
g

8-bit
8 bit timer Set interrupt
flag TMR2IF

Fosc/4

T2CKPS1:T2CKPS0

TOUTPS3:TOUTPS0

CCP Special Event Trigger


22
T2CON ((Timer2 control)) register
g

23
PIR1 ((interrupt
p control register
g 1))

24
Assume that XTAL = 10 MHz, turn on PORTB4 when
TMR2 reach 100
BCF TRISB, 4
BCF PORTB 4
PORTB,
MOVLW 0x0 ; Timer2, no prescale or postscale
MOVWF T2CON
MOVLW 0 0
0x0
MOVWF TMR2
MOVLW D100
MOVWF PR2
BCF PIF1, TMR2IF ; clear Timer2 interrupt flag
BSF T2CON, TMR2ON ; start Timer2
AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag
BRA AGAIN
BSF PORTB, 4
BCF T2CON, TMR2ON ; stop Timer2
HERE BRA HERE

25
Timer3 block diagram
g

16-bit
16 bit timer or counter

Set interrupt flag CCP Special Trigger


TMR3IF on overflow

Fosc/4

T3CKPS1:T3CKPS0

26
T3CON ((Timer3 control)) register
g

27
PIR2 ((interrupt
p control register
g 2))

28
Generate a square wave of 50 Hz on the PORTB.5
bit if XTAL = 10 MHz
BCF TRISB, 5
MOVLW 0x0 ; Timer3, 16-bit, internal clock, no prescale
MOVWF T3CON
HERE MOVLW 0x9E FFFFH 9E58H + 1 = 25,000 clocks
MOVWF TMR3H time delay = 25,000 x 0.4 s = 1 ms
MOVLW 0x58 frequency = 1/(1 ms x 2) = 50 Hz
MOVWF TMR3L
BCF PIR2, TMR3IF ; clear Timer3 interrupt flag
CALL DELAY
BTG PORTB,, RB5
BRA HERE

DELAY BSF T3CON,, TMR3ON ; start Timer3


AGAIN BTFSS PIR2, TMR3IF ; monitor Timer3 interrupt flag
BRA AGAIN
BCF T3CON, TMR3ON ; stop Timer3
RETURN
29
Reference

M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller


and Embedded Systems Using Assembly and C for PIC18,
Pearson Education Inc., 2008.
Han-Way Huang, PIC Microcontroller: An Introduction to
Software and Hardware Interfacing, Thomson Delmar
L
Learning,
i 2005.
2005

30

You might also like