Shop OBEX P1 Docs P2 Docs Learn Events
PWM_32_v2 Frquency set math — Parallax Forums

PWM_32_v2 Frquency set math

bennettdanbennettdan Posts: 614
edited 2011-03-11 17:15 in Propeller 1
Hello I have been looking at Beau's object and want to know how to figure the Period of the PWM.Duty comand to get adifferent frequency.
example:
PWM.Duty(7,50,5000)
I understand this is a PWM signal on Pin 7 at 50% duty cycle but I cant wrap my head around what frequncy the 5000 represents and how to figure it to a different frquency.
Thanks

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-03-10 20:30
    PUB Duty(Pin,DutyCycle,Period)|BasePeriod,Ton,Toff     '' Pin = 0 to 31
        If Period <> 0
           If DutyCycle == 0                               '' DutyCycle = 0 to 100
              DutyMode(Pin,2)
              StateMode(Pin,1)
           If DutyCycle == 100
              DutyMode(Pin,1)
              StateMode(Pin,1)       
           If DutyCycle <> 0 and DutyCycle <> 100
              BasePeriod := (Period*1000)/Resolution       '' [COLOR="red"]Period is in micro seconds and[/COLOR]
              Ton := (DutyCycle * BasePeriod) / 100        '' [COLOR="red"]represents the repetition rate[/COLOR] 
              Toff := BasePeriod - Ton                     '' [COLOR="red"]of the PWM signal.[/COLOR] 
              if Ton == 0
                 DutyMode(Pin,1)
              if Toff == 0
                 DutyMode(Pin,2)
              if Ton ==0 and Toff == 0
                 StateMode(Pin,0)
              if Ton<>0 or Toff<>0
                 PWM(Pin,Ton ,Toff)                '' If Period = 0 then the pin is disabled
                 DutyMode(Pin,0)       
        else
           StateMode(Pin,0)
    
  • bennettdanbennettdan Posts: 614
    edited 2011-03-11 00:17
    I still dont see how to calculate this into a frequecy. sorry
  • kuronekokuroneko Posts: 3,623
    edited 2011-03-11 00:45
    From the included demo program:
    PWM.Duty(1,50,[COLOR="red"]16665[/COLOR])        ''Create a [COLOR="red"]60Hz[/COLOR] 50% duty cycle on Pin1
    
    A 60Hz signal has a 1/60Hz = 16.667ms period (16667us). Can you see the connection here? The 5000us from your example are equivalent to 200Hz.
  • bennettdanbennettdan Posts: 614
    edited 2011-03-11 14:09
    I believe I have it now so if I want a 8khz PWM signal at 50% duty cycle on pin one then
    PWM.Duty(1,50,125) = 1/8000hz = 0.000125ms (125us)
    Is this correct?
  • kuronekokuroneko Posts: 3,623
    edited 2011-03-11 17:15
    bennettdan wrote: »
    I believe I have it now so if I want a 8khz PWM signal at 50% duty cycle on pin one then
    PWM.Duty(1,50,125) = 1/8000hz = 0.125ms (125us)
    Yes, provided API and implementation match (good chance) that will give you an 8kHz signal.
Sign In or Register to comment.