Bai9 TimerCounter
Bai9 TimerCounter
Lesson 9
TIMER
PIC16F887
TIMER
The PIC16F887 microcontroller
has three completely separate
timers/counters marked as:
TMR0
TMR1
TMR2
PIC16F887
TIMER0
The timer TMR0 has a wide range of
application in practice. Very few
programs don’t use it in some way. It is
very convenient and easy to use for
writing programs or subroutines for
generating pulses of arbitrary duration,
time measurement or counting external
pulses (events) with almost no
limitations.
PIC16F887
TIMER0
TMR0 module is an 8-bit timer/counter with
the following features:
8-bit timer/counter
8 level prescaler (shared with Watchdog
timer)
Programmable internal or external clock
source
Interrupt on overflow
Programmable external clock edge
selection.
PIC16F887
TIMER0
PIC16F887
PIC16F887
PIC16F887
TIMER0 PRESCALER
PS2 PS1 PS0 TMR0 WDT
0 0 0 1:2 1:1
0 0 1 1:4 1:2
0 1 0 1:8 1:4
0 1 1 1:16 1:8
1 0 0 1:32 1:16
1 0 1 1:64 1:32
1 1 0 1:128 1:64
1 1 1 1:256 1:128
PIC16F887
TO SELECT MODE
Timer mode is selected by the T0CS bit of the
OPTION_REG register, (T0CS: 0=timer,
1=counter).
When used, the prescaler should be assigned
to the timer/counter by clearing the PSA bit of
the OPTION_REG register. The prescaler rate
is set by using the PS2-PS0 bits of the same
register.
When using interrupt, the GIE and TMR0IE
bits of the INTCON register should be set.
PIC16F887
TO MEASURE TIME
Reset the TMR0 register or write some
known value to it.
Elapsed time is measured by reading
the TMR0 register.
The flag bit TMR0IF of the INTCON
register is automatically set every time
the TMR0 register overflows. If enabled,
an interrupt occurs.
PIC16F887
TO COUNT PULSES
The polarity of pulses are to be counted
on the RA4 pin is selected by the TOSE
bit of the OPTION_REG register (T0SE:
0=positive, 1=negative pulses).
Number of pulses may be read from the
TMR0 register.
The prescaler and interrupt are used in
the same manner as in timer mode.
PIC16F887
TIMER1
Timer TMR1 module is a 16-bit
timer/counter, which means that it
consists of two registers (TMR1L and
TMR1H).
TMR1 can count up 65.535 pulses in a
single cycle before the counting starts
from zero.
PIC16F887
TIMER1 FEATURES
16-bit timer/counter register pair
Programmable internal or external clock source
3 level prescaler
Optional LP oscillator
Synchronous or asynchronous operation
Timer TMR1 gate control (count enable) via
comparator or T1G pin
Interrupt on overflow
Wake-up on overflow (external clock)
Time base for Capture/Compare function.
PIC16F887
TIMER1 FEATURES
PIC16F887
TIMER MODE
PIC16F887
TIMER MODE
In this mode, the T1SYNC bit does not
affect the timer because it counts
internal clock pulses. Since the whole
electronics uses these pulses, there is
no need for synchronization.
The microcontroller’s clock oscillator
does not operate during sleep mode so
the timer register overflow cannot cause
any interrupt.
PIC16F887
COUNTER MODE
PIC16F887
COUNTER MODE
Timer TMR1 starts to operate as a counter by
setting the TMR1CS bit. It counts pulses
brought to the PC0/T1CKI pin and is
incremented on the rising edge of the
external clock input T1CKI.
If the control bit T1SYNC of the T1CON
register is cleared, the timer TMR1 is
synchronized to the microcontroller system
clock and is called a synchronous counter.
PIC16F887
TIMER 2
Timer TMR2 module is an 8-bit timer which
operates in a very specific way
Pulses from the quartz oscillator first pass
through the prescaler. The output of the
prescaler is then used to increment the TMR2
register starting from 00h.
The values of TMR2 and PR2 are constantly
compared and the TMR2 register keeps on
being incremented until it matches the value
in PR2.
PIC16F887
TIMER 2
When the match occurs, the TMR2 register is
automatically cleared to 00h. The timer TMR2
postscaler is incremented and its output is
used to generate an interrupt if it is enabled.
The TMR2 and PR2 registers are both fully
readable and writable.
Counting may be stopped by clearing the
TMR2ON bit, which results in power saving.
The moment of TMR2 reset may also be used
to determine the baud rate of synchronous
serial communication.
PIC16F887
PIC16F887
X: 0-2
Value:
Timers 1 get a 16 bit int.
Timer 0 and 2 gets an 8 bit int.
PIC16F887
X: 0-2
Value:
If use timer 1: value is a 16 bit variable
If use timer 0 or 2 : value is an 8 bit variable
PIC16F887
EXAMPLE
SCHEMATIC
PIC16F887
CODE
PIC16F887
CODE
PIC16F887
CODE
PIC16F887
EXAMPLE
ANALYZE
8
5M 625K
20M
SCHEMATIC
CODE
PIC16F887
CODE
PIC16F887
CODE
PIC16F887
Lesson 9
TIMER