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 CCS Technical Support

Timers to keep track of time and time events

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



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Timers to keep track of time and time events
PostPosted: Mon May 10, 2004 9:38 am     Reply with quote

In the following example it is assumed that the Clock_Service function is called much more often than once per mS. It is also assumed that other functions will be executed only when their flags are set. The timer value is never set it is free running. The timer is setup as follows

with a xtl @ 4Mhz the clock will increment ever 1uS
For a 1mS interupt the clock must increment 1000 times

pre-scaler is set to 4 and the count is set to 250

250 * 4 = 1000

The post-scaler is set to 1

If the xtl was 8Mhz the post-scaler would need to be 2
If the xtl was 20Mhz the post-scaler would need to be 5

Code:
int1  Clock_Been_Initialized=0;
Int16 Miliseconds;
Int16 Seconds;
int1  miliSecond_Tick=0;
int1  Second_Tick=0;

int1  Read_Temperature=0;
int1  Read_ADC_Now=0;

// Global Real Time Clock Interupt
#int_TIMER2                                                 
void TIMER2_isr()
{  Miliseconds++;
   miliSecond_Tick=1;
   if(Miliseconds>999)
   {  Miliseconds=0;
      Seconds++;
      Second_Tick=1;
   }
}


/***********************************************************
*    Service Clock                                         *
***********************************************************/
#inline
void Clock_Service(void)
{  if(!Clock_Been_Initialized)
   {  setup_timer_2(T2_DIV_BY_4,249,1);                     // Set 1mS period on a 4Mhz xtl
      enable_interrupts(INT_TIMER2);
      Seconds=0;
      Clock_Been_Initialized=1;
   }
   if(Second_Tick)
   {  Second_Tick=0;
      Read_Temperature=1;                                   // This flag will trigger another event to occur
   }
   if(miliSecond_Tick)
   {  miliSecond_Tick=0;
      Read_ADC_Now=1;                                       // This flag will trigger another event to occur
   }
}
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