#include<reg51.h>/*51单片机头文件*/
#include<absacc.h>
#include<stdio.h>
#define COM8253 XBYTE[0x0003]
#define COM0 XBYTE[0x0000]
#define COM1 XBYTE[0x0001]
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="Clock Speed:";
uchar code table1[]="range:";
uchar code table2[]=" gap:";
sbit icden=P3^0;
sbit rs=P3^1;/*1602使能icden和写rs*/
sbit set=P2^0;
sbit wei=P2^1;
sbit wei1=P2^7;
sbit wei2=P2^5; /*set进入扫频频率设置窗口,wei扫频初始频率设置,wei1终值频率窗口*/
sbit add=P2^2;
sbit red=P2^3; /*add数值增加,red数值减少,*/
sbit gap=P2^6;/*步长设置*/
sbit exit=P2^4;/*exit退出设定程序*/
sbit a1=P3^2;
sbit b1=P3^3;
sbit c1=P3^4;/*74151数据选择器,ABC端口*/
uchar num=5,num1=5,num2=5,ad=0,ad1=0,gap1=0;/*num,num1用于设置光标位置,ad,ad1,gap1允许设置初始或终值频率的标示符*/
uchar set1=0,set2=0,set3=0,set4=0,set5=0,set6=0;/*初始频率各位的变量*/
uchar set11=0,set22=0,set33=0,set44=0,set55=0,set66=0;/*终值频率各位的变量*/
uchar sett1=0,sett2=0,sett3=0,sett4=0,sett5=0,sett6=0;/*步长各位的变量*/
unsigned long low,high,n1,n2,n3;/*low、high分别存储初始和终值频率,n2存储实时频率,n3存储步长,n1存放8253初值*/
int n=0;/*n每中断一次加一,最大值50,即发生50次中断n归0,用于规定扫频间隔时间。*/
unsigned long sw,wan,qian,bai,shi,ge;/*存储从n2分离的各位,用于实时显示频率*/
uchar flag1,flag2,star=1;/*flag1、flag2用于确定使用定时器0还是定时器1。star允许频率产生的标示符*/
unsigned long c,d;
uint a,b,e,f,h;
uchar cs=0,i,z;
void delay(uint z)/*延迟函数*/
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)/*1602写入指令*/
{
rs=0;
P1=com;
delay(5);
icden=1;
delay(5);
icden=0;
}
void write_data(uchar date)/*1602写入数据*/
{
rs=1;
P1=date;
delay(5);
icden=1;
delay(5);
icden=0;
}
void init()/*1602初始化*/
{
icden=0;
write_com(0x38);
write_com(0x0e);
write_com(0x01);
}
void display()/*扫频显示界面*/
{
write_com(0x01);
write_com(0x80);
for(num=0;num<12;num++)
{
write_data(table[num]);
delay(5);
}
}
void display1(unsigned long n2)/*扫频频率显示*/
{
if(star==1)
{
sw=n2/100000;
wan=n2/10000%10;
qian=n2/1000%10;
bai=n2/100%10;
shi=n2/10%10;
ge=n2%10;
write_com(0x80+0x40);
write_data(0x30+sw);
write_data(0x30+wan);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
}
void setlow(uint set1,set2,set3,set4,set5,set6,set11,set22,set33,set44,set55,set66)/*将键盘输入的数据存入low、high中,确定flag1、flag2值*/
{
low=set1*100000+set2*10000+set3*1000+set4*100+set5*10+set6;
high=set11*100000+set22*10000+set33*1000+set44*100+set55*10+set66;
if(low<5000)
{flag1=1;}
else flag1=2;
if(high<5000)
{flag2=1;}
else flag2=2;
}
void keyscan()/*键盘扫描*/
{
if(set==0)/*进入设置界面*/
{
delay(10);
if(set==0)
{
write_com(0x01);
write_com(0x80);
for(num=0;num<6;num++)
{
write_data(table1[num]);
}
write_com(0x80+6);
write_data(0x30+set1);
write_data(0x30+set2);
write_data(0x30+set3);
write_data(0x30+set4);
write_data(0x30+set5);
write_data(0x30+set6);
write_com(0x80+12);
write_data('H');
write_data('Z');
write_com(0x80+0x40+5);
write_data(':');
write_com(0x80+0x40+6);
write_data(0x30+set11);
write_data(0x30+set22);
write_data(0x30+set33);
write_data(0x30+set44);
write_data(0x30+set55);
write_data(0x30+set66);
write_com(0x80+0x40+12);
write_data('H');
write_data('Z');
num=6;
write_com(0x80+6);
ad=1;/*ad=1,ad=0,标志只可进行初始频率设置*/
ad1=0;
gap1=0;
star=0;
while(!set);
}
}
if(gap==0)/*对扫频步长进行设定,初始为20HZ*/
{
delay(10);
if(gap==0)
{
write_com(0x01);
write_com(0x80);
for(num2=0;num2<6;num2++)
{write_data(table2[num2]);}
write_com(0x80+6);
write_data(0x30+sett1);
write_data(0x30+sett2);
write_data(0x30+sett3);
write_data(0x30+sett4);
write_data(0x30+sett5);
write_data(0x30+sett6);
write_com(0x80+12);
write_data('H');
write_data('Z');
num2=6;
write_com(0x80+6);
gap1=1;/*选位的同时,gap1置1,只可对步长进行设置*/
ad=0;
ad1=0;
star=0;
while(!gap);
}
}
if(wei==0)/*初始频率进行位选,选择要增加或减少的位置*/
{
delay(10);
if(wei==0)
{
num++;
if(num>11)
{num=6;}
write_com(0x80+num);
ad=1;/*选位的同时,ad置1,只可对初始频率进行设置*/
ad1=0;
gap1=0;
star=0;
while(!wei);
}
}
if(wei1==0)/*终值频率进行位选,选择要增加或减少的数据位*/
{
delay(10);
if(wei1==0)
{
num1++;
if(num1>11)
{num1=6;}
write_com(0x80+0x40+num1);
ad1=1;/*选位的同时,ad1置1,只可对终值频率进行设置*/
ad=0;
gap1=0;
star=0;
while(!wei1);
}
}
if(wei2==0)/*步长进行位选,选择要增加或减少的数据位*/
{
delay(10);
if(wei2==0)
{
num2++;
if(num2>11)
{num2=6;}
write_com(0x80+num2);
gap1=1;/*选位的同时,gap1置1,只可步长进行设置*/
ad1=0;
ad=0;
star=0;
while(!wei2);
}
}
if(add==0)/*增加数值*/
{
delay(10);
if(add==0)
{
if(ad==1)/*扫频初值增加*/
{
switch(num)/*由num值来确定设置哪个数据位*/
{
case 6:
set1++;
if(set1>9)
{set1=0;}
write_com(0x80+num);
write_data(0x30+set1);
write_com(0x80+num);
break;
case 7:
set2++;
if(set2>9)
{set2=0;}
write_com(0x80+num);
write_data(0x30+set2);
write_com(0x80+num);
break;
case 8:
set3++;
if(set3>9)
{set3=0;}
write_com(0x80+num);
write_data(0x30+set3);
write_com(0x80+num);
break;
case 9:
set4++;
if(set4>9)
{set4=0;}
write_com(0x80+num);
write_data(0x30+set4);
write_com(0x80+num);
break;
case 10:
set5++;
if(set5>9)
{set5=0;}
write_com(0x80+num);
write_data(0x30+set5);
write_com(0x80+num);
break;
case 11:
set6++;
if(set6>9)
{set6=0;}
write_com(0x80+num);
write_data(0x30+set6);
write_com(0x80+num);
break;
}
}
if(gap1==1)/*步长增加*/
{
switch(num2)/*由num2来确定增加哪个数据位*/
{
case 6:
if(sett1==9)
{sett1=0;}
else sett1++;
write_com(0x80+num2);
write_data(0x30+sett1);
write_com(0x80+num2);
break;
case 7:
if(sett2==9)
{sett2=0;}
else sett2++;
write_com(0x80+num2);
write_data(0x30+sett2);
write_com(0x80+num2);
break;
case 8:
if(sett3==9)
{sett3=0;}
else sett3++;
write_com(0x80+num2);
write_data(0x30+sett3);
write_com(0x80+num2);
break;
case 9:
if(sett4==9)
{sett4=0;}
else sett4++;
write_com(0x80+num2);
write_data(0x30+sett4);
write_com(0x80+num2);
break;
case 10:
if(sett5==9)
{sett5=0;}
else sett5++;
write_com(0x80+num2);
write_data(0x30+sett5);
write_com(0x80+num2);
break;
case 11:
if(sett6==9)
{sett6=0;}
else sett6++;
write_com(0x80+num2);
write_data(0x30+sett6);
write_com(0x80+num2);
break;
}
}
if(ad1==1)/*终值频率增加*/
{
switch(num1)/*由num1确定增加的位置*/
{
case 6:
if(set11>=9)
{set11=0;}
else set11++;
write_com(0x80+0x40+num1);
write_data(0x30+set11);
write_com(0x80+0x40+num1);
break;
case 7:
if(set22>=9)
{set22=0;}
else set22++;
write_com(0x80+0x40+num1);
write_data(0x30+set22);
write_com(0x80+0x40+num1);
break;
case 8:
if(set33>=9)
{set33=0;}
else set33++;
write_com(0x80+0x40+num1);
write_data(0x30+set33);
write_com(0x80+0x40+num1);
break;
case 9:
if(set44>=9)
{set44=0;}
else set44++;
write_com(0x80+0x40+num1);
write_data(0x30+set44);
write_com(0x80+0x40+num1);
break;
case 10:
if(set55>=9)
{set55=0;}
else set55++;
write_com(0x80+0x40+num1);
write_data(0x30+set55);
write_com(0x80+0x40+num1);
break;
case 11:
if(set66>=9)
{set66=0;}
else set66++;
write_com(0x80+0x40+num1);
write_data(0x30+set66);
write_com(0x80+0x40+num1);
break;
}
}
star=0;
while(!add);
}
}
if(red==0)/*对数据进行减操作*/
{
delay(10);
if(red==0)
{
if(ad==1)/*初始频率减少*/
{
switch(num)
{
case 6:
if(set1==0)
{set1=9;}
else set1--;
write_com(0x80+num);
write_data(0x30+set1);
write_com(0x80+num);
break;
case 7:
if(set2==0)
{set2=9;}
else set2--;
write_com(0x80+num);
write_data(0x30+set2);
write_com(0x80+num);
break;
case 8:
if(set3==0)
{set3=9;}
else set3--;
write_com(0x80+num);
write_data(0x30+set3);
write_com(0x80+num);
break;
case 9:
if(set4==0) |