Help, "PwmAddr" is "address of PWM variables in hub ram", please.

I found a piece of code and was reading thru it when I ran into something I didn't understand.

In the PUB section the parameters in the start call are pin, Freq and PwmAddr.
The code is commented stating that "PwmAddr" is "address of PWM variables in hub ram".

I could not find any reference to "PWM variables" in the forum.
Would someone explain this, or point me to documentation?

Thank You
}}

VAR

long  cog
  
PUB start(pin,Freq,PwmAddr) : okay | pmask
' pin number (first of two adjacent pins), frequency in Hz, address of PWM variables in hub ram

  T_            := clkfreq/(Freq)                 
  pin_          := pin 
  okay := cog   := cognew(@entry, PwmAddr) + 1     ' Start cog

PUB getT     ' This is the period of the PWM which is also the resolution, lower PWM frequencies give more resolution.
   return T_
  
PUB stop
  if cog
    cogstop(cog~ - 1)

DAT

entry           shl     pmask_,pin_
                or      dira,pmask_           ' Set pins as outputs
                       
                mov     pwmaddr1_,par         ' Set up pointers to duty data
                mov     pwmaddr2_,pwmaddr1_
                add     pwmaddr2_,#4

                mov     t1, ctraval
                add     t1, pin_              ' set up counter A                        
                mov     frqa, #0       
                mov     ctra, t1              ' nco mode on step pin

                mov     t1, ctrbval
                add     pin_, #1
                add     t1, pin_              ' set up counter B                       
                mov     frqb, #0           
                mov     ctrb, t1              ' nco mode on step pin

                mov     frqa,#1
                mov     frqb,#1
                mov     time,cnt
                add     time,T_
                                
                ' PWM loop
:loop           {
                 Place other code here, as long as it takes less time than T_, roughly clkfreq/(4*(Freq)) instructions 
                 That's about 2000 instructions at 10khz PWM frequency :) 
                }

                rdlong  duty1_,pwmaddr1_   ' Read duty1 from hub ram
                rdlong  duty2_,pwmaddr2_   ' Read duty2 from hub ram 
                waitcnt time, T_
                neg     phsa, duty1_
                neg     phsb, duty2_ 
                jmp     #:loop

time            long   0
pmask_          long   %11           ' Pin mask
pin_            long   0
duty1_          long   0
duty2_          long   0
T_              long   0             ' Time period
ctraval         long   %00100  << 26
ctrbval         long   %00100  << 26
t1              long   0
pwmaddr1_       long   0
pwmaddr2_       long   0
p               long   0

Comments

  • That start method just gets given a pointer to some memory that the cog can use. Remeber cogs can be
    instantiated more than once so its normal to pass in a pointer to working memory. I presume this memory
    is live - ie can be written to to dynamically change the behaviour of the cog. Note the rdlong's into the duty1/2
    variables.
  • That parameter would be the base address of an array of two longs. For example, if you defined:
    var
      long  duty0
      long  duty1
    
    The parameter would be @duty0 -- @ provides the run-time address of an object.

    The code is taking advantage of the counters for dual PWM output. I've attached an object I wrote to do the same thing, though it may be easier to use because it's in Spin. Note, though, that using Spin to create the period timing for the desired frequency limits the upper end of the frequency to about 38kHz when running a standard 80MHz clock.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • JonnyMac wrote: »
    Note, though, that using Spin to create the period timing for the desired frequency limits the upper end of the frequency to about 38kHz when running a standard 80MHz clock.

    Unless you use fastspin, of course :)
  • Thank you all for your responses.
Sign In or Register to comment.