Basic implementation of a slow real-time clock, with and
ticks.
This module can provide up to 8 independent
16 bit clocks.
It also provides one 32 bit
clock.
Moreover, a flag is toggled each
, and provides a faster clock.
void timer0_init()
This function initialize timer0 sub-system (mainly prescaler register).
It calls reload_timer0()
, then starts timer0 activity.
void reload_timer0()
Reloads timer0 for
delay.
void timer0_ISR()
Interrupt Service Routine for timer0 interrupts. You must install an interrupt handler which calls this ISR. The following code will do the job.
__interrupt__ void hi_pri_ISR() { if (INTCON & (1 << TMR0IF)) // does interrupt come from timer0 ? { timer0_ISR() ; // yes, call interrupt handling code } }
void start_clock( unsigned clocknum)
Sets clock count of clock number clocknum to 0, then start it.
void stop_clock(unsigned int clocknum)
Stops clock clocknum. Stopped clocks can be restarted.
void restart_clock(unsigned int clocknum)
Restarts a stopped clock.
unsigned long get_clock(unsigned int clocknum)
Gets number of seconds elapsed since clock clocknum has been started or restarted.
unsigned long get_clockm(unsigned int clocknum)
Gets number of minutes elapsed since clock clocknum has been started or restarted.
void clear_clock(unsigned int clocknum)
Explicitely sets clock clocknum to zero.
unsigned long *get_globalclock()
Returns addr of first element of an array of two unsigned long containing global clock. First element of this array contains low part of global clock. Global clock is statically initialized and started when timer0_init()
is called. There is no way to stop it.
insigned int timer0_flags()
Returns current state of clocks flags. One bit of the value returned by this function is toggled each second. Another bit is toggled each second.
The T0_1S_FLAG
and T0_0_1S_FLAG
constants must be used to get the flag you need.
Here is an example of code executing a task each second.
unsigned int old_flag = timer0_flags() & T0_1S_FLAG, new_flag ; for( new_flag = old_flag ; ; ) { if( (new_flag = timer0_flags() & T0_1S_FLAG) != old_flag ) { old_flag = new_flag ; // do something each second } }
Alain Gibaud 2015-07-09