Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Timers to keep track of time and time events |
Posted: Mon May 10, 2004 9:38 am |
|
|
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
}
}
|
|
|