Вы находитесь на странице: 1из 41

C2– Timers

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
Introduction

• The PIC18 has from two (2) to five (5)


timers depending on the family member.
They are referred to as Timer 0, 1, 2, 3
and 4.
• Timer application
1) Generate a time delay
2) As a counter to count events happening
outside the microcontroller.
• PIC18F4580 has 4 Timers.
• Every timer needs a clock pulse to tick. The
clock source can be internal or external.
• Internal clock: The 1/4th of the frequency of
the crystal oscillator on the OSC1 and OSC2
pins (fosc/4) is fed into the timer. Therefore, it
is used for time delay generation. This is
called a timer.
• External clock: Fed pulses thru’ one of the
PIC18’s pins: This is called a counter.
• Many of the PIC18 timers are 16 bits wide.
• Each 16-bit timer is accessed as two
separate register, low byte (TMRxL) and
high byte (TMRxH)
• Each timer also has Timer Control register
(TCON) for setting modes of operation.
Timer0
• Software selectable operation as a timer or
counter in both 8-bit or 16-bit modes
• Readable and writable registers
• Dedicated 8-bit, software programmable
prescaler
• Selectable clock source (internal or external)
• Edge select for external clock
• Interrupt-on-overflow
• Consists of a low-byte (TMR0L) and a high-
byte (TMR0H) register

High byte (8-bit) Low byte (8-bit)


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
External Source

Used as an event counter

Internal Source
EX1
• What is the value of T0CON if the Timer0
settings are as below ?
– 16-bit mode
– No pre-scaler
– Internal clock (from oscillator) source
– Increment on positive-edge.

Answer:
T0CON = 00001000 With 64 prescaller

T0CON = 0000 0101


EX2
• Find the timer’s clock frequency and its
period for various PIC18 based systems,
with the following crystal frequency.
Assume that no pre-scaller is used.

a) 10 MHz 2.5 MHz @ 0.4 uS


b) 16 MHz 4 MHz @ 0.25 uS
c) 4 MHz 1 MHz @ 1 uS
INTCON (Interrupt Control)
• TMR0IF Flag Bit
D7 D0
16-bit Timer Programming
• It allows values of 0000H to FFFFH to be
loaded into the registers TMR0H and
TMR0L
• After loaded, the timer must be started
(BSF T0CON, TMR0ON)
• It start to count up until it reaches its limit of
FFFFH. When it rolls over from FFFFH to
0000H, it sets HIGH a flag bit (TMR0IF)
• Repeat the process:
• 1)Reload the TMR0H and TMR0L
2)TMR0IF flag must be reset to 0
Step to Program Timer0 in 16-bit Mode
1) Load the value into the T0CON register
2) Load register TMR0H followed by
register TMR0L
3) Start the timer, (BSF T0CON, TMR0ON)
4) Keep monitoring the timer flag (TMR0IF)
5) Stop the timer, (BCF T0CON, TMR0ON)
6) Clear the TMR0IF flag for the next round
7) Go back to the step 2)
Time Delay

EX
For TMR0H = B8 and TMR0L = 3E, calculate the time delay
Generated by assume XTAL = 10MHz with no pre-scaler

(FFFF – B83E + 1) = 47C2H = 18370 x 0.4uS = 7.348mS or

65536 – 47166 = 18370 x 0.4uS = 7.348mS


EX. Use Timer0, 16-bit and no per-scale for for the 7.348mS
time delay

#include <p18f458.h>
void T0Delay()
{
T0CON = 0x08; //T0, 16-bit , no pre-scale
TMR0H = 0xB8;
TMR0L = 0x3E;
T0CONbits.TMR0ON = 0; //turn on T0
while(INTCONTbits.TMR0IF == 0); //wait for TF0 to roll over
T0CONbits.TMR0ON = 0; //turn off T0
INTCONbits.TMR0IF = 0; //clear TF0

}
EX
• Calculate the amount of time delay
generated by the timer.
• For TMR0H = FF and TMR0L = F2
• Assume that XTAL = 10MHz

Solution:
T = 4/10MHz = 0.4us (Each tick consume 0.4us)
How many tick? (FFFF-FFF2) + 1 = 14 Decimal
Rolls Over( from
ticks)
FFFF to 0
Time delay
1 = 14 x20.4us = 5.6us for half the
13 pulse 14

FFF2 FFF3 FFF4 FFFE FFFF 0000

TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=1


EX
Write a 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:
T0CON = 00001000 //T0, 16-bit no pre-scale
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 TMR0H = ECH
@ TMR0L = 78H
Simply take the negative value of the tick counts
-1388H = EC78H
Pre-scaller and Generating a Large Time Delay

• The time delay depends on two factors,


a) The crystal frequency
b) The timer’s 16-bit register
• We can use the prescaler option in the
T0CON register to increase the delay by
reducing the period
• Prescaler option from 2 to 256

XTAL Osc ÷4 ÷ 64 TMRx


EX
Write a 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!!)

With 1:4 prescaller


250000/4 = 62500 ticks = F424H ticks!

Therefore, register counts = -F424H = 0BDCH


EX

• Find the timer’s clock frequency and its


period with the following crystal frequency.
Assume that a prescaler of 1:64 is used.
a) 10 MHz
b) 16 MHz
Solution:
a) 10MHz/4 = 2.5MHz  2.5MHz/64 =
39062.5Hz  T = 1/39062.5Hz = 25.6uS
b) 16MHZ/4 = 4MHz  4MHZ/64 =
62500Hz T = 1/62500Hz = 16uS
8-bit Mode Programming of Timer0

• Set the T0CON value register indicating 8-


bit mode
• Load the TMR0L register only!
• Start the timer
• Keep monitoring the timer flag (TMR0IF)
• Stop the timer
• Clear the timer flag
• Reload TMR0L
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)


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
EX
Write a 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
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

Refer Example 9-35, 9-36 & 9-37 in the textbook


Eg. Assume that 1-Hz external clock is being fed into pin
T0CKI (RA4). Write a C program for Counter 0 in mode 1
(16-bit) to count the pulses and display the TMR0H and
TMR0L registers on PORTD and PORTB, respectively (EX
9.35)

#include <p18f458.h>
void main(void)
{
TRISCbits.TRISA4=1; //make RA4 an input for T0CK1
TRISB = 0x0;
TRISD = 0x0;
T0CON = 0x25; //T0,16-bit, prescale 1:64
TMR0H = 0x0; //set cnt = 0
TMR0L = 0x0; //set cnt = 0
while(1)
{
do
{
T0CONbits.TMR0ON = 1; // turn on T0
PORTD = TMR0H;
PORTB = TMR0L;
}
while(INTCONTbits.TMR0IF == 0); //wait for roll over
T0CONbits.TMR0ON = 0; // turn off T0
INTCONbits.TMR0IF = 0; // clear TF0
}
}
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, coz no external clock
source.
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
Timer3
• 16-bit wide
• Consists of a low-byte (TMR3L) and a high-
byte (TMR3H) register
• Enable the CCP Mode for PWM Application
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)


Eg. Write a program to create a frequency of 2500 Hz on PB.1 by using
Timer 3
1/2500Hz = 400 μs
400/2 = 200 μs
200 μs/ 4 μs = 500
65536 – 500 = 65036 = FE0CH
#include <p18f458.h>
void T3Delay(void);
#define mybit PORTBbits.RB1
void main(void)
{
TRISBbits.TRISB1=0; //PB1 as output;
T3CON = 0x00; //T3,16-bit, no prescale
while(1)
{
mybit=-mybit; //Toggle PB.1
T3Delay();
}
}
void T3Delay()
{
TMR3H = 0xFE;
TMR3L =0x0C
T3CONbits.TMR3ON = 1; // turn on T3
while(PIR2bits.TMR3IF == 0); //wait for roll over
T3CONbits.TMR3ON = 0; // turn off T3
PIR2bits.TMR3IF = 0; // clear TF3
}

Вам также может понравиться