|
#include <reg52. h>
#define uchar unsigned char
#define uint unsigned int
define duan P0 //數(shù)碼管顯顯示段選定義
define wei P2 //數(shù)碼管顯示位選定義
define key P3 //鍵盤接口定義
Sbit OFF=P1^0;//關(guān)機(jī)鍵定義
Sbit sw=P^5;
sbit sp=P^7;
unsigned long int shu1,shu2;//進(jìn)行運(yùn)算的兩個(gè)變量數(shù)
unsigned char count;
uchar num;//鍵盤掃描返回回值
char
flag l, flag shu, flag fuhao, fuhao, newkey,update;
∥flag1開(kāi)機(jī)標(biāo)志 newkey新按鍵標(biāo)志, fuhao運(yùn)算符,update表示等于號(hào)
∥之后緊接著輸入的是數(shù)的話則清零shul
char key_shu;//按鍵值
unsigned char code Wela[]={0xf7, 0xfb,0xfd,0xfe}
//六位數(shù)碼管的位選
unsigned char code Duan[]={0x3f, 0x06,0x5b
//0 1 2
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00}
//3 4 5 6 7 8 9無(wú)顯示共陰陰極數(shù)碼管
unsigned char count;
uchar Keyscan(); //鍵盤掃描函數(shù)
void display(unsigned long int); //數(shù)碼管顯示函數(shù)
void delay(uint i);//延時(shí)函數(shù)
{
while(i--);
}
void main()
{
flag1=0; //標(biāo)志關(guān)機(jī)
{
while(1)
if(keyscan()==15; //開(kāi)機(jī)檢測(cè)
flag1=1;//標(biāo)志開(kāi)機(jī)
shu1=shu2=fuhao=flag_shu=newkey=0;//初始化變量
while(flag1)
{
if(!flag fuhao)display(shu1);
else display(shu2);
key_shu=keyscan();
if(newkey==1); //有新鍵值
{if(key_shu==15) //按下ONC鍵,清零
{flag_fuhao=0, update=0
shul=shu2=fuhao_flag=shu=newkey=0;
}
else if(key_shu=14&&flag_shu==1&&fuhao) //按下
{switch(fuhao)
{case 10: shu1=shu2+shu1; break;
case 11: shu1==shu2-shu1; break;}
fuhao=0; flag_fuhao=0; update=1;
}
else if(key_shu>=0)&&(key shu<=9)
//按下數(shù)字鍵
{flag_fuhao=0
if(update) shu1=0;
if(shul<100000)
shu1=key_shu+shu1*10; flag_shu=1;}
update=0;
}
else if((key_shu>=10)&&(key_shu<=11)) //按下運(yùn)算符
{flag_fuhao=1;update=0;
if(flag_shu==1)
{if(fuhao=0;
{shu2=shu1; shu1=0;
fuhao=key_shu;flag_shu=2; }
else
{switch(fuhao)
{case 10: shu2=shu2+shu1; break;
case 11: shu2=shu2-shu1;break;
}
shu1=0; fuhao=key_shu;
}
else if(flag_shu==2)
fuhao=key_shu
}
newkey=0
}
}
}
}
鍵盤掃描
uchar keyscan()
{
key=0xfe;
if(key!=0xfe)
{
delay(500);
if(key! =0xfe)
{
switch(key)
{
case 0xee;num=7;break;
case 0xde;num=8;break;
case 0xbe;num=9;break;
case 0x7e;num=13;break;
}
newkey=1
delay(500);
while(key!=0xfe)
{
if(flag1)
if(!shu1&&num>=0&&num<=9)
display(num);
else
display(shu1);
}
return num;
}
}
key=0xfd;
if(key!=0xfd)
{
delay(500);
if(key!=0xfd)
{
switch(key)
{
case 0xed;num=4;break;
case 0xdd;num=5;break;
case 0xbd;num=6;break;
case 0x7d;num=12;break;//
newkey=1;
delay(500);
while(key!=0xfd)
{
if(flag1)
if(! shu1&&num>=0&&num<=9)
display(num);
else
display(shu1);
}
return num;
}
}
key=0xf7;
if(key! =0xfb)
{
delay(500);
if(key!=0xfb)
{
switch(key)
{
case 0xeb:num=1;break;
case 0xdb:num=2;break;
case 0xbb:num=3;break;
case 0x7b:num=11;break;
}
newkey=1;
delay(500);
while(key!=0xfb)
{if(flag1)
if(shu1&&num>=0&&num<=9)
display(num);
else
display(shu1);
}
return num;
}
}
key=0xf7;
if(key! =0xf7)
{delay(500);
if(key! =0xf7)
{
switch(key)
{
case 0xe7:num=15;break;//ON/C
case 0xd7:num=0;break;
case 0xb7:num=14;break;/
case 0x77:num=10;break;//
}
newkey=1;
delay(500);
while(key!=0xf7)
if(flag1)
if(!shul&&num>=&&num<=9)
display(num);
else
display(shu1);
}
return num;
}
}
//4
if(!OFF)
{
delay(500);
if(!OFF)
{flag1=0;}
}
return 100;
}
|
|