Pages: [1] 2   Go Down
Author Topic: DAC  (Read 2560 times)
0 Members and 1 Guest are viewing this topic.
Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
DAC
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 538
Posts: 27082
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!
Logged

United States (Texas)
Offline Offline
God Member
*****
Karma: 3
Posts: 525
Arduino socks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34143
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34143
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-br
http://bitlash.net
http://entropymouse.com
Logged

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5575
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Connecticut
Offline Offline
Edison Member
*
Karma: 17
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz smiley-razz smiley-razz smiley-razz smiley-razz [they need a "DOH!" smiley.]
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34143
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Boston, MA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1024
wiblocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I have a sine wave example at http://wiblocks.luciani.org/docs/app-notes/nb1a-nco.html
The articles referenced at the bottom of the page discuss THD trade-offs.

(* jcl *)
Logged


Pages: [1] 2   Go Up
Jump to: