AnsweredAssumed Answered

AD9833 Can't Change Frequency

Question asked by solijoli on Nov 24, 2014
Latest reply on Nov 26, 2014 by sitti

Hello, I have the AD9833 waveform generator and I'm trying to control it with my STM32F4 Discovery board. I've written the code below for the frequency part. I'm able to choose the waveform that I want but I can't set the frequency or the phase for the signal. Where am I doing wrong? I really appreciate help. Thanks!

 

 

#define AD_F_MCLK25000000
#define AD_2POW28268435456

 

 

#define AD_FREQ_CALC(freq)(uint32_t)(((double)AD_2POW28/(double)AD_F_MCLK*freq)*4)

 

#define AD_B28    13
#define AD_HLB    12
#define AD_FSELECT11
#define AD_PSELECT10
#define AD_RESET  8
#define AD_SLEEP17
#define AD_SLEEP126
#define AD_OPBITEN5
#define AD_DIV2    3
#define AD_MODE    1

 

#define AD_OFF      0
#define AD_TRIANGLE1
#define AD_SQUARE  2
#define AD_SINE    3

 

#define AD_FREQ0        (1<<14)

#define AD_FREQ1        (1<<15)

#define AD_PHASE0(3<<14)
#define AD_PHASE1((3<<14)|(1<<13))

 

void AD9833_setFreq(double freq)

{

 

  uint32_t freq_reg = AD_FREQ_CALC(freq);

  ad_settings.freq[AD_FREQ0] = freq;

 

  GPIO_ResetBits(GPIOE, GPIO_Pin_7);

  delay(0xFF);

  AD9833_word((1<<AD_B28) |ad_settings.command_reg);

  AD9833_word(AD_FREQ0 | (0x3FFF & (uint16_t) (freq_reg)));

  AD9833_word(AD_FREQ0 | (0x3FFF & (uint16_t) (freq_reg>>14)));

  GPIO_SetBits(GPIOE, GPIO_Pin_7);

}

 

void AD9833_setFreqOut(uint8_t freq_out){

    ad_settings.freq_out = freq_out;

    switch (freq_out){

        case 0:

            ad_settings.command_reg &= ~(1<<AD_FSELECT);

            break;

        case 1:

            ad_settings.command_reg |= (1<<AD_FSELECT);

            break;

        case 2:

            break;

    }

    AD9833_write(ad_settings.command_reg);

}

 

int main(void) {

 

    init_SPI1();

    AD9833_setFreq(1000);

    AD9833_setFreqOut(0);

    AD9833_setMode(AD_SQUARE);

 

  while(1){

  }

}

 

Where am I doing wrong? Thanks.

Outcomes