两位数以内加减法,比较适合初学者
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- unsigned int k=0,k1,k2,k3,k4,z=0;
- unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- void delay(unsigned char x)
- {
- unsigned char y;
- for(;x>0;x--)
- for(y=110;y>0;y--);
- }
- unsigned char key()
- {
- unsigned char row,col,m=0xff;
- P2=0xf0;
- if(P2==0xf0)
- return m;
- delay(20);
- if(P2==0xff)
- return m;
- for(row=0;row<4;row++)
- {
- P2=~(1<<row);
- if((P2&0xf0)!=0xf0)
- break;
- }
- for(col=0;col<4;col++)
- {
- P2=~(1<<(col+4));
- if((P2&0x0f)!=0x0f)
- break;
- }
- if(row<4&&col<4)
- m=row*4+col;
- P2=0xf0;
- while((P2&0xf0)!=0xf0);
- z++;
- return m;
- }
- void display(unsigned char a)
- {
- unsigned shi,ge;
- shi=a/10;
- ge=a%10;
- if(shi==0)
- P0=0xff;
- else
- P0=tab[shi];
- P1=0x01;
- delay(5);
- P0=0xff;
- P0=tab[ge];
- P1=0x02;
- delay(5);
- P0=0xff;
- }
- void main()
- {
- while(1)
- {
- k=key();
- if(k>=0&&k<=9&&z==1&&k!=0xff)
- {
- k1=k;
- k3=k1;
- }
- delay(20);
- if(k>=0&&k<=9&&z==2&&k!=0xff)
- {
- k2=k;
- k1=k3*10+k2;
- }
- if(k==10)
- {
- k4=k1;
- z=0;
- }
- if(k==11)
- {
- k1=k1+k4;
- }
- display(k1);
- }
- }
复制代码
所有资料51hei提供下载:
两位数以内加减法.zip
(17.58 KB, 下载次数: 55)
|