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

Chapter 6: Radio-Controlled Systems and Telemetr y             151


                               ‘frqa := 134_218                      ‘ 2500 Hz
                               frqa := 3_222                         ‘ 60 Hz
                               ‘frqa := 672                          ‘ ~12.5 Hz
                               dira[0] := 1                          ‘ make p0 output

                               repeat
                                 term.str(string(HOME, “Freq: “))
                                 f := fc.freq                        ‘ get frequency
                                 if f > 0                            ‘ valid?
                                   term.dec(f/10)                    ‘ print whole part
                                   term.tx(“.”)
                                   term.dec(f//10)                   ‘ print fractional part
                                   term.str(string(“ Hz”, CLREOL))
                                 else
                                   term.str(string(“???”, CLREOL))
                                 waitcnt(clkfreq + cnt)
                                This program makes use of another  Spin program named jm_freqin, which actually
                             measures and displays the pulse frequency on the Propeller Serial Terminal. The program
                             was created by Jon “JonnyMac” McPhalen (also known as Jon Williams), who is a prolific
                             contributor to the Parallax forums. Jon also made provision for self-generating pulses to test
                             the program. Those pulses emit from pin 0. However, I used pin 14 as an input, since that is
                             one of the servo ports on the BOE. The jm_freqin program is shown below with Jon’s
                             introductory comments included because I think they are very helpful in understanding
                             how this program functions:

                                {{
                             This object uses ctra and ctrb of its own cog to measure the period
                             of an input waveform. The period is measured in clock ticks; this
                             value can be divided into the Propeller clock frequency to
                             determine the frequency of the input waveform. In application, the
                             period is divided into 10x the clock frequency to increase the
                             resolution to 0.1Hz; this is especially helpful for low
                             frequencies. Estimated range is 0.5Hz to ~40MHz (using 80MHz
                             clkfreq).

                             The counters are setup such that ctra measures the high phase of
                             the input and ctrb measures low phase. Measuring each phase
                             independently allows the input waveform to be asymmetric. In order
                             to prevent a loss of signal from causing an erroneous value from
                             the freq() method the fcCycles value is cleared after a valid
                             frequency is calculated; this means that you should not call this
                             method at a rate faster than the expected input frequency.
                             }}

                             VAR
   167   168   169   170   171   172   173   174   175   176   177