AD9854原理图如下(pdf文档清晰版请下载附件):
AD9854单片机源程序如下:
- //=====================================================================
- // AD9854 驱动程序设计
- //AD9854.c
- //说明:10MHZ 有源晶振
- //=====================================================================
- #include <reg52.h>
- #include <lcd1602.h>
- #include <intrins.h>
- sfr P4 = 0xC0; //1111,1111 端口4
- sbit KEY1 = P3^5;
- sbit KEY2 = P3^4;
- sbit KEY3 = P3^3;
- sbit KEY4 = P3^2;
- unsigned char FreqWord[6]; //6个字节频率控制字
- /*
- CLK_Set为时钟倍频设置,可设置4~20倍倍频,但最大不能超过300MHZ
- Freq_mult_unsigned long和Freq_mult_doulle均为2的48次方除以系统时钟,一个为长整形,一个为双精度型
- */
- #define CLK_Set 20
- const unsigned long Freq_mult_ulong = 1407375;
- const double Freq_mult_doulle = 1407374.88355328;
- //**************************修改硬件时要修改的部分********************************
- #define AD9854_DataBus P2
- #define AD9854_AdrBus P0
- sbit AD9854_RST = P3^6; //AD9854复位端口
- sbit AD9854_UDCLK = P3^7; //AD9854更新时钟
- sbit AD9854_WR = P4^1; //AD9854写使能,低有效
- sbit AD9854_RD = P4^2; //AD9854读使能,低有效
- sbit AD9854_FDATA = P4^3; //AD9854 FSK,PSK控制
- sbit AD9854_OSK = P4^4; //AD9854 OSK控制端
- unsigned char flag = 1;
- unsigned int fc = 1000;
- unsigned long LFerq = 750;
- unsigned long HFerq = 1250;
- void AD9854_WR_Byte(unsigned char addr,unsigned char dat);
- void AD9854_Init(void);
- void Freq_convert(long Freq);
- void AD9854_InitFSK(void);
- void AD9854_SetFSK(unsigned long Freq1,unsigned long Freq2);
- //void delay (unsigned int us);
- void UpDisplay()
- {
- unsigned char disbuf[5];
- LCD_Write_String(0, 0, "BaseFerq M");
- LCD_Write_String(0, 1, "Now Ferq M");
- disbuf[0] = fc / 1000 + '0';
- disbuf[1] = '.';
- disbuf[2] = fc % 1000 / 100 + '0';
- disbuf[3] = fc % 100 / 10 + '0';
- disbuf[4] = fc % 10 + '0';
- disbuf[5] = 0;
- LCD_Write_String(9, 0, disbuf);
- disbuf[0] = HFerq / 1000 + '0';
- disbuf[1] = '.';
- disbuf[2] = HFerq % 1000 / 100 + '0';
- disbuf[3] = HFerq % 100 / 10 + '0';
- disbuf[4] = HFerq % 10 + '0';
- disbuf[5] = 0;
- LCD_Write_String(9, 1, disbuf);
- }
- void DownDisplay()
- {
- unsigned char disbuf[5];
- LCD_Write_String(0, 0, "BaseFerq M");
- LCD_Write_String(0, 1, "Now Ferq M");
- disbuf[0] = fc / 1000 + '0';
- disbuf[1] = '.';
- disbuf[2] = fc % 1000 / 100 + '0';
- disbuf[3] = fc % 100 / 10 + '0';
- disbuf[4] = fc % 10 + '0';
- disbuf[5] = 0;
- LCD_Write_String(9, 0, disbuf);
- disbuf[0] = LFerq / 1000 + '0';
- disbuf[1] = '.';
- disbuf[2] = LFerq % 1000 / 100 + '0';
- disbuf[3] = LFerq % 100 / 10 + '0';
- disbuf[4] = LFerq % 10 + '0';
- disbuf[5] = 0;
- LCD_Write_String(9, 1, disbuf);
- }
- void AD9854_SetSine(unsigned long Freq,unsigned int Shape)
- {
- unsigned char count;
- unsigned char Adress;
- Adress = 0x04; //选择频率控制字地址的初值
- Freq_convert(Freq); //频率转换
- for(count=6;count>0;) //写入6字节的频率控制字
- {
- AD9854_WR_Byte(Adress++,FreqWord[--count]);
- }
-
- AD9854_WR_Byte(0x21,Shape>>8); //设置I通道幅度
- AD9854_WR_Byte(0x22,(unsigned char)(Shape&0xff));
-
- AD9854_WR_Byte(0x23,Shape>>8); //设置Q通道幅度
- AD9854_WR_Byte(0x24,(unsigned char)(Shape&0xff));
- AD9854_UDCLK=1; //更新AD9854输出
- AD9854_UDCLK=0;
- }
- void main()
- {
- LCD_Init();
- LCD_Clear();
- /*LCD_Write_String(0,0,"BaseFerq 0.875 M");
- LCD_Write_String(0,1,"Now Ferq 1.125 M");*/
- UpDisplay();
- AD9854_Init();
- AD9854_SetSine(HFerq * 1000,4000);
- // AD9854_SetSine(1000000,4000);
- // while(1);
- // AD9854_InitFSK();
- //
- // AD9854_SetFSK(HFerq * 1000, LFerq * 1000);
-
- // AD9854_SetFSK(1000000,1000000);
- // AD9854_FDATA = 1;
- // while(1);
- while(1)
- {
- if(!KEY1)
- {
- if (flag == 1)
- {
- AD9854_SetSine(0,0);
- //AD9854_InitFSK();
- flag = 0;
- }
- else
- {
- // AD9854_InitFSK();
- // AD9854_SetFSK(HFerq * 1000, LFerq * 1000);
- AD9854_SetSine(HFerq * 1000,4000);
- flag = 1;
- }
-
- while (!KEY1);
- }
- if (flag == 1)
- {
- if (!KEY2)
- {
- AD9854_SetSine(0,0);
- fc += 100;
- if (fc == 7900) fc = 7800;
- LFerq = fc * 3 / 4;
- HFerq = fc * 5 / 4;
- UpDisplay();
- AD9854_SetSine(HFerq * 1000,4000);
- // AD9854_InitFSK();
- // AD9854_SetFSK(HFerq * 1000, LFerq * 1000);
-
- while (!KEY2);
- }
- if (!KEY3)
- {
- AD9854_SetSine(0,0);
- fc -= 100;
- if (fc == 300) fc = 400;
- LFerq = fc * 3 / 4;
- HFerq = fc * 5 / 4;
- UpDisplay();
- AD9854_SetSine(HFerq * 1000,4000);
- // AD9854_InitFSK();
- // AD9854_SetFSK(HFerq * 1000, LFerq * 1000);
- while (!KEY3);
- }
- if (!KEY4)
- {
- //AD9854_FDATA = 1;
- AD9854_SetSine(LFerq * 1000,4000);
- DownDisplay();
- while (!KEY4);
- AD9854_SetSine(HFerq * 1000,4000);
- //AD9854_FDATA = 0;
- UpDisplay();
- }
- }
- }
- }
- //====================================================================================
- //函数名称:void AD9854_WR_Byte(unsigned char addr,unsigned char dat)
- //函数功能:AD9854并行口写入数据
- //入口参数:addr 6位地址
- // dat 写入的数据
- //出口参数:无
- //====================================================================================
- void AD9854_WR_Byte(unsigned char addr,unsigned char dat)
- {
- AD9854_AdrBus = (addr&0x3f) | (AD9854_AdrBus&0xc0);
- AD9854_DataBus = dat;
- AD9854_WR = 0;
- AD9854_WR = 1;
- }
- //====================================================================================
- //函数名称:void AD9854_Init(void)
- //函数功能:AD9854初始化
- //入口参数:无
- //出口参数:无
- //====================================================================================
- void AD9854_Init(void)
- {
- AD9854_WR=1;//将读、写控制端口设为无效
- AD9854_RD=1;
- AD9854_UDCLK=0;
- AD9854_RST=1; //复位AD9854
- AD9854_RST=0;
- AD9854_WR_Byte(0x1d,0x10); //关闭比较器
- AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
- AD9854_WR_Byte(0x1f,0x00); //设置系统为模式0,由外部更新
- AD9854_WR_Byte(0x20,0x60); //设置为可调节幅度,取消插值补偿
- AD9854_UDCLK=1; //更新AD9854输出
- AD9854_UDCLK=0;
- }
- //====================================================================================
- //函数名称:void Freq_convert(long Freq)
- //函数功能:正弦信号频率数据转换
- //入口参数:Freq 需要转换的频率,取值从0~SYSCLK/2
- //出口参数:无 但是影响全局变量FreqWord[6]的值
- //说明: 该算法位多字节相乘算法,有公式FTW = (Desired Output Frequency × 2N)/SYSCLK
- // 得到该算法,其中N=48,Desired Output Frequency 为所需要的频率,即Freq,SYSCLK
- // 为可编程的系统时钟,FTW为48Bit的频率控制字,即FreqWord[6]
- //====================================================================================
- void Freq_convert(long Freq)
- {
- unsigned long FreqBuf;
- unsigned long Temp=Freq_mult_ulong;
- unsigned char Array_Freq[4]; //将输入频率因子分为四个字节
- Array_Freq[0]=(unsigned char)Freq;
- Array_Freq[1]=(unsigned char)(Freq>>8);
- Array_Freq[2]=(unsigned char)(Freq>>16);
- Array_Freq[3]=(unsigned char)(Freq>>24);
- FreqBuf=Temp*Array_Freq[0];
- FreqWord[0]=FreqBuf;
- FreqBuf>>=8;
- FreqBuf+=(Temp*Array_Freq[1]);
- FreqWord[1]=FreqBuf;
- FreqBuf>>=8;
- FreqBuf+=(Temp*Array_Freq[2]);
- FreqWord[2]=FreqBuf;
- FreqBuf>>=8;
- FreqBuf+=(Temp*Array_Freq[3]);
- FreqWord[3]=FreqBuf;
- FreqBuf>>=8;
- FreqWord[4]=FreqBuf;
- FreqWord[5]=FreqBuf>>8;
- }
- //====================================================================================
- //函数名称:void AD9854_InitFSK(void)
- //函数功能:AD9854的FSK初始化
- //入口参数:无
- //出口参数:无
- //====================================================================================
- void AD9854_InitFSK(void)
- {
- AD9854_WR=1; //将读、写控制端口设为无效
- AD9854_RD=1;
- AD9854_UDCLK=0;
- AD9854_RST=1; //复位AD9854
- AD9854_RST=0;
- AD9854_WR_Byte(0x1d,0x10); //关闭比较器
- AD9854_WR_Byte(0x1e,CLK_Set); //设置系统时钟倍频
- AD9854_WR_Byte(0x1f,0x02); //设置系统为模式1,由外部更新
- AD9854_WR_Byte(0x20,0x60); //设置为可调节幅度,取消插值补偿
- AD9854_UDCLK=1; //更新AD9854输出
- AD9854_UDCLK=0;
- }
- //====================================================================================
- //函数名称:void AD9854_SetFSK(unsigned long Freq1,unsigned long Freq2)
- //函数功能:AD9854的FSK设置
- //入口参数:Freq1 FSK频率1
- // Freq2 FSK频率2
- //出口参数:无
- //====================================================================================
- void AD9854_SetFSK(unsigned long Freq1,unsigned long Freq2)
- {
- unsigned char count=6;
- unsigned char Adress1,Adress2;
- const unsigned int Shape=4000; //幅度设置. 为12 Bit,取值范围为(0~4095)
-
- Adress1=0x04; //选择频率控制字1地址的初值
- Adress2=0x0a; //选择频率控制字2地址的初值
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
63535281AD9854-msk-for-stm32.zip
(125.05 KB, 下载次数: 125)
|