0% found this document useful (0 votes)
16 views20 pages

Chapter 3 - Timer Counter Modules

Uploaded by

DUY HUỲNH ANH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views20 pages

Chapter 3 - Timer Counter Modules

Uploaded by

DUY HUỲNH ANH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

MICROCONTROLLERS

CHAPTER 3
TIMERS – COUNTERS MODULES

Assoc.Prof.Dr Tuong Quan Vo

HCMUT - 2017
TIMER - COUNTER
Definition
-The operations of Timer and Counter in microcontroller are
similar.
-Timer: use to count the time
-Counter: use to count the event or some other things
-The capacity of Timer/Counter is based on the number of bits
they have.
Ex: Timer 8 bits  can be count from 0 to 255
Timer 16 bits  can be count from 0 to 65535
Timer 32 bits  can be count from 0 to 4294967296
-Timer/Counter have two modes: Count Up/ Count Down
- Timer needs a clock source, for example of the clock source
of 10KHz is input to a timer then one increment will take
100uS (micro second). This clock source can be obtained
from the MCU clock.
2
 2011 – Vo Tuong Quan
TIMER - COUNTER
Definition
-The operations of Timer and Counter in microcontroller are
similar.
-Timer: use to count the time
-Counter: use to count the event or some other things
-The capacity of Timer/Counter is based on the number of bits
they have.
Ex: Timer 8 bits  can be count from 0 to 255
Timer 16 bits  can be count from 0 to 65535
Timer 32 bits  can be count from 0 to 4294967296
-Timer/Counter have two modes: Count Up/ Count Down
- Timer needs a clock source, for example of the clock source
of 10KHz is input to a timer then one increment will take
100uS (micro second). This clock source can be obtained
from the MCU clock.
3
 2011 – Vo Tuong Quan
TIMER - COUNTER
Prescaler
The function of prescaler is to divide the CPU clock to obtain a
smaller frequency. Some typicle prescaler division factors:
256 128 64 32 16 8 4 2 1 (Prescaler by-passed)
What is watchdog timer?
A timer that monitors how long it takes the MCU to complete a
scan. Watchdog timers output an error message if the MCU
scan takes too long.

4
 2011 – Vo Tuong Quan
TIMER - COUNTER
Example: Using Timer of pic18f4520
TIMER0 control register

Name TMR0ON T08BIT T0CS T0SE PSA PS2 PS1 PS0


T0CON

Initial
1 1 1 1 1 1 1 1
Value

BIT7 - TMR0ON: Timer0 On, set this to 1 to start the timer.


BIT6 - T08BIT: =1 for 8 bit mode and =0 for 16 bit mode.
BIT5 - T0CS: Timer0 clock source. =1 for T0CLK pin input i.e.
counter mode. Set to 0 for internal instruction clock.
BIT4 - T0SE: Used in counter mode only. Please see
datasheet for details.
BIT3 - PSA: Prescaler Assignment. Set this to 0 to assign
prescaler or 1 to by pass it.
BIT2 to BIT0 - PS2 to PS0: Prescaler Division factor select 5
 2011 – Vo Tuong Quan
TIMER - COUNTER
PS2 PS1 PS0
0 0 0 Divide by 2
0 0 1 Divide by 4
0 1 0 Divide by 8
0 1 1 Divide by 16
1 0 0 Divide by 32
1 0 1 Divide by 64
Divide by
1 1 0
128
Divide by
1 1 1
256

6
 2011 – Vo Tuong Quan
TIMER - COUNTER
Example code:
// Using FOSC = 20MHZ, Timer 8 bit
unsigned char counter=0;//Overflow counter
void main()
{
//Setup Timer0
T0PS0=1; //Prescaler is divide by 256
T0PS1=1;
T0PS2=1;
PSA=0; //Timer Clock Source is from Prescaler
T0CS=0; //Prescaler gets clock from FMCU (5MHz)
T08BIT=1; //8 BIT MODE
TMR0IE=1; //Enable TIMER0 Interrupt
GIE=1; //Enable INTs globally
TMR0ON=1; //Now start the timer
//Set RB1 as output because we have LED on it
TRISB=0B11111101;
while(1); //Infinited loop
}
7
 2011 – Vo Tuong Quan
TIMER - COUNTER
//Main Interrupt Service Routine (ISR)
void interrupt ISR()
{
//Check if it is TMR0 Overflow ISR
if(TMR0IF == 1) //Interrupt Flag
{
//TMR0 Overflow ISR
counter++; //Increment Over Flow Counter
if(counter==76) //How to get the 76 value? How many second to delay?
{
if(RB1==0)
RB1=1;
else
RB1=0;
counter=0; //Reset Counter
}
//Clear Flag
TMR0IF=0;
TMR0ON=1; } }
8
 2011 – Vo Tuong Quan
TIMER - COUNTER
How to get the value 76 of counting value?
Prescaler = FMCU/256 (Note: FMCU= Fosc/4)
As our FMCU=20MHz/4 (We are running 20MHz XTAL) =5MHz
Time Period = 0.2uS
Prescaller Period = 0.2 x 256 = 51.2uS
Overflow Period = 51.2 x 256 = 13107.2 uS = 0.0131072 sec
So we need 1/0.0131072
Over Flows to count for 1 sec = 76.2939 Overflows

9
 2011 – Vo Tuong Quan
TIMER - COUNTER

OPTION_REG:
PSA=0; // Prescaler is assigned to the Timer0 module
PS0=1; // Prescaler rate bits
PS1=1; // are set to “111”
PS2=1; // which means divide by 256
TOSE=0; // rising edge
TOCS=0; // Internal instruction cycle clock

10
 2011 – Vo Tuong Quan
TIMER - COUNTER
Another method to calculate the value of Timer:
The TIMER0 period could be calculated using this formula:
TIMER0 period = [(TMR0 + 1)] x 4 x Tosc x (TIMER0 prescaler
value)
Ex: By selecting the TIMER0 prescaler of 2; PS2=0, PS1=0 and
PS0=0 bits in OPTION_REG register and Initial the TMR0
register value to 156 (99 more counts to reach its maximum
value of 255) with the system frequency clock of 8 Mhz.
the PIC microcontroller TIMER0 overflow period can be
calculated as follow:
TIMER0 period = [((255 - 156) + 1)] x 4 x 1/8000000 x 2 =
0.0001s = 0.1 ms
T = 500ms, Pre-scaller = 32  TMR0 = ?

11
 2011 – Vo Tuong Quan
Sample Code TIMER - COUNTER
/* Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
0.0005 ms x 2 * 100 = 0.1 ms */
OPTION = 0b00000000; // 1:2 Prescaller
TMR0 = 156; // Interupt every 0.1 ms
TMR0IE = 1; // Enable interrupt on TMR0 overflow
GIE = 1; // Global interrupt enable
………………..
// ISR (Interrupt Service Routine)
if(TMR0IF) { // TIMER0 Interrupt Flag
pulse_max++; // Pulse Max Increment
pulse_top++; // Pulse Top Increment
if (pulse_max >= MAX_VALUE) {
pulse_max=0;
pulse_top=0;
RC2=0; // Turn Off RC2
}
if (pulse_top == top_value) {
RC2=1; }
TMR0 = 156; // Initial Value for 0.1ms Interrupt
TMR0IF = 0; // Clear TIMER0 interrupt flag
} 12
 2011 – Vo Tuong Quan
TIMER - COUNTER
How to calculate the value of 156 or the value 99 of the TMR0?
TIMER0 period = [(TMR0 + 1)] x 4 x Tosc x (TIMER0 prescaler value)
 TMR0 = {TIMER0 period/(4 x Tosc x TIMER0 prescaler value)} – 1
 TMR0 = {0.1ms / (4 x (1/8MHz)) x 2)} – 1 = 99
 TMR0 = {0.0001s / (4 x (1/8000000) x 2)} - 1= 99 (Count value)
The value input to the TMR0 register =
= Max timer value – Count = 255 – 99 = 156

13
 2011 – Vo Tuong Quan
TIMER - COUNTER
If using INTERNAL crystal as clock:

fout– The output frequency after the division.


Tout – The Cycle Time after the division.
4 - The division of the original clock (4 MHz) by 4,
when using internal crystal as clock (and not external oscillator).
Count - A numeric value to be placed to obtain the desired
output frequency - Fout.
(256 - TMR0) - The number of times in the timer will count
based on the register TMR0.

14
 2011 – Vo Tuong Quan
TIMER - COUNTER
Example:
Suppose we want to create a delay of 0.5 second using
Timer0. What is the value of Count?
Calculation:
First, let’s assume that the frequency division by the Prescaler
will be 1:256. Second, let’s set TMR0=0. Thus:

15
 2011 – Vo Tuong Quan
TIMER - COUNTER
If using EXTERNAL crystal as clock:

Example:
What is the output frequency - Fout, when the external oscillator
is 100kHz and Count=8?
Calculation:
First, let’s assume that the frequency division by the Prescaler will
be 1:256. Second, let’s set TMR0=0.

16
 2011 – Vo Tuong Quan
TIMER - COUNTER
Example: Delay 1 second

17
 2011 – Vo Tuong Quan
TIMER - COUNTER
Exercise:
1. Using PIC16f877, the oscillator is 20MHz, write the
program to create the square pulse on pin A1 with the duty
cycle is 50% and the frequency is 20KHz.
Pre = 2, TMRx = 100
2. Using PIC16f877, the oscillator is 20MHz, write the
program to create the square pulse on pin A1 with the duty
cycle is 70% and the frequency is 20KHz.
Pre = 256, TMRx = 100
Exercise 1 Exercise 2
Method 1 Count = 2 Count (70) = 1
Count (30) = 1
Method 2 TMR0 = TMR0 =
Method 3 Count = Count =
TMR0 = TMR0 = 18
 2011 – Vo Tuong Quan
TIMER - COUNTER
Timer 8 bits – Timer 16 bits
What is the differences between them?

Requirements for GP2D02 exercise:


1. Control circuit using PIC MCU?
2. Control algorithm to get the real distance from robot to
obstacle?
3. Write the control program?

19
 2011 – Vo Tuong Quan
TIMER - COUNTER

Sử dụng Led 7 đoạn, sử dụng Timer viết chương trình


tạo đồng hồ số (Giờ, phút, giây)!

20
 2011 – Vo Tuong Quan

You might also like