PWMDUTY Circuit Testing

Bill z

Member
New guy here again asking for guidance with a 08M2 and its PWMDUTY command and circuit. In the editor, this program I found on this forum looks like it is working, and at one time I did have it running on my 08M2 chip. But now, the LED won’t light up. Terminal in the editor shows the program is running, looping and incrementing but no light in the LED.

The LED is good and in series with a 268 ohm resistor from pin C.2 to ground.

The LED works on the same 08M2 on pin C.4 when running a PWM command.

Being new, I have been trying all sorts of things and commands to experiment and learn.

I hope, I’m just overlooking something.

Can someone help.

Here is the program and the Terminal results:


1 17->400 18->425 19->450 20->475 21->500 22->525 23->550 24->575 25->600 26->625 27->650 28->675 29->700 30->725 31->750 32->775 33->800 34->825 35->850 36->875 37->900 38->925 39->950 40->975 41->1000
2 1->975 2->950 3->925 4->900 5->875 6->850 7->825 8->800 9->775 10->750 11->725 12->700 13->675 14->650 15->625 16->600 17->575 18->550 19->525 20->500 21->475 22->450 23->425 24->400 25->375 26->350 27->325 28->300 29->275 30->250 31->225 32->200 33->175 34->150 35->125 36->100 37->75 38->50 39->25 40->0
3 1->0 2->25 3->50 4->75 5->100 6->125 7->150 8->175 9->200 10->225 11->250 12->275 13->300 14->325 15->350 16->375 17->400 18->425 19->450 20->475 21->500 22->525 23->550 24->575 25->600 26->625 27->650 28->675 29->700 30->725 31->750 32->775 33->800 34->825 35->850 36->875 37->900 38->925 39->950 40->975 41->1000
4 1->975 2->950 3->925 4->900 5->875 6->850 7->825 8->800 9->775 10->750 11->725 12->700 13->675 14->650 15->625 16->600 17->575 18->550 19->525 20->500 21->475 22->450 23->425 24->400 25->375 26->350 27->325 28->300 29->275 30->250 31->225 32->200 33->175 34->150 35->125 36->100 37->75 38->50 39->25 40->0
5 1->0 2->25 3->50 4->75 5->100 6->125 7->150 8->175 9->200 10->225 11->250 12->275 13->300 14->325 15->350 16->375 17->400 18->425 19->450 20->475 21->500 22->525 23->550 24->575 25->600 26->625 27->650 28->675 29->700 30->725 31->750 32->775 33->800 34->825 35->850 36->875 37->900 38->925 39->950 40->975 5


Code:
'#picaxe 08M2

symbol pLED  = C.2 ' 08M2
symbol wDutyCycle=s_w1
symbol wIncrement=s_w2
symbol loopCnt=b27
symbol INCREMENT=25  ; 10-103  15-69   20-52   25-41

wDutyCycle=0-INCREMENT
wIncrement=INCREMENT

main:

      for loopCnt=1 to 41
        wDutyCycle=wDutyCycle+wIncrement
        if wDutyCycle < 1023 then
          pwmduty pLED,wDutyCycle
        sertxd(#loopCnt,"->",#wDutyCycle," ")
        pause 250
        endif
      next loopCnt
      wIncrement=0-wIncrement ' toggle between 100 & -100
    sertxd(cr,lf)
    
    goto main
 

AllyCat

Senior Member
Hi,

It looks as if the problem is that you can't use a PWMDUTY command until after a PWMOUT command has been declared (because PWMDUTY doesn't say what frequency is to be used). As the command reference says: "A pwmout command must be issued on the appropriate pin before this command will function."

In cases like dimming a LED, the PWMDUTY command is not really needed; it's slower and uses much more program memory than PWMOUT.

Also, using instructions like wDutyCycle=0-INCREMENT (i.e. negative value) and in a "System Variable" (S_W2) may be rather adding to the complication/confusion. ;)

Cheers, Alan.
 

lbenson

Senior Member
As AllyCat says, PWMOUT is needed before main, e.g., "pwmout pwmdiv4, pLED, 249, 0" for off or "pwmout pwmdiv4, pLED, 249, 999" for on.

Also, using instructions like wDutyCycle=0-INCREMENT (i.e. negative value) and in a "System Variable" (S_W2) may be rather adding to the complication/confusion.
We complicate to simplify. I originally wrote this code to test a new piece of hardware. It worked. Later someone wanted ramping and fading code, so I posted it. Poster found a glitch, so I've modified it as follows:
Code:
' 08_pwm12v
#picaxe 08M2
'#picaxe 14M2

symbol pLED  = C.2 ' 08M2
'symbol pLED  = B.4  ' 14M2
symbol wDutyCycle=s_w1
symbol wIncrement=s_w2
symbol loopCnt=b27
symbol INCREMENT=100

pwmout pwmdiv4, pLED, 249, 0
pause 3000
pwmout pwmdiv4, pLED, 249, 999
pause 3000
wDutyCycle=0-INCREMENT
wIncrement=INCREMENT

main:
  do
    for loopCnt=1 to 11
      wDutyCycle=wDutyCycle+wIncrement
      if wDutyCycle < 1023 then
        sertxd(#wDutyCycle," ")
        pwmduty pLED,wDutyCycle
        pause 2000
      endif
    next loopCnt
    wIncrement=0-wIncrement ' toggle between 100 & -100
    sertxd(cr,lf)
  loop
 
Last edited:

Bill z

Member
Thanks a bunch both AllyCat and ibenson.

Yes that fixed it. I misunderstood the importance of the pwmdiv4 statement.

About the speed, Running this program with a pause 25 is plenty fast enough for my purpose (about 4 second cycle). I can connect pin C.2 to a 5 amp MOS module and do plenty with this little chip now.
 
Top