#include <reg52.h>
#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80,0x80,0x80,0x80}; //顯示內(nèi)容
uchar hang[]={0xfe,0xfd,0xfb,0xf7}; //第幾行按鍵
void delayms(uint lym);
uint scan(void); //掃描函數(shù)
void suanfa(); //傳送數(shù)據(jù)
uchar temp,key,num,num1;
uint i,num3,num2,a,b,c;
void main()
{
P0=0x00;
while(1)
{
scan();
suanfa();
}
}
uint scan() //按鍵掃描函數(shù)
{
for(i=0;i<4;i++) //掃描各行
{
P3=hang[i];
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xbe:key=1;break; //1
case 0xbd:key=2;break; //2
case 0xbb:key=3;break; //3
case 0x77:key=10;break; //+
case 0xde:key=4;break; //4
case 0xdd:key=5;break; //5
case 0xdb:key=6;break; //6
case 0xb7:key=11;break; //-
case 0xee:key=7;break; //7
case 0xed:key=8;break; //8
case 0xeb:key=9;break; //9
case 0xd7:key=12;break; //*
case 0x7e:key=14;break; //清0
case 0x7d:key=0;break; //0
case 0x7b:key=15;break; //=
case 0xe7:key=13;break; ///
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
P0=table[key]; //顯示
}
}
}
return key; //返回 key
}
void delayms(uint lym)
{
uint i,j;
for(i=lym;i>0;i--)
for(j=110;j>0;j--);
}
void suanfa()
{
if(key==0||key==1||key==2||key==3||key==4||key==5||key==6||key==7||key==8||key==9) //判斷0~9是否被按下
{
if(b==1) //某個數(shù)被按下
{
num2=key; //給num2
P1=0x01;
}
else //某個數(shù)被按下
{
num=key; //給num
P1=0x01;
}
c++;
}
if(key==10||key==11|key==12||key==13)
{
num1=key; //運算被按下
switch(num1)
{
case 10:P1=0x08;a=1;break; //+
case 11:P1=0x04;a=2;break; //-
case 12:P1=0x02;a=3;break; //*
case 13:P1=0x01;a=4;break; ///
}
b=1; //下次按下第2個數(shù)
}
if(key==15)
{
switch(a)
{
case 1:num3=num+num2;break; //+運算
case 2:num3=num-num2;break; //-運算
case 3:num3=num*num2;break; //*運算
case 4:num3=num/num2;break; ///運算
} //'{'不能放在下面那個delayms(10)后面,因為break的緣故不會在執(zhí)行下面
if(num3<10)
{
P0=table[num3%10]; //個位顯示
P1=0xf1;
delayms(10);
}
else if(num3<100)
{
P1=0xf1;
P0=table[num3%10]; //個位顯示,yu
delayms(10);
P1=0xf2;
P0=table[num3/10]; //十位顯示,mo
delayms(10);
}
}
if(key==14)
{
b=0;
num=0;
num1=0;
num2=0;
num3=0; //結果
P0=0x3f; //顯示0
P1=0xf1;
}
}
|