DAC

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

Thanks!

watch out! sin() doesnt take input in degrees, it uses radians. convert them first!

Also it takes quite a time so the frequency won't be very high, you are better off with a look up table.

So I could calculate 16384 values for my 14-bit resolution DAC, store them in a very large int array, and then just read it?

Wrong way round. A look up table normally consists of say 512 samples, each one has a 14 bit resolution.

Nevermind-- just realized that comes out to about 64KB! using floating point.

Some help? I basically want a primitive function generator with the highest frequency range and the greatest range.

Searching for "Arduino DDS" or Direct Digital Synthesis will yield a trove of threads that do approximately what you want.

-br

http://entropymouse.com

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?

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)...

OK. so my 14-bit DAC should operate at max with a lookup table of 1024 values?

1024 for bottom left, another 1024 for top left, 1024 for top right, 1024 for bottom right.

Edit: 4096/4=1024 values, right?

DOH! its 12-bit resolution :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue: [they need a "DOH!" smiley.]

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.

Still 16KB though!

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.

How "true" depends on the resolution and the sampling frequency.

I have a sine wave example at Redirecting...
The articles referenced at the bottom of the page discuss THD trade-offs.

(* jcl *)

Thanks...
I think I'll just use the sine (sin) wave approach. Also, is there a floating point PI constant that I can use with the sin() function?

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.

Some students at Georgia Tech used the code as a DDS example
for a synth project --
http://www.ece.gatech.edu/academic/courses/ece4007/10fall/ECE4007L01/dk4/index.html

(* jcl *)