Hello Bud,
To generate non-critical time delays, such as for switch debounce, LED flashing, etc, there are a couple of simple methods using the timer. The first uses a periodic interrupt such as RTI or timer overflow. The second method requires simply to read the timer count register, and does not specifically require any interrupts to be enabled.
Method 1
The periodic interrupt should occur multiple times within the timeout period - the actual timeout period will have an uncertainty of one interrupt period (i.e. +/- one half period). Allocate one or more global variables (of byte or word size), one variable for each simultaneous timeout that may need to occur, say timeout1, timeout2.
Within the timer ISR place the following code for each variable, to decrement the variable unless already zero.
if (timeout1) timeout1 -= 1;
if (timeout2) timeout2 -= 1;
etc.
Then within the main program, where you need the timeout delay period, the following code should work.
for (timeout1 = DEBOUNCE; timeout1; );
or alternatively
timeout1 = DEBOUNCE;
while (timeout1);
Method 2
If the timer overflow method should have insufficient resolution because the overflow period is too long (and you do not wish to alter timer settings for other reasons), simply chose a bit within the timer count register (TCR) that toggles at a suitable rate, and use this as the basis for decrementing a timeout variable. The following example uses bit 9 for the timing.
#define MASK 0x0100
for (timeout1 = DEBOUNCE; timeout; ) {
while (TCR & MASK == 0);
while (TCR & MASK);
timeout1--;
}
I hope this provides some ideas.
Regards Mac
Message Edited by bigmac on 2006-06-30 04:59 PM