Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
hricks
PostPosted: Aug 22, 2011 - 04:32 PM
Hangaround


Joined: May 01, 2007
Posts: 120
Location: Minneasota

I have a project i am working on at home...... I got a skeleton code from online . One thing that is not provided is how to implement the sineWave function using the DAC,STK timer, Scale formula and look up table.

The list of things I have in the file :

1.LookUptable base on 5V is given
2.Formular for calculating the Scale Value using LookUp Tale Provided.
3. I have a DAC module using SPI for DAC



What I do not know or is trying to figure out is what are the steps using the lookUp and the timer to generate sine wave . Can someone Bless me with explaining how to go about ?
God Bless
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Aug 22, 2011 - 04:35 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

What language? Are you just playing samples of a fixed frequency or are you doing DDS?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
hricks
PostPosted: Aug 22, 2011 - 06:02 PM
Hangaround


Joined: May 01, 2007
Posts: 120
Location: Minneasota

Language is in C. The goal of this project is to create sine Wave at 5v, 2 V, 1v and 3v Amplitude.
The initial frequency is 40hz, initial timing requirement is 500ms
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Aug 22, 2011 - 06:42 PM
Resident


Joined: Sep 19, 2005
Posts: 768
Location: Belgium

Check out this article on DDS
http://www.electricdruid.net/index.php?page=info.dds

What do you mean with initial timing requirement ?
How many entries in the lookup table ? Is it one full wave or half a wave .. ?
What is the samplerate ?

DDS explained in one paragraph:

Calculate phase increment as follows :
Code:
phase_inc = freq * phaseaccum_max / samplerate;

So to generate a 40Hz wave with a 16 bit phase accumulator and 1kHz samplerate, the phase increment would be
Code:
phase_inc = 40 * 0xffff / 1000;

If you now increment the phase accumulator with the phase increment at the samplerate, it will give you a 16 bit index of where in the waveform the current sample should be.
Now, depending on the size of the lookup table, take the MSB's from the phase accumulator and use this as index for your lookup table, and write the sample to the DAC.

So for instance if your table is 1024 entries (10bit), you get the sample as follows :
Code:
i = (phase_accum & 0xffc0) >> 6;
current_sample = lut_sine[i];

So to put it all together :
Code:
// init
phase_inc = freq * phaseaccum_max / samplerate;

// timer isr running at samplerate
phase_accum += phase_inc;
current_sample = lut_sine[(phase_accum & 0xffc0) >> 6];
dac_write(current_sample);


Last edited by thygate on Aug 22, 2011 - 07:23 PM; edited 3 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
DocJC
PostPosted: Aug 22, 2011 - 07:05 PM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6980
Location: Cleveland, OH

Quote:
What I do not know or is trying to figure out is what are the steps using the lookUp and the timer to generate sine wave . Can someone Bless me with explaining how to go about ?


What frequency?
Low freq's are easy using a simple table lookup without using a DDS approach.

One general approach is outlined below. Note that at every step there are a number of possible optimizations available. It is easiest if you understand the basics, however, before you start "improving" the approach.

Draw a sin wave, one cycle, on a sheet of paper.

Make a table, left column degrees from 0-360 in 1 degree steps.

Fill in the table with the sin of that degree entry.

Note that the table can also be in radians, 0 - 2 Pi radians.

Note that the sin values can also be scaled, so instead of -1 to +1 full scale they are multiplied by your factor of choice, say 1000, and now the table goes from -1000 to + 1000.

Note that storing the entire table, for 0 to 360 degrees, isn't really necessary, due to the symmetry of the sin wave. You could store 0 - 90 degrees. For 90 - 180 degrees you read the table "backwards". For 180 to 360 degrees you just change the sign of the reading from 0 - 180 degrees, but the magnitude is the same.

Note that entering the data into program is rather arduous, and the micro can just generate the table itself following the program initializations.

Now the micro part:

Set up a timer to trigger an interrupt once every millisecond, (i.e. 1000 interrupts per second).

On every interrupt read the next value from the table. Scale the value and send it to the DAC.

Assume your sin table went from -1000 to + 1000. Assume your DAC could handle that range of inputs. The output from the DAC would now be a sin wave with 360 points making up the waveform, and it would have a frequency of 360 data points per cycle, 1 mSec per data point, Period of 360 mSec, Freq of 1/Period, Freq of 2.77 Hz.

Change your interrupt to generate an interrupt at once every 50 uSec. Now it takes 360 data points * 50 uSec each for a Period of 18 mSec. Freq = 1/T = 55.5 Hz.

Change your interrupt rate to change the frequency.

Hold on, however. The DAC doesn't really accept an input of -1000 to + 1000.
An 8 bit DAC will accept 0 - 255.
So one has to scale the table values to meet the DAC's acceptable input range.
To keep it simple, say you wanted to use only 0 - 249 on the DAC, total of 250 intervals, and almost using its full scale range.
-1000 to +1000 = 2000 range. Divide it by 8 to get an output range of 250 units, (-125 to + 125).
But, many DACs don't take negative number inputs, so shift the -125 to +125 up to 0 - 250 by adding 125 to the value.

As your read the sequential table values from 0 - 360 degrees, the output now has a range from 0 - 255, and a sin pattern to it.

How does one scale the output for a variable amplitude, Peak-To-Peak of 1, 2, or 5 V?

Feed the DAC to an Op-Amp and set the gain of the op-amp for 5 V. The sin wave now goes from 0 - 5 V, Peak-To-Peak. The "Average" value, or "0" point, is 2.5 Volts.

Now simple multiply the output values fed to the DAC by 1/5 or by 2/5 to scale the P-P output put out by the DAC.

In practice, when viewing the sin on an O'scope, you will see it is made up of a series of stair-steps. Each step is a discrete DAC output voltage. One can "smooth" the output to make it look like a sin wave without the individual stair steps by feeding the DAC output, or Buffered DAC output, through a Low Pass Filter, (and then feeding it to another buffer op amp, for the real output).
The LPF will remove the higher frequency components of the stair step sin and one gets a better sin wave.

I started this discussion by generating a sin table with one data point per 1 degree, 360 data points for the full period of the sin wave. One could have a bigger table, with steps of 1/10th of a degree, or a smaller table with steps of 10 degrees per step, 36 steps for the full sin wave period. The trade off here is the fewer the steps in the software lookup table, the better the real world, hardware, LPF has to be, to get a true sin wave. (That is a "mostly" true statement, for the purists out there...).

What is the Maximum frequency you can generate using this method?

Since the micro has to read the table, calculate the scaling of the table, and write it to the DAC, it takes a finite amount of time to process each data point being generated.

A 20 MHz uC can easily generate audio frequencies using the direct table lookup method. But it won't get you a 5 MHz sin wave.

Once you understand the concepts, and have built one of these, then read up on DDS. Direct Digital Synthesis of the sin, (or other), waveform is an alternative approach to generating the signal. It is a bit more complex, conceptually, but very cool.

Lastly, note that the sin table doesn't have to be a sin wave. It can be a triangle wave, ramp, exponential, EKG, etc.).

The two photos show an EKG instead of a sin wave, generated using the above technique, and an Olimex PCB (with an LCD, LEDs, PB switches, etc.), generating the signal. The STK was to program the uC. In this case PMW was used instead of a true DAC, but the concepts are the same.

Hope this helps get you started.
The implementation is up to you.

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
hricks
PostPosted: Aug 23, 2011 - 02:06 AM
Hangaround


Joined: May 01, 2007
Posts: 120
Location: Minneasota

Sampling rate is 100 Hz.
 
 View user's profile Send private message  
Reply with quote Back to top
DocJC
PostPosted: Aug 23, 2011 - 04:22 AM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6980
Location: Cleveland, OH

So, if you want 40 Hz then:
F = 40 Hz
T = 1/40 = 0.025 Sec Period

If you sample at 100 Hz, i.e. 100 Samples/Sec, then you have 100 samples in 0.025 Sec.

This is the same as taking a sample every 0.25 mSec,
or every 250 uSec.

It would be easiest if you divided the single period into 100 samples for your table. Make your table go from 0 to 360 degrees in 100 steps, i.e. 3.6 degrees per step.

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
thygate
PostPosted: Aug 24, 2011 - 11:48 AM
Resident


Joined: Sep 19, 2005
Posts: 768
Location: Belgium

I find it incredible that two people go out of their way to write you a complete explanation of how to approach things, and all you can say is "Sampling rate is 100 Hz.".
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Aug 24, 2011 - 01:38 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21390
Location: Orlando Florida

That means the explanation was too technical to understand.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
thygate
PostPosted: Aug 24, 2011 - 02:28 PM
Resident


Joined: Sep 19, 2005
Posts: 768
Location: Belgium

Aren't we all technical people here..
I personally don't think my or Doc's post was too technical to understand.
All terms are relevant to the topic of synthesis.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Aug 25, 2011 - 12:42 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21390
Location: Orlando Florida

Yeah, but I think the guy that doesnt get the explanation yet is hricks. Hello hricks: Is your sw development system pretty solid? Can you edit, compile, burn and run a c program in your avr? If you tell us what AVR you have, what xtal it has, whether or not it has a uart, I betcha someone would post a sample program that will output a sine wave.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
joeyAVR
PostPosted: Aug 25, 2011 - 01:22 PM
Hangaround


Joined: Aug 06, 2008
Posts: 365
Location: Rockall

I can vouch for Jesper's mini-DDS. The code works 'out of the box' and is pretty much portable to any AVR (sinewave only for small parts like tiny13).

http://www.myplace.nu/avr/minidds/index.htm

Here's the results I got, I was so chuffed I added an AVR to control it (instead of a PC link) using a second tiny 2313. I've since added a buffered output with level and offset controls (as per the CA3140 datasheet).

_________________
Cheers,

Joey
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits