I have asked about DACs before, but it was about I2C addressing.
Now I want to make a sine wave. Is there any equation I can easily implement to create a 5V peak to peak sine wave? I don't really care much about the frequency, and I want to get as close to a "true" sine wave without to caps, just the arduino and DAC.
Isn't there a sin function in the reference section?
for x=0 to 360 {
output = sin(x), output will be -1 to 1, offset/scale as needed to make your DAC happy
ex, add 1 to output such that output is 0 to 2, and mutiply by 2.5
send to DAC
x=x+1 (or .5, or ,1, or ...)
}
repeat as needed
baum:
But I want to use a DAC, and I don't have 4.7mH inductors.
How about this:
Store a lookup table in Memory, consisting of a some (?) values pre-calculated
Loop these values, with delay on each loop setting freq.
i.e. a delay of 10usecs on each iteration will have a higher frequency than one with 100u secs.
How many lookup values should I have?
Remember that if the wave is of a regularly repeating nature, of equal amplitude on both sides (like a standard sine wave), you can get by with a look-up table of only one-quarter of the wave, then just mirror/flip this table as needed to generate a complete waveform (to reduce memory needs)...
baum:
Nevermind-- just realized that comes out to about 64KB! using floating point.
You could save some space by only storing the first 1/4 of the sine. Store data for the first quarter from 0 to 1.0. Read the table from 0-90 deg forwards. Then from 90-180 deg read the stored data back "backwards". Repeat for 180-360, but swap the sign.
OK. so my 14-bit DAC should operate at max with a lookup table of 1024 values?
You are mixing up the amplitude resolution (based on the resolution of your D/A) which is the size of the numeric entries in the look up table with time resolution which is the number of entries in your look up table.
A look up table of 128 entries will be just fine as most of the time you will not be outputting every entry because the size of increment of the address gives you the frequency of the final note. Not that this increment can be fractional and you only need use the integer part of the address.
By building a wavetable you offload the computation to your PC.
I build the wavetable table using Perl with its builtin SIN function.
That way the uC only has to perform a lookup rather than
a calculation.