Page 103 - Build Your Own Quadcopter_ Power Up Your Designs with the Parallax Elev-8
P. 103
82 Bu il d Y o ur O w n Q u a d c o p t e r
Clock Timing
This section is concerned with timing from both delay and duration aspects. Timing is a key
feature in the real-time operations that the Prop performs. The duration of the LED blink in
the Blinker1 program is based on the passage of a predesignated number of clock cycles that
are assigned in the Delay variable. The total numbers of blinks are assigned by the Count
variable, which also directly affects the total elapsed time that a specific LED blinks. The key
instruction that implements the LED blink duration is:
waitcnt(Delay + cnt)
This instruction is within a loop in the Output’s Toggle method. The number of loop
iterations is controlled by the Count variable; hence, the total elapsed time for a blinking
event is approximately computed by this formula:
Total blink time = (Delay time) * Count
Notice that I used the word approximately in describing the formula’s accuracy because
there is an extra brief delay from what is called overhead processing that involves executing
loop instructions other than the actual delay instruction. This overhead, while typically very
small, must be accounted for in extremely precise timing applications.
I conducted a very informal experiment to verify that the “Total blink time” formula was
somewhat accurate. I simply timed the blinking duration of the LED controlled by pin 15.
I did a set of four timing measurements and came up with a result of 13.4 seconds for the
average total duration. But how does this compare with the theoretical time duration? To
answer this question I had to compute the expected delay, as shown below:
Parameters: Delay = 4,000,000
Count = 40
Delay in seconds = 4,000,000 / 12,000,000 = 0.3333 seconds
Total delay = 0.3333 * 40 = 13.33 seconds
Measured = 13.40
Calculated = 13.33
Less than 0.1 seconds between measured and calculated results isn’t too bad for a crude
experiment. However, you may be wondering where the 12,000,000 value came from in the
“Delay in seconds” calculation. I will discuss this in the next section.
RC Oscillator Operations
Remember that at the beginning of this chapter I explained that the Prop chip had a variety
of clock operational settings, including using an internal RC oscillator. It turns out that if
you write a Spin program without specifying a particular clock configuration, the Spin
compiler will automatically invoke what is called the “fast” RC oscillator (RCFAST) mode
that runs at 12 MHz—this is where the 12,000,000 value comes from. This mode of operation
is fine for initial prototyping and any non-time-critical application but is definitely not
recommended for precision timing operations for all the reasons I have previously
discussed. You should also note that the 12-MHz rating is what Parallax terms a nominal
rating because it could range from 8 to 20 MHz depending upon a number of factors. I will