PIC 201 (Timer0)
xxalexx at ix.netcom.com
xxalexx at ix.netcom.com
Wed Jul 1 18:00:33 GMT 1998
Timers
1 instruction cycle takes 4 clock periods to run.
This is the fastest a PIC can do something.
If using a 4 MHz oscillator
period=1/frequency = 1/4MHz=250ns
1 instruction cycle = 4*period = 1us
The timers are based on the above instruction cycle
as the min. time they can measure.
Timer0
timer0 is on every model of PIC
It can also be used as a input pulse counter using the pin labeled
RA4/TOCK1
Timer0 has a 8bit counter that counts from 0 to 255 then
again from 0 to 255. each count is equal to the instruction cycle
time base, or if using as external counter on a edge of a external
pulse.
Using the CCS PCM compiler the function
SETUP_COUNTERS ( rtcc_state, ps_state );
will set up and start the timer.
the rtcc_state variable sets internal timer or external counter
the ps_state variable sets a prescaler or watchdog timer.
The prescaler will lengthen the min. time base, this will reduce
resolution but increase duration before count rollovers from 255 back
to 0 . A watchdog timer will reset your program if it stays frozen
for a specifed time. You can either set a prescaler or watchdog not
both. The prescaler values range from 2 to 256 in multiples of 2.
Example you are using a 4Mhz cyrstal thus instruction cycle is 1us
as calculated above. With no prescaler the timer will count from
0 to 255 in 255us using a time base of 1us. (0 to 0 in 256us)
With prescaler of 2 the timers time base is lengthend to 2us thus
each count resoultion is 2us but now can measure to 510us=2*255
this procedure will set the counter to a value in the( )
set_timer0( a number you want to reset the countert to 0-255 );
this function will return the current timer count 0-255
current count = get_timer0();
//ex2.c measures elasped time of printf
#include<c:\picc\bin\examples\16F84.h>
#fuses XT, NOWDT, NOPROTECT
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_A0, rcv=PIN_A1, invert)
//define a conversion factor to scale results to 16 bit
#define timer0_conv 128
main()
{
// decalare variables
long t; //16 bit value for counts and corrected time
//setup timer0 with resolution of 128us range = 32640us
setup_counters( rtcc_internal, rtcc_div_128 );
while(TRUE)
{
//reset the timer count to 0
set_timer0(0);
// timer0 is measuring time to send bytes
printf("Hello Fuel Inj.\n\r");
//this will read the 8bit 0-255 count of timer0
t = get_timer0();
// this will convert counts to us. Each count = 128us
t = t * timer0_conv;
// this will round t using integer div. % is modulus
// which is remainder
if ( t % 100 > 4 )
t=t/100 + 1;
else
t=t/100;
//this will print t as floating pt with units ms
printf( "ET=%u.%u ms\n\r",(int)(t/10),(int)(t%10) );
}
}
More information about the Diy_efi
mailing list