Shop OBEX P1 Docs P2 Docs Learn Events
PWM Command with BS2 — Parallax Forums

PWM Command with BS2

geneshultsgeneshults Posts: 22
edited 2011-09-12 10:34 in BASIC Stamp
I'm not for sure what I'm doing wrong here.

BS2 code wrote:
PWM 0,PWMD,255
PWMD = 191
PAUSE 50

At this setting of the PWMD (191) shouldn't I be getting an output voltage of 3.75 vdc?
and at 127 PWMD I should be getting a 2.5 volts.

I'm not, what I'm getting a totally different reading.
What I'm getting is at 191 is a 2.8 and at 127 is a .79 volts.

I did the calculations per manual Average Voltage = (Duty / 255) * 5 volts
AV=(127 / 255) * 5
AV=(.498) * 5
AV=2.49

Help Please
Thanks
Gene

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-09-06 08:57
    For one thing, the line with PWMD = x
    needs to be placed before the line with the PWM command
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-09-06 09:40
    Gene,

    Is the code in a loop and are you using the filter described in the BASIC Stamp Manual?
  • geneshultsgeneshults Posts: 22
    edited 2011-09-06 16:14
    Gene,

    Is the code in a loop and are you using the filter described in the BASIC Stamp Manual?

    Yes I am. The code is in a loop. And I am using the 1uf cap and a 10k resistor for the filter as per manual.
    I had if buffered through a LM358 and have taken it out of the circuit to see if it was causing my issue. Still not getting what I would expect.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-09-06 16:26
    Well, PJ was right, the value should be before the statement, but if you're in a loop it should correct after the first pass. Why not post your entire program and I will test it here.
  • geneshultsgeneshults Posts: 22
    edited 2011-09-08 08:11
    Hi Chris
    Here is the complete code I'm using.
    I have tried different pins for the PWM output, but get the same result.
    Hope you may see a mistake I made. LOL have looked at it tried different things so maybe new set of eyes on it may see some thing.

    THanks
    Gene

    '
    [ Title ]
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Range PWMD 0 - 255 For a 0 - 5 Volt Range Output
    ' PWM3.bs2

    '
    [ Declarations ]

    PWMD VAR Word

    '
    [ Initialize ]

    DEBUG CLS 'Start display.

    '
    [ Main Routine ]

    DO
    GOSUB PWMD_1
    GOSUB PWMDOUT
    LOOP

    '
    [ PWM Subroutines ]
    PWMD_1:
    PWM 0,PWMD,255
    PWMD = 255
    PAUSE 50
    RETURN
    PWMDOUT:
    DEBUG HOME
    DEBUG CR, DEC PWMD,CR
    RETURN
  • PublisonPublison Posts: 12,366
    edited 2011-09-08 08:29
    Gene,

    As PJ pointed out, and Chris confirmed, you must put the PWMD variable before the PWM command.

    incorrect:
    PWMD_1:
     [COLOR="red"] PWM 0,PWMD,255
      PWMD = 255[/COLOR]
      PAUSE 50
      RETURN
    

    correct:
    PWMD_1:
    [COLOR="red"]  PWMD = 255
      PWM 0,PWMD,255[/COLOR]
      PAUSE 50
      RETURN
    

    EDIT: OK, as Chris points out, if you are in a loop, your value will get updated after the first go around. I guess it's just not a good practice to put PWMD after PWM, in case you do not loop the sub routines.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-09-08 09:29
    Firstly, try it without the PAUSE 50 and see if that changes anything. That would update the value much faster.

    I suspect there is something loading down the output circuit. Do you still have the LM358 buffer circuit?

    What voltage do you measure at the output if you simply have
    DO
       HIGH 0
    LOOP
    

    Is your "PWM" output measured solidly high at 5 volts?

    How about this:
    DO
       TOGGLE 0
    LOOP
    

    That should hover around 2.5 volts as it alternates high and low with a period of about 0.5 milliseconds.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-09-08 13:06
    In addition to what Tracy said, I will mention this...you're calling a subroutine which delats the code a bit, then you're calling another subroutine with DEBUG statements, which really slows the code down. The voltage the code produces is 'unloaded' s Tracy was getting at. Without the Op-Amp set in Unity Gain you may be causing the circuit to discharge too fast. One last recommendation would be to use code tags to post code. The formatting is lost without them and the code difficult to follow.
  • geneshultsgeneshults Posts: 22
    edited 2011-09-12 07:08
    Firstly, try it without the PAUSE 50 and see if that changes anything. That would update the value much faster.

    I suspect there is something loading down the output circuit. Do you still have the LM358 buffer circuit?

    What voltage do you measure at the output if you simply have
    DO
       HIGH 0
    LOOP
    
    Is your "PWM" output measured solidly high at 5 volts?

    How about this:
    DO
       TOGGLE 0
    LOOP
    
    That should hover around 2.5 volts as it alternates high and low with a period of about 0.5 milliseconds.

    Hello Tracy
    Found my error. I had a bad row on my bread board that wasn't completing my filter circuit.
    After doing every thing you asked and every thing coming out ok. I broke down my circuit and built it again on a different board and every thing worked great. Just letting you know what I found and what I did to correct it. thanks so much for your help and replys.
  • geneshultsgeneshults Posts: 22
    edited 2011-09-12 07:09
    Hello Chris
    Found my error. I had a bad row on my bread board that wasn't completing my filter circuit.
    After doing every thing you asked and every thing coming out ok. I broke down my circuit and built it again on a different board and every thing worked great. Just letting you know what I found and what I did to correct it. thanks so much for your help and replies.
  • PublisonPublison Posts: 12,366
    edited 2011-09-12 09:35
    Gene,

    Thanks for letting us know what the problem was. Too often people are too embarrassed to post the resolution.

    This will help people in the future researching a similar problem.

    Happy Stamping!

    Jim
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-09-12 10:34
    I agree...sharing your solution, even human error helps others in the future and gives them another option when troubeshooting. Thanks for sharing. Glad you got it working.
Sign In or Register to comment.