Page 99 - Build Your Own Quadcopter_ Power Up Your Designs with the Parallax Elev-8
P. 99
78 Bu il d Y o ur O w n Q u a d c o p t e r
Figure 4.8 Output Full Source view.
component object. It does, however, contain a new section named VAR. The instruction
contained in this section is:
long Stack[9]
This instruction reserves nine long data words in the hub’s shared memory. Recall that
the standard word for the Prop chip is 32 bits in length, which is equivalent to four bytes.
Thus, nine longs reserves 36 bytes of space in the common hub RAM. This memory space is
required to support the creation for a new cog’s stack operating area. Without going into a
detailed explanation, I will define a stack as simply a memory area in which an operating
cog may store temporary data and an area where the Prop’s firmware can logically access
the cog as needed. A new cog is created when the Top Object, Blinker1, calls Output’s Start
method, which is the reason for reserving stack space.
Output also contains two public methods that I referred to earlier in the Blinker1 code
discussion. The first method is named Start, and the second method is named Toggle. It is
standard practice in Spin programming to have a Start method in a component object so that
the Top Object may “kick it off” in a known and consistent manner. Output’s Start method
contains only one complex instruction, which causes a new cog to run that in turn executes
the Toggle method. The Toggle method is Output’s other public method. This new cog is
automatically selected by the Spin interpreter from the first available cog, which in this case
is cog #1. Cog #0 is always selected to start executing the Top Object code, and therefore, is
already busy. The heart of the Output object is really the Toggle method. I have repeated the
source code below so I could amplify the comments already contained in the code.
PUB Toggle(Pin, Delay, Count)
{{Toggle Pin, Count times with Delay clock cycles in between.}}
dira[Pin]~~ ‘ Set I/O pin to output direction
repeat Count ‘ Repeat for Count iterations
!outa[Pin] ‘ Toggle I/O Pin
waitcnt(Delay + cnt) ‘ Wait for Delay cycles