Shop OBEX P1 Docs P2 Docs Learn Events
PWM DAC that outputs a 8bit resolution 306.4hz triangle audio wave — Parallax Forums

PWM DAC that outputs a 8bit resolution 306.4hz triangle audio wave

Phillip Y.Phillip Y. Posts: 62
edited 2006-05-31 03:20 in Propeller 1
I have partialy figured out pwm output from the counters.
Here is a PWM DAC that outputs a 8bit resolution 306.4hz triangle audio wave.
or change to a·slower cycle to watch P7 led pwm wink on/off slowly.

The things I found out about pwm are:yeah.gif
frq = 0 has·a constant 0 output
frq =·FF_FF_FF_FF ·has a constant 1 output

frq =·7F_FF_FF_FF ·has·a 40mhz·output with 50% duty cycle
frq =·80_00_00_00··is nearly the same

frq =·40_00_00_00··and··C0_00_00_00·have·a 20mhz·output
frq =·20_00_00_00··and··E0_00_00_00·have·a 10mhz·output
frq =·10_00_00_00··and··f0_00_00_00·have·a·· 5mhz·output

frq =·00_00_00_01··and··FF_FF_FF_FE ·have·a··output with a ~ 55 sec period

frq = 00_00_00_01 is mostly all·0 with·single 1·pulse
frq =·FF_FF_FF_FE is mostly all·1 with·single·0·pulse

Comments

  • Tracy AllenTracy Allen Posts: 6,656
    edited 2006-05-28 23:22
    The BASIC Stamp uses the same algorithm for generating PWM. It is really more aptly called PDM, pulse density modulation.
    www.emesys.com/BS2PWM.htm

    frqa is added to phsa at each clock cycle, and the output is the carry bit from the addition. So with frqa=1, the carry only occurs when phsa rolls over from FFFF_FFFF to 0. That happens at intervals of 2^32 / clkfreq. (53.6870912 seconds when clkfreq=80mhz).

    When frqa=FFFF_FFFF, the only time a carry does not occur is when FFFF_FFFF is added to zero, so I think that is the situation that gives all high, except one pulse low. (I think FFFF_FFFE gives two time slots low, and there is no value of frqa that gives 100% high.

    There is an interesting relationship between the NCO mode and the PWM mode. At least for values of frqa < 8000_0000. The NCO output is bit 31, while PWM output is the carry bit. There is a carry any time bit 31 goes from 1 to 0. So the PWM is kind of the derivative, negative edge version of the NCO. (Again, that is true only for frqa<8000_0000.)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Ym2413aYm2413a Posts: 630
    edited 2006-05-30 19:38
    I wonder if it would be best to limit the range to the center at times to make things easier to filter. That's if you don't need the full 32bits of resolution and aren't going to have a problem with a DC offset.

    Just an idea. ^^
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2006-05-31 02:07
    Indeed, the output will be easiest to filter when it is a high frequency. As Phillip pointed out, with an 80mhz clock, the output frequency from the phsa carry bit, when frqa=$8000_0000, is a 40 mhz symmetrical square wave.

    By the way, Phillip, thanks for the nice example program!

    At frqa=$4000_0000, the output frequency is 20 mhz, but one period in four is high. At frqa=$2000_0000, the output frequency is 10mhz, with one period in 8 high. And so on. The ratio is the duty cycle that produces the proportional output that can be filtered to an analog voltage or current.

    frqa         output frequency from phsa carry         pulse ratio proporion of time slots high
    $8000_0000           40 mhz                                 1:2     one high one low
    $4000_0000           20 mhz                                 1:4     one in 4 high
    $2000_0000           10 mhz                                 1:8     one in 8 high
    $1000_0000           5 mhz                                 1:16
    $800_0000           2.5 mhz                                 1:32
    $400_0000           625 khz                                 1:64
    $200_0000           312.5 khz                                 1:128
    $100_0000           156.25 khz                                 1:256
    $80_0000           78.125 khz                                 1:512
    



    A waveform that consists of one high time slot and 511 low time slots is going to be much harder to filter than one that consists of 1 in 8 or 1 in 16. Values of frqa in between the ones there give a proportional duty cycle, but I have caveats about that below.

    On the other side of $8000_0000, the situation is the same, except that the proportion of time high is greater than the time low. So the pulse ratio is the proportion of time slots low, and the pattern of fundamental frequencies is symmetrical around frqa=$8000_0000:

    frqa         output frequency from phsa carry         pulse ratio proporion of time slots low
    $8000_0000           40 mhz                                 1:2
    $C000_0000           20 mhz                                 1:4    one in 4 low
    $E000_0000           10 mhz                                 1:8     one in 8 low
    $F000_0000           5 mhz                                 1:16      etc.
    $F800_0000           2.5 mhz                                 1:32
    $FC00_0000           625 khz                                 1:64
    $FE00_0000           312.5 khz                                 1:128
    $FF00_0000           156.25 khz                                 1:256
    $FF80_0000           78.125 khz                                 1:512
    



    So there is good reason if analog filtering is the goal to limit the pulse modulation to frqa values between, (arbitrarily), $1000_0000 to $F000_0000, so the output frequency stays above 5 mhz and the pulse ratio stays below 1:16.

    This is what is called the DUTY mode of the counter (%00110 or %00111), not the NCO/PWM mode(%00100 or %00101). The nomenclature might be confusing. In the DUTY mode the output comes from the carry bit of the phs register. The waveform produced might better be called "pulse density modulation". In the mode that is called NCO/PWM, the numerical oscillator that takes its output from bit 31 of the phs register and the freerunning output is always very nearly a square wave. It does NOT by itself produce PWM. To make the NCO produce PWM, some programming tricks are necessary. (I haven't thought of how to do it without cycle by cycle intervention from a PASM or spin program -- anybody? Chip?). The DUTY mode on the other hand produces a pulse density that is proportional to the value in the frq register, and is thus well suited for analog filtering to produce an analog output.

    There is more that can be said, a rant into theory. One might think that the DUTY mode could be used to produce a filtered analog output that varies smoothly with essentially 32 bit resolution. However, it is not quite that simple. The DUTY mode shares numerical qualities with the NCO mode, qualities that came up in an earlier thread about distortion and birdies in the NCO output. Just as the NCO does not produce a pure square wave, the DUTY mode does not produce a continuously variable pulse density. For example, consider the small change in moving from frqa=$8000_0000 to frqa=$7FFF_FFFF. Although the output duty averaged over the entire one minute cycle is a factor of 1/(2^32) shorter, that does not mean that the analog voltage will decrease by that same amount. The overall frequency from phsa does not budge from 40 mhz, a 1:1 ratio of high to low. At one point and only one point in the one minute (53.6870912 seconds exactly) cycle, there are two low time slots instead of one. Any reasonable filter will roll right over that. There are also frequencies that will be harder to filter than others. Like the NCO, the output of the DUTY mode will contain subharmonic frequecies that depend on the exact value of frqa. A frequency that involves a golden ratio (limit of Fibbonacci sequence) will be the hardest to filter because of closest spacing of the subharmonics. Example: frqa:=1_327_217_885 is (2^32 /(SQR(5) + 1)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Phillip Y.Phillip Y. Posts: 62
    edited 2006-05-31 03:20
    Here is the same program tweaked to output more slowly on the LED's of the demo board.
    I call it the inch worm.

    The PWM output is centered on $7F_FF_FF_FF with a variable for 2 to 32 bits of resolution,
    for the bits of resolution the·FRQ min, max and lsb step values are precalculated by the compiler,
    one lsb is added each loop after a delay, this delay needs to be as long as the lsb pwm time requires·or it may never be output.

    My oscilloscope shows the 300hz triangle is quite straight at 4 bits or more, and kinky with 2 and 3 bits of resolution.

    Does anyone know·how to program·the velocity profile of a pendulum·?
    overlap 2 adjacent leds so net brightness is constant.

    A pendulum would·look better on·a PropStick with 16 LEDS.

    Radio Shack #276-209 $1.30 are 12v resistor leds,
    they are great for the proto boards where you don't have space for·separate resistors,
    but add some insulation to prevent shorts.
Sign In or Register to comment.