??? 09/12/07 04:25 Read: times Msg Score: +1 +1 Good Answer/Helpful |
#144417 - Software timers Responding to: ???'s previous message |
Try searching for something like "Software Timer"
The general principle is that you use a single (hardware) timer to generate a timebase or "tick"; On each "tick", the hardware timer updates each of the currently-running software timers; When a software timer expires it could call a function and/or set flags to notify the main code loop. Something like this: unsigned int timer_1; // Countdown value for Timer-1 unsigned int timer_2; // Countdown value for Timer-2 unsigned int timer_3; // Countdown value for Timer-3 // etc... void timer_isr( void ) // Hardware timer interrupt handler // use appropriate compiler-specific syntax! { if( timer_1 ) { // The countdown is running if( --timer_1 == 0 ) { // The countdown has reached zero; ie, the timer has expired // Set flags and/or call functions as appropriate } } if( timer_2 ) { // The countdown is running if( --timer_2 == 0 ) { // The countdown has reached zero; ie, the timer has expired // Set flags and/or call functions as appropriate } } if( timer_3 ) { // The countdown is running if( --timer_3 == 0 ) { // The countdown has reached zero; ie, the timer has expired // Set flags and/or call functions as appropriate } } // etc, etc,... } You start a timer by setting its countdown (global variable) to a suitable non-zero value. Depending on how many timers you have, it may be more efficient to put them into an array, or even a linked-list, and have a loop to handle them in the ISR... This is exactly the kind of technique that an RTOS would use for its timers! |