#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
#include<main.h>
#define unchar unsigned char
#define uint unsigned int
#define _Nop() _nop_()
#define DAT P0
uchar go;
sbit K1=P3^1;
sbit K2=P3^2;
sbit K3=P3^3;
sbie WR=P3^0;
//位定义
#define Lcd_Data P0
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit Busy=P0^7;
bit hold=0;
bit _Int=0;
bit k=0;
bit m=0;
bit fushu=0;
bit q=0;
uint DAdat;//存放送到DA的数据;
uint x;
unchar ADdat;//存放从ADC读出的数据
uchar vol;//存入输入电压
uchar keynum;
uchar kyreg;
uchar temp;//存放功能状态
uchar hh;
//定义数组
static code uchar Disp[]="0123456789-";
static code uchar Disp2[]="Error";
static code uchar Disp3[]="Vol is:";
//函数声明
uchar keyread(void);//读键函数
uchar keyread2(void);//读键函数2
uchar keyread3(void);//读键函数3
void reADC(void);//AD反馈读数函数
uchar cmp(uchar ADdat,uchar DAdat);//反馈比较函数
uchar lcdinit();//LCD初始化函数
void lcdcmd(uchar cmd);//LCD写控制字函数
void lcddata(uchar dat);//LCD写数据函数
void seDAC(uchar DAdata);//DAC送数函数
void delay(uchar t);
void extint(void);
void volchange();//输出电压自增自减函数
main()
{
uint vol;
uchar i,j,l=0,a=0,b=0,c=0;
bit dian=0;
bit o=0,p=0;//负数标志位,确认标志位
delay(255);
EA=1;
EX0=1;
PX0=1;
IT0=1;
pp;
a=b=0;
dian=0;
o=p=0;
x=0;
P1=0;
lcdinit();
lcdcmd(0x80);
for(j=0;j<7;j++)
{
lcddata(Disp3[j]);
}
seDAC(128);
DAdat=128;
lcddata('0');
lcddata('0');
lcddata('.');
lcddata('V');
while(1)
{
while(!k)
{
delay(200);
i=keyread();
if(!m)
{
lcdcmd(0x01);//清屏
for(j=0;j<7;j++)
lcddata(Disp3[j]);//Vol is:
m=1;
}
else if(i==11){m=0;goto pp;} //复位
else if(i==12){temp=1;k=1;q=!q;hh=1;;}//存放功能状态temp
else if(i==13){temp=2;k=1;q=!q;hh=1;;}
}
if(vol>150)
{
lcdcmd(0x01);//清屏
delay(2);
lcdcmd(0x80);
for(i=0;i<16;i++)
lcddata(Disp2[i]);//"Error"
p=1;
}
if(!fushu&&!hh)//送入DAC的数字量
{
DAdat=256-256*vol/150;
fushu=0;
hh=0;
}
if((temp==0)&&(!p))
seDAC(DAdat);//DAC送数函数
else if(temp==1)
{
volchange();
seDAC(DAdat);
keyread2();
temp=0;
}
else if(temp==2)
{
volchange();
seDAC(DAdat);
keyread3();
temp=0;
}
p=0;
k=0;
}
}
void volchange()
{
uchar i,a,b,c,y,z;
if(temp==1&&(DAdat<511))
{
if(q)
{
if(z==9)
{
DAdat+=3;z=0;//进位
}
else DAdat+=2;
}
else DAdat+=3;
z++;
if(DAdat>=129)
{
x=x+1;
lcdcmd(0x01);
for(i=0;i<7;i++)
lcddata(Disp3[i]);
c=x/100;
a=x%100/10;
b=x%10;
lcdcmd(0x01);
for(i=0;i<7;i++)
lcddata(Disp3[i]);//电压显示
lcddata(Disp[c]);
lcddata(Disp[a]);
lcddata('.');
lcddata(Disp[b]);
lcddata('V');
}
}
else if(temp==2&&(DAdat>1))
{
if(q)
{
if(y==9)
{
DAdat-=3;y=0;//借位
}
else DAdat-=2;
}
else DAdat-=3;
y++;
if(DAdat>127)
{
c=x/100;
a=x%100/10;
b=x%10;
lcdcmd(0x01);
for(i=0;i<7;i++)
lcddata(Disp3[i]);//电压显示
lcddata(Disp[c]);
lcddata(Disp[a]);
lcddata('.');
lcddata(Disp[b]);
lcddata('V');
}
}
hh=0;
}
void seDAC(uchar DAdat)//DAC送数
{
WR=1;
_Nop();_Nop();
DAT=DAdat;
WR=0;
_Nop();_Nop();
WR=1;
}
ucahr keyread()
{
uchar kysta=1;//按键标志,kysta=1无按键
while(kysta)//锁P3口低三位
{
P3=0x0e;//00001110
kyreg=P3;
if(kyreg!=0x0e)
{
delay(20);
kyreg=P3;
if(keyreg!=0x0e)
{
if(kyreg==0x0c) return 11;//按第一个键00001100
if(kyreg==0x0a) return 12;//按第二个键00001010
if(kyreg==0x06) return 13;//按第三个键00000110
}
}
while(kyreg!=0x0e)//键复位
{
P3=0x0e;
kyreg=P3;
}
}
}
uchar keyread2()
{
if(K2==0)//KEY2键实现数据增加
{
WR=1;
delay(50);
WR=0;
if(K2==0)//第二个键按下
delay(50);
P1=go;
go++;
while(!K2);
}
}
uchar keyread3()
{
if(K3==0)//KEY3键实现数据减少
{
WR=1;
delay(50);
WR=0;
if(K3==0)//第三个键按下
delay(50);
P1=go;
go--;//p1口数值减1
while(!K3);
}
}
void delay(uchar t)
{
uchar i=100;
while(t--)
{while(i--);}
}
void lcdcmd(uchar cmd)//写控制字函数
{
RS=0;
RW=0;
DAT=cmd;
E=1;
_Nop();_Nop();
E=0;
delay(2);
}
void lcddata(uchar dat)//写数据函数
{
RS=1;
RW=0;
DAT=dat;
_Nop();
E=1;
_Nop();_Nop();
E=0;
delay(2);
}
void lcdinit()//显示初始化
{
lcdcmd(0x38);
delay(2);
lcdcmd(0x38);
delay(2);
lcdcmd(0x38);
delay(2);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x0c);
}
void reADC()interrupt 2 using 1
{
uchar result;
bit state;
DAT=0xff;
_Nop();_Nop();
if(ADdat>=DAdat)//输出大于显示
{
result=ADdat-DAdat;
state=0;
}
else
{
result=DAdat-ADdat;
state=1;
}
if(result>=2)
{
result/=2;
if(state) result=DAdat-result;
else result=DAdat+result;
}
else result=DAdat;
_Int=1;
}
uchar cmp(uchar x,uchar y)
{
ucahr result;
bit state;
if(x>=y)
{
result=x-y;
state=0;
}
else
{
result=y-x;
state=1;
}
if(result>=2)
{
result/=2;
if(state) result=y-result;
else result=y+result;
}
else result=y;
return(result);
}
|