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

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


                               long  cog
                               long  fcPin                           ‘ frequency counter pin
                               long  fcCycles                        ‘ frequency counter cycles

                             PUB init(p) : okay
                             ‘’ Start frequency counter on pin p
                             ‘’ -- valid input pins are 0..27

                               if p < 28                             ‘ protect rx, tx, i2c
                                 fcPin := p
                                 fcCycles := 0
                                 okay := cog := cognew(@frcntr, @fcPin) + 1
                               else
                                 okay := false

                             PUB cleanup
                             ‘’ Stop frequency counter cog if running

                               if cog
                                 cogstop(cog~ - 1)

                             PUB period
                             ‘’ Returns period of input waveform

                               return fcCycles

                             PUB freq | p, f
                             ‘’ Converts input period to frequency
                             ‘’ -- returns frequency in 0.1Hz units (1Hz = 10 units)
                             ‘’ -- should not be called faster than expected minimum input
                                   frequency

                               p := period
                               if p
                                 f := clkfreq * 10 / p               ‘ calculate frequency
                                 fcCycles := 0                       ‘ clear for loss of input
                               else
                                 f := 0

                               return f

                             DAT

                                                   org     0

                             frcntr                mov     tmp1, par     ‘ start of  structure
   168   169   170   171   172   173   174   175   176   177   178