Controlling AD9833 with STM32F4 Discovery Board

Thread Starter

solijoli

Joined Nov 20, 2014
3
Hello, I have the module of the AD9833 wave generator chip as in the link:

https://www.google.com/search?q=ad9833 module&biw=1600&bih=799&noj=1&source=lnms&tbm=isch&sa=X&ei=tC9uVPGZLYmqywOl5IL4Ag&ved=0CAkQ_AUoAg

I'm trying to control the chip with the STM32F4 Discovery board via SPI. I need the user to be able to select the waveform type (sinusoidal, square or triangular) and select the frequency and phase for the signal. I don't have much experience with ARM based controllers but I've written a code like this:

Code:
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_spi.h"


#define AD_MCLK 25000000
uint16_t stats = 0;
uint16_t tosend = 0;
#define constant1   268435456 //2^28, (1<<28)
#define constant2   4096 //2^12, (1<<12)

#define pi2       6.2832

#define AD_B28     13
#define AD_HLB     12
#define AD_FSELECT 11
#define AD_PSELECT 10
#define AD_RESET   8
#define AD_SLEEP1  7
#define AD_SLEEP12 6
#define AD_OPBITEN 5
#define AD_DIV2    3
#define AD_MODE    1

#define AD_OFF       0
#define AD_SINUS     1
#define AD_RECT1     2
#define AD_RECT2     3
#define AD_TRIANGLE  4

#define AD_FREQ0     18
#define AD_FREQ1     28
#define AD_PHASE0    38
#define AD_PHASE1    48

void Delay1ms(uint32_t nCount);
volatile uint32_t delay1ms;

void Delay1ms(uint32_t nCount)
{
  delay1ms = nCount;
  while(delay1ms);
}

void SysTick_Handler(void)
{
if(delay1ms) delay1ms--;
}


void AD9833_write(uint16_t data)
{

    GPIO_ResetBits(GPIOE, GPIO_Pin_7);

    SPI_I2S_SendData(SPI1, (data>>8)); //MSB_First
    //while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); // wait until transmit complete
    while(SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_BSY) == SET); // wait until SPI is not busy anymore
    SPI_I2S_SendData(SPI1, (uint8_t)(data & 255));  //LSB
    //while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); // wait until transmit complete
    while(SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_BSY) == SET); // wait until SPI is not busy anymore

    GPIO_SetBits(GPIOE, GPIO_Pin_7);
}


void AD9833_setfreq(uint32_t frequency, uint8_t reg){
    double temp = ((double)frequency/(double)AD_MCLK);
    uint32_t regist = temp * constant1;
    tosend = regist & 0x3FFF;
    if (reg == AD_FREQ0){tosend |= (1<<14);}
    if (reg == AD_FREQ1){tosend |= (1<<15);}
    Delay1ms(5);
    AD9833_write(tosend);
    Delay1ms(5);
    tosend = (regist & ~0x3FFF)>>14;
    if (reg == AD_FREQ0){tosend |= (1<<14);}
    if (reg == AD_FREQ1){tosend |= (1<<15);}
    AD9833_write(tosend);

}

void AD9833_setphase(uint16_t phase, uint8_t reg){
    uint16_t regist = (uint16_t) (phase*constant2)/pi2;
    tosend = (1<<15) | (1<<14);
    if (reg == AD_PHASE1){tosend |= (1<<13);}
    AD9833_write(tosend | regist);
}

void AD9833_setmode(uint8_t signal){
    switch (signal){
        case AD_OFF:
            AD9833_off();
            break;
        case AD_SINUS:
            stats &=  ~((1<<AD_OPBITEN) | (1<<AD_MODE) | (1<<AD_SLEEP1));
            break;
        case AD_RECT2:
            stats &=  ~((1<<AD_MODE) | (1<<AD_DIV2) | (1<<AD_SLEEP1));
            stats |= (1<<AD_OPBITEN);
            break;
        case AD_RECT1:
            stats |= (1<<AD_OPBITEN) | (1<<AD_DIV2);
            stats &= ~((1<<AD_MODE) | (1<<AD_SLEEP1));
            break;
        case AD_TRIANGLE:
            stats &= ~((1<<AD_OPBITEN) | (1<<AD_SLEEP1));
            stats |= (1<<AD_MODE);
            break;
        default:
            stats |= (1<<AD_OPBITEN) | (1<<AD_MODE);
    }
    AD9833_write(stats);
}

void AD9833_reg(uint8_t reg){
    switch (reg){
        case AD_FREQ0:
            stats &= ~(1<<AD_FSELECT);
            break;
        case AD_FREQ1:
            stats |= (1<<AD_FSELECT);
            break;
        case AD_PHASE0:
            stats &= ~(1<<AD_PSELECT);
            break;
        case AD_PHASE1:
            stats |= (1<<AD_PSELECT);
            break;
        default:
            stats &= ~((1<<AD_FSELECT) | (1<<AD_PSELECT));
    }
    AD9833_write(stats);
}


void init_SPI1(void)
  {

        GPIO_InitTypeDef GPIO_InitStruct;
        SPI_InitTypeDef SPI_InitStruct;


           // enable peripheral clock
           RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);


           SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx;
           SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
           SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b;
           SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
           SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
           SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
           SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
           SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
           SPI_Init(SPI1, &SPI_InitStruct);


        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        /* configure pins used by SPI1
         * PA5 = SCK
         * PA6 = MISO
         * PA7 = MOSI
         */
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_5;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStruct);

        // connect SPI1 pins to SPI alternate function
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

        // enable clock for used IO pins
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

        /* Configure the chip select pin
           we will use PE7 */
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOE, &GPIO_InitStruct);


        GPIO_SetBits(GPIOE, GPIO_Pin_7); // set PE7 high

        SPI_Cmd(SPI1, ENABLE); // enable SPI1
    }

int main(void) {

    init_SPI1();

    AD9833_setmode(AD_SINUS);
    AD9833_setfreq(1000, AD_FREQ0);
    AD9833_setphase(0,AD_PHASE0);


    while(1){

    }
}
But the code doesn't function properly. I couldn't tell if my SPI configuration or the way I send data is wrong or if there any other bugs. Where am I doing wrong and how can this be fixed? Thanks for the help!

Link for AD9833 datasheet if needed:

http://www.analog.com/static/imported-files/data_sheets/AD9833.pdf
 

MrChips

Joined Oct 2, 2009
22,893
But the code doesn't function properly.
That doesn't give us much to work on. Tell us what it does and what it doesn't.

So you have setmode, setfreq and setphase. But you haven't told it to do anything.

Do you have access to an oscilloscope?
 

Thread Starter

solijoli

Joined Nov 20, 2014
3
I'm sorry, there was a mistake, can you consider this code:

Code:
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_spi.h"


#define AD_F_MCLK 25000000 //Clock speed of the ad9833 reference clock
#define AD_2POW28 268435456 //used in calculating output freq
#define AD_FREQ_CALC(freq) (uint32_t)(((double)AD_2POW28/(double)AD_F_MCLK*freq))
#define AD_PHASE_CALC(phase_deg) (uint16_t)((512*phase_deg)/45)
static uint16_t CONTROL_REGISTER;

#define USE_FREQ0_REG             0
#define USE_FREQ1_REG             1
#define USE_PHASE0_REG             0
#define USE_PHASE1_REG             1

#define WRITE_TO_FREQ1_REG         0x8000
#define WRITE_TO_FREQ0_REG         0x4000
#define WRITE_TO_PHASE1_REG         0xE000
#define WRITE_TO_PHASE0_REG         0xC000

#define AD_B28     13
#define AD_HLB     12
#define AD_FSELECT 11
#define AD_PSELECT 10
#define AD_RESET   8
#define AD_SLEEP1  7
#define AD_SLEEP12 6
#define AD_OPBITEN 5
#define AD_DIV2    3
#define AD_MODE    1

#define AD_OFF      0
#define AD_TRIANGLE 1
#define AD_SQUARE   2
#define AD_SINE     3


typedef struct {
uint8_t  freq_out;
uint8_t  phase_out;
uint8_t  mode;
uint16_t command_reg;
} ad9833_settings_t;

typedef struct {
    float    freq[2];
    float    phase[2];
} ad9833_settings_f;


ad9833_settings_t ad_settings;
ad9833_settings_f ad_settings2;


void delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}


static void AD9833_word(uint16_t data)
{
    SPI1->DR = (data>>8); // High 8 bits, MSB first
    while(!(SPI1->SR & SPI_I2S_FLAG_TXE));
    while( SPI1->SR & SPI_I2S_FLAG_BSY );
    SPI1->DR = (uint8_t) data; // Low 8 bits
    while(!(SPI1->SR & SPI_I2S_FLAG_TXE));
    while( SPI1->SR & SPI_I2S_FLAG_BSY );
}

static void AD9833_write(uint16_t data)
{
    GPIO_ResetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
    AD9833_word(data);
    delay(0xFFF);
    GPIO_SetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
}

void ad9833_setPhase(uint8_t reg, double phase){
    uint16_t reg_reg;
    if (reg==1)
        reg_reg = WRITE_TO_PHASE1_REG;
    else
        reg_reg = WRITE_TO_PHASE0_REG;

    ad_settings2.phase[reg] = phase;

    GPIO_ResetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
    AD9833_word(reg_reg | AD_PHASE_CALC(ad_settings2.phase[reg]));
    delay(0xFFF);
    GPIO_SetBits(GPIOE, GPIO_Pin_7);
}

void AD9833_setFreq(uint8_t reg, double freq)
{/* WRITE 28-BIT FREQUENCE */
    uint32_t freq_reg;
    uint16_t reg_reg;
    freq_reg = AD_FREQ_CALC(freq);
    ad_settings2.freq[reg] = freq;
    if (reg==1)
        reg_reg = WRITE_TO_FREQ1_REG;
    else
        reg_reg = WRITE_TO_FREQ0_REG;

    GPIO_ResetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
    AD9833_write((1<<AD_B28) | ad_settings.command_reg);
    AD9833_word(reg_reg | (0x3FFF&(uint16_t)(freq_reg>>2 )));
    AD9833_word(reg_reg | (0x3FFF&(uint16_t)(freq_reg>>16)));
    delay(0xFFF);
    GPIO_SetBits(GPIOE, GPIO_Pin_7);
}


void AD9833_setMode(uint8_t mode)
{

    ad_settings.mode = mode;
    switch(mode)
    {

            case AD_OFF:
                ad_settings.command_reg |= (1<<AD_SLEEP12);
                ad_settings.command_reg |= (1<<AD_SLEEP1);
                break;
            case AD_TRIANGLE:
                ad_settings.command_reg &= ~(1<<AD_OPBITEN);
                ad_settings.command_reg |=  (1<<AD_MODE);
                ad_settings.command_reg &= ~(1<<AD_SLEEP12);
                ad_settings.command_reg &= ~(1<<AD_SLEEP1);
                break;
            case AD_SQUARE:
                ad_settings.command_reg |=  (1<<AD_OPBITEN);
                ad_settings.command_reg &= ~(1<<AD_MODE);
                ad_settings.command_reg |=  (1<<AD_DIV2);
                ad_settings.command_reg &= ~(1<<AD_SLEEP12);
                ad_settings.command_reg &= ~(1<<AD_SLEEP1);
                break;
            case AD_SINE:
                ad_settings.command_reg &= ~(1<<AD_OPBITEN);
                ad_settings.command_reg &= ~(1<<AD_MODE);
                ad_settings.command_reg &= ~(1<<AD_SLEEP12);
                ad_settings.command_reg &= ~(1<<AD_SLEEP1);
                break;
    }

    AD9833_write(ad_settings.command_reg); // Send selected MODE to AD9833
}


void init_SPI1(void)
  {

        GPIO_InitTypeDef GPIO_InitStruct;
        SPI_InitTypeDef SPI_InitStruct;


        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        /* configure pins used by SPI1
         * PA5 = SCK
         * PA6 = MISO
         * PA7 = MOSI
         */
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_5;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStruct);

        // connect SPI1 pins to SPI alternate function
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

        // enable clock for used IO pins
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

        /* Configure the chip select pin
           in this case we will use PE7 */
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOE, &GPIO_InitStruct);

        GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high

        // enable peripheral clock
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

        /* configure SPI1 in Mode 0
         * CPOL = 1 --> clock is high when idle
         * CPHA = 0 --> data is sampled at the first edge
         */
        SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx;
        SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
        SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b; // one packet of data is 8 bits wide
        SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;        // clock is low when idle
        SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
        SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
        SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4
        SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
        SPI_Init(SPI1, &SPI_InitStruct);

        SPI_Cmd(SPI1, ENABLE); // enable SPI1
    }

int main(void) {

    init_SPI1();
    CONTROL_REGISTER |= (1<<AD_B28)|(1<<AD_RESET)|(1<<AD_DIV2);
    AD9833_write(CONTROL_REGISTER);

    AD9833_setFreq(0, 1000);
    ad9833_setPhase(0, 0);
    AD9833_setMode(AD_TRIANGLE);
    while(1){

    }
}
In this code, I was able to generate the desired signals but I can't change the frequency or the phase. The signals are always in the same frequency. What do you think is wrong and how can it be fixed. Thanks a lot.

Edit:
I realized when AD9833_setMode() is written above of AD9833_setFreq() and AD9833_setPhase() in the main part, I get a sinusoidal signal whether I chose the mode to be square or triangular. I couldn't understand why or if it is important but I wanted to let you know.
 
Last edited:

Thread Starter

solijoli

Joined Nov 20, 2014
3
So is there anyone who can tell why the frequency and phase can't change? This is my current code:
Code:
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_spi.h"


#define AD_F_MCLK 25000000 //Clock speed of the ad9833 reference clock
#define AD_2POW28 268435456 //used in calculating output freq
#define AD_FREQ_CALC(freq) (uint32_t)(((double)AD_2POW28/(double)AD_F_MCLK*freq))
#define AD_PHASE_CALC(phase_deg) (uint16_t)((4096*phase_deg)/360)
static uint16_t CONTROL_REGISTER;


#define WRITE_TO_FREQ1_REG         0x8000
#define WRITE_TO_FREQ0_REG         0x4000
#define WRITE_TO_PHASE1_REG         0xE000
#define WRITE_TO_PHASE0_REG         0xC000

#define AD_B28     13
#define AD_HLB     12
#define AD_FSELECT 11
#define AD_PSELECT 10
#define AD_RESET   8
#define AD_SLEEP1  7
#define AD_SLEEP12 6
#define AD_OPBITEN 5
#define AD_DIV2    3
#define AD_MODE    1

#define AD_OFF      0
#define AD_TRIANGLE 1
#define AD_SQUARE   2
#define AD_SINE     3


typedef struct {
uint8_t  freq_out;
uint8_t  phase_out;
uint8_t  mode;
uint16_t command_reg;
} ad9833_settings_t;

typedef struct {
    float    freq[2];
    float    phase[2];
} ad9833_settings_f;


ad9833_settings_t ad_settings;
ad9833_settings_f ad_settings2;


void delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}


static void AD9833_word(uint16_t data)
{
    SPI1->DR = (data>>8); // High 8 bits, MSB first
    while(!(SPI1->SR & SPI_I2S_FLAG_TXE));
    while( SPI1->SR & SPI_I2S_FLAG_BSY );
    SPI1->DR = (uint8_t) data; // Low 8 bits
    while(!(SPI1->SR & SPI_I2S_FLAG_TXE));
    while( SPI1->SR & SPI_I2S_FLAG_BSY );
}

static void AD9833_write(uint16_t data)
{
    GPIO_ResetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
    AD9833_word(data);
    delay(0xFFF);
    GPIO_SetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
}

void AD9833_setPhase(uint8_t reg, double phase){
    uint16_t reg_reg;
    if (reg==1)
        reg_reg = WRITE_TO_PHASE1_REG;
    else
        reg_reg = WRITE_TO_PHASE0_REG;

    ad_settings2.phase[reg] = phase;

    GPIO_ResetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
    AD9833_word(reg_reg | AD_PHASE_CALC(ad_settings2.phase[reg]));
    delay(0xFFF);
    GPIO_SetBits(GPIOE, GPIO_Pin_7);
}

void AD9833_setPhaseOut(uint8_t phase_out){
    ad_settings.phase_out = phase_out;
    switch (phase_out){
        case 0:
            ad_settings.command_reg &= ~(1<<AD_PSELECT);
            break;
        case 1:
            ad_settings.command_reg |= (1<<AD_PSELECT);
            break;
        case 2:
            //TODO
            break;
    }

    AD9833_write(ad_settings.command_reg);

}

void AD9833_setFreq(uint8_t reg, double freq)
{/* WRITE 28-BIT FREQUENCE */
    uint32_t freq_reg;
    uint16_t reg_reg;
    freq_reg = AD_FREQ_CALC(freq);
    ad_settings2.freq[reg] = freq;
    if (reg==1)
        reg_reg = WRITE_TO_FREQ1_REG;
    else
        reg_reg = WRITE_TO_FREQ0_REG;

    GPIO_ResetBits(GPIOE, GPIO_Pin_7);
    delay(0xFFF);
    //AD9833_word((1<<AD_B28) | ad_settings.command_reg);
    AD9833_word(reg_reg | (0x3FFF & (uint16_t) (freq_reg)));
    AD9833_word(reg_reg | (0x3FFF & (uint16_t) (freq_reg>>14)));
    delay(0xFFF);
    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:
            //TODO
            break;
    }
    AD9833_write(ad_settings.command_reg);

}


void AD9833_setMode(uint8_t mode)
{

    ad_settings.mode = mode;
    switch(mode)
    {

            case AD_OFF:
                ad_settings.command_reg |= (1<<AD_SLEEP12);
                ad_settings.command_reg |= (1<<AD_SLEEP1);
                break;
            case AD_TRIANGLE:
                ad_settings.command_reg &= ~(1<<AD_OPBITEN);
                ad_settings.command_reg |=  (1<<AD_MODE);
                ad_settings.command_reg &= ~(1<<AD_SLEEP12);
                ad_settings.command_reg &= ~(1<<AD_SLEEP1);
                break;
            case AD_SQUARE:
                ad_settings.command_reg |=  (1<<AD_OPBITEN);
                ad_settings.command_reg &= ~(1<<AD_MODE);
                ad_settings.command_reg |=  (1<<AD_DIV2);
                ad_settings.command_reg &= ~(1<<AD_SLEEP12);
                ad_settings.command_reg &= ~(1<<AD_SLEEP1);
                break;
            case AD_SINE:
                ad_settings.command_reg &= ~(1<<AD_OPBITEN);
                ad_settings.command_reg &= ~(1<<AD_MODE);
                ad_settings.command_reg &= ~(1<<AD_SLEEP12);
                ad_settings.command_reg &= ~(1<<AD_SLEEP1);
                break;
    }

    AD9833_write(ad_settings.command_reg); // Send selected MODE to AD9833
}


void init_SPI1(void)
  {

        GPIO_InitTypeDef GPIO_InitStruct;
        SPI_InitTypeDef SPI_InitStruct;


        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        /* configure pins used by SPI1
         * PA5 = SCK
         * PA6 = MISO
         * PA7 = MOSI
         */
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_5;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStruct);

        // connect SPI1 pins to SPI alternate function
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

        // enable clock for used IO pins
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

        /* Configure the chip select pin
           in this case we will use PE7 */
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOE, &GPIO_InitStruct);

        GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high

        // enable peripheral clock
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

        /* configure SPI1 in Mode 0
         * CPOL = 1 --> clock is high when idle
         * CPHA = 0 --> data is sampled at the first edge
         */
        SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx;
        SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
        SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b; // one packet of data is 8 bits wide
        SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;        // clock is low when idle
        SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
        SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
        SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4
        SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
        SPI_Init(SPI1, &SPI_InitStruct);

        SPI_Cmd(SPI1, ENABLE); // enable SPI1
    }



int main(void) {

    init_SPI1();
    CONTROL_REGISTER |= (1<<AD_B28)|(1<<AD_RESET)|(1<<AD_DIV2);
    AD9833_write(CONTROL_REGISTER);

    AD9833_setPhase(0, 1);
    AD9833_setPhaseOut(0);
    AD9833_setFreq(0, 1000);
    AD9833_setFreqOut(0);
    AD9833_setMode(AD_SQUARE);

    while(1){
    }
}
I really appreciate the help!
 
Top