//---------------------------------------------------////AD9833模块测试程序//功能:循环输出1KHz方波,正弦波。三角波//接口:////---------------------------------------------------////sum为按键输入临时变量//K3为外部中断口控制通道开关//R=33为选择通道A标志R34为选择通道B标志//y1A通道的开关标志1:关闭;0:打开y2也一样//temp1A通道的频率控制字变量temp3A通道的相位控制字变量//temp2B通道的频率控制字变量temp4B通道的相位控制字变量//c1c2为频率比变量b1为选择比例标志位变量//定义头文件#includereg51.h#includestdio.h#includeintrins.h#includea5110.h#includeKey.h//---------------------------------------------------//sbitad9833_sdata=P2^2;//ad9833数据线sbitad9833_sclk=P2^3;//ad9833时钟线sbitad9833_fsync1=P2^4;//通道A使能sbitad9833_fsync2=P2^1;//通道2使能//外部中断的IOsbitK3=P3^2;//外部中断0sbitK4=P3^3;//外部中断1#defineucharunsignedchar#defineuintunsignedintlongintm;uintw1=38,w2=38,w3=38,w4=38,floag1=0;ucharz,y=1,y1=1,y2=1;externunsignedlongintsum,temp1,temp2,temp3,temp4,temp5=0,temp6=0;externuchars,di,R,b1,q;voidoutput_f1(unsignedlongfreq_value);//输出子函数voidoutput_f2(unsignedlongphase_value);//相位输出函数voidoutput_p1(unsignedlongfreq_value);//输出子函数voidoutput_p2(unsignedlongphase_value);//相位输出函数voiddisplay(longintp,uchars,ucharb1,ucharfloag1);//显示处理函数voidIntConfiguration();//外部中断函数//---------------------------------------------------////延时程序//---------------------------------------------------//voiddelay(unsignedintm){unsignedintn;for(n=0;n=m;n++){}}voiddelayms(unsignedintii)//1ms延时函数{unsignedinti,x;for(x=0;xii;x++){for(i=0;i100;i++);}}//---------------------------------------------------////write_ad9833:向AD9833_ic1写16位数据软件SPI方式////---------------------------------------------------//voidwrite_ad9833ic1_d16(unsignedintx){inti,w;ad9833_sclk=1;//scl置为高电平ad9833_fsync2=1;//关闭ic2ad9833_fsync1=1;//fsync置为高电平,关闭使能ic1ad9833_fsync1=0;//fsync置为低电平,使能ic1//写16位数据w=x;for(i=0;i16;i++){ad9833_sdata=w&0x8000;ad9833_sclk=0;ad9833_sclk=1;w=w1;}ad9833_fsync1=1;ad9833_sclk=0;}//---------------------------------------------------////向16位AD9833_ic2写数据////---------------------------------------------------//voidwrite_ad9833ic2_d16(unsignedintx){inti,w;ad9833_sclk=1;//scl置为高电平ad9833_fsync1=1;//关闭ic1ad9833_fsync2=1;//fsync置为高电平,关闭使能ic2ad9833_fsync2=0;//fsync置为低电平,使能ic2w=x;for(i=0;i16;i++){ad9833_sdata=w&0x8000;ad9833_sclk=0;ad9833_sclk=1;w=w1;}ad9833_fsync2=1;ad9833_sclk=0;}//---------------------------------------------------////向16位AD9833_ic2和ic2同时写数据写数据////---------------------------------------------------//voidwrite_ad9833_d16(unsignedintx){inti,w;ad9833_sclk=1;ad9833_fsync1=1;ad9833_fsync2=1;ad9833_fsync1=0;ad9833_fsync2=0;//写16位数据w=x;for(i=0;i16;i++){ad9833_sdata=w&0x8000;ad9833_sclk=0;ad9833_sclk=1;w=w1;}ad9833_fsync1=1;ad9833_fsync2=1;ad9833_sclk=0;}//---------------------------------------------------////主程序////---------------------------------------------------//main(){P0=0XFF;P1=0Xff;P2=0XFF;P3=0Xff;chushi();IntConfiguration();//寄存器频率0输出1KHz波形write_ad9833_d16(0x0100);//复位AD9833write_ad9833_d16(0x2100);//选择数据一次写入write_ad9833_d16(0x6000);//L14write_ad9833_d16(0x4000);//H14频率寄存器0为1KHzwrite_ad9833_d16(0x9001);//L14write_ad9833_d16(0x8000);//H14频率寄存器1为2KHzwrite_ad9833_d16(0x2100);//芯片进入关闭工作状态,寄存器频率0输出1KHz波形////write_ad9833_d16(0xc000);//L14相位寄存器0//write_ad9833_d16(0xe000);//H14相位寄存器1//write_ad9833_d16(0x93E3);//L14//write_ad9833_d16(0x8001);//H14频率寄存器1为2KHz//寄存器频率0输出1KHz波形write_ad9833_d16(0x0100);//复位AD9833write_ad9833_d16(0x2100);//选择数据一次写入//output1(80000);write_ad9833_d16(0x2000);//芯片进入工作状态,寄存器频率0输出1KHz波形delay(60);write_ad9833_d16(0x0100);//复位AD9833write_ad9833_d16(0x2100);//选择数据一次写入write_ad9833ic1_d16(0x0c000);//L14相位寄存器0delay(100);write_ad9833ic2_d16(0x0c000);//L14相位寄存器0while(1){q=floag1;display(sum,s,b1,floag1);KeyDown();if(K4==0)z++;delayms(100);}}voidoutput_f1(unsignedlongfreq_value)//A频率输出函数{unsignedlongdds;//频率变量unsignedintdds_l,dds_h;//h为高14位,l为低14位if(freq_value=100)//高于100hz的情况{dds=(freq_value/100*1074)+freq_value%100*10.74;//268435456/FMCLK}else//低于100hz的情况dds=freq_value*10.74;//输出频率=频率控制字=按键输入值*(MCLK/2^28)系数10.7根据实际情况修改dds_l=dds;//低字节dds_h=dds14;//高字节dds_l=dds_l&0x3FFF;dds_l=dds_l|0x4000;dds_h=dds_h&0x3FFF;dds_h=dds_h|0x4000;write_ad9833ic1_d16(0x0100);//28位连续,选择频率0,相位0,RESET=0write_ad9833ic1_d16(0x2100);//28位连续,选择频率0,相位0,RESET=0write_ad9833ic1_d16(dds_l);write_ad9833ic1_d16(dds_h);}voidoutput_f2(unsignedlongfreq_value)//B频率输出函数{unsignedlongdds;unsignedintdds_l,dds_h;if(freq_value=100){dds=(freq_value/100*1074)+freq_value%100*10.74;//268435456/FMCLK}elsedds=freq_value*10.74;dds_l=dds;//低字节dds_h=dds14;//高字节dds_l=dds_l&0x3FFF;dds_l=dds_l|0x4000;dds_h=dds_h&0x3FFF;dds_h=dds_h|0x4000;write_ad9833ic2_d16(0x0100);//28位连续,选择频率0,相位0,RESET=0write_ad9833ic2_d16(0x2100);//28位连续,选择频率0,相位0,RESET=0write_ad9833ic2_d16(dds_l);write_ad9833ic2_d16(dds_h);}voiddisplay(longintp,uchars,ucharb1,ucharfloag2)//显示处理函数{uchari=0,j=0;floatc1=0,c2=0;if(y1==0){w1=46;w2=0;}//y1=0显示on(打开通道)if(y1==1)w1=w2=38;//y1=1显示off(关闭通道)if(y2==0){w3=46;w4=0;}//同上if(y2==1)w3=w4=38;//if(R==33&di==1)temp1=p;//r=33,di=1则写a通道的频率控制字if(R==34&di==1)temp2=p;//r=34,di=1则写b通道的频率控制字if(s==5){if(b1==1){c1=c2=1;}if(b1==2){c1=1;c2=2;