Shop OBEX P1 Docs P2 Docs Learn Events
Program for an audio sine wave osc? — Parallax Forums

Program for an audio sine wave osc?

Evan in L.A.Evan in L.A. Posts: 6
edited 2006-09-26 02:46 in Propeller 1
What's the best way to emulate the freqout command from the BS2 on a Propeller?· Say you want to generate a 1KHz tone based on a button push...· Thanks.

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-09-21 17:50
    Have you tried the FREQOUT command in Martin Hebel's BS2 object, or Beau Schwabe's frequency synthesis object?

    Both are availible at the Propeller Object Exchange.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2006-09-21 17:51
    Evan said...
    What's the best way to emulate the freqout command from the BS2 on a Propeller?· Say you want to generate a 1KHz tone based on a button push...· Thanks.
    I have used (with alot of help) the bs2_functions object......it works great....which includes the freqout method.

    Just download the object from the object exchange.

    Hope this helps,

    James L

    wow....Paul hit the submit button just a little faster.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-09-21 19:24
    I think the methods cited generate a square wave, not a sine wave. The BS2 generates a sine wave by modulating PWM at 100% with a sine waveform. It takes a few more steps to accomplish that with the Propeller. Here's the outline using the NCO mode of ctra and the DUTY mode of ctrb. The idea is that the program sets up ctra for 1khz, just as in the methods cited, but without output to a pin from phsa(31).

    ctra :=%0_11111_000_00000000_000000_000_000000 ' logic always mode
    frqa := 53687 ' to give 1000 hz cycle with 80mhz CLKFREQ

    The program reads the value of phsa into a temporary variable and shifts it right 18 bits to give a 13 bit value of phase. The top 2 bits call out the quadrant, 0 to 3, and the bottom 11 bits call out an entry in the sine table that is found in the hub ROM. The sine table is 2048 entries representing sine from 0 to 90 degrees, and a full sine routine returns a value for the entire phase circle. That is then used to set the duty cycle of ctrb. The modulation value changes in a tight repeat loop that has to execute much faster than the desired sine frequency.

    fullSine(phsa>>18)
    bits 11, 12 return this (sphsa is the low 11 bits):
    00: look up fullSine := sine(sphsa)
    01: look up fullSine := sine(2048-sphsa)
    10: look up fullSine := -sine(sphsa)
    11: look up fullSine := -sine(2048-sphsa)

    The value returned will be the sine of the angle, in the range of +/- 65535 ($ffff0001 to $ffff). For duty cycle modulation, that can be scaled and offset from the center value of $8000_0000 and fed in as the frqb parameter for counter b. Counter b runs in duty mode with output (from carry) tied to a pin.

    ctrb:=%0_00110_000_00000000_000000_000_000101 ' output to pin p5.
    frqb = $8000_0000 + (fullSine << 15) ' frqb varies in sine fashion from near 0% to near 100%

    The output is a PWM (of the carrry overflow variety), just like the Stamp FREQOUT except that the Stamp uses a smaller accumulator. The amplitude can be controlled by a multiplier less than << 15, which gives near 100% modulation. By the way, The modulation above doesn't go all the way to zero. The value of frqb would go from $1_0000 to $FFFF_0000, which is 0.0015% to 99.9985%.

    The scheme could work at low frequencies in Spin, but at higher frequencies it requires pasm. The method needs run in a repeat loop in its own COG, constantly fetching sine values and updating the duty cycle. There is a delay for the cog to grab the current phase, find the corresponding sine, and then update duty. That may be a more or less constant phase shift. The counters help, but they can't generate a sine wave on their own!

    The Stamp also allows two frequencies mixed (also found in the DTMF command). That could be done using a second counter module to generate the second NCO frequency, with the two amplitudes being added. The output could be sent to pins in parallel with a resistor D/A converter, instead of using counter b in duty mode. Or, the parallel phase accumulators could be written in pasm, tied to waitcnt, instead of using ctra for the phase. So many possibilities with the propeller!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 9/23/2006 4:56:36 AM GMT
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-09-22 04:02
    The resolution of the sine tables in the HUB rom is 16 bit values of sine at 2048 angles from 0 to 90 degrees. (Not 12 bit resolution as I had stated off the top of my head in th previous post). So I edited that and tried to reword some of the other stuff too. I'm trying to understand the capabilities of the counters.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 9/23/2006 4:55:43 AM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-09-22 05:22
    I think this would be a good "advanced" counter demonstration of the duty cycle mode. I'll look into including it in the docs probably the middle of next week. I have an urgent priority activity that will occupy me until then.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-09-26 02:46
    It was easier done than said. I posted a tutorial in the sticky thread at,
    http://forums.parallax.com/showthread.php?p=606978

    There is both a spin and an asm version. Both use the phase of counter A NCO to look up a value of sine in the hub ROM table, and then use that to modulate the duty cycle of counter B in duty mode. The asm version updates the pwm at one microsecond intervals, so it is capable of making a relatively nice audio sine wave. It gets dicey at 100 khertz. The spin version updates at about 50 microsecond intervals, so it does okay up to 200 hertz or so, and makes a spiffy pulsating light show at ~1 hz.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.