Program for an audio sine wave osc?
Evan in L.A.
Posts: 6
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
Both are availible at the Propeller Object Exchange.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Just download the object from the object exchange.
Hope this helps,
James L
wow....Paul hit the submit button just a little faster.
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 Allen
www.emesystems.com
Post Edited (Tracy Allen) : 9/23/2006 4:55:43 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
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