4.6 Timers
4.6 Timers
4.6 Timers
What is a timer
a counter:
can be driven by :
three timers:
TMR0 :
writing to TMR0:
configuration of Timer0
Prescaler Assignment
1: WDT
0: TMR0
1.
2.
3.
Bit Value
Timer0 Rate
000
1:2
001
1:4
010
1:8
011
1 : 16
100
1 : 32
101
1 : 64
110
1 : 128
111
1 : 256
prepared by : Maher Al-omari
Prescaler Assignment
timer0 source edge
PROGRAMMABLE PRESCALER:
Given that the PIC is clocked at 4 MHZ, what is the maximum
period that Timer0 can measure without the prescaler?
SUMMARY OF REGISTERS
ASSOCIATED WITH TIMER0
SETUP_TIMER_0(mode)
Function:
configure timer 0 .
Mode:
One constant may be used from each group or'ed
together with the | operator.
RTCC_INTERNAL,
RTCC_EXT_L_TO_H
RTCC_EXT_H_TO_L
RTCC_DIV_1,
RTCC_DIV_2,
RTCC_DIV_4,
RTCC_DIV_8,
RTCC_DIV_16,
RTCC_DIV_32,
RTCC_DIV_64,
RTCC_DIV_128,
RTCC_DIV_256
prepared by : Maher Al-omari
SETUP_TIMER_0(mode)
Example1:
setup_timer_0 (RTCC_DIV_8 | RTCC_INTERNAL);
Example2:
setup_timer_0 (RTCC_EXT_H_TO_L | RTCC_DIV_64);
Example3:
setup_timer_0 (RTCC_INTERNAL);
Example4:
setup_timer_0 (RTCC_EXT_L_TO_H);
Prescaler=2
Prescaler=2
Example4:
No prescaler
setup_timer_0 (RTCC_EXT_L_TO_H | RTCC_DIV_1);
prepared by : Maher Al-omari
set_timer0(value)
1.
2.
3.
value=get_timer0()
Parameters: None
Returns: Timer 0 returns a 8 bit int
Function: Returns the count value of a real time
clock/counter. RTCC and Timer0 are the
same.
prepared by : Maher Al-omari
#include "16F877A.H"
void main() {
setup_timer_0( RTCC_INTERNAL|RTCC_DIV_256 );
set_timer0(0);
If the pic freq. is 4mhz
How long will program
output_b(0x0f);
wait here?
while ( get_timer0() < 200 ) ;
output_b(0xf0);
}
prepared by : Maher Al-omari
#include "16F877A.H"
void del_1m() {
#BIT T0IF = 0x0B.2 // map a bit to a c variable
setup_timer_0( RTCC_INTERNAL|RTCC_DIV_8 );
set_timer0(130);
while (!T0IF);
//wait for timer0 interrupt flag
clear_interrupt(int_timer0); } // must clear TOIF bit in INTCON
void main() {
while (1) {
del_1m(); output_b(0x0f);
del_1m(); output_b(0xf0); }}
# include "16F877A.h"
void del_1m() {
#BIT T0IF = 0x0B.2
setup_timer_0( RTCC_INTERNAL|RTCC_DIV_32 );
set_timer0(130);
while (!T0IF);
replace
clear_interrupt(int_timer0);}
void main(){
int n;
Given that the
int8 anum[10];
freq. is 4mhz ;
anum[0] =0x3f; anum[1] =0x06;
What is the
anum[2] =0x5b;anum[3] =0x4f;
function of the
while(1)
program?
for ( n = 0; n < 4; n++ )
{ output_c(anum[n]);
del_1m();}
}
prepared by : Maher Al-omari
void main() {
#BIT flag = 0x0B.2
setup_timer_0( RTCC_INTERNAL|RTCC_DIV_128 );
set_timer0(59);
while (!flag);
How much time
Flag=0;}
will the PIC take to
999 msec
execute this
statement?
No PSA
Stop watch-1
connect a virtual terminal to the RS232 hardware in the PIC
Terminal
screen
RS232 PIC
connection
Virtual
prepared by : Maher Al-omari
terminal
Stop watch-2
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define INTS_PER_SECOND 31
# of interrupts /sec
BYTE seconds;
BYTE int_count;
#int_rtcc
void clock_isr() {
if(--int_count==0) { ++seconds;
int_count=INTS_PER_SECOND;}}
Stop watch-3
void main() {
BYTE start;
int_count=INTS_PER_SECOND;
set_timer0(0);
setup_timer_0( RTCC_INTERNAL|RTCC_DIV_256);
enable_interrupts(INT_RTCC);
An interrupt every
32.6 msec
enable_interrupts(GLOBAL);
do {
printf("press any key to begin timming:( )\n\r");
getc(); // waits for a key to be entered.
start=seconds; // the start time.
printf("Press any key to stop the watch:( )\n\r");
Current time start time
getc(); // waits for a key to be entered.
printf("your time is %u seconds.\n\r", seconds-start ) ;
while (TRUE); }
}
prepared by : Maher Al-omari