新基于51单片机的简易计算器.doc

上传人:小飞机 文档编号:4264592 上传时间:2023-04-12 格式:DOC 页数:30 大小:311.50KB
返回 下载 相关 举报
新基于51单片机的简易计算器.doc_第1页
第1页 / 共30页
新基于51单片机的简易计算器.doc_第2页
第2页 / 共30页
新基于51单片机的简易计算器.doc_第3页
第3页 / 共30页
新基于51单片机的简易计算器.doc_第4页
第4页 / 共30页
新基于51单片机的简易计算器.doc_第5页
第5页 / 共30页
点击查看更多>>
资源描述

《新基于51单片机的简易计算器.doc》由会员分享,可在线阅读,更多相关《新基于51单片机的简易计算器.doc(30页珍藏版)》请在三一办公上搜索。

1、基于51单片机的简易计算器1、 前言:本设计是基于51系列单片机来进行的数字计算器系统设计,可以完成计算器的键盘输入,进行加、减、乘、除基本四则运算,并在LCD上显示相应的结果;设计电路采用STC90C51单片机为主要控制电路,利用MM74C922作为计算器4*4键盘的扫描IC读取键盘上的输入;显示采用字符LCD静态显示;软件方面使用C语言编程,并用PROTUES仿真。2、 设计任务:计算器软件程序要完成以下模块的设计:(1)键盘输入检测模块;(2)LCD显示模块;(3)算术运算模块;(4)错误处理及提示模块。3、主体设计部分:(1)、系统模块图:(2)、系统总流程图:4、硬件部分 单片机部分

2、+矩阵键盘+1602显示如图所示为简易计算器的电路原理图。P3口用于键盘输入,接4*4矩阵键盘,键值与键盘的对应表如表-所示,p0口和p2口用于显示,p2口用于显示数值的高位,po口用于显示数值的低位。简易计算器电路原理图矩阵键盘有16个按键,满足对简易计算器的计算实现,显示部分采用LCD1602,第一行显示计算的数值符号,第二行显示计算结果。LCD显示模块:本设计采用LCD液晶显示器来显示输出数据。通过D0-D7引脚向LCD写指令字或写数据以使LCD实现不同的功能或显示相应数据。5、软件部分#include#include#define uchar unsigned charsbit lcd

3、en=P27;sbit lcdrs=P26;sbit lcdrw=P25;sbit lcdbf=P07;uchar temp,key,i,j,flag,fh,k;long a,b,c;uchar code table=1,2,3,0,4,5,6,0,7,8,9,0,0,0,0,0;uchar code table2=123+456-789*000/;void delay(uchar ms)uchar x,y;for(x=ms;x0;x-)for(y=110;y0;y-);/*-对LCD1602的操作-*/bit busy(void)/判断忙碌bit res;lcdrs=0;lcdrw=1;lc

4、den=1;_nop_();_nop_();res=lcdbf;lcden=0;return res;void write_inst (uchar cmd)/写命令while(busy()=1); /忙碌就等待lcdrs=0;lcdrw=0;lcden=0;_nop_();_nop_();P0=cmd;_nop_();_nop_();lcden=1;_nop_();_nop_();lcden=0;void write_(uchar )/写地址write_inst(|0x80);void write_date(uchar dat) /写数据while(busy()=1);lcdrs=1;lcdr

5、w=0;lcden=0;P0=dat;_nop_();_nop_();lcden=1;_nop_();_nop_();lcden=0;void init()/初始化lcden=1;write_inst(0x38);/显示8位2行delay(5);write_inst(0x0c);/显示开,光标关,不闪烁delay(5);write_inst(0x06);/增量方式不位移delay(5);write_inst(0x80);/检测忙碌信号delay(5);write_inst(0x01);/delay(5);/*-键盘扫描-*/void keyscan()/键盘扫描P3=0xfe;if(P3!=0

6、xfe)delay(100);if(P3!=0xfe)temp=P3&0xf0;switch(temp)case 0xe0:key=0;break;case 0xd0:key=1;break;case 0xb0:key=2;break;case 0x70:key=3;break;while(P3!=0xfe);if(key=0|key=1|key=2)if(j!=0)write_inst(0x01);j=0;if(flag=0)a=a*10+tablekey;if(flag=1)b=b*10+tablekey;write_date(table2key);elseif(k=0)flag=1;k=

7、1;fh=1;write_date(table2key);P3=0xfd;if(P3!=0xfd)delay(100);if(P3!=0xfd)temp=P3&0xf0;switch(temp)case 0xe0:key=4;break;case 0xd0:key=5;break;case 0xb0:key=6;break;case 0x70:key=7;break;while(P3!=0xfd);if(key=4|key=5|key=6)if(j!=0)write_inst(0x01);j=0;if(flag=0)a=a*10+tablekey;if(flag=1)b=b*10+tablek

8、ey;write_date(table2key);elseif(k=0)flag=1;k=1;fh=2;write_date(table2key);P3=0xfb;if(P3!=0xfb)delay(100);if(P3!=0xfb)temp=P3&0xf0;switch(temp)case 0xe0:key=8;break;case 0xd0:key=9;break;case 0xb0:key=10;break;case 0x70:key=11;break;while(P3!=0xfb);if(key=8|key=9|key=10)if(j!=0)write_inst(0x01);j=0;i

9、f(flag=0)a=a*10+tablekey;if(flag=1)b=b*10+tablekey;write_date(table2key);elseif(k=0)flag=1;k=1;fh=3;write_date(table2key);P3=0xf7;if(P3!=0xf7)delay(100);if(P3!=0xf7)temp=P3&0xf0;switch(temp)case 0xe0:key=12;break;case 0xd0:key=13;break;case 0xb0:key=14;break;case 0x70:key=15;break;while(P3!=0xf7);sw

10、itch(key)case 12:write_inst(0x01);a=0;b=0;flag=0;fh=0;j=0;k=0; break;case 13:if(flag=0)a=a*10;write_date(0x30);P1=0;else if(flag=1)b=b*10;write_date(0x30); break;case 14: j=1; if(fh=1) write_(0x4f);write_inst(0x04);c=a+b;while(c!=0)write_date(0x30+c%10);c=c/10;write_date(0x3d);a=0;b=0;flag=0;fh=0;k=

11、0; else if(fh=2) write_(0x4f);write_inst(0x04);if(a-b)0)c=a-b;elsec=b-a;if(c=0)write_date(0x30+0);while(c!=0)write_date(0x30+c%10);c=c/10;if(a-b)*(-1)0)write_date(0x2d);write_date(0x3d);a=0;b=0;flag=0;fh=0;k=0; else if(fh=3) write_(0x4f);write_inst(0x04);c=a*b;if(c=0)write_date(0x30+0);while(c!=0)wr

12、ite_date(0x30+c%10);c=c/10;write_date(0x3d);a=0;b=0;flag=0;fh=0;k=0; else if(fh=4) write_(0x4f); write_inst(0x04); i=0; c=(long)(float)a/b)*1000); /计算c的数据 if(c=0)write_date(0x30+0); while(c!=0) write_date(0x30+c%10); c=c/10; i+; if(i=3) write_date(0x2e); /写数据 if(a/b)=0) write_date(0x30); write_date(0x3d); a=0;b=0;flag=0;fh=0;k=0; break;case 15:if(k=0) write_date(table2key);flag=1;k=1;fh=4; break; void main()init();i=0;j=0;a=0;b=0;c=0;k=0;flag=0;fh=0;while(1)keyscan();6、 总结通过该计算器的设计我深入学习数码管扫描和键盘控制,提高对了51系列单片机的实际应用能力。同时也掌握应用程序控制51系列单片机进行简单的数学运算。提高了对51系列单片机的编程能力。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号