-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.. -Timer: use to count the tim
Trang 1CHAPTER 3
TIMERS – COUNTERS MODULES
Dr Vo Tuong Quan
Trang 2TIMER - 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
Trang 3TIMER - 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
Trang 4TIMER - 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.
Trang 5TIMER - COUNTER
Example: Using Timer of pic18f4520
TIMER0 control rsegister
1 1
1 1
1 1
1 1
Initial
Value
PS0 PS1
PS2 PSA
T0SE T0CS
T08BIT TMR0ON
Name
T0CON
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
Trang 6TIMER - COUNTER
Divide by 256
1 1
1
Divide by 128
0 1
1
Divide by 64
1 0
1
Divide by 32
0 0
1
Divide by 16
1 1
0
Divide by 8
0 1
0
Divide by 4
1 0
0
Divide by 2
0 0
0
PS0 PS1
PS2
Trang 7TIMER - COUNTER
// 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
}
Example code:
Trang 8TIMER - COUNTER
//Main Interrupt Service Routine (ISR)
void interrupt ISR()
{
//Check if it is TMR0 Overflow ISR
if(TMR0IF) //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; } }
Trang 9TIMER - 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
Trang 10TIMER - 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
Trang 11TIMER - 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
Trang 12TIMER - COUNTER
Sample Code
/* 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
T0IE = 1; // Enable interrupt on TMR0 overflow
GIE = 1; // Global interrupt enable
………
// ISR (Interrupt Service Routine)
if(T0IF) { // 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) {
TMR0 = 156 ; // Initial Value for 0.1ms Interrupt
T0IF = 0; // Clear TIMER0 interrupt flag
Trang 13TIMER - 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
Trang 14TIMER - 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
Trang 15TIMER - 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:
Trang 16TIMER - COUNTER
If using EXTERNAL crystal as clock:
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
Example:
Trang 17TIMER - COUNTER
Example: Delay 1 second
Trang 18TIMER - 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 5KHz
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 10KHz
Trang 19TIMER - COUNTER
Timer 8 bits – Timer 16 bits
What is the differences between them?