# problem with AD9833 Wave from generator.

#### veerubiji

Joined Oct 9, 2012
13
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:

#### ErnieM

Joined Apr 24, 2011
8,108
Rich (BB code):
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:

Rich (BB code):
freg2 = (unsigned long)freq*33.554432;   //Number based on a MCLK of 8 MHz
// AND freq in Hz
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:

#### ErnieM

Joined Apr 24, 2011
8,108
It occurred to me that by eliminating the floating point arithmetic several complications drop away:

Rich (BB code):
freg2 = (freq * 33554432) / 1000000;   //Number based on a MCLK of 8 MHz
// 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.

#### veerubiji

Joined Oct 9, 2012
13
It occurred to me that by eliminating the floating point arithmetic several complications drop away:

Rich (BB code):
freg2 = (freq * 33554432) / 1000000;   //Number based on a MCLK of 8 MHz
// 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.
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.
125300*33554432=4190725159600/1000000
'
I will try by declaring as float then i will check what will happen and i will inform you.