#define FSYNCHIGH GPIO_WriteBit(GPIOB,GPIO_Pin_7,Bit_SET);
#define FSYNCLOW GPIO_WriteBit(GPIOB,GPIO_Pin_7,Bit_RESET);
#define SDATAHIGH GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_SET);
#define SDATALOW GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_RESET);
#define SCLKHIGH GPIO_WriteBit(GPIOB,GPIO_Pin_6,Bit_SET);
#define SCLKLOW GPIO_WriteBit(GPIOB,GPIO_Pin_6,Bit_RESET);
void set_freq(long frequency);
void AD9837Write(int dat);
FSYNCHIGH;
SDATAHIGH;
SCLKHIGH;
set_freq(1000);
void set_freq(long frequency)
{
int MSB;
int LSB;
int phase = 0;
long FREQ0_REG0;
float AD9837Val = 0.00000000;
AD9837Val = (((float)(frequency))/25000000);
FREQ0_REG0 = AD9837Val*0x10000000;
MSB = (int)((FREQ0_REG0 & 0xFFFC000)>>14); //14 bits
LSB = (int)(FREQ0_REG0 & 0x3FFF);
//Set control bits DB15 ande DB14 to 0 and 1, FREQ0 register write(D15, D14 = 01)
LSB |= 0x4000; //0b 0100 0000 0000 0000
MSB |= 0x4000; //0b 0100 0000 0000 0000
phase &= 0xC000; //0b1100 0000 0000 0000 =>When writing to a phase register, Bit D15 and Bit D14 are set to 11.
AD9837Write(0x2100);//0b 0010 0001 0000 0000 => Set D13(set frequency registers. B28 bit)and D8 (Reset=1)
delay(100);
AD9837Write(LSB); //Write Freq lower 14 bits
AD9837Write(MSB); //Write Freq upper 14 bits
AD9837Write(phase); //mid-low
delay(10);
AD9837Write(0x2000); //sin wave
//AD9837Write(0x2020); //square
//AD9837Write(0x2002); //triangle
}
void AD9837Write(int dat)
{
int x;
FSYNCLOW; //Set FSYNC low
for (x = 0; x < 16; x++)
{
if (dat & 0x8000)
{
SDATAHIGH; //Set SDATA according to MSB in cmd
}
else
{
SDATALOW;
}
SCLKLOW; //CLK transistion to latch the bit into the AD9835
SCLKHIGH;
dat <<= 1; //Shift to next bit in cmd
}
FSYNCHIGH; //Set FSYNC high
}