输出阻抗50欧,未滤波(后面可以根据需要增加外部的滤波)
HardwareSPI spi(1);
int EN=14;
double F_MAIN=20000000; //20MHZ
void command(unsigned int com){
digitalWrite(EN,LOW);
spi.send((byte)((com >> 8)&0xFF));
spi.send((byte)(com & 0xFF));
digitalWrite(EN,HIGH);
}
/* input purpose frequency Unit Hz*/
unsigned long long toFValue(double fre){
if(fre>(double)F_MAIN/2){
SerialUSB.println("Error:Frequency To Large.");
return 0;
}
return (fre/F_MAIN)*0xFFFFFFFF;
}
/*
FQ 1 Choose Phase Reg ; 0 Choose Frequency Reg
*/
void writeReg(unsigned long long Value,unsigned int regNum,byte FQ){
unsigned int buf=0;
if(regNum>2){
SerialUSB.println("Error:Error Frequency Registor.");
return;
}
if(FQ>2){
SerialUSB.println("Error:Error On Choose Freq Or Phase Registor.");
return;
}
buf= 1<<13;
buf= buf | (FQ<<11);
buf= buf | (regNum<<10);
for(unsigned int i=4;i>0;i--){
command(buf | ((Value>>((i-1)*8)) & 0xFF) | ((i-1)<<8));
}
}
void initAD9832(){
//复位AD9832
command(0xF800);
//用位选择频率相位寄存器
command(0xB000);
//写入频率
/*
command(0x3088);
command(0x2188);
command(0x3288);
command(0x2300);
*/
writeReg(toFValue(0),0,0);
//初始化相位寄存器
command(0x1800);
command(0x0900);
//选择频率寄存器0,相位寄存器0
command(0x6000);
//开始输出
command(0xc000);
}
void setup(){
pinMode(EN,OUTPUT);
digitalWrite(EN,HIGH);
spi.begin(SPI_4_5MHZ, MSBFIRST, 1);
initAD9832();
}
int k=0;
void loop(){//扫频
if(k<10000000)k=k+1000;
else k=1000;
writeReg(toFValue(k),0,0);
delay(5);
}
复制代码