
#ifndef _DDS_H
#define _DDS_H
#define AD9851_W_FLK_H GPIO_ResetBits(GPIOB,GPIO_Pin_10);
#define AD9851_W_FLK_L GPIO_SetBits(GPIOB,GPIO_Pin_10);
#define AD9851_FQ_UD_L GPIO_SetBits(GPIOB,GPIO_Pin_11);
#define AD9851_FQ_UD_H GPIO_ResetBits(GPIOB,GPIO_Pin_11);
#define AD9851_RESET_L GPIO_SetBits(GPIOB,GPIO_Pin_13);
#define AD9851_RESET_H GPIO_ResetBits(GPIOB,GPIO_Pin_13);
#define AD9851_Bit_Set GPIO_SetBits(GPIOD,GPIO_Pin_15);
#define AD9851_Bit_Reset GPIO_ResetBits(GPIOD,GPIO_Pin_15);
void AD9851_Reset(void);
void AD9851_Reset_Serial(void);
void AD9851_Write_Word(unsigned char Word);
void AD9851_WR_Parrel(unsigned char W0,double Frequence);
void AD9851_WR_Serial(unsigned char W0,double Frequence);
void Sweep_Frequency(double Start_Frequency,double End_Frequency,unsigned int Sweep_Speed_10Ms);
#endif
/*9851是最较简单的DDS,只有频率字跟相位字,将上面的头文件直接包含进工程的main文件里面,就可以直接调用下面的函数体了*/
#include "DDS.h"
#include extern void Delay(vu32 nCount); //---------------------------------------------------// // ad9851复位(并口模式) // //---------------------------------------------------// void AD9851_Reset(void) { AD9851_W_FLK_L; AD9851_FQ_UD_L; AD9851_RESET_L; AD9851_RESET_H; Delay(1); AD9851_RESET_L; Delay(1); } //***************************************************// // ad9851复位(并口模式) // //---------------------------------------------------// void AD9851_Reset_Serial(void) { AD9851_W_FLK_L; AD9851_FQ_UD_L; AD9851_Write_Word(0X03); //rest信号 AD9851_RESET_L; AD9851_RESET_H; Delay(1); AD9851_RESET_L; Delay(1); //w_clk信号 AD9851_W_FLK_H; Delay(1); AD9851_W_FLK_L; //fq_up信号 AD9851_FQ_UD_H; Delay(1); AD9851_FQ_UD_L; } __inline void AD9851_Write_Word(unsigned char Word) { u16 Port_Data; Port_Data=Word; Port_Data=Port_Data<<8; GPIO_Write(GPIOD, Port_Data); Delay(1); AD9851_W_FLK_L; AD9851_W_FLK_H; Delay(1); AD9851_W_FLK_L; } //***************************************************// // 向ad9851中写命令与数据(并口) // //---------------------------------------------------// void AD9851_WR_Parrel(unsigned char W0,double Frequence) { u8 Word; u32 Frequency_Hex; double Ratio; Ratio=4294967295/180;//适合180M晶振 Frequence=Frequence/1000000; Frequence=Frequence*Ratio; Frequency_Hex=Frequence; //写w0数据 Word=W0; AD9851_Write_Word(Word); //写w1数据 Word=(Frequency_Hex>>24); AD9851_Write_Word(Word); //写w2数据 Word=(Frequency_Hex>>16); AD9851_Write_Word(Word); //写w3数据 Word=(Frequency_Hex>>8); AD9851_Write_Word(Word); //写w4数据 Word=(Frequency_Hex>>0); AD9851_Write_Word(Word); //移入始能 AD9851_FQ_UD_L; AD9851_FQ_UD_H; Delay(1); AD9851_FQ_UD_L; } //***************************************************// // 向ad9851中写命令与数据(串口) // //---------------------------------------------------// void AD9851_WR_Serial(unsigned char W0,double Frequence) { u8 Word; u32 Frequency_Hex; double Ratio; unsigned char i; Ratio=4294967295/180;//适合180M晶振 Frequence=Frequence/1000000; Frequence=Frequence*Ratio; Frequency_Hex=Frequence; //写w4数据 Word=(Frequency_Hex>>=0); for(i=0;i<8;i++) { if((Word>>i)&0x01) { AD9851_Bit_Set; } else { AD9851_Bit_Reset; } AD9851_W_FLK_H; Delay(1); AD9851_W_FLK_L; } //写w3数据 Word=(Frequency_Hex>>=8); for(i=0;i<8;i++) { if((Word>>i)&0x01) { AD9851_Bit_Set; } else { AD9851_Bit_Reset; } AD9851_W_FLK_H; Delay(1); AD9851_W_FLK_L; } //写w2数据 Word=(Frequency_Hex>>=16); for(i=0;i<8;i++) { if((Word>>i)&0x01) { AD9851_Bit_Set; } else { AD9851_Bit_Reset; } AD9851_W_FLK_H; Delay(1); AD9851_W_FLK_L; } //写w1数据 Word=(Frequency_Hex>>=24); for(i=0;i<8;i++) { if((Word>>i)&0x01) { AD9851_Bit_Set; } else { AD9851_Bit_Reset; } AD9851_W_FLK_H; Delay(1); AD9851_W_FLK_L; } //写w0数据 Word=W0; for(i=0;i<8;i++) { if((Word>>i)&0x01) { AD9851_Bit_Set; } else { AD9851_Bit_Reset; } AD9851_W_FLK_H; Delay(1); AD9851_W_FLK_L; } //移入始能 AD9851_FQ_UD_H; Delay(1); AD9851_FQ_UD_L; } void Sweep_Frequency(double Start_Frequency,double End_Frequency,unsigned int Sweep_Speed_10Ms) { double Frequency; unsigned int i; Frequency=Start_Frequency; do { AD9851_WR_Parrel(0x01,Frequency); for(i=Sweep_Speed_10Ms;i>0;i--) { Delay(10000); } Frequency=Frequency+20; } while(Frequency<=End_Frequency); }
