Chapter 9 - TIMER - COUNTER

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

HCMC University of Technology and Education

Faculty of Electrical & Electronic Engineering


No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

MICROCONTROLLERS

NGUYEN THANH NGHIA


11/25/2020 11
NGUYEN THANH NGHIA
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

CHAPTER 9:
TIMER - COUNTER

NGUYEN THANH NGHIA


11/25/2020 22
NGUYEN THANH NGHIA
Outline
1. Introduction.
2. Timer main commands.
3. Application: TIMER MODE.
4. Application: COUNTER MODE.

NGUYEN THANH NGHIA 3


Chapter 9: TIMER - COUNTER
1. Introduction
 The PIC16F887 microcontroller has three
completely separate timers/counters marked as:

 TMR0
 TMR1
 TMR2

NGUYEN THANH NGHIA 4


Chapter 9: TIMER - COUNTER
1. Introduction
1.1 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.

NGUYEN THANH NGHIA 5


Chapter 9: TIMER - COUNTER
1. Introduction
1.1 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.
NGUYEN THANH NGHIA 6
Chapter 9: TIMER - COUNTER
1. Introduction
1.1 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.
NGUYEN THANH NGHIA 7
Chapter 9: TIMER - COUNTER
1. Introduction
1.1 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.
NGUYEN THANH NGHIA 8
Chapter 9: TIMER - COUNTER
1. Introduction
1.1 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.
NGUYEN THANH NGHIA 9
Chapter 9: TIMER - COUNTER
1. Introduction
1.1 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 NGUYEN1 1:256
THANH NGHIA 1:128 10
Chapter 9: TIMER - COUNTER
1. Introduction
1.1 TIMER0
 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.
NGUYEN THANH NGHIA 11
Chapter 9: TIMER - COUNTER
1. Introduction
1.1 TIMER0
 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.

NGUYEN THANH NGHIA 12


Chapter 9: TIMER - COUNTER
1. Introduction
1.1 TIMER0
 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.

NGUYEN THANH NGHIA 13


Chapter 9: TIMER - COUNTER
1. Introduction
1.2 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.

NGUYEN THANH NGHIA 14


Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 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.
NGUYEN THANH NGHIA 15
Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 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.
NGUYEN THANH NGHIA 16
Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 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.
NGUYEN THANH NGHIA 17
Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 Timer TMR1 oscillator

NGUYEN THANH NGHIA 18


Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 Timer mode

NGUYEN THANH NGHIA 19


Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 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.

NGUYEN THANH NGHIA 20


Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 Counter mode

NGUYEN THANH NGHIA 21


Chapter 9: TIMER - COUNTER
1. Introduction
1.2 TIMER1
 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.
NGUYEN THANH NGHIA 22
Chapter 9: TIMER - COUNTER
1. Introduction
1.3 TIMER2
 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.

NGUYEN THANH NGHIA 23


Chapter 9: TIMER - COUNTER
1. Introduction
1.3 TIMER2
 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.
NGUYEN THANH NGHIA 24
Chapter 9: TIMER - COUNTER
1. Introduction
1.3 TIMER2
 Counter mode

NGUYEN THANH NGHIA 25


Chapter 9: TIMER - COUNTER
2. 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
NGUYEN THANH NGHIA 26
Chapter 9: TIMER - COUNTER
2. 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.

NGUYEN THANH NGHIA 27


Chapter 9: TIMER - COUNTER
2. 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

NGUYEN THANH NGHIA 28


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.1 Application 1: using TIMER1
 Control 8 LEDs by using PIC 16F887 with
TIMER1. In which the delay time is 210ms.
 Schematic:

NGUYEN THANH NGHIA 29


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.1 Application 1: using TIMER1
 Control 8 LEDs by using PIC 16F887 with
TIMER1. In which the delay time is 210ms.
 Flowchart:

NGUYEN THANH NGHIA 30


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.1 Application 1: using TIMER1
 Code: #INCLUDE <TV_16F887.C>
#BIT TMR1IF = 0x0C.0 //bit thu 0 (TMR1IF) cua PIR1
UNSIGNED INT8 X;
VOID MAIN()
{
SET_TRIS_D(0x00);
X=0X00;
OUTPUT_D(X);
SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
SET_TIMER1(0);
WHILE(TRUE)
{
IF (TMR1IF==1)
{
X=~X; OUTPUT_D(X); TMR1IF=0;
}
}
}

NGUYEN THANH NGHIA 31


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.1 Application 1: using TIMER1
 Explanation:
 Initialize timer1 to count internal pulses of 20MHz through
the divider 4, so frequency pulse is 5MHz. Using a prescaler
with a divider ratio of 8, frequency pulse is 5MHz/8 =
0.625MHz or equal to 625kHz, the period is 1.6µs.
 If statement checks overflow flag TMR1IF of timer1: if
not overflow then wait until overflow then TMR1IF is 1, the
command reverses X, this value sends out the port will light
up the LEDs, clear the overflow flag.
NGUYEN THANH NGHIA 32
Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.1 Application 1: using TIMER1
 Explanation:
 Timer T1 counts 65536, the counting time is equal to the
number of pulses multiplied by the cycle each pulse: pulse ×
Txung = 65536 × 1.6µs = 104857.6 µs ≈ 105ms, so the light
on and off time of 8 LEDs are equal to 105ms or cycle equal
to 210ms.

NGUYEN THANH NGHIA 33


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.2 Application 2: using TIMER1
 Explanation:
 In this program, we use bit TMR1IF to check but library
<16F887.H> does not define this bit, so there is command
line "#bit tmr1if = 0x0C.0" to define overflow flag bit.
 Overflow flag is located in bit 0, in register PIR1 has
address 0x0C.

NGUYEN THANH NGHIA 34


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.2 Application 2: using TIMER1
 Control 8 LEDs by using PIC 16F887 with
TIMER1. In which the delay time is 200ms.
 Schematic:

NGUYEN THANH NGHIA 35


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.2 Application 2: using TIMER1
 Control 8 LEDs by using PIC 16F887 with
TIMER1. In which the delay time is 200ms.
 Flowchart:

NGUYEN THANH NGHIA 36


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.2 Application 2: using TIMER1
 Control 8 LEDs by #INCLUDE <TV_16F887.C>
#BIT TMR1IF = 0x0C.0
UNSIGNED INT8 X;
using PIC 16F887 with VOID MAIN()
{
TIMER1. In which the SET_TRIS_D(0x00); X=0X00; OUTPUT_D(X);
SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
delay time is 200ms. SET_TIMER1(3036);
WHILE(TRUE)
 Code: {
IF (TMR1IF==1)
{
X=~X; OUTPUT_D(X); SET_TIMER1(3036);
TMR1IF=0;
}
}
}

NGUYEN THANH NGHIA 37


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.2 Application 2: using TIMER1
 Explanation:
 This program initializes the count for timer T1 is
3036, timer counts to 62500 pulses then overflow,
the number of pulses count is 62500 pulses, each
pulse is 1.6µs so the ON time LED is equal to OFF
time LED and it is 62500 ×1,6µs = 100.000µs =
100ms. Therefore, the period is 200ms.

NGUYEN THANH NGHIA 38


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.2 Application 2: using TIMER1
 Explanation:
 You can calculate the period time as the following: the
delay time is 100ms or 100,000µs, each pulse cycle is 1.6µs.
Then the number of pulses counted by the delay time
divided by the pulse cycle. It means that number of pulse
is 100,000µs / 1.6µs = 62500 pulses. It matches with the 16
bit value of Timer1.
 If the number of pulses is greater than the number of the
timer bits, we must reduce the count time.
NGUYEN THANH NGHIA 39
Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.3 Application 3: using TIMER1
 Control 8 LEDs by using PIC 16F887 with TIMER1.
In which the delay time is 2s, 1s ON, 1s OFF.
 Schematic:

NGUYEN THANH NGHIA 40


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.3 Application 3: using TIMER1
 Control 8 LEDs by using PIC 16F887 with TIMER1.
In which the delay time is 2s, 1s ON, 1s OFF.
 Flowchart:

NGUYEN THANH NGHIA 41


Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.3 Application 3: using#INCLUDE
TIMER1 <TV_16F887.C>
 Control 8 LEDs by #BIT TMR1IF = 0x0C.0
UNSIGNED INT8 X, BDT;

using PIC 16F887 VOID {


MAIN()

SET_TRIS_D(0x00); X=0; BDT=0; OUTPUT_D(X);


with TIMER1. In SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
SET_TIMER1(3036);
which the delay WHILE(TRUE)
{
time is 2s, 1s ON, IF (TMR1IF==1)
{
TMR1IF=0; SET_TIMER1(3036); BDT++;
1s OFF. //BDT=5 se 0.5s sang, 0.5s tat
IF (BDT ==10)
 Code: {
OUTPUT_D(X); X=~X; BDT = 0;
}
}
}
}
NGUYEN THANH NGHIA 42
Chapter 9: TIMER - COUNTER
3. Application: TIMER MODE
3.3 Application 3: using TIMER1
 Explanation:
 The main program checks the overflow flag, if
overflows, then clear overflow flag and increase the
overflow counter OCV to 1. Checking the variable OCV
is 10 or not, if not, then exit and wait for the next
overflow. If it OCV is 10 then invert logic level of LED
and clear variable OCV.
 The time ON equals time OFF and it is
100ms×10=1000ms = 1s.
NGUYEN THANH NGHIA 43
Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX1
 Count externals pulse using Counter T0 in
PIC 16F887. The counter value is displayed on
3 seven segments led connected directly with 3
ports. The counter value range is from 000 to
255.

NGUYEN THANH NGHIA 44


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX1
 Schematic:

NGUYEN THANH NGHIA 45


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX1
 Explanation:
 The pulse generator circuit uses the receiver and transmitter
leds, when there is no cover between the receiver and
transmitter leds, the transistor is biased to lead saturation, the
output C pin is at 0. When there is a product covering
between the transmitter and transistor, the transistor is not
biased, so the output C pin is at 1. When the product passes,
the signal returns to zero, so each passing product will
generate 1 pulse.
NGUYEN THANH NGHIA 46
Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX1
 Flowchart:

NGUYEN THANH NGHIA 47


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX1
 Code:
#INCLUDE<TV_16F887.C>
UNSIGNED INT8 T0;
VOID MAIN()
{
SET_TRIS_B(0x00); SET_TRIS_C(0x00); SET_TRIS_D(0x00);
SETUP_TIMER_0(T0_EXT_L_TO_H | T0_DIV_1);
SET_TIMER0(0);
WHILE(TRUE)
{
T0=GET_TIMER0();
OUTPUT_B(MA7DOAN[T0%10]);
OUTPUT_C(MA7DOAN[T0/10%10]);
OUTPUT_D(MA7DOAN[T0/100]);
}
}
NGUYEN THANH NGHIA 48
Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX1
 Explanation:
 The program configures 3 ports to output data, the timer
operates in external pulse count mode, the prescaler is
divided by 1, for the initial count value is 0.
 While loop proceed to read the value of timer0 assigned to
the variable "T0", proceed to divide each number of units,
tens, hundred, decode 7 segments and send out the control
port led light.

NGUYEN THANH NGHIA 49


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX1
 Explanation:
 The program configures 3 ports to output data, the timer
operates in external pulse count mode, the prescaler is divided
by 1, for the initial count value is 0.
 While loop proceed to read the value of timer0 assigned to the
variable "T0", proceed to divide each number of units, tens,
hundred, decode 7 segments and send out the control port led
light.
 Because the counter T0 has only 8 bits, the counter value is
limited from 0 to 255, if you want to count a larger value, you
must handle or use counter T1 to count 16 bits.
NGUYEN THANH NGHIA 50
Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX2
 Create program is same as EX1. However,
this circuit is remove leading zeros. It means
that the number 0 in hundred and ten will be
OFF when counting.

NGUYEN THANH NGHIA 51


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX2
 Schematic:

NGUYEN THANH NGHIA 52


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.1 Count externals pulse using COUNTER T0 EX2
#INCLUDE<TV_16F887.C>
 Code: UNSIGNED INT8 T0, MATRAM, MACHUC;
VOID MAIN()
{
SET_TRIS_B(0x00); SET_TRIS_C(0x00); SET_TRIS_D(0x00);
SETUP_TIMER_0(T0_EXT_L_TO_H | T0_DIV_1);
SET_TIMER0(0);
WHILE(TRUE)
{
T0=GET_TIMER0();
MATRAM=MA7DOAN[T0/100];
MACHUC=MA7DOAN[T0/10%10];
IF (MATRAM==0XC0)
{
MATRAM=0XFF;
IF (MACHUC==0XC0) MACHUC=0XFF;
}
OUTPUT_B(MA7DOAN[T0%10]);
OUTPUT_C(MACHUC);
OUTPUT_D(MATRAM);
}
}

NGUYEN THANH NGHIA 53


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.2 Count externals pulse using COUNTER T1
 Count externals pulse using COUNTER T1,
the counter value displays on two led
connected directly with 2 ports. The counter
range value is from 00 to 99.

NGUYEN THANH NGHIA 54


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.2 Count externals pulse using COUNTER T1
 Schematic:

NGUYEN THANH NGHIA 55


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.2 Count externals pulse using COUNTER T1
 Flowchart:

NGUYEN THANH NGHIA 56


Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.2 Count externals pulse using COUNTER T1
 Code: #INCLUDE<TV_16F887.C>
UNSIGNED INT16 T1;
VOID MAIN()
{
SET_TRIS_B(0x00); SET_TRIS_D(0x00);
SETUP_TIMER_1(T1_EXTERNAL |
T1_DIV_BY_1 );
SET_TIMER1(0);
WHILE(TRUE)
{
T1=GET_TIMER1();
OUTPUT_B(MA7DOAN[T1%10]);
IF (T1/10==0) OUTPUT_D(0XFF);
ELSE OUTPUT_D(MA7DOAN[T1/10]);
IF(T1==100) SET_TIMER1(0);
}
}
NGUYEN THANH NGHIA 57
Chapter 9: TIMER - COUNTER
4. Application: COUNTER MODE
4.2 Count externals pulse using COUNTER T1
 Explanation:
 The program initializes 2 ports to output data,
the timer operates in the external pulse count
mode, the coefficient is divided by 1, the initial
count value is 0.
 While loop proceeds to read the value of
TIMER1 assigned to the variable "T1", proceed
to divide each number of units, tens, decode to 7
segments led and send out the control port.
NGUYEN THANH NGHIA 58
Chapter 9: TIMER - COUNTER

The end!

NGUYEN THANH NGHIA 59

You might also like