#include <reg52.h>
#define GPIO_KEY P1
typedef unsigned char u8;
typedef unsigned int u16;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0-9
u8 tab[8];
u16 shu;
//延时函数us
void delay(u16 i){
while(i--);
}
//矩阵按键扫描
u8 kskan(void)
{
u8 a=0,KeyValue;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//读取按键是否按下
{
delay(1000);//延时10ms进行消抖
if(GPIO_KEY!=0x0f)//再次检测键盘是否按下
{
//测试列
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//测试行
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //检测按键松手检测
{
delay(1000);
a++;
}
}
}
return KeyValue;
}
//1.判断结果数字的位数和数码管应显示的位数
//2.数码管显示结果
void display(){
u8 m,j;
if(shu<=9)j=0;
if(shu>=10&&shu<=99)j=1;
if(shu>=100&&shu<=999)j=2;
if(shu>=1000&&shu<=9999)j=3;
if(shu>=10000&&shu<=99999)j=4;
if(shu>=100000&&shu<=999999)j=5;
if(shu>=1000000&&shu<=9999999)j=6;
if(shu>=10000000&&shu<=99999999)j=7; //判断显示数字位数
tab[0]=shu%10;
tab[1]=shu/10%10;
tab[2]=shu/100%10;
tab[3]=shu/1000%10;
tab[4]=shu/10000%10;
tab[5]=shu/100000%10;
tab[6]=shu/1000000%10;
tab[7]=shu/10000000%10;
for(m=0;m<j;m++){
switch(m){
case 0:LSA=0;LSB=0;LSC=0;break;
case 1:LSA=1;LSB=0;LSC=0;break;
case 2:LSA=0;LSB=1;LSC=0;break;
case 3:LSA=1;LSB=1;LSC=0;break;
case 4:LSA=0;LSB=0;LSC=1;break;
case 5:LSA=1;LSB=0;LSC=1;break;
case 6:LSA=0;LSB=1;LSC=1;break;
case 7:LSA=1;LSB=1;LSC=1;break; //三八译码器
default:break;
}
P0=smgduan[tab[m]];
delay(100);
P0=0x00; //消影
}
}
void main(){
u8 num=0;
u16 shu1=0,shu2=0,jieguo;
u8 fuhao,flag=0;
while(1){
num=kskan();
if(num<=9&&flag==0){
shu1=shu1*10+num;
delay(65536);
shu=shu1;
}
if(num<=9&&flag==1){
shu2=shu2*10+num;
delay(65536);
shu=shu2;
}
if(num>=11||num<=15){
fuhao=num;
flag=1;
}
if(num==10){
flag=0;
switch(fuhao){
case 11:jieguo=shu1;break;
case 12:jieguo=shu1+shu2;break; //加
case 13:jieguo=shu1-shu2;break; //减
case 14:jieguo=shu1*shu2;break; //乘
case 15:jieguo=shu1/shu2;break; //除
default:break;
}
shu=jieguo;
shu1=0;
shu2=0;
}
display();
}
}
|