Vous êtes sur la page 1sur 22

Timer 0 operations

prepared by : Maher Al-omari

What is a timer


a counter:

can be driven by :

Counts independently while the PIC performs other tasks.

the processor's instruction clock  a timer.


an external signal  counting transitions on an input pin.

commonly used: to drive interrupts to allow regularly timed


"background" tasks to run.

prepared by : Maher Al-omari

Timer0 and TMR0




three timers:

TMR0 :

the simplest of these is referred to as Timer0.


the visible part, an 8-bit register,
holds the current value of the timer.
It is readable and writeable.

writing to TMR0:

starts incrementing from the written value.


sets an "overflow flag (the TOIF bit in the INTCON register)
to indicate that a rollover happened.

prepared by : Maher Al-omari

configuration of Timer0

timer0 clock source


0 : the instruction
clock.
1 : the TOCKI pin.

The clock edge (counter


mode)
0 : rising edge of TOCKI.
1 : falling edge of TOCKI.
prepared by : Maher Al-omari

configuring the pre-scaler of Timer0

Prescaler Assignment
1: WDT
0: TMR0

1.
2.
3.

reduces the clock rate seen by the timer, by


dividing it by a power of two
To use the prescalar, clear the PSA bit.
The ratio is set by the PS<2:0> bits, as
shown

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

Block diagram for Timer0.

Prescaler Assignment
timer0 source edge

timer0 clock source

prepared by : Maher Al-omari

PROGRAMMABLE PRESCALER:
Given that the PIC is clocked at 4 MHZ, what is the maximum
period that Timer0 can measure without the prescaler?

Given that the PIC is clocked at 4 MHZ , PSA = 0 and


PS<2:0> = '111 What is the Maximum period that Timer0
can measure directly?
Given that the PIC is clocked at 8 MHZ , PSA = 0 and
PS<2:0> = 011 What is the Maximum period that Timer0
can measure directly?
prepared by : Maher Al-omari

SUMMARY OF REGISTERS
ASSOCIATED WITH TIMER0

prepared by : Maher Al-omari

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.

Sets the count value TIMER0.


All timers count up.
When a timer reaches the maximum value it will
flip over to 0 and continue counting (254, 255, 0, 1,
2...)

prepared by : Maher Al-omari

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

Delay using timer_0


What is the function of this
program segment ?

#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

What is the function of this program?


#include "16F877A.H"
void main() {
setup_timer_0( RTCC_INTERNAL|RTCC_DIV_16 );
WHILE (1)
{ set_timer0(100);
output_b(0x0f);
while ( get_timer0() < 151 ) ;
output_b(0xf0);
while ( get_timer0() < 251 ) ; }
}
prepared by : Maher Al-omari

Using timer0 to create a 1msec delay


Given that
function
the freq. is
4mhz ;

#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); }}

prepared by : Maher Al-omari

Count from 0-3 with 4 msec delay between


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

# 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

Given that PIC is running at 100khz ;


1.
2.
3.
4.
5.
6.

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?

prepared by : Maher Al-omari

Programming Timer0 as a Counter


RA4/TOCKI is used to count events or pulses
# include "16F877A.h"
//________________________________
void main(){
int n;
int8 anum[16];
anum[0]=0x3f; anum[1]=0x06; anum[2]=0x5b;
anum[3]=0x4f; anum[4]=0x66; anum[5]=0x6D;
anum[6]=0x7D; anum[7]=0x07; anum[8]=0x7F;
anum[9]=0x6f; anum[10]=0x77; anum[11]=0x7c;
anum[12]=0x39; anum[13]=0x5e; anum[14]=0x79;
anum[15]=0x71;

No PSA

setup_timer_0( RTCC_EXT_H_TO_L | RTCC_DIV_1 );


set_timer0(0);
while(1)
{
n=get_timer0() & 0x0f ;
output_d ( anum[n] ) ;
}
}
prepared by : Maher Al-omari

what is the output at the display after pressing SW1 7 times ?


# include "16F877A.h"
//________________________________
void main(){
int n;
int8 anum[16];
anum[0]=0x3f; anum[1]=0x06; anum[2]=0x5b;
anum[3]=0x4f; anum[4]=0x66; anum[5]=0x6D;
anum[6]=0x7D; anum[7]=0x07; anum[8]=0x7F;
anum[9]=0x6f; anum[10]=0x77; anum[11]=0x7c;
anum[12]=0x39; anum[13]=0x5e; anum[14]=0x79;
anum[15]=0x71;
setup_timer_0( RTCC_EXT_H_TO_L);
set_timer0(3) ;
while(1)
{
n=get_timer0() & 0x0f ;
output_d ( anum[n] ) ;
}
}
prepared by : Maher Al-omari

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;}}

prepared by : Maher Al-omari

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

Vous aimerez peut-être aussi