Page 105 - Build Your Own Quadcopter_ Power Up Your Designs with the Parallax Elev-8
P. 105
84 Bu il d Y o ur O w n Q u a d c o p t e r
expression would be referred to as a “magic number”—it would take an act of magic to
figure out what it represents. The programming practice that you should follow is to avoid
using a magic number if at all possible, and if you do use one, ensure that you add a comment
regarding what the number represents.
The RCSLOW clock mode is nominally rated for 20 kHz, but as was the case with the
RCFAST mode, it has a potentially wide variation. This range goes from 13 kHz to 33 kHz,
which would cause some serious issues if the time in your code was dependent on a preset
clock cycle. In the following section, I will discuss how using a crystal oscillator can vastly
improve the clock-cycle precision.
Crystal Oscillator Operations
Using the crystal oscillator is a simple matter of changing the clock mode. In this case, it
involves two statements that need to be put into the CON section of the program Editor. The
statements for a 5 MHz external crystal would be:
CON
_CLKMODE = XTAL1
_XINFREQ = 5_000_000
In this example, XTAL1 sets the clock mode for a crystal oscillator, and _XINFREQ
specifies the external crystal resonant frequency that is connected to the X1 Prop pin. Recall
that there is also a PLL frequency multiplier that may be used with an external crystal. In this
example, no multiplicative factor is specified so the Prop clock frequency will be 5 MHz, the
same as the external crystal. The next example shows you how to use a PLL multiplication
factor.
CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
This example is almost the same as the one above, except for the addition of the PLL
clock-multiplier specification after the XTAL1 mode specifier. The multiplier specification is
simply “ + PLL16X,” which means multiply the external crystal frequency by 16. This means
that a 5 MHz external crystal would create an 80 MHz clock frequency.
I next modified the SlowBlinker1 code to use the high-speed crystal oscillator with a
16 times PLL multiplier factor. I named this revised program FastBlinker1, and Figure 4.13 is
a screenshot of the Full Source view. I also restored the delays to their original values from
the scaled-down values used in the SlowBlinker1 program.
This program ran considerably faster than the Blinker1 program, as you might expect.
I estimated that the pin 15 blinking operation would last approximately 2 seconds because
the clock speedup was a factor of 6.5, which is the ratio between 80 MHz and 12 MHz.
I calculated the 2-second result by dividing the 13-second operation for Blinker1 by 6.5. The
actual operation was indeed around 2 seconds, but it was very hard to determine because of
the additional time it takes to load the program from EEPROM into RAM.
Reducing Dependence on Absolute Clock-Cycle Times
In this section, I will show you how to get rid of the bothersome dependence on absolute
clock-cycle timing when trying to set delay and duration times within your program. By