For my on-going exploration of ad9833 audio generation using FlashForth 5, a code repository is now available at codeberg:
So far, I have a
demo-pitches word that plays c4, e4, g4, and c5 frequencies, but I can set other frequencies ranging from a4 to g#6, using the words I have so far. There is no system yet for setting duration, so I can’t play notes proper. But it was fun setting up a system for setting the equal temperament pitches.
In microcontroller programming, it is much easier to justify efforts to save memory. I needed to store the precalculated frequency register data values for each pitch. Originally I had a table like so:
$5274 , $4000 , $538d , $4000 , $54b7 , $4000 , $55f2 , $4000 , ... $4b5a , $4002 ,
With each line (two 16 bits words) representing one pitch. However, the first two bits in each 16-bit word are actually not frequency data, but register addressing bits, which can easily be added in later. Also, the second 16-bit word in each pair has (in my application) only two bits of actual frequency data, which are the two right most bits. So, I cut the table memory usage in half by packing those two bits into the left-most two bits of the first word in each pair:
\ packaged freq register data for equal temp \ pitches from A4, A#4, B4 , ... , G#6 \ format numbered from lsb to msb: \ 0-13: the 14 LSBs of the lsb register load \ 14-15: the 2 LSBs of the msb register load create packed-pitches $1274 , $138d , $14b7 , $15f2 , $1740 , $18a2 , $1a19 , $1ba7 , ...
Now each line in the table is actually two separate pitches. Then I just need a few words to separate the bits back out into separate words, and recombine them with the frequency register addressing bits:
0 constant na 1 constant na# 2 constant nb 3 constant nc 4 constant nc# 5 constant nd 6 constant nd# 7 constant ne 8 constant nf 9 constant nf# 10 constant ng 11 constant ng# 0 constant o4 1 constant o5 2 constant o6 : pull-pitch ( note octave -- u ) 12 * + cells packed-pitches + @ ; : pull-14lsb %0011111111111111 and ; : pull-2msb %1100000000000000 and 14 rshift ; %0100000000000000 constant FREG0 : tx-pitch ( note octave -- ) pull-pitch cp>r pull-14lsb FREG0 or 2tx-spi r> pull-2msb FREG0 or 2tx-spi ;
In testing, I have some trouble with some pitches getting distorted when I set the volume to higher levels. I suspect the problem is that I am still using the USB power supply and need to feed in my +5V from an external PS. I plan to try that next week, God willing.
2 thoughts on “ff-ad9833 Repo”
You are one motivational speaker!!!
I think you are right about the need for an external power supply to generate the sound, you probably don’t have much current through the USB supply through the board.