AD9834驱动
//AD9834.h #ifndef _AD9834_H_ #define _AD9834_H_ #include <STC12C5A60S2.H> #define FCLK 12000000 //50MµÄ¾§Õñ #define FREQ_0 0 #define FREQ_1 1 sbit FSYNC = P1^0; sbit SCLK = P1^1; sbit SDATA = P1^2; sbit RESET = P1^3; void Send_Con(unsigned int Con_word) ; void Send_Freq(unsigned int Freq_word); void Send_Phase(); void writing_Freq(unsigned char freq_number, unsigned long Freq_value); #endif
//AD9834.c #include <STC12C5A60S2.H> #include <ad9834.h> /***************************************************** AD9834波形产生程序 *****************************************************/ /************************** 发送控制字 **************************/ void Send_Con(unsigned int Con_word) { unsigned char i; FSYNC =0; //低电平使数据有效 for(i=0;i<16;i++) //传输16位控制字 { SDATA =Con_word&0x8000; //最高位不变,最高位赋给SDATA Con_word<<=1; //控制字左移一位,准备下一次 SCLK =0; //产生下降沿,传输SDATA SCLK =1; } FSYNC =1; //高电平,无效 } /**************************** 发送频率字 ****************************/ void Send_Freq(unsigned int Freq_word) { unsigned char i; FSYNC =0; //低电平使数据有效 for(i=0;i<16;i++) //传输16位频率字 { SDATA =Freq_word&0x8000; //最高位不变,最高位赋给SDATA Freq_word<<=1; //频率字左移一位,准备下一次 SCLK =0; //产生下降沿,传输SDATA SCLK =1; } FSYNC =1; //高电平,无效 } /***************************** 发送相位字 注:根据情况可设定入口参数,设置相位 *****************************/ void Send_Phase() { Send_Con(0xC000); //设置初相位为0 } /******************************************************* 设置频率值 Freq_value(value)=Freq_data(data)*FCLK/2^28 2^28=268435456=0x10000000 *******************************************************/ void writing_Freq(unsigned char freq_number, unsigned long Freq_value) { unsigned long FREQREG = (unsigned long)(268435456.0/FCLK*Freq_value); unsigned int LSB_D = (unsigned int)FREQREG; unsigned int MSB_D = (unsigned int)(FREQREG>>14); if(freq_number == FREQ_0) { LSB_D &= ~(1U<<15); LSB_D |= 1<<14; MSB_D &= ~(1U<<15); MSB_D |= 1<<14; } else { LSB_D &= ~(1<<14); LSB_D |= 1U<<15; MSB_D &= ~(1<<14); MSB_D |= 1U<<15; } Send_Freq(LSB_D); Send_Freq(MSB_D); }
我自己写的AD9834的示例工程,仅供参考
中文手册