View previous topic :: View next topic |
Author |
Message |
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Thu Dec 18, 2014 12:01 am Post subject: DDS for generate sin wave |
|
|
Hello
Luciano have wrote programma that use DDS technics forma generate wave. There is a thread on it
I need generate 40Hz - 100Hz sine wave with 1 hertz step
I would ask if someone have written a more simply version of it.
Thanks
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
amirf
Joined: 25 Aug 2009 Posts: 154
|
Posted: Thu Dec 18, 2014 1:35 am Post subject: |
|
|
search forum sinewave |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 585 Location: Cleveland, OH
|
Posted: Thu Dec 18, 2014 7:25 pm Post subject: |
|
|
A DDS approach is certainly a great way to generate a sin wave of just about any desired frequency, up to the limits of the microcontroller.
100 Hz, however, is certainly a very low frequency as far as a microcontroller is concerned.
The theory behind a DDS is rather complicated, and it takes a while to understand how it works.
An option is to have a look up table and just output each sample one at a time, at a given interrupt rate.
The following link on AVRFreaks is a very long Thread, but it includes a Bascom program for generating a 50 Hz sin wave.
http://www.avrfreaks.net/forum/generating-wave-form-attiny2313
Note that it uses an older AVR, the Mega8515, and if you use a newer uC you would need to change the routine that generates the periodic interrupt.
This program generates a periodic interrupt, and every time the interrupt fires it loads the PWM register with the next value in a look up table.
This program calculates the sin lookup table for the PWM output before it runs the program.
It will guide you through this, if you haven't worked it out already.
If you need 40 - 100 Hz, you could simply recalculate the table every time you change the frequency, if you can afford a small delay when you make the frequency change.
Or you could look at how the samples are calculated, and just calculate the next sample in the interrupt routine.
In the Interrupt routine you would output the next sample to the PWM register, and then calculate what the next value will be, and then return.
The program also includes a push button debounce discussion and method, (and code).
If you use an Up and Down push button switch for selecting the frequency you might find this to also be useful.
If you do not already have an account on AVRFreaks, you might need one to see the photos included in the Thread.
(It doesn't cost anything to sign up.)
Note that most of the AVRFreaks members program in C, not Bascom.
Although this program was posted there, you will likely get more help from your Bascom project here, on the Bascom Forum.
Presumably you already know that there are a couple ways to generate a sin wave, and using the PWM output of the uC and feeding it through a Low Pass Filter is one of the easiest methods.
If you switch to an Xmega many of them include a DAC module, giving you another method of generating a sin wave.
The XmegaE5 is the smallest Xmega with a DAC, it is a surface mount only chip, (no DIP package), however.
JC |
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Fri Dec 19, 2014 1:41 pm Post subject: |
|
|
Hello JC
thanks for your post but my firmware need to change frequency so I need to use Luciano like solution.
Since you have more experience on this field, are you able to comment a very simply program extract from Luciano Thread?
http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=1825
I would understand what program do and not ony use it
Thanks for your time |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 585 Location: Cleveland, OH
|
Posted: Fri Dec 19, 2014 6:49 pm Post subject: |
|
|
How often does your program change frequencies?
Does it matter if there is a sharp transition in the sin wave when you change frequencies?
I think you missed the point the 100 Hz is a very low frequency for a uC running in the MHz clock range.
It can easily generate it using the direct approach, instead of the DDS approach.
You just calculate the next point in the ISR, to be out putted to the PWM register on the next pass.
That said, a DDS gives a "smooth" transition from one frequency to the next.
For most applications this doesn't matter.
Sorry, I can't rewrite Lucian's code for you.
JC |
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Sat Dec 20, 2014 1:31 pm Post subject: |
|
|
Hi
which kind of hardware I need to generate waveform? R2R ?
Thanks |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Sun Dec 21, 2014 2:23 am Post subject: R2R |
|
|
R2R. 20 k from outputs. 10 K in series between these 20 K.
We made a filter to make it a nice 30 hertz sinus.
Have fun
Ben Zijlstra
Have corrected the values, got them wrong. Will add a picture of the filter later on. |
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Sun Dec 21, 2014 10:45 pm Post subject: |
|
|
Hello
I have put R2R on PORT D and it work.
Now questione: which is the difference between your very easy program and very difficult version of Luciano?
Thanks |
|
Back to top |
|
|
|