# problem with AD9833 Wave from generator.

Discussion in 'Embedded Systems and Microcontrollers' started by veerubiji, Nov 30, 2012.

1. ### veerubiji Thread Starter New Member

Oct 9, 2012
13
0
Hi,
I have problem in generating sine wave with specific frequency. I am using ATmega32-A micro controller and AD9833 programmable waveform generator. I am able to generate sine wave with 125 KHz.

Last edited: Jul 24, 2013
2. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,442
1,628
Code ( (Unknown Language)):
1. void SetWGFreq(unsigned int freq)
passes in freq which has units of kilohertz, thus you cannot use it to define anything with a finer unit.

So when you pass in freq treat it as hertz, not kiloherts. That could be as simple as changing one line:

Code ( (Unknown Language)):
1. freg2 = (unsigned long)freq*33.554432;   //Number based on a MCLK of 8 MHz
2.                                          // AND freq in Hz
3.
but also check the size of your variables to insure you can fit your range.

I'm also unsure of the precision of the multiplication in this statement: just when does it convert the floating point number to an integer: before or after the multiplication. That cast may force an incorrect answer. I would single step this code to see just what that line is doing.

Last edited: Nov 30, 2012
3. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,442
1,628
It occurred to me that by eliminating the floating point arithmetic several complications drop away:

Code ( (Unknown Language)):
1. freg2 = (freq * 33554432) / 1000000;   //Number based on a MCLK of 8 MHz
2.                                        // AND freq in Hz
One could also add 500000 to the product (before division) to compute a rounded result.

Just insure the variables can handle the range. It is pretty big.

4. ### veerubiji Thread Starter New Member

Oct 9, 2012
13
0
I have small doubt i am using unsigned int for freq variable variable. If i want to set 125.3 Khz then what will happen, i think it exceeds the limit.
'
I will try by declaring as float then i will check what will happen and i will inform you.