Problem with setting frequency with AD9833.

General discussion on mikroBasic PRO for AVR.
Post Reply
Author
Message
vebi1000
Posts: 2
Joined: 30 Nov 2012 10:38

Problem with setting frequency with AD9833.

#1 Post by vebi1000 » 30 Nov 2012 10:54

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. 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;
  }
from the above i am able to set frequency as 125KHz or 33KHz or 250KHz etc. but my problem is what if i want to frequency as 33.5KHz or 125.5KHz. I am not able to set frequency like that. can any one help me with this. I am using SPI communication.

robert_d1968
Posts: 145
Joined: 14 Nov 2012 00:30
Location: China
Contact:

Re: Problem with setting frequency with AD9833.

#2 Post by robert_d1968 » 30 Nov 2012 15:35

I don't dabble much in C if I can help it.

But, you may want to try posting it in the C forum, they will have more of a clue than me.

Best of luck,

Robert

Post Reply

Return to “mikroBasic PRO for AVR General”