9851电路程序设计
AD9851工作前必須對其進(jìn)行初始化,AD9851上電后默認(rèn)為并行通信方式,如需采用串行通信方式,上電后應(yīng)將AD9851的D7~D0置為二進(jìn)制數(shù)XXXXX011,緊接著依次往W_CLK和FQ_UD端輸入寬度約1 S的高電平脈沖就可以使AD9851工作在串行模式下。
?
?
//AVR并行驅(qū)動,數(shù)據(jù)輸出口用PA口,控制線放在PC口
//端口定義
#define? clk????????? 0
#define? fq_ud???? 1
#define? rst????????? 2
unsigned char phase_word=0x00;?????????? //相位控制字,使用低5位
unsigned char power_down=0x00;?? //低功耗模式
unsigned char mult=0x01;??????????????? //6倍頻是否打開
unsigned char ww[5]={0x09,0x0E,0x38,0xE3,0x8E};
unsigned long freq,set_value;
//往AD9851寫數(shù)據(jù)
void write_dds(unsigned long dds)
{
?????? unsigned char i;
?????? ww[0]=0x01;????????????????????? //開啟6倍頻功能
?????? ww[0]= (phase_word<<3)|| power_down<<2 || mult;
?????? ww[1]=dds>>24;???????????????? //頻率字最高字節(jié)
?????? ww[2]=(dds>>16)&0xFF;
?????? ww[3]=(dds>>8)&0xFF;
?????? ww[4]=dds&0xFF; ??????????????????? //頻率字最低字節(jié)
?????? PORTC&=~(1<<fq_ud);??????? //FQ_UD置低
?????? PORTC&=~(1<<clk);??????????????????? //CLK置低
?????? for(i=0;i<5;i++)?????????????????? //送入5個(gè)字節(jié)的控制字
?????? {
????????????? PORTA=ww[i];
????????????? delay_us(2);
????????????? PORTC|=(1<<clk);
????????????? delay_us(2);
????????????? PORTC&=~(1<<clk);
????????????? delay_us(2);
?????? }
?????? PORTC|=(1<<fq_ud);?????????? //使能信號
?????? delay_us(3);
?????? PORTC&=~(1<<fq_ud);
}??
//寫入頻率
void write_freq(unsigned long freq)
{
?????? unsigned long dds;
?????? dds=23.860929422*freq;??????????? //頻率字計(jì)算,可進(jìn)行微調(diào)
?????? write_dds(dds);
}
?
void init_dds(void)
{
PORTC|=(1<<rst); ????????????? //高電平復(fù)位
?????? delay_us(10000);???????? //延時(shí)10mS
?????? PORTC&=~(1<<rst);
?????? delay_us(10000);???????? //延時(shí)10mS
}
?
?
?
//51并行驅(qū)動,數(shù)據(jù)輸出口用PA口,控制線放在PC口
//端口定義
sbit clk????????? =P2^0;
sbit fq_ud???? =P2^1;
sbit rst????????? =P2^2;
?
unsigned char phase_word=0x00;?????????? //相位控制字,使用低5位
unsigned char power_down=0x00;?? //低功耗模式
unsigned char mult=0x01;??????????????? //6倍頻是否打開
unsigned char ww[5]={0x09,0x0E,0x38,0xE3,0x8E};
unsigned long freq,set_value;
//往AD9851寫數(shù)據(jù)
void write_dds(unsigned long dds)
{
?????? unsigned char i;
???????????????????????????????????????????????? //控制字
?????? ww[0]= (phase_word<<3)|| power_down<<2 || mult;
?????? ww[1]=dds>>24;???????????????? //頻率字最高字節(jié)
?????? ww[2]=(dds>>16)&0xFF;
?????? ww[3]=(dds>>8)&0xFF;
?????? ww[4]=dds&0xFF; ??????????????????? //頻率字最低字節(jié)
?????? fq_ud=0;????????????? ???????????????????? //FQ_UD置低
?????? clk=0;?????????????????? ????????????? //CLK置低
?????? for(i=0;i<5;i++)???? ????????????? //送入5個(gè)字節(jié)的控制字
?????? {
????????????? P0=ww[i];
????????????? delay_us(2);
????????????? clk=1;
????????????? delay_us(2);
????????????? clk=0;
????????????? delay_us(2);
?????? }
?????? fq_ud=1;????????????? //使能信號
?????? delay_us(3);
?????? fq_ud=0;
}??
//寫入頻率
void write_freq(unsigned long freq)
{
?????? unsigned long dds;
?????? dds=23.860929422*freq;??????????? //頻率字計(jì)算,可進(jìn)行微調(diào)
?????? write_dds(dds);
}
?
void init_dds(void)
{
rst=1;????????????????????????? //高電平復(fù)位
?????? delay_us(10000);???????? //延時(shí)10mS
?????? rst=0;
?????? delay_us(10000);???????? //延時(shí)10mS
}
?
//AVR串行驅(qū)動,數(shù)據(jù)輸出口用PA口,控制線放在PC口
//端口定義
#define? clk????????? 0
#define? fq_ud???? 1
#define? rst????????? 2
#define? dat????????????? 3
unsigned char phase_word=0x00;?????????? //相位控制字,使用低5位
unsigned char power_down=0x00;?? //低功耗模式
unsigned char mult=0x01;??????????????? //6倍頻是否打開
unsigned char ww[5]={0x09,0x0E,0x38,0xE3,0x8E};
unsigned long freq,set_value;
?
//AD9851初始化,在此之前請確認(rèn)D0,D1和D2的電平狀態(tài)
?
void init_dds(void)
{??
PORTC|=(1<<rst); ????????????? //高電平復(fù)位
?????? delay_us(10000);???????? //延時(shí)10mS
?????? PORTC&=~(1<<rst);
?????? delay_us(10000);???????? //延時(shí)10mS
?????? PORTC&=~(1<<clk);
?????? PORTC&=~(1<<fq_ud);
?????? delay_us(2);
?????? PORTC|=(1<<clk);????? //送入W0
?????? delay_us(2);
?????? PORTC&=~(1<<clk);
?????? delay_us(2);
?????? PORTC|=(1<<fq_ud);??? //使能
?????? delay_us(2);
?????? PORTC&=~(1<<fq_ud);
?????? delay_us(2);
}
?
//往AD9851寫數(shù)據(jù)
void write_dds(unsigned long dds)
{
?????? unsigned char i,j,temp;
????????????????????????????????????????????????
?????? ww[0]=dds&0xFF; ??????????????????? //頻率字最低字節(jié)
?????? ww[1]=(dds>>8)&0xFF;
?????? ww[2]=(dds>>16)&0xFF;
?????? ww[3]=dds>>24;???????????????? //頻率字最高字節(jié)
ww[4]= (phase_word<<3)|| power_down<<2 || mult; //控制字
??????
?????? PORTC&=~(1<<fq_ud);???????????????????????????? //FQ_UD置低
?????? PORTC&=~(1<<clk);????????????????????????????????? //CLK置低
?????? for(i=0;i<5;i++)?????????????????? //送入5個(gè)字節(jié)的控制字
?????? {
????????????? temp=ww[i];
for(j=0;j<8;j++)
{
?????? PORTC&=~(1<<clk);
?????? if(temp&0x01)PORTC|=(1<<dat);
?????? else PORTC&=~(1<<dat);
?????? delay_us(2);
?????? PORTC|=(1<<clk);
?????? delay_us(2);
}
?????? }
PORTC&=~(1<<clk);
?????? PORTC|=(1<<fq_ud);?????????? //使能信號
?????? delay_us(3);
?????? PORTC&=~(1<<fq_ud);
}
?
//寫入頻率
void write_freq(unsigned long freq)
{
?????? unsigned long dds;
?????? dds=23.860929422*freq;??????????? //頻率字計(jì)算,可進(jìn)行微調(diào)
?????? write_dds(dds);
}
?
?
?
?
//51串行驅(qū)動
sbit clk????????? =P2^0;
sbit fq_ud???? =P2^1;
sbit rst????????? =P2^2;
sbit dat???????? =P2^3;
?
unsigned char phase_word=0x00;?????????? //相位控制字,使用低5位
unsigned char power_down=0x00;?? //低功耗模式
unsigned char mult=0x01;??????????????? //6倍頻是否打開
unsigned char ww[5]={0x09,0x0E,0x38,0xE3,0x8E};
unsigned long freq,set_value;
?
//AD9851初始化,在此之前請確認(rèn)D0,D1和D2的電平狀態(tài)
void init_dds(void)
{??
rst=1; ????????? //高電平復(fù)位
?????? delay_us(10000);???????? //延時(shí)10mS
?????? rst=0;
?????? delay_us(10000);???????? //延時(shí)10mS
?????? clk=0;
?????? fq_ud=0;
?????? delay_us(2);
?????? clk=1;????? ??????????????? //送入W0
?????? delay_us(2);
?????? clk=0;
?????? delay_us(2);
?????? fq_ud=1;??????????????????????????? //使能
?????? delay_us(2);
?????? fq_ud=0;
?????? delay_us(2);
}
?
//往AD9851寫數(shù)據(jù)
void write_dds(unsigned long dds)
{
?????? unsigned char i,j,temp;
????????????????????????????????????????????????
?????? ww[0]=dds&0xFF; ??????????????????? //頻率字最低字節(jié)
?????? ww[1]=(dds>>8)&0xFF;
?????? ww[2]=(dds>>16)&0xFF;
?????? ww[3]=dds>>24;???????????????? //頻率字最高字節(jié)
ww[4]= (phase_word<<3)|| power_down<<2 || mult; //控制字
??????
?????? fq_ud=0;?????????????????????????????????? //FQ_UD置低
?????? clk=0;???????????????????????????????? //CLK置低
?????? for(i=0;i<5;i++)?????????????????? //送入5個(gè)字節(jié)的控制字
?????? {
????????????? temp=ww[i];
for(j=0;j<8;j++)
{
?????? clk=0;
?????? if(temp&0x01)dat=1;
?????? else dat=0;
?????? delay_us(2);
?????? clk=1;
?????? delay_us(2);
}
?????? }
clk=0;
?????? fq_ud=1;????????????? //使能信號
?????? delay_us(3);
?????? fq_ud=0;
}
//寫入頻率
void write_freq(unsigned long freq)
{
?????? unsigned long dds;
?????? dds=23.860929422*freq;??????????? //頻率字計(jì)算,可進(jìn)行微調(diào)
?????? write_dds(dds);
}
?
總結(jié)
以上是生活随笔為你收集整理的9851电路程序设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AD基础知识小课堂
- 下一篇: 图文并茂详细教程之——用pixhawk飞