Shop OBEX P1 Docs P2 Docs Learn Events
How to specify counter module A or B in SquareWave.spin - newbie question — Parallax Forums

How to specify counter module A or B in SquareWave.spin - newbie question

roboticsrobotics Posts: 90
edited 2010-06-18 05:30 in Propeller 1
Hi,

I am new to Spin and would appreciate assistance in how to specify a particular counter module A or B in the public Object SquareWave.spin (below). Thank you!

PUB Freq(Module, Pin, Frequency) | s, d, ctr

'' Determine CTR settings for synthesis of 0..128 MHz in 1 Hz steps
''
'' in: Pin = pin to output frequency on
'' Freq = actual Hz to synthesize
''
'' out: ctr and frq hold ctra/ctrb and frqa/frqb values
''
'' Uses NCO mode %00100 for 0..499_999 Hz
'' Uses PLL mode %00010 for 500_000..128_000_000 Hz
''

Frequency := Frequency #> 0 <# 128_000_000 'limit frequency range

if Frequency < 500_000 'if 0 to 499_999 Hz,
ctr := constant(%00100 << 26) '..set NCO mode
s := 1 '..shift = 1

else 'if 500_000 to 128_000_000 Hz,
ctr := constant(%00010 << 26) '..set PLL mode
d := >|((Frequency - 1) / 1_000_000) 'determine PLLDIV
s := 4 - d 'determine shift
ctr |= d << 23 'set PLLDIV

spr[noparse][[/noparse]10 + module] := fraction(Frequency, CLKFREQ, s) 'Compute frqa/frqb value
ctr |= Pin 'set PINA to complete ctra/ctrb value
spr[noparse][[/noparse]8 + module] := ctr

dira[noparse][[/noparse]pin]~~


PUB NcoFrqReg(frequency) : frqReg
{{
Returns frqReg = frequency

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2010-06-18 05:23
    From a quick glance A (Module = 0) and B (Module = 1). Do you need to know why?
  • roboticsrobotics Posts: 90
    edited 2010-06-18 05:25
    Yes, I would very much appreciate the logic behind this.

    In advance, thank you
  • kuronekokuroneko Posts: 3,623
    edited 2010-06-18 05:30
      spr[noparse][[/noparse]10 + module] := fraction(Frequency, CLKFREQ, s)    'Compute frqa/frqb value
      ctr |= Pin                           'set PINA to complete ctra/ctrb value
      spr[noparse][[/noparse]8 + module] := ctr
    
    


    SPR is an array covering the special registers (SPR[noparse][[/noparse]0..15] ~ $1F0..$1FF). Meaning SPR[noparse][[/noparse]10] is $1FA == frqa and SPR[noparse][[/noparse]8] is $1F8 == ctra. Using module as an offset gives you frqb/ctrb.

    This object looks awfully similar to Synth.spin as part of the PropellerTool library. Synth.spin should be easier to understand.
Sign In or Register to comment.