Page 197 - Build Your Own Quadcopter_ Power Up Your Designs with the Parallax Elev-8
P. 197

176     Bu il d  Y o ur  O w n  Q u a d c o p t e r


                             {{
                             Theory of Operation:

                             Launch three cogs using the object RX which in turn each start two
                             counters. This approach does not need the pulses to arrive on the
                             pins in any certain order nor does it require all the pins to be
                             connected. Whatever pulse is received on pin 14 is then sent out to
                             pin 0 and so on.
                             }}



                             VAR
                               byte cog[3]
                               long stack[60]
                               long uS

                             PUB start(pins_array_address, pulsewidth_array_address)  |  i

                               uS := clkfreq/1_000_000

                               stop                               ‘call the stop method to stop
                                                                   cogs that may be already started
                               repeat i from 0 to 2
                                  cog[i] := cognew(readPins(@long[pins_array_address][i*2],
                                @long[pulsewidth_array_address][i*2]), @stack[i*20]) + 1



                             PUB stop | i
                                 repeat i from 0 to 2
                                   if cog[i]
                                     cogstop(cog[i]~ -1)

                             PUB readPins (pins_address, pulsewidth_address) | i, p1, p2,
                             synCnt, active1, active2

                               repeat i from 0 to 1
                                  spr[8+i]  := %01000 << 26 + long[pins_address][i]   ‘set the
                                                                            mode and pin for ctra/b
                                  spr[10+i] := 1                 ‘set frqa/b

                               p1 := long[pins_address][0]
                               p2 := long[pins_address][1]
                               dira[p1]~
                               dira[p2]~
                               long[pulsewidth_address][0]~      ‘these lines ensure that the
                                                                           0
   192   193   194   195   196   197   198   199   200   201   202