Beruflich Dokumente
Kultur Dokumente
Microcontrollers
PIC Timer0
PIC Timer1
PIC Timer2
Microcontrollers
PIC Timer0
The Timer0 module timer/counter has the following
features:
8-bit timer/counter
Readable and writable
8-bit software programmable prescaler
Internal (FOSC/4) or external clock select
Interrupt on overflow from FFh to 00h
Edge select (rising or falling) for external clock
Microcontrollers
Microcontrollers
Fout
The output frequency after the division.
Tout
The Cycle Time after the division.
4
The division of the original clock by 4
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.
Microcontrollers
Microcontrollers
Microcontrollers
Test circuit
Microcontrollers
Microcontrollers
11
Microcontrollers
Timer0-Counter
Microcontrollers
#include<16f877a.h>
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
void main() {
unsigned int8 count;;
setup_counters (RTCC_EXT_L_TO_H,RTCC_DIV_1);
set_timer0(0);
do {
if (count !=get_timer0()){
count=get_timer0();
printf("%U times.\n\r", count);
}
} while (TRUE);
}
12
13
Microcontrollers
TIMER1 MODULE
Microcontrollers
Microcontrollers
TIMER1 BLOCK
DIAGRAM
15
Microcontrollers
Microcontrollers
18
Microcontrollers
Microcontrollers
Timer1-Counter
#include<16f877a.h>
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
void main() {
unsigned int16 count;
set_timer1(3600);
setup_timer_1(t1_external | T1_DIV_BY_1);
delay_ms(500);
count=get_timer1();
do {
if (count !=get_timer1()){
count=get_timer1();
printf("%LU times.\n\r", count);
}
} while (TRUE);
}
19
Microcontrollers
Timer1-Counter
20
Timer2
Microcontrollers
21
Microcontrollers
TIMER2 BLOCK
DIAGRAM
22
Microcontrollers
TIMER2 BLOCK
DIAGRAM
Timer2 has 2 count registers 8-bits: TMR2 and PR2.
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.
23
Microcontrollers
TIMER2 BLOCK
DIAGRAM
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
24
Comparator again for the last time.
Microcontrollers
25
Microcontrollers
26
Microcontrollers
//Interrupt procedure
//called every time Timer2
//flips from 156 to 0
if(high_count==0) {
++seconds;
high_count=high_start;
//Inc SECONDS counter every
}
//125 times to keep time
}
27
Microcontrollers
void main() {
//a simple stopwatch program
unsigned int8 start, time;
high_count = high_start;
setup_timer_2(T2_DIV_BY_16,0x9c,16); //PR2=0x9C=156, Postscaler=16
set_timer2(0);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
do {
printf("Press any key to begin.\n\r");
printf("Press any key to stop.\r\n");
printf("%U seconds.\n\r", time);
} while (TRUE);
}
getc();
getc();
start = seconds;
time = seconds - start;
28
29
Microcontrollers