单片机计算器仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<at89x52.h>
- #define uchar unsigned char
- uchar LED[]={0x3f,0x60,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar Lamp[]={0x07,0x06,0x05,0x04,0x30,0x02,0x01,0x00} ;
- unsigned char key=0;
- unsigned long int num=0;
- unsigned long int num_old=0;
- long int result=0;
- unsigned char res[10]={0};
- unsigned char pos=0;
- unsigned char flag=0;
- unsigned char hang=0,lie=0;
- unsigned char i=0;
- void delayms(unsigned char i)
- { unsigned char k;
- while(i--)
- for(k=0;k<120;k++) ;
- }
- unsigned char keycode[4][4]={{1,2,3,'*'},
- {4,5,6,'/'},
- {7,8,9,'C'},
- {0,'+','-','='} };
- unsigned char keyScan()
- {
- P1 = 0x0f;
- switch(P1&0x0f)
- {
- case 0x0e: hang=0;break;
- case 0x0d: hang=1;break;
- case 0x0b: hang=2;break;
- case 0x07: hang=3;break;
- default: break;
- }
- P1 = 0xf0;
- switch(P1&0xf0)
- {
- case 0xe0: lie=0;break;
- case 0xd0: lie=1;break;
- case 0xb0: lie=2;break;
- case 0x70: lie=3;break;
- default: break;
- }
- return keycode[hang][lie];
- }
- void Display(uchar DisplayNum,uchar LEDNum)
- {
- P2=Lamp[LEDNum];
- P0=LED[DisplayNum];
- delayms(3);
- }
- void T1_isr() interrupt 3
- {
- #define KEY_DOWN P1!=0x0f
- #define KEY_UP P1==0x0f
- static uchar flag = 0;
- TH1 = (65536-50000)/256;
- TL1 = (65536-50000)%256;
- P1 = 0x0f;
- if(flag == 0 && KEY_DOWN)
- flag = 1;
- else if(flag == 1 && KEY_UP)
- flag = 0;
- else if(flag == 1 && KEY_DOWN)
- {
- flag = 2;
- pos++;
- Display(keyScan(),pos);
- }
- else if(flag == 2 && KEY_UP)
- flag = 0;
- }
- void show()
- {
- unsigned char i=0;
- if(pos==0) Display(0,0);
- else
- for(i=0;i<pos;i++)
- {
- Display(keyScan(),pos-i-1);
-
- }
- }
- void main()
- {
- TMOD = 0x10;
- TH1 = (65536-50000)/256;
- TL1 = (65536-50000)%256;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
新建文件夹01.rar
(40.24 KB, 下载次数: 34)
|