PIC16F877 Timer Modules tutorials - Timer2
PIC
Timer2 tutorial
The Timer2 module, timer/counter,
has the following features:
- two 8-bit registers (TMR2 and PR2)
- readable and writable
- a prescaler and a postscaler
- connected only to an internal clock - 4 MHz crystal
- Interrupt on overflow
Let’s
explain the features of PIC Timer2 we have listed above:
• Timer2
has 2 count registers: TMR2 and PR2. The size of each registers is 8-bit in
which we can write numbers from 0 to 255. The TMR2 register is readable and
writable and is cleared on any device Reset. PR2 is a readable and writable
register and initialized to FFh upon Reset.
Register TMR2 is used to store the "initial" count value (the value from which it begins to count). Register PR2 is used to store the "ending" count value (the maximum value we need/want to reach). ie: using Timer2 we can determine the started count value, the final count value, and the count will be between these two values. The Timer2 increments from 00h until it matches PR2 and then resets to 00h on the next increment cycle.
Register TMR2 is used to store the "initial" count value (the value from which it begins to count). Register PR2 is used to store the "ending" count value (the maximum value we need/want to reach). ie: using Timer2 we can determine the started count value, the final count value, and the count will be between these two values. The Timer2 increments from 00h until it matches PR2 and then resets to 00h on the next increment cycle.
• Prescaler
and Postscaler - Timer2 is an 8-bit timer with a prescaler and a postscaler.
Each allows to make additional division of the frequency clock source.
Prescaler divides the frequency clock source BEFORE the counting take place at the register TMR2, thus the counting inside the TMR2 register is performed based on the divided frequency clock source by the Prescaler
The match output of TMR2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive) to generate a TMR2 interrupt (latched in flag bit, TMR2IF (PIR1<1>)).
Postscaler divides the frequency that comes out of the Comparator again for the last time.
Prescaler divides the frequency clock source BEFORE the counting take place at the register TMR2, thus the counting inside the TMR2 register is performed based on the divided frequency clock source by the Prescaler
The match output of TMR2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive) to generate a TMR2 interrupt (latched in flag bit, TMR2IF (PIR1<1>)).
Postscaler divides the frequency that comes out of the Comparator again for the last time.
TIMER2
Prescaler and Postscaler
• TMR2IF - TMR2 to PR2 Match Interrupt Flag bit.
• Comparator – Compares the value of the register TMR2 and the maximum value of the register PR2.
• TMR2 – The register in which the "initial" count value is written.
• PR2 – The register in which the final or the maximum count value is written.
We perform
all the necessary settings with T2CON Register The structure of the T2CON
register:
As we can see, the size of the register is 8 bits. Let's explore the relevant bits:
T2CKPS1:T2CKPS0:
Timer2 Clock Prescale Select bits
The input clock (FOSC/4) has a prescale option of 1:1, 1:4 or 1:16, selected by control bits T2CKPS1:T2CKPS0 (T2CON<1:0>).
The input clock (FOSC/4) has a prescale option of 1:1, 1:4 or 1:16, selected by control bits T2CKPS1:T2CKPS0 (T2CON<1:0>).
00 =
Prescaler is 1
01 = Prescaler is 4
1x = Prescaler is 16
01 = Prescaler is 4
1x = Prescaler is 16
TMR2ON:
Timer2 On bit
Timer2 can be shut-off by clearing control bit, TMR2ON (T2CON<2>), to minimize power consumption.
Timer2 can be shut-off by clearing control bit, TMR2ON (T2CON<2>), to minimize power consumption.
1 = Timer2
is on
0 = Timer2 is off
0 = Timer2 is off
TOUTPS3:TOUTPS0:
Timer2 Output Postscale Select bits
The match output of TMR2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive selected by control bits TOUTPS3:TOUTPS0 (T2CON<6:3>).
The match output of TMR2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive selected by control bits TOUTPS3:TOUTPS0 (T2CON<6:3>).
0000 = 1:1
postscale
0001 = 1:2 postscale
0010 = 1:3 postscale
•
•
•
1111 = 1:16 postscale
0001 = 1:2 postscale
0010 = 1:3 postscale
•
•
•
1111 = 1:16 postscale
The
following is an example how we can initialize the T2CON register:
1. TMR2ON=1;
// the timer is enable
2. T2CKPS0=0; // Prescaler – 1:1
3. T2CKPS1=0;
2. T2CKPS0=0; // Prescaler – 1:1
3. T2CKPS1=0;
4.
TOUTPS0=1; // Postscaler – 1:16
5. TOUTPS0=1;
6. TOUTPS0=1;
7. TOUTPS0=1;
5. TOUTPS0=1;
6. TOUTPS0=1;
7. TOUTPS0=1;
Or you can
set all the T2CON register at once as follows:
T2CON=0b01111100;
T2CON=0b01111100;
TIMER2 BLOCK
DIAGRAM
How to calculate the required values of the TIMER2:
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.
(PR2 – TMR2) - The number of times the counter will count.
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.
(PR2 – TMR2) - The number of times the counter will count.
Simple
example and calculation of how to use TIMER2:
Suppose we
want to create a delay of 1 second in the our program using Timer2. What is the
value of Count?
Calculation:
First, let's
assume that the frequency division by the Prescaler will be 1:1 and Postscaler
will be 1:16. Second, let's set TMR1=0 and PR2=255. Thus:
The following simple C language program creates a delay of 1 sec using TIMER2:
No comments:
Post a Comment