0% found this document useful (0 votes)
13 views35 pages

PIC18 Timer Programming Guide

This document discusses programming timers and counters on PIC18 microcontrollers. It describes the timer registers like TMR0H, TMR0L, and T0CON for Timer 0 and how to use them. Examples are provided to generate time delays using Timer 0 in 16-bit mode and 8-bit mode. External clock sources and prescalers are covered to produce longer delays. Counter programming is also explained, using timers to count external clock pulses on pins like RA4.

Uploaded by

Shwe Eain
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)
13 views35 pages

PIC18 Timer Programming Guide

This document discusses programming timers and counters on PIC18 microcontrollers. It describes the timer registers like TMR0H, TMR0L, and T0CON for Timer 0 and how to use them. Examples are provided to generate time delays using Timer 0 in 16-bit mode and 8-bit mode. External clock sources and prescalers are covered to produce longer delays. Counter programming is also explained, using timers to count external clock pulses on pins like RA4.

Uploaded by

Shwe Eain
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

PIC Microcontroller and

Embedded Systems
Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey

Eng. Husam Alzaq


The Islamic Uni. Of Gaza

The PIC uCs 9-1


Chapter 9: PIC18 Timer
Programming in
Assembly and C

PIC Microcontroller
and Embedded Systems
Muhammad Ali Mazidi,
Rolin McKinlay and
Danny Causey, February
2007.

The PIC uCs 9-2


Objective
 List the Timers of PIC18 and their associated
registers
 Describe the various modes of the PIC18
timers
 Program the PIC18 timers in Assembly to
generate time delays
 Program the PIC18 timers in Assembly as
event counters

The PIC uCs 9-3


Outlines
 Programming timers 0 and 1
 Counter Programming

The PIC uCs 9-4


Introduction
 PIC18 has two to five timers
 Depending on the family number

 These timers can be used as


 Timers to generate a time delay
 Counters to count events happening outside the
uC

The PIC uCs 9-5


Section 9.1: Programming
timers 0 and 1
 Every timer needs a clock pulse to tick
 Clock source can be
 Internal  1/4th of the frequency of the crystal
oscillator on OSC1 and OSC2 pins (Fosc/4) is fed into
timer
 External: pulses are fed through one of the PIC18’s
pins  Counter
 Timers are 16-bit wide
 Can be accessed as two separate reg. (TMRxL &
TMRxH)
 Each timer has TCON (timer Control) reg.

The PIC uCs 9-6


Timer0 registers and
programming
 TMR0L & TMR0H are 8-bit Reg.
 MOVWF TMR0L
 MOVFF TMR0L, PORTB

The PIC uCs 9-7


T0CON
Reg
 Determine
the timer
operations
modes
 Example
 If T0CON=
0000 1000
 16-bit
 No prescaler
 Rising edge

The PIC uCs


Figure 9-2. T0CON (Timer0 Control) 9-8
TMR0IF flag bit
 Part of INTCON

The PIC uCs


Figure 9-3. INTCON (Interrupt Control 9-9
Figure 9-4. Timer0 Overflow
Flag

The PIC uCs 9-10


Characteristics and operations
of 16-bit mode
1. 16-bit timer, 0000 to FFFFH.
2. After loading TMR0H and TMR0L, the
timer must be started.
3. Count up, till it reaches FFFFH, then it
rolls over to 0000 and activate TMR0IF
bit.
4. Then TMR0H and TMR0L must be
reloaded with the original value and
deactivate TMR0IF bit.

The PIC uCs 9-11


Steps to program Timer0 in 16-
bit mode to generate time delay
1. Load the value into the T0CON register
2. Load reg. TMR0H followed by reg. TMR0L
with initial value
3. Start the timer with instruction
BSF T0CON, TMR0ON
4. Keep monitoring the timer flag (TMR0IF) to
see if it is raised.
5. Stop the timer
6. Clear the TMR0IF flag 3
7. Go Back to step 2
The PIC uCs 9-12
Figure 9-5. Timer0 16-bit Block
Diagram

The PIC uCs 9-13


BCF TRISB,5
MOVLW 0x08
Example 9-3 MOVWF T0CON
HERE
 A square wave of MOVLW 0xFF
50% duty cycle on MOVWF TMR0H
the PORTB.5 is MOVLW 0xF2
created MOVWF TMR0L
BCF INTCON, TMR0IF
 Analyze the
BTG PORTB,5
program BSF T0CON, TMR0ON
AGAIN
FFF2 FFFF 0000
FFF3
FFF4 BTFSS INTCON, TMR0IF
BRA AGAIN
BCF T0CON, TMR0ON
TMR0IF=1
TMR0IF=0 BRA HERE
The PIC uCs 9-14
HERE
MOVLW 0xFF 1
Example 9-5 MOVWF TMR0H 1
MOVLW -D'48' 1
 Calculate the MOVWF TMR0L
1
frequency of the CALL DELAY
2
wave generated on BTG PORTB,5
PIN PORTB 5. 1
BRA HERE
DELAY 2

BCF TRISB,5 BSF T0CON, TMR0ON


AGAIN 1
MOVLW 0x08
BTFSS INTCON, TMR0IF
MOVWF T0CON BRA AGAIN 48
BCF INTCON, BCF T0CON, TMR0ON
TMR0IF BCF INTCON, TMR0IF 1
RETURN 1
The PIC uCs
Figure 9-6. Timer Delay Calculation
for XTAL = 10 MHz with No Prescaler

 General formula for delay calculation


 T = 4/(10MHz) = 0.4 usecond

The PIC uCs 9-16


Example 9-8
 Write a program to  T = 10 ms
generate a square  Time delay =
wave with a period 10ms/2 = 5 ms.
of ms on pin  We need 5ms/0.4us
PORTB.3 (XALT=10 = 12500 clocks
Mhz)  FFFF - 30D4 +1
=CF2C
 TMR0H = CFH
 TMR0L= 2CH

The PIC uCs 9-17


Example 9-8, Cont.
BCF TRISB,3
MOVLW 0x08 DELAY
MOVWF T0CON BSF T0CON,TMR0ON
HERE AGAIN
MOVLW 0xCF BTFSS
MOVWF TMR0H INTCON,TMR0IF
MOVLW 0x2C BRA AGAIN
MOVWF TMR0L
BCF T0CON,TMR0ON
BCF INTCON,TMR0IF
RETURN
CALL DELAY
BTG PORTB,3
BRA HERE

The PIC uCs 9-18


Example 9-8, Cont.

The PIC uCs 9-19


Prescaler and generating larger
delay
 The size of delay depend on
 The Crystal frequency
 The timer’s 16-bit register.

 The largest timer happens when


TMR0L=TMR0H=0
 Prescaler option is used to duplicate the
delay by dividing the clock by a factor of
2,4, 8,16, 32,64 ,128,256
 If T0CON=0000 0101, then T = 4*64/f

XTAL Osc ÷4 ÷ 64 TMRx


The PIC uCs 9-20
BCF TRISB,2
Example 9-13 MOVLW 0x05
MOVWF T0CON
 Examine the
HERE
following program
and find the time MOVLW 0x01
delay in second. MOVWF TMR0H
 Assume that XALT MOVLW 0x08
= 10 MHz. MOVWF TMR0L
BCF INTCON,TMR0IF
CALL DELAY
BTG PORTB,2
BRA HERE
The PIC uCs 9-21
Figure 9-7. Timer0 8-bit Block
Diagram

The PIC uCs 9-22


Figure 9-8. Timer1 High and
Low Registers
 Can be programmed in 16-bit mode only
 It has 2 bytes named as TMR1L and RMR1H
 It has also T1CON and TMR1IF
 The module incorporates its own low-power
oscillator to provide an additional clocking option.
 Used as a low-power clock source for the
microcontroller in power-managed operation.

The PIC uCs 9-23


Figure 9-9. Timer1 Block
Diagram

The PIC uCs 9-24


Figure 9-
10.
T1CON
(Timer 1
Control )
Register

The PIC uCs 9-25


Figure 9-11. PIR1 (Interrupt
Control Register 1) Contains the
TMR1IF Flag

The PIC uCs 9-26


SECTION 9.2: Counter
Programming
 Used to counts event outside the PIC
 Increments the TMR0H and TMR0L registers

 T0CS in T0CON reg. determines the clock


source,
 If T0CS = 1, the timer is used as a counter
 Counts up as pulses are fed from pin RA4
(T0CKI)
 What does T0CON=0110 1000 mean?

 If TMR1CS=1, the timer 1 counts up as


clock pulses are fed into pin RC0
The PIC uCs 9-27
Using external Crystal for External Source
Timer1 clock
 Timer1 comes with two
options,
 clock fed into T1CKI
• T1OSCEN=0

Internal Source
 Clock from a crystal
connected to T1OSI-T1OSO
(additional)
• T1OSCEN=1
• 32 kHz Crystal is connected
• Used for saving power during
SLEEP mode  doesn’t disable
Timer1 while the main crystal is
shut down

The PIC uCs 9-28


BSF TRISA,RA
Example 9-23 CLRF TRISB
MOVLW 0x68
 Assuming that MOVWF T0CON
clock pulses are HERE MOVLW 0x0
fed into pin T0CK1, MOVWF TMR0L
write a program for BCF INTCON,TMR0IF
counter 0 in 8-bit BSF T0CON,TMR0ON
mode to count the AGAIN MOVFF TMR0L,PORTB
pulses and display BTFSS INTCON,TMR0IF
the state of the BRA AGAIN
TMR0L count on
BCF T0CON,TMR0ON
PORTB.
GOTO HERE
The PIC uCs 9-29
NUME EQU 0x00
Example 9-24 QU EQU 0x20
RMND_L EQU 0x30
 Assume that a 1 Hz RMND_M EQU 0x31
frequency pulse is RMND_H EQU 0x32
connected to input MYDEN EQU D'10'
for Timer0(T0CKI) BSF TRISA,RA4
 Write a program to MOVLW 0x68
display counter 0 MOVWF T0CON
on PORTB, C and D HERE MOVLW 0x0
in decimal. MOVWF TMR0L
 Ser the initial value BCF INTCON,TMR0IF
of TMR0L to -60. BSF T0CON,TMR0ON
The PIC uCs 9-30
BIN_ASC_CON
MOVFF PORTB,WREG
Example 9-24 MOVWFNUME
MOVLWMYDEN
CLRF QU
AGAIN MOVFTMR0L,W D_1 INCF QU
SUBWF NUME
CALL BIN_ASC_CON BC D_1
BTFSS ADDWF NUME
INTCON,TMR0IF DECF QU
MOVFF NUME,RMND_L
BRA AGAIN MOVFF QU,NUME
BCF T0CON,TMR0ON CLRF QU
D_2 INCF QU
GOTOHERE SUBWF NUM
BC D_2
ADDWF NUM
DECF QU
MOVFF NUME,RMND_M
MOVFF QU,RMND_H
The PIC uCs RETURN 9-31
BCF TRISB,1
Example 9-26 BSF TRISA,4
MOVLW 0x68
MOVWF T0CON
 Assuming that clock
MOVLW -D'100'
pulses are fed into MOVWF TMR0L
pin T0CKI and a BCF INTCON,TMR0IF
buzzer is connected BSF T0CON,TMR0ON
to pin PORTB.1 AGAIN BTFSS
write a program for INTCON,TMR0IF
counter0 in 8-bit BRA AGAIN
mode to sound the BCF T0CON,TMR0ON
buzzer every 100 OVER BTG PORTB,1
pulses CALL DELAY
GOTOOVER
The PIC uCs 9-32
BSF TRISC,RC0
Example 9-27 CLRF TRISB
CLRF TRISD
 Assume that a 1 Hz MOVLW 0x02
frequency pulse is MOVWF T1CON
connected to input for
Timer1(RC0) HERE MOVLW 0x0
 Write a program to MOVWF TMR1H
display the counter MOVLW 0x0
values on PORTB and D in
decimal. MOVWF TMR1L
 Initial value=0 BCF PIR1,TMR1IF
 16-bit and no Prescaler BSF T1CON,TMR1ON

The PIC uCs 9-33


Example 9-27
AGAINMOVFF
TMR1H,PORTD
MOVFF
TMR1L,PORTB
BTFSS
PIR1,TMR1IF
BRA AGAIN
BCF PIR1,TMR1ON
GOTO HERE

The PIC uCs 9-34


Next: Chapter 10
PIC18 Serial Port
Chapter 9: Summary Programming in
Assembly and C
 The PIC18 can have up to four or more
timers/counters. Depending on the family
member
 Timers: Generate Time Delays (using Crystal)
 Counters: Event counter (using Pulse outside)
 Timers are accessed as two 8-bit registers,
TMRLx and TMRHx
 Can be used either 8-bit or 16-bit
 Each timer has its own Timer Control register

The PIC uCs 9-35

You might also like