asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
Hardware millisecond timer, code efficient |
Posted: Wed Apr 23, 2014 10:03 am |
|
|
This function creates approximate millisecond delays using timer0,
requires no interrupts, and consequently avoids using processor
delay loops. ( CCS delay_ms() function uses loops).
If many different duration delays are required in a program,
and especially when the amount of delay is not determined until
program execution, it can be very code efficient.
The actual time of each tick is 1.024 msec for an absolute error
of 2.4% slow. using an 8.388608 mhz crystal will give you a very good 1.000 msec exact timing.
There is also a single absolute delay error of some extra usecs produced
by the "wrapper" of the function.
"As is", it is usable with 12f/16f parts and needs only
minor setup changes to work with other PIC timers, 18F parts and beyond.
timer0() in 8 bit configuration for 18F parts
Do not attempt to use it inside an ISR.
Presented for example only.
Code: |
#bit T0IF = GETENV("bit:T0IF") // timer 0 overflow
#use delay( clock=8000000,INTERNAL )
setup_timer_0( RTCC_DIV_8 ); // set for rollovers 1 ms
//....
void delayTMS( unsigned int16 tix){
if (!tix) return; // never zero
set_timer0(0); T0IF=0; // 1.024 ms /tick
do {
if (T0IF) { --TIX; T0IF=0; }
}while (tix);
}
|
|
|