Sie sind auf Seite 1von 39

TIMER

PROGRAMMING

The PIC18 has two to five timers depending on


the family member.

They are referred to as Timers 0, 1, 2, 3, and 4.

They can be used

As timers to generate a time delay.

As counters to count events happening outside the


microcontroller.

PROGRAMMING TIMERS 0 AND 1

Every timer needs a clock pulse to tick.

Clock source can be internal or external.

If we use the internal clock source, then (1/4) th of the frequency


of the crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) is
fed into the timer.

It can be used for the generation of a Time Delay and for that

reason is called a timer.

By choosing the external clock option, we can feed pulses


through one of the PICI8's pins: Then this is called a Counter
-Counting the external event.

Basic registers of the timer

Many of the PIC18 timers are 16 bits wide.

Because the PIC18 has an 8-bit architecture, each


16-bit timer is accessed as two separate registers
of low byte (TMRxL) and high byte (TMRxH).

Each timer also has the TxCON (timer control)


register for setting modes of operation.

Timer0 registers and programming


Timer0 can be used as an 8-bit or a 16-bit timer.

The 16-bit register of Timer0 is accessed as low byte and high byte.
The low-byte register is called TMR0L (Timer0 low byte) and the high-byte

register is referred to as TMR0H (Timer0 high byte).


These registers can be accessed like any other special function registers.
The instruction "MOVWF TMR0L moves the value in WREG into TMR0L, the low

byte of Timer0.
These registers can also be read like any other register.
"MOVFF TMR0L, PORTB copies TMR0L (low byte of Timer0) to PORTB.

T0CON (Timer0 control) register

Each timer has a control register, called TnCON (n=0,1,2), to set the corresponding

timers operation modes. T0CON is an 8-bit register used for control of Timer0.

T0CS (Timer0 clock source)


This bit in the T0CON register is used to decide whether the

clock source is internal (Fosc/4) or external.


If T0CS = 0, then the Fosc/4 is used as clock source. In this

case, the timers are often used for time delay generation.
If T0CS = 1, the clock source is external and comes from the

RA4/T0CKI, which is pin 6 on the DIP package of PIC


18F4580/4520.
When the clock source comes from an external source, the

timer is used as an event counter.

TMR0IF flag bit in INTCON register

The TMR0IF bit (Timer0 interrupt flag) is part of the INTCON

(interrupt control) register.


When the timer reaches its maximum value of FFFFH, it rolls over to

0000, and TMR0IF is set to 1

TIMER0 BLOCK DIAGRAM IN 8 / 16-BIT MODE

16-bit timer programming


It is a 16-bit timer; therefore, it allows values of 0000

to FFFFH to be loaded into the registers TMR0H and


TMR0L.
After TMR0H and TMR0L are loaded with a

16-bit

initial value, the timer must be started.


This is done by "BSF T0CON, TMR0ON" for Timer0.
After the timer is started, it starts to count up.

counts up until it reaches its limit of FFFFH.

It

16-bit timer programming


When it rolls over from FFFFH to 0000, it sets

HIGH a flag bit called TMR0IF (timer interrupt


which is part of the INTCON register).

flag,

This timer flag can be monitored. When this timer

flag is raised, one option would be to stop the timer.


After the timer reaches its limit and rolls over,

in
order to repeat the process, the registers TMR0H
and TMR0L must be reloaded with the original value,
and the TMR0IF flag must be reset to 0 for the next
round.

Steps to program Timer0 in 16-bit mode

Load the value into the T0CON register indicating which mode
(8-bit or 16- bit) is to be used and the selected pre-scaler option.

Load register TMR0H followed by register TMR0L with initial


count values.

Start the timer with the instruction "BSF T0CON, TMR0ON".

Keep monitoring the timer flag (TMR0IF) to see if it is raised.


Get out of the loop when TMR0IF becomes high

Stop the timer with the instruction "BCF T0CON, TMR0ON".

Clear the TMROIF flag for the next round.

Go back to Step 2 to load TMR0H and TMR0L again.

Notice that we should load TMR0H first, and then load


TMR0L, because the value for TMR0H is kept in a temporary
register and written to TMR0H when TMR0L is loaded. This
will prevent any error in counting if the TMR0ON flag is set
HIGH.

Generate a square wave of 50% duty cycle (with equal


portions high and low) on the PORTB.5 bit. Timer0 is used to
generate the time delay.

BCF

TRISB,5

;PB5 as as output pin

MOVLW 0x08

;Timer0, 16-bit,int. Clk., no prescale

MOVWF T0CON

;load T0CON register

HERE MOVLW 0xFF

;TMROH = FFH, The High Byte

MOVWF TMR0H

;load TMR0 High byte

MOVLW 0xF2

;TMROL = F2H, The low byte.

MOVWF TMR0L

;load Timer0 low byte.

BCF

INTCON,

BTG

PORTB, 5

BSF

T0CON,

TMR0ON ;start timer 0

INTCON,

TMR0IF

AGAIN BTFSS
BRA

AGAIN

BCF

T0CON,

BRA

HERE

TMR0IF

;clear timer interrupt flag bit.


;toggle PB5

;monitor timer0 flag until


;it rolls over

TMR0ON ;stop timer0


;load TH, TL again

TOCON is loaded.
FFF2H is loaded into TMR0H- TMRO0L.
The Timer0 interrupt flag is cleared by the "BCF INTCON,
TMR0IF" instruction.
PORTB.5 is toggled for the high and low portions of the pulse.
Timer0 is started by the "BSF T0CON, TMR0ON" instruction.
Timer0 counts up with the passing of each clock, which is
provided by the crystal oscillator. As the timer counts up, it
goes through the states of FFF3, FFF4, FFF5, FFF6, FFF7,
FFF8, FFF9, FFFA, FFFB, and so on until it reaches FFFFH.
One more clock rolls it to 0000, raising the Timer0 flag
(TMR0IF = 1).
At that point, the "BTFSS INTCON, TMR0IF" instruction
bypasses the "BRA AGAIN" instruction.
Timer0 is stopped by the instruction "BCF T0CON, TMR0ON",
and the process is repeated.

Find the frequency of the square wave generated by the following

program if XTAL = 10 MHz. In your calculation do not include the


overhead due to instructions in the loop.

BCF

TRISB,5

;PB5 as as output pin

MOVLW 0x08

;Timer0, 16-bit,int. Clk., no prescale

MOVWF T0CON

;load it to T0CON register

HERE MOVLW 0x76

;TMR0H = 76H, The High Byte

MOVWF TMR0H

;load TMR0 High byte

MOVLW 0x34

;TMR0L = 34H, The low byte.

MOVWF TMR0L

;load Timer0 low byte.

BCF

INTCON,

TMR0IF

;clear timer interrupt flag bit.

CALL

DELAY

BTG

PORTB, 5

;toggle PB5

BRA

HERE

; Load TH, TL again

DELAY BSF

T0CON

TMR0ON ;Start Timer0

AGAIN BTFSS

INTCON

TMR0IF

BRA

AGAIN

BCF

T0CON,

RETURN

;Monitor Timer0 flag until


;it rolls over

TMROON ;stop timer0


;load TH, TL again

Because FFFFH - 7634H = 89CBH + 1 = 89CCH and


89CCH = 35,276 clock count.
10/4 = 2.5 MHz clock
1 / 2.5 = 0.4 s.
35,276 x 0.4 s = 14.11 ms and frequency = 1/(14.11
ms x 2) = 35.434 Hz.
In this calculation, the overhead due to the instructions
in the loop is neglected.

Finding values to be loaded into the timer


How to find the values needed for the TMR0H and TMR0L

registers, if the amount of timer delay is known.


Assuming XTAL = 10 MHz and no prescaler:
1. As each clock is 0.4us, to determine how many clocks we want,
divide the desired time delay by 0.4 s to get the number of clocks
needed (n) (as instruction freq = 10/4
Mhz, instruction period =
4/10 s).
2. As the roll-over takes place (and the interrupt flag is set) at FFFF
(65535), Perform 65,536 - n, where n is the decimal value from
Step 1.
3. Convert the result of Step 2 to hex, where yyxx is the initial hex
value to be loaded into the timer's registers.
4. Set TMROL = xx and TMROH = yy.

Prescaler and generating a large time


The maximum size of the time delay depends on two factors,

(a) the crystal frequency, and (b) the timer's (16-bit ) register

size.
Both of these factors are beyond the control of the PIC18

programmer.
The largest time delay is achieved by making both TMR0H and

TMR0L zero. What if that is not enough?


We can use the prescaler option in the T0CON register to increase

the delay by reducing the period.


The prescaler option of T0CON allows us to divide the instruction

clock by a factor of 2 to 256.

Prescaler and generating a large time


With no prescaler enabled, the crystal oscillator frequency is divided

by 4 (Fosc/4) and then fed into Timer0.


If we enable the prescaler bit in the T0CON register, however, then

we can divide the instruction clock (Fosc/4) further before it is fed


into Timer0.
The lower 3 bits of the T0CON register give the options of the

number we can divide by.


This number can be 2, 4, 8, 16,32,64, and so on.
Notice that the lowest number is 2 and the highest number is 256.

Prescaler and generating a large time


Find the value for T0CON if we want to program Timer0 in 16-bit

mode with a prescaler of 64 and use internal clock (Fosc/4) for the
clock source, positive-edge.

T0CON = 0000 0101; 16-bit mode, XTAL clock source,


prescaler of 64.

Find the timer's clock frequency and its period for various PICl8based systems, with the following crystal frequencies. Assume that a
prescaler of 1 :64 is used.
( a) 1 0 MH z
1/4 x 10 MHz = 2.5 MHz and 1/64 x 2.5 MHz = 39062.5 Hz due to
1 :64 prescaler and T = 1/39062.5Hz = 25.6 us
(b) 1 6 MH z
1/4 x 16 MHz = 4 MHz and 1/64 x 4 MHz = 62500 Hz due to
prescaler and T = 1/62500 Hz = 16 us

8-bit mode programming of Timer0


Timer0 can also be used in 8-bit mode. The 8-bit mode allows only

values of 00 to FFH to be loaded into the timer's register TMR0L.


After the timer is started, it starts to count up by incrementing the

TMR0L register. It counts up until it reaches its limit of FFH. When it


rolls over from FFH to 00, it sets HIGH the TMR0IF.

Steps to program 8-bit mode of Timer0


1.
Load the T0CON value register indicating 8-bit mode is
selected.
2. Load the TMR0L registers with the initial count value.
3. Start the timer.
4. Keep monitoring the timer flag (TMR0IF) to see if it is raised.
Get out of the loop when TMR0IF becomes HIGH.
5. Stop the timer with the instruction "BCF T0CON, TMROON".
6. Clear the TMR0IF flag for the next round.
7. Go back to Step 2 to load TMR0L again.

BCF

TRISA,2

;PA2 as output pin

MOVLW 0x48

;Timer0, 8 bit,int. Clk., no prescale

MOVWF T0CON

;load it to T0CON register

HERE MOVLW 0x5

;TMR0L = 5, The low Byte

MOVWF TMR0L

;load TMR0 low byte

BCF

INTCON,

TMR0IF

;clear timer interrupt flag bit.

CALL

DELAY

BTG

PORTA, 2

;toggle PA2

BRA

HERE

; Load TH, TL again

DELAY BSF

T0CON

TMR0ON ;Start Timer0

AGAIN BTFSS

INTCON

TMR0IF

BRA

AGAIN

BCF

T0CON,

RETURN

;Monitor Timer0 flag until


;it rolls over

TMROON ;stop timer0


;load TH, TL again

Assuming that XTAL = 10 MHz, find (a) the frequency of the


square wave generated on pin PORTA2 in the above program,
and (b) the smallest frequency achievable in this program, and
the TH value to do that.

(256 - 05) = 251 x 0.4 us = 100.4 us is the high portion of the


pulse.
As it toggles on equal intervals, it is a 50% duty cycle square
wave, the period T is twice that; as a result T = 2 x100.4 us =
200.8 us, and the frequency = 4.98 kHz.
(b) To get the smallest frequency, we need the largest T, and that
is achieved when TMR0L = 00. In that case, we have T = 2 x 256
x 0.4 us= 204.8 us and the frequency = 1 / 204.8 us = 4,882.8
Hz.

Assemblers and negative values


Because the timer is in 8-bit mode, we can let the assembler

calculate the value for TMR0L. For example, in "MOVLW, -D '100'


", the assembler will calculate the -100 = 9C in and make WREG =
9C in hex. This makes our job easier.
Then, it will tick (wait) for 100 clocks for the interrupt to occur.
FF 9c = 63h = 99 decimal. At clock 100, it will roll-over , IF will

be set.

Timer1 programming (RC0 PIN)


Timer1 is a 16-bit timer, and its 16-bit register is split into two
bytes, referred to as TMR1L (Timer1 low byte) and TMR1H
(Timer1 high byte).
Timer1 can be programmed in 16-bit mode only and unlike

Timer0, it does not support 8-bit mode.


Timer1 also has the T1CON (Timer 1 control) register. In

addition, it has a TMR1IF (Timer 1 interrupt flag) bit.


The TMR1IF flag bit goes HIGH when TMR1H:TMR1L overflows

from FFFF to 0000.


Timer1 also has the prescaler option, but it only supports

of 1:2, 1:4, and 1:8


The PIR1 register contains the TMR1IF flags.

factors

T1CON

PIR1 (Priority Interrupt Register 1- Interrupt Control


Register ) Contains the TMR1IF Flag

COUNTER PROGRAMMING
We were using the timers of the PIC18 to generate time delays.
These timers can also be used as counters to count events
happening outside the PICI8.
When the timer is used as a timer, the PICI8's crystal is used as
the source of the frequency.
When it is used as a counter, however, it is a pulse outside the
PIC 18 that increments the TH, TL registers.
These clock pulses could represent the number of people
passing through an entrance, or the number of wheel rotations,
or any other event that can be converted to pulses.
In counter mode, the registers such as T0CON, TMR0H, and
TMR0L are the same as for the timer .

T0CS bit in T0CON register

The T0CS bit (Timer0 clock source) in the T0CON register decides the source of

the clock for the timer.


If T0CS = 0, the timer gets pulses from the crystal oscillator connected to the OSC

1 and OSC2 pins (Fosc/4).


When T0CS = 1, the timer is used as a counter and gets its pulses from outside the

PICI8.
When T0CS = 1, the counter counts up as pulses are fed from pin RA4 (PORTA4).
The pin is called T0CKI (Timer0 clock input
In the case of Timer0, when T0CS = 1, pin RA4 (PORTA4) provides the clock

pulse and the counter counts up for each clock pulse coming from that pin.
Similarly, for Timerl , when TMR1CS = 1, each clock pulse coming in from pin

RC0 (PORTC.0) makes the counter count up.

Find the value for T0CON if we want to program


Timer0 as an 8-bit mode counter, no prescaler. Use
an external clock for the clock source and increment
on the positive edge.

TOCON = 0110 1000 ;8-bit, external clock source,


no prescaler.

Assuming that clock pulses are fed into pin T0CKI, write a program for counter 0 in

8- bit mode to count the pulses and display the state of the TMROL count on PORTB.

BSF

TRISA,4

;PA4 as as input pin

CLRF

TRISB

;PORTB as an output port

MOVLW 0x68

;Timer0, 8-bit,iext. Clk., no prescale

MOVWF T0CON

;load it to T0CON register

HERE MOVLW 0x0

;TMR0L = 0H,

MOVWF TMR0L

;Clear Timer0L .

BCF

INTCON,

TMR0IF

BSF

T0CON

TMR0ON ;Start Timer0

AGAIN MOVFF TMROL,


BTFSS

INTCON

BRA

AGAIN

BCF

T0CON,

GOTO

HERE

;clear timer interrupt flag bit.

PORTB

;display the count on PORTB

TMR0IF

;Monitor Timer0 flag until


;it rolls over

TMROON ;stop timer0

Compare and Capture can use either Timer 1 or

Timer 3.
T3CON (Timer 3 Control Register can be used to

tell which timer to be used as clock source for CCP


module (T3CCP1: T3CCP2 Bits).
PWM module can use only Timer 2 (8Bit timer,

only timer with post scale, No external clock


cannot be used as a counter).

Das könnte Ihnen auch gefallen