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. I have the fallowing code .
Code: Select all
unsigned char spi(unsigned char data)
{
//Start transmision
SPDR = data;
//Wait for transmision complete
while (!(SPSR & 0x80));
return SPDR;
}
//Sets the waveform generator output to given kHz
void SetWGFreq(unsigned int freq)
{
unsigned long freg;
char fByte0;
char fByte1;
char fByte2;
char fByte3;
freg = (unsigned long)freq*33554.432; //Number based on a MCLK of 8 MHz
fByte0 = (char)freg;
fByte1 = (char)(freg>>8);
fByte1 = (fByte1 & 0x3F) | 0x40; //clears bits 15 and 14, then sets for FREQ0
fByte2 = (char)(freg>>14); //byte1 only has 6 bits, so move over by 8+6
fByte3 = (char)(freg>>22); //byte1 only has 6 bits, so move over by 8+8+6
fByte3 = (fByte3 & 0x3F) | 0x40; //clears bits 15 and 14, then sets for FREQ0
SPCR = 0x5A; //Set SPI to mode 2 and Fosc/64
WG_CS = 0;
while(WG_CS_PIN); //Wait for chip select pin to go low
spi(0x20); //Load control register with B28 high
spi(0x00);
spi(fByte1);
spi(fByte0);
spi(fByte3);
spi(fByte2);
WG_CS = 1;
}