功能很简单,只能实现十以内的加、减、乘、除。不过这个不是问题,这个好改。只是就能实现一次计算,不能多次。我考虑到了用递归,但是这个学的不是很好,所以还得继续往这方面努力。
以下是我的程序:
#include<reg52.h> #define uint unsigned int #define uchar unsigned char uint temp,b,c,d,a,sum,s,flag,i=0; uchar code table[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98 }; uchar data num[10]; void panduan(); void disp(uint); void dely(uint x); void key(); void main() { while(1) { key(); } } void key() { P1=0xfe; temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xee:num[i]=1; break; case 0xde:num[i]=2; break; case 0xbe:num[i]=3; break; case 0x7e: i=0; while(1){ key(); } } temp=temp&0xf0; } } P1=0xfd; temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xed:num[i]=4; break; case 0xdd:num[i]=5; break; case 0xbd:num[i]=6; break; case 0x7d:num[i]=0; break; } temp=temp&0xf0; } } P1=0xfb; temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xeb:num[i]=7; break; case 0xdb:num[i]=8; break; case 0xbb:num[i]=9; break; case 0x7b:panduan(); break; } temp=temp&0xf0; } } P1=0xf7; temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xe7: s=1; i++; break; case 0xd7: s=2; i++; break; case 0xb7: s=3; i++; break; case 0x77: s=4; i++; break; } temp=temp&0xf0; } } } void disp(uint sum) { dely(5); flag=0; temp=temp&0xf0; if(temp!=0xf0) { flag=1; } if(flag==1){ c=sum; d=sum/10; while(1){ P3=0xfe; P0=table[c]; dely(5); P3=0xfd; P0=table[d]; dely(5); //key(); } } } void panduan() { if(s==1){ sum=num[0]+num[i]; } if(s==2) sum=num[0]-num[i]; if(s==3) sum=num[0]*num[i]; if(s==4) { if(num[i]==0) sum=0; else sum=num[0]/num[i]; } disp(sum); dely(50); } void dely(uint x) { uchar i,j; for(i=0;i<x;i++) for(j=0;j<110;j++); }
求sum的时候用的是num[i],因为我的消抖延时写的不是很完善,按一次,i的值不是只加1.因此我就直接用num[i],表示了。
终于知道了为啥i总是增加那么多了,因为没有写松手检测。需要写个下面的函数:
while(temp!=0xf0)
{
P1=temp;
temp=temp&0xf0;
}