Shop OBEX P1 Docs P2 Docs Learn Events
Does PWM really look like DAC output? — Parallax Forums

Does PWM really look like DAC output?

StephenMooreStephenMoore Posts: 188
edited 2012-02-13 19:27 in Propeller 1
I recently had access to an oscilloscope so I decided to see if I could reproduce the PWM example from the Propeller Application Note AN001.

The measured waveform was depicted as a saw tooth with a 125ms period (Fig 6). I can in no way repeat this. All I get is a square wave with varying full on time depending on the state of the loop. The note mentioned no special circuitry required as for duty cycle DAC. I interpreted this to mean that, as an example, for LED brightness control the human eye is acting as some sort of integrator device and the LED really only sees full voltage for varying amounts of time.

Am I missing something here?


''Demonstration of PWM version of NCO counter mode
CON   _clkmode = xtal1 + pll16x
          _xinfreq = 5_000_000


VAR    long parameter


PUB   go | x


cognew(@entry, @parameter) 'start assembly cog
 repeat
  repeat x from 0 to period                      'linearly advance parameter from 0 to 100
    parameter := x                                  'a constant here locks to value x percent high
    waitcnt(100_000 +cnt)                        'wait a little while (1000 periods) before next update


DAT
''assembly cog fetches the value in parameter for PWM percentage
                             org
entry                      mov dira, diraval                            'set APIN to output
                             mov ctra, ctraval                            'establish counter A mode and APIN
                             mov frqa, #1                                  'set counter to increment 1 each cycle
                             mov time, cnt                               'record current time
                             add time, period                             'establish next period
:loop                      rdlong value, par                             'get an up to date pulse width
                            waitcnt time, period                         'wait until next period
                            neg phsa, value                               'back up phsa so that it trips "value" cycles from now
                            jmp #:loop                                      'loop for next cycle


diraval                   long             |< 0                                'APIN=0
ctraval                   long             100 << 26 + 0          'NCO/PWM APIN=0 
period                   long             100                              '800kHz period (_clkfreq / period)
time                     res 1
value                     res 1

Comments

  • tonyp12tonyp12 Posts: 1,951
    edited 2012-02-13 17:09
    Correct, a LED only looks dim to a human eye. (and acctualy would not work with an analog voltage anyway)
    If you want a DAC, you would have to add 1k resistor in series and then 8.1nf cap to ground (or some other values)
  • pedwardpedward Posts: 1,642
    edited 2012-02-13 17:13
    To smooth the output requires a filter. The values described above cutoff at 19649Hz, so any frequency lower than that is passed through, above that is smoothed.

    EDIT: An 82 ohm resistor and .1uf cap will give about the same frequency. A 1k resistor and .1uf cap is 1.5Khz, 10k/.1uf is 159Hz.
  • DelusDelus Posts: 79
    edited 2012-02-13 17:17
    In order to get a saw-tooth output you need to add a low-pass filter to the output of the pin, a PWM or Pulse Width Modulated signal is supposed to be exactly as you described. A fixed period signal with varying duty(full on) time. In the case of an LED your eye does the low pass filtering, instead of seeing a very high speed flicker you see the average light output of both the led in it's on and off state, by varying the amount of time spent in both of these states you vary the observable brightness of the LED.

    Hope this helps,
    David
  • RaymanRayman Posts: 14,845
    edited 2012-02-13 17:24
    Well, I just took a look at the AppNote and I think it is a little vague, so I'm not surprised you have trouble.

    Here's how you can get that waveform (I think):
    Set your scope to 20 ms per division (really, really slow).
    Turn your bandwidth down to about 10 MHz or less.

    Then, I think you'll see it...

    You may still have trouble with a good scope though. Then, you can use an RC filter to smooth it out to the triangle shape...
  • StephenMooreStephenMoore Posts: 188
    edited 2012-02-13 18:01
    Thanks.

    My scope is set to 20ms/div but I do not know what you mean set the bandwidth to <10MHz.

    Is there an article where I can get more info on this low pass cutoff filter effect?
  • Heater.Heater. Posts: 21,230
    edited 2012-02-13 18:08
    A google search, something like "low pass RC filter" or find any basic electronics tutorial, ther are many.
  • jmgjmg Posts: 15,183
    edited 2012-02-13 19:03
    The measured waveform was depicted as a saw tooth with a 125ms period (Fig 6). I can in no way repeat this. All I get is a square wave with varying full on time depending on the state of the loop. The note mentioned no special circuitry required as for duty cycle DAC. I interpreted this to mean that, as an example, for LED brightness control the human eye is acting as some sort of integrator device and the LED really only sees full voltage for varying amounts of time.

    Am I missing something here?

    The Fig 6 has to include some Low Pass filtering, which is why it is not a clean line - you can see some of the PWM still here. - ie it is NOT a direct good-scope on a pin.

    Notice also how it has the most ripple at 50% duty cycle.

    You are right, that averaging systems like heating, and the human eye, will do their own integration/low pass filtering.
  • LeonLeon Posts: 7,620
    edited 2012-02-13 19:27
    Many scopes (including my 100 MHz Tek 2235) have a switch that reduces the bandwidth.
Sign In or Register to comment.