You are on page 1of 26

Timers

Chapter 2

Objectives
Upon completion of this chapter, you will be able to:
List the timers of the PIC18 and their associated registers Describe the various modes of the PIC18 timers Program the PIC18 counters in C as event counters

Timer0
16-bit wide Consists of a low-byte (TMR0L) and a high-byte (TMR0H) register Can be used as 8-bit or 16-bit timer

High byte (8-bit)

Low byte (8-bit)

Timer0 (contd)
Important Registers:
i) T0CON (Timer0 Control Register) To start & stop Timer0 and other configurations ii) TMR0H:TMR0L (for counting purposes) Act as counting buffer iii) INTCON (Interrupt Control Register)
D7 D0

T0CON (Timer0 Control) Register

Example 1
What is the value of T0CON if the Timer0 settings are as below ?
16-bit timer No pre-scaler Internal clock (from oscillator) source Increment on positive-edge Answer: T0CON = 00001000

Example 2
Calculate the amount of time delay generated by the Timer0 with the following specification:
XTAL = 10MHz TMR0H:TMR0L = FFF2H Solution: TCY = 4/10MHz = 0.4us (Each tick consume 0.4us) How many tick? (FFFF-FFF2) + 1 = 14 ticks Time delay = 14 x 0.4us = 5.6us 1 FFF2 TMR0IF=0 FFF3 2 FFF4 FFFE 13 FFFF 14 0000

TMR0IF=0 TMR0IF=0

TMR0IF=0 TMR0IF=0 TMR0IF=1

Example 3
Write a C program to toggle all the bits of PORTB continuously with 1ms delay. Use Timer0, 16-bit mode, no prescaler options to generate the delay. (Assume XTAL=20MHz) Solution:
TCY = 4/20MHz = 0.2us (Each tick consume 0.2us) How many ticks in 1ms delay?
1ms/0.2us = 5000 ticks = 1388H ticks!

To find register value for TMR0H:TMR0L


FFFF - register value + 1 = 1388H ticks register value = EC78H

@ Simply take the negative value of the tick counts This is much more easier! -1388H = EC78H

Example 3 (contd)

Exercise 1
Write a C program to toggle only the PORTB.4 bit continuously every 50ms. Use Timer0, 16-bit mode and the 1:4 prescaler to create the delay. (Assume XTAL = 20MHz) Solution:
TCY = 4/20MHz = 0.2us (Each tick consume 0.2us) How many ticks in 50ms delay? 50ms/0.2us = 250000 ticks = 3D090H ticks! (out of range!!) 250000/4 = 62500 ticks = F424H ticks!

With 1:4 prescaller Therefore, register counts = -F424H = 0BDCH

Exercise 1 (contd)

Timer1
16-bit wide Consists of a low-byte (TMR1L) and a high-byte (TMR1H) register Can be used as 16-bit timer only!

High byte (8-bit)

Low byte (8-bit)

Timer1 (contd)
Important Registers:
i) T1CON (Timer1 Control Register) To start & stop Timer1 and other configurations ii) TMR1H:TMR1L (for counting purposes) Act as counting buffer iii) PIR1 (Peripheral Interrupt Request Register 1)
D7

T1CON (Timer1 Control) Register

Exercise 2
Write a C program to create pulses with a frequency of 2500Hz with 50% duty cycle on pin PORTB.1. Use Timer1 to create the delay. (Assume XTAL = 20MHz) Solution: T = 1/2500 = 400us (HIGH: 200us; LOW: 200us) How many ticks in 200us delay? 200us/0.2us = 1000 ticks = 03E8H ticks! Therefore, register counts = - 03E8H = FC18H

Exercise 2 (contd)

Exercise 2 (contd)

Approx.1000 ins. cycles

Approx.1000 ins. cycles

Timer0 & Timer1 as Counter


Can used as Counters Counter0 (Timer0 counter):
Count pulses on T0CKI (RA4) pin

Counter1 (Timer1 counter):


Count pulses on T13CKI (RC0) pin

Example - Counter
Please refer Example 9-35, 9-36 & 9-37 in the textbook

Timer2
8-bit wide Consists of a PR2 (Period Register 2) TMR2 will increment from 00 until reaches PR2 value before TMR2IF flag is set Consists of prescaler and postscaler No counter function

Timer2 (contd)
Important Registers:
i) T2CON (Timer2 Control Register) To start & stop Timer2 and other configurations ii) PR2 (to set the counting value) If TMR2 = PR2; TMR2IF flag is set iii) PIR1 (Peripheral Interrupt Request Register 1)
D7

Timer2 (contd)

Timer3
16-bit wide Consists of a low-byte (TMR3L) and a high-byte (TMR3H) register Enable the CCP Mode for PWM Application

Timer3 (contd)
Important Registers:
i) T3CON (Timer3 Control Register) To start & stop Timer3 and other configurations ii) TMR3H:TMR3L (for counting purposes) Act as counting buffer iii) PIR2 (Peripheral Interrupt Request Register 2)

Timer3 (contd)

End of Chapter 2

You might also like