0% found this document useful (0 votes)
12 views

Bai9 TimerCounter

Uploaded by

Dương Thịnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Bai9 TimerCounter

Uploaded by

Dương Thịnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

PIC16F887

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

CLOCK SOURCE SELECTION


CLOCK SOURCE TMR1CS
Fosc/4 0
T1CKI pin 1
 When the internal clock source is selected, the TMR1H-
TMR1L register pair will be incremented on multiples of
Fosc pulses as determined by the prescaler.
 When the external clock source is selected, this timer
may operate as a timer or a counter. Clock in counter
mode can be synchronized with the microcontroller
internal clock or run asynchronously. In the event that an
external clock oscillator is needed and the PIC16F887
microcontroller is using INTOSC with CLKOUT, timer
TMR1 can use the LP oscillator as a clock source.
PIC16F887

TIMER TMR1 OSCILLATOR


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

TIMER MAIN COMMANDS


setup_timer_X( mode )
X: 0-2
Mode:
- Timer or counter mode or disable timer
- Prescaler
Constants from different groups may be or'ed
together with |.

Ex: setup_timer_1 (T1_INTERNAL| T1_DIV_BY_8);


Setup timer 1 as timer mode and use prescaler 8
* Open file 16f887.h and find timer used to see
options you can use with this command
PIC16F887

TIMER MAIN COMMANDS


SET_TIMERX( value)
Sets the count value

X: 0-2
Value:
Timers 1 get a 16 bit int.
Timer 0 and 2 gets an 8 bit int.
PIC16F887

TIMER MAIN COMMANDS


value=get_timerX();
Gets the count value

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

Count externals pulse and display


the counted value on the second line
of LCD using timer 0
PIC16F887

SCHEMATIC
PIC16F887

CODE
PIC16F887

CODE
PIC16F887

CODE
PIC16F887

EXAMPLE

Measure one second using timer 1


and display the result of counting
second from 0 to 59 on the second
line of LCD
PIC16F887

ANALYZE
8

5M 625K
20M

 Consider that crystal used is 20Mhz


 After divided 4 the clock is 5Mhz => It mean that counter
must count 5M pulses to be one second
 The counter is limited to 65535 so we must use prescaler =>
Choose prescaler 8 => counter now must count 625k pulses
to be one second=> Count ten times each 62500 pulses=>
need to set counter to count from 65536 – 62500 = 3036
PIC16F887

SCHEMATIC

Control LCD display string


“Test LCD 16x2” on line 2
PIC16F887

CODE
PIC16F887

CODE
PIC16F887

CODE
PIC16F887

Test the theory by simulink


PIC16F887

Lesson 9

TIMER

You might also like