新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 16键计算器 c51实现

16键计算器 c51实现

作者: 时间:2016-11-29 来源:网络 收藏
1、定时显示

开辟一显示缓冲区,显示中断程序定时读取缓冲区显示各灯位,每灯位显示2.5ms,显示4只灯需要10ms,也就是显示的刷新频率是100Hz,亮度为25%。这样既保证亮度又不闪烁,同时CPU还有足够时间做其他事情。

本文引用地址:https://www.eepw.com.cn/article/201611/323343.htm

2、键盘中断扫描

利用键盘中断扫描程序,读出按键的键码,存入键盘缓冲区,供主程序读区。

3、计算器的主程序读取键盘缓冲区的键码,驱动计算器的运行,计算器的运行过程要清晰明了;

4、计算中需要十进制与十六进制(或二进制)转化。


#define LED_seg      XBYTE[0x6000]     //段码地址#define LED_light    XBYTE[0x8000]      //灯位地址#include "reg51.h"#include "absacc.h"unsigned char read_key(unsigned char);unsigned char display(unsigned char,unsigned char);void delay(unsigned int);unsigned char light[4]={0xfe,0xfd,0xfb,0xf7};   //扫描值 灯位码unsigned char seg[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //段码unsigned char disp_buffer[5]={1,0,0,0,0};            //当前灯位、 显示区unsigned char key_buffer[2]={0,0xff};           //缓冲区满标志、键码void main(){Unsigned char temp[5];unsigned char i;unsigned char operator=0;unsigned int tempa,tempb;bit first=1;TMOD=0x01;       //置T0为方式1TL0=0x0;          //延时2.5mS的定时器初始值TH0=0xf7;          //赋初值PT0=1;             //定时中断0优先设置TR0=1;           //启动定时ET0=1;           //允许定时IT1=0;        //电平触发低电平有效EX1=1;       //允许外中断PX1=1;       // 外部中断1优先级设定EA=1;        //中断总允许while(1){P1=0xf0;                           //扫描if (key_buffer[0]){switch(key_buffer[1])              //缓冲区满{case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9: if (first)  for(i=1;i<=4;i++)   //保存数据,等待另一个数据输入{temp[i]=disp_buffer[i];disp_buffer[i]=0;                                           }first=0;for(i=1;i<=3;i++)  disp_buffer[i]=disp_buffer[i+1];disp_buffer[4]=key_buffer[1];break;case 10:case 11:case 12:case 13:case 14: first=1;tempa=1000*disp_buffer[1]+100*disp_buffer[2]+10*disp_buffer[3]+disp_buffer[4];tempb=1000*temp[1]+100*temp[2]+10*temp[3]+temp[4];switch(operator){case 10:tempa+=tempb;break;case 11:tempa=tempb-tempa;break;case 12:tempa*=tempb;break;case 13:tempa=tempb/tempa;}tempa%=10000;disp_buffer[1]=tempa/1000;tempa%=1000;disp_buffer[2]=tempa/100;tempa%=100;disp_buffer[3]=tempa/10;disp_buffer[4]=tempa%10;operator=key_buffer[1];break;case 15: for(i=1;i<=4;i++)  disp_buffer[i]=temp[i]=0;first=1;operator=0;}key_buffer[0]=0;                     //缓冲区空}          }}
				
            
                
			
							
上一页 1 2 下一页

关键词: 16键计算器c5

评论


技术专区

关闭