一个简易计算器程序,包括keil4程序和protues仿真图
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h>
- unsigned char show[8]={0,0,0,0,0,0,0,0};
- unsigned char code seg7_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x00,0x50,0x5c};
- unsigned char code wei_table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- unsigned char code err[]={0x0e,0x12,0x12,0x13,0x12,0x11,0x11,0x11};
- unsigned char count=0;
- unsigned long a,b;
- unsigned char flag=0;
- //unsigned char flag1=1;
- unsigned char key_code;
- bit key_press=0;
- void delay(long dly)
- {
- while(dly--);
- }
- void disp(long dat)
- {
-
- bit flag1=0;
- long e;
- unsigned char i;
- e=dat;
- if(e==0)
- {
- for(i=0;i<7;i++)
- {
- show[i]=0x11;
- }
- show[7]=0x00;
- }
- else if((e>99999999)||(e<-9999999))
- {
- for(i=0;i<8;i++)
- {
- show[i]=err[i];
- }
- }
-
- else
- {
- if(e<0)
- {
- flag1=1;
- e=-e;
- }
- for(i=0;i<8;i++)
- {
- if(e>=1)
- {
- show[7-i]=e%10;
- e=e/10;
- }
- else
- {
- if(flag1==1)
- {
- show[7-i]=0x10;
- flag1=0;
- }
- else
- {
- show[7-i]=0x11;
- }
- }
- }
- }
- }
- void read_key() //4X4矩阵键盘说明:行为输入,列为输出
- {
- char x,y;
- char i;
- P1=0xf0;
- if(P1!=0xf0)
- {
- delay(1000);
- if(P1!=0xf0)
- {
- y=P1;
- x=0xfe;
- for(i=0;i<4;i++)
- {
- P1=x |0xf0;
- if((P1 & 0xf0)!=0xf0)
- {
- x=P1;
- key_code=y | (x & 0x0f);
- key_press=1;
- }
- x=(x<<1)+0x01;
- }
- switch(key_code)
- {
- case 0xee: key_code=7;break;
- case 0xde: key_code=8;break;
- case 0xbe: key_code=9;break;
- case 0x7e: key_code=14;break;
- case 0xed: key_code=4;break;
- case 0xdd: key_code=5;break;
- case 0xbd: key_code=6;break;
- case 0x7d: key_code=13;break;
- case 0xeb: key_code=1;break;
- case 0xdb: key_code=2;break;
- case 0xbb: key_code=3;break;
- case 0x7b: key_code=12;break;
- case 0xe7: key_code=15;break;
- case 0xd7: key_code=0;break;
- case 0xb7: key_code=16;break;
- case 0x77: key_code=11;break;
- }
-
- }
-
- }
- }
- void calculate()
- {
- if(key_code<10)
- {
- if(flag==0)
- {
- a=a*10+key_code;
- disp(a);
- }
- else
- {
- b=b*10+key_code;
- disp(b);
- }
- }
- else
- {
- switch(key_code)
- {
- case 11:flag=1;break;
- case 12:flag=2;break;
- case 13:flag=3;break;
- case 14:flag=4;break;
- case 15:a=0;b=0;disp(a);flag=0;break;
- case 16:{
- switch(flag)
- {
- case 1:a=a+b;disp(a);b=0;break;
- case 2:a=a-b;disp(a);b=0;break;
- case 3:a=a*b;disp(a);b=0;break;
- case 4:a=a/b;disp(a);b=0;break;
- }
- }
- }
- }
-
-
- }
- void main()
- {
- TMOD=0x01;
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- TR0=1;
- EA=1;
- ET0=1;
- a=0;
- b=0;
- disp(a);
- // get_keycode();
- while(1)
- {
- read_key();
- if(key_press==1)
- {
- calculate();
- key_press=0;
- }
- delay(3000);
- }
- }
- void TIMER0_ISR() interrupt 1 using 1
- {
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- P0=0x00;
- P2=(P2 & 0xe3)|(count<<2);
- P0=seg7_table[show[count]];
- count++;
- if(count>7)
- {
- count=0;
- }
- }
复制代码
所有资料51hei提供下载:
简易计算器.zip
(64.61 KB, 下载次数: 51)
|