//======================================================== // Define timers for use with TRT // Bruce Land, Feb 2009 // //======================================================== #define MAXNBRTIMERS 3 // Three values for TIMERTICK 0.1 mS or 1 mS or 10 mS // only choose one value #define TIMERTICK 10 //#define TIMERTICK 1 //#define TIMERTICK 0.1 #define SECONDS2TIMERTICK(t) ((t)*1000/TIMERTICK) // bit definitions for the mode byte #define ENABLEBIT 7 // =1 if enabled #define RUNBIT 0 // =1 if running #define MODEBIT 1 //periodic=0 oneshot=1 #define PERIODIC 0 #define ONESHOT 1 #define READ(U, N) ((U) >> (N) & 1u) #define SET(U, N) ((void)((U) |= 1u << (N))) #define CLR(U, N) ((void)((U) &= ~(1u << (N)))) #define FLIP(U, N) ((void)((U) ^= 1u << (N))) // define a timer struct { uint16_t period; // the timeout value uint16_t count; // the current value uint16_t Nperiods; // number of timeouts which have occured uint8_t status; // running/stopped, enabled/disabled, periodic/oneshot uint8_t semaphore; // what to signal: zero means no signal } timers[MAXNBRTIMERS] ; //==================================================== //timer 0 overflow ISR ISR (TIMER0_COMPA_vect) { uint8_t i ; // Handle array of soft timers for (i=0; i 0) { timers[i].count-- ; }//if (timers[i].count > 0) else { // timer count==0 timers[i].Nperiods++ ; //reset for next cycle timers[i].count = timers[i].period-1; // signal if a semaphore is defined if (timers[i].semaphore>0) trtSignal(timers[i].semaphore); // stop the timer if mode==ONESHOT if (READ(timers[i].status, MODEBIT)) CLR(timers[i].status,RUNBIT); }// else timer count == 0 }//if (timers[i].status & (1<