Pacer clock saves subroutine calls
Enver Torlakovic, Willmot, New South Wales, Australia; Edited by Brad Thompson and Fran Granville -- 11/10/2005
This Design Idea outlines an easy-to-implement time-delay routine that requires no subroutine calls and thus avoids possible stack-overflow problems (Listing 1). This method also saves RAM space by requiring in most cases only two variables: the PACER_CLOCK as a free-running counter and another variable introduced at a particular instance (for example, TIME_VAR). The routine dedicates the microcontroller's Timer 0 to generate an interrupt-on-overflow instruction every 10 msec or at any other desired interval. You assign the Timer 0 interrupt a low priority in the initialization code and then enable the Timer 0 any convenient time. After assignment, do not alter the interval because many services likely depend on the pacer-clock routine. Note that the routine can achieve delays of as much as 255 times the Timer 0 overflow period.
Listing 1 is written for Microchip's PIC18F242 flash-memory controller, but porting the routine to another microcontroller should pose few problems. When copying the code to paste it into routines, note that you must change the labels—in this example, "wait_ loop100"—at each application of the code between the rows of asterisks in the listing.
Click here for more Design Ideas!
© 2009, Reed Business Information, a division of Reed Elsevier Inc. All Rights Reserved.
