0
#include
#define uint unsigned int
#define uchar unsigned char
***it duanxuan=P2^6;
***it weixuan=P2^7;
uchar fuhao;
uint num1,num2,numk;
uchar code duan[]={ //数码管段码
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
uchar code wei[]={0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //数码管位码
uchar weishow[]={0,0,0,0,0,0}; //段码缓存,为了循环,定义成数组
void delay(uint i) //延时ims函数
{
uint j;
for(i;i>0;i--)
for(j=110;j>0;j--);
}
unsigned char keyboard() //矩阵键盘检测,共11行即可返回0~16键值.
{
char i,j,temp,hang[]={0xfe,0xfd,0xfb,0xf7};
for(j=0;j<4;j++)
{
P3=hang[j];
temp=0x10;
for(i=0;i<4;i++)
{
if(!(P3&temp))
{
delay(5); //延时5ms消抖
if(!(P3&temp))
return (i+j*4);
}
temp<<=1;
}
}
return 16;
}
void display() //显示函数
{
uchar i;
for(i=0;i<6;i++)
{
duanxuan=0;
weixuan=1;
P0=wei[i];
weixuan=0;
P0=0;
duanxuan=1;
P0=duan[weishow[i]];
duanxuan=0;
delay(1);
P0=0;
delay(1);
}
}
qingchu() //清除函数
{
uchar i;
num2=num1=numk=0;
fuhao=0;
for(i=0;i<6;i++)
weishow[i]=0;
}
void chunzhi() //储值函数
{
uchar i;
num1=weishow[5]*100000+weishow[4]*10000+weishow[3]*1000+weishow[2]*100+weishow[1]*10+weishow[0];
for(i=0;i<6;i++)
weishow[i]=0;
}
yunsuan() //运算函数
{
num2=num1;
chunzhi();
switch(fuhao)
{
case '+':numk=num2+num1;break;
case '-':numk=num2-num1;break;
case '*':numk=num2*num1;break;
case '/':numk=num2/num1;break;
}
weishow[5]=numk/100000;
weishow[4]=numk%100000/10000;
weishow[3]=numk%10000/1000;
weishow[2]=numk%1000/100;
weishow[1]=numk%100/10;
weishow[0]=numk%10;
display();
}
void chuli() //按键处理函数
{
uchar n;
n=keyboard();
if(n<10)
{
weishow[5]=weishow[4];
weishow[4]=weishow[3];
weishow[3]=weishow[2];
weishow[2]=weishow[1];
weishow[1]=weishow[0];
weishow[0]=n;
}
if(n>9)
{
switch(n)
{
case 10:qingchu();break;
case 11:yunsuan();break;
case 12:chunzhi();fuhao='+';break;
case 13:chunzhi();fuhao='-';break;
case 14:chunzhi();fuhao='*';break;
case 15:chunzhi();fuhao='/';break;
}
}
while(n<16) //等待按键释放
{
n=keyboard();
display();
}
}
void main()
{
while(1)
{
chuli();
display();
}
}
|
|