#include "reg51.H"
#include "ad9834.h"
#include "LCD1602.h"
#include "keyScan.h"
#define MAX_VOLT 47 //为啥47代表4.7V
//sbit LED = P0^0;
//sbit key1 = P1^2;
//sbit key2 = P1^3;
//sbit key3 = P1^4;
//sbit key4 = P1^5;
//sbit key5 = P1^6;
//sbit FSK = P3^6;
/*
*********************************************************************************************************
* 函 数 名: DelayMs ‘i
* 功能说明: 时钟延时
* 形 参: ms 以毫秒为单位.
* 返 回 值: 无
*********************************************************************************************************
*/
void DelayMs( unsigned int ms)
{
unsigned int i,j;
for (i = 0; i < ms; i++)
for(j=0;j<1000;j++); //单片机晶振12MHZ,j是随便取吗
}
void main()
{
unsigned long FreqData=20000;
unsigned int volt=MAX_VOLT;
unsigned int ratio;
unsigned char AmpData=255; //不懂这两行,255是啥
unsigned char WaveModle=0;
unsigned char keyNum = 16;
unsigned char SetFreq_flag=0;//设置频率的标志
ratio=2550/MAX_VOLT;// 5.7= 255/47; ratio放大10倍了取整数, 44最高输出4.7V
InitLCD();
DelayMs( 100);
DispStrAt("AD9834", 0, 0);
DispStrAt("sinWave", 8, 0);
DispDecSpaceAt(FreqData, 0, 1, 7);DispStrAt("HZ", 7, 1); //0,1,7的0是啥
AD9834_WaveSeting(FreqData,0,SIN_WAVE,0 );//2KHz, 频率寄存器0,正弦波输出 ,初相位0
AD9834_AmpSet(AmpData); //设置幅值,幅值最大 255
DispStrAt("am V", 10, 1);
DispDecShiftAt(volt, 12, 1, 1);
while(1)
{
keyNum = keyScan();
if(keyNum!=16)
{
if(SetFreq_flag==0) //不是键盘输入设置频率状态, 步进设置状态
{
if(keyNum==10 && SetFreq_flag==0)
{
SetFreq_flag=1;
FreqData=0;
DispDecSpaceAt(FreqData, 0, 1, 7);DispStrAt(" ", 7, 1);
//AD9833_WaveSeting(FreqData,0,SIN_WAVE,0 );
}
if(keyNum==12)
{
FreqData=FreqData+1000;
AD9834_WaveSeting(FreqData,0,WaveModle,0 );
DispDecSpaceAt(FreqData, 0, 1, 7);DispStrAt("HZ", 7, 1);
//while(key1==0);
}
else if(keyNum==13)
{
if(FreqData>=1000)
FreqData=FreqData-1000;
AD9834_WaveSeting(FreqData,0,WaveModle,0 );
DispDecSpaceAt(FreqData, 0, 1, 7);DispStrAt("HZ", 7, 1);
//while(key2==0);
}
else if(keyNum==14)
{
if(volt<MAX_VOLT)
volt=volt+1;
AmpData=volt*ratio/10;
AD9834_AmpSet(AmpData);
//DispDecSpaceAt(AmpData, 13, 1, 3);
DispDecShiftAt(volt, 12, 1, 1);
//while(key3==0);
}
else if(keyNum==15)
{
if(volt>0)
volt=volt-1;
AmpData=volt*ratio/10;
AD9834_AmpSet(AmpData);
//DispDecSpaceAt(AmpData, 13, 1, 3);
DispDecShiftAt(volt, 12, 1, 1);
//while(key4==0);
}
else if(keyNum==11)
{
WaveModle=WaveModle+1;
if(WaveModle==3)WaveModle=0;
if(WaveModle==0){AD9834_WaveSeting(FreqData,0,SIN_WAVE,0 );DispStrAt("sinWave", 8, 0);}
else if(WaveModle==1){AD9834_WaveSeting(FreqData,0,TRI_WAVE,0 );DispStrAt("TriWave", 8, 0);}
else if(WaveModle==2){AD9834_WaveSeting(FreqData,0,SQU_WAVE,0 );DispStrAt("SquWave", 8, 0);}
//else if(WaveModle==3){AD9834_SetFSK(1000,5000);DispStrAt("FSKWave", 8, 0);}
//else if(WaveModle==4){AD9834_SetPSK(0,180,5000);DispStrAt("PSKWave", 8, 0);}
//while(key5==0);
}
}
else if(SetFreq_flag==1) //是矩阵键盘输入设置频率状态
{
if(keyNum<10)//输入频率
{
FreqData=FreqData*10+keyNum;
DispDecSpaceAt(FreqData, 0, 1, 7);DispStrAt(" ", 7, 1);
}
else if(keyNum==15)//输错倒退
{
FreqData=FreqData/10;
DispDecSpaceAt(FreqData, 0, 1,7);
}
else if(keyNum==10)//HZ确定输出
{
DispDecSpaceAt(FreqData, 0, 1, 7);DispStrAt("HZ", 7, 1);
AD9834_WaveSeting(FreqData,0,WaveModle,0 );
SetFreq_flag=0;
}
else if(keyNum==11)////KHZ确定输出
{
DispDecSpaceAt(FreqData, 0, 1, 6);DispStrAt("KHZ", 6, 1);
FreqData=FreqData*1000;
AD9834_WaveSeting(FreqData,0,WaveModle,0 );
SetFreq_flag=0;
}
else if(keyNum==12)////MHZ确定输出
{
DispDecSpaceAt(FreqData, 0, 1, 6);DispStrAt("MHZ", 6, 1);
FreqData=FreqData*1000000;
AD9834_WaveSeting(FreqData,0,WaveModle,0 );
SetFreq_flag=0;
}
}
}
}
}
以上是主程序,问题放在了程序中,还请大家帮忙看看
|