第一部分 main.c
#include <c8051f020.h>
#include <AD9833.h>
#include <delay.h>
void close_dog() //关看门狗
{
WDTCN=0xDE;
WDTCN=0xAD;
}
void system_clock()
{
int i;
OSCXCN=0x67; //启动外部晶体振荡器
for(i=0;i<256;i++) //等待振荡器启动(>1ms)
;
while(OSCXCN&0x80==0)
; //等待振荡器稳定
OSCICN = 0x88;//选择外部振荡器作为系统时钟源并使能丢失时钟检测器
}
void port_initialize () //端**叉开关设置
{
XBR2=0x00; //禁止交叉开关使能 端口0,1,2,3强制为输入方式
}
void main()
{
close_dog();
system_clock();
port_initialize ();
delay(3);
//1000Hz对应的频率寄存器值为13422,二进制0011 0100 0110 1110
AD9833_LoadWave(0x2100,0x746E,0x4000,0xC000,0x2000);
while(1);
}
......................................................................................
第二部分 AD9833.C
#include <c8051f020.h>
#include <delay.h>
sbit FSYNC1 =P2^5; //DDS使能引脚,低电平有效
sbit SCLK9833 =P2^6; //DDS串行时钟输入
sbit SDATA9833=P2^7; //DDS串行数据输入
//模拟SPI总线
void AD9833_write(unsigned int DDSdata)
{
unsigned char i ;
unsigned int DDSdata_temp;
DDSdata_temp=DDSdata;
SCLK9833=1;
delay(10);
FSYNC1 =1;
delay(10);
FSYNC1 =0;
delay(10);
for(i=0;i<16;i++)
{
if (DDSdata_temp&0x0001)
{
SDATA9833=1;
}
else
{
SDATA9833=0;
}
delay(10);
SCLK9833=0; //时钟信号下降沿数据送入I/O口
delay(10);
SCLK9833=1;
DDSdata_temp=DDSdata_temp>>1; //16位数据从低位到高位送入
}
FSYNC1 =1;
}
void AD9833_LoadWave(unsigned int Contr_Reg_date, unsigned int Fre_MSBdata, unsigned
int Fre_LSBdata, unsigned int Phs_data, unsigned int Reset_data)
{
unsigned int Fre_MSBdata_temp=Fre_MSBdata;
unsigned int Fre_LSBdata_temp=Fre_LSBdata;
unsigned int Contr_Reg_data_temp=Contr_Reg_date;
unsigned int Phs_data_temp=Phs_data;
unsigned int Reset_data_temp=Reset_data;
AD9833_write(Contr_Reg_data_temp);
AD9833_write(Fre_MSBdata_temp);
AD9833_write(Fre_LSBdata_temp);
AD9833_write(Phs_data_temp);
AD9833_write(Reset_data_temp);
}
......................................................................................
第三部分 delay.c
#include<c8051f020.h>
void delay(unsigned int i)
{
unsigned int j;
for(;i>0;i--)
for(j=125;j>0;j--)
;
}
|