CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Hardware millisecond timer, code efficient

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

Hardware millisecond timer, code efficient
PostPosted: Wed Apr 23, 2014 10:03 am     Reply with quote

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);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group