使用keil c软件基于单片机系统设计一个“简单的计算器”。要求设计的模拟计算器必须从按钮输入0~9组成一个数,再从按钮输入加、减、乘和除运算符(+、-、*、/),同样从按钮上输入另一个数,之后按下等于按钮能够实现两个数之间的和、差、积和商的运算并且显示相关的操作步骤。
1、AT89C51单片机
2、手动复位电路
3、时钟电路
4、LCD602液晶显示
5、4*4按键矩阵
#include "reg52.h" #include "stdio.h" #include "key.h" #include "1602.h" #define uchar unsigned char #define uint unsigned int uchar Line1[20]; //第一行显示数组 uchar Line2[20]; //第二行显示数组 uint key_flag; //按键标志 int result1 = 0; //存储第一个整数 int result2 = 0; int Oper = 0; //存储运算符 uint succeed_flag = 0; //计算完成标志 void display_line1(uint key) { static uint num = 0; uchar str = 0; if(succeed_flag == 1) { sprintf((char *)Line1," "); //直接输入空格便会清除 print_string(Line1,1); sprintf((char *)Line2," "); print_string(Line2,2); succeed_flag = 0; num = 0; } switch(key) { case 0 : str = '1'; break; case 1 : str = '2'; break; case 2 : str = '3'; break; case 3 : str = '+'; break; case 4 : str = '4'; break; case 5 : str = '5'; break; case 6 : str = '6'; break; case 7 : str = '-'; break; case 8 : str = '7'; break; case 9 : str = '8'; break; case 10 : str = '9'; break; case 11 : str = '*'; break; case 12 : break; case 13 : str = '0'; break; case 14 : str = '='; break; case 15 : str = '/'; break; default:break; } Line1[num] = str; print_string(Line1,1); num ++; if(num >= 20) num = 0; } void distinguish_num1(uint key) { static int num = 0; switch(key) { case 0 : num = num * 10 + 1; break; case 1 : num = num * 10 + 2; break; case 2 : num = num * 10 + 3; break; case 4 : num = num * 10 + 4; break; case 5 : num = num * 10 + 5; break; case 6 : num = num * 10 + 6; break; case 8 : num = num * 10 + 7; break; case 9 : num = num * 10 + 8; break; case 10 : num = num * 10 + 9; break; case 13 : num = num * 10 + 0; break; default:break; } if(key == 3) { Oper = 1; result1 = num; num = 0; //将num清零,避免影响之后的计算 } else if(key == 7) { Oper = 2; result1 = num; num = 0; //将num清零,避免影响之后的计算 } else if(key == 11) { Oper = 3; result1 = num; num = 0; //将num清零,避免影响之后的计算 } else if(key == 15) { Oper = 4; result1 = num; num = 0; //将num清零,避免影响之后的计算 } } void distinguish_num2(uint key) { static int num = 0; switch(key) { case 0 : num = num * 10 + 1; break; case 1 : num = num * 10 + 2; break; case 2 : num = num * 10 + 3; break; case 4 : num = num * 10 + 4; break; case 5 : num = num * 10 + 5; break; case 6 : num = num * 10 + 6; break; case 8 : num = num * 10 + 7; break; case 9 : num = num * 10 + 8; break; case 10 : num = num * 10 + 9; break; case 13 : num = num * 10 + 0; break; default:break; } if(key == 14) { result2 = num; if(Oper == 1) { result2 = result1 + result2; } else if(Oper == 2) { result2 = result1 - result2; } else if(Oper == 3) { result2 = result1 * result2; } else if(Oper == 4) { result2 = result1 / result2; } sprintf((char *)Line2,"%d ",result2); print_string(Line2,2); result1 = 0; result2 = 0; Oper = 0; num = 0; succeed_flag = 1; } } void main() { lcd_init(); //lcd初始化 while(1) { key_flag = key_scan(); //按键检测 没有按键被按下时 返回值为16 if(key_flag != 16) //有按键被按下 { display_line1(key_flag); if(Oper == 0) { distinguish_num1(key_flag); } else { distinguish_num2(key_flag); } } } }
程序设计图:
如需源文件请私信我