单片机源程序如下:
- #include "reg52.h"
- #include "string.h"
- #include "stdio.h"
- #include "lcd1602.h"
- #include "keyboard.h"
- #define keydeng 14
- #define keyzuoyi 12
- code unsigned char KeyDisPlaybuf[16] =
- {
- '1', '2', '3', '+',\
- '4', '5', '6', '-',\
- '7', '8', '9', '*',\
- '<', '0', '=', '/'
- };
- long Count(char *ch, char len)//简单计算器 仅支持+-*/
- {
- unsigned char i = 0;
- char Operation;
- long Num1=0,Num2=0;
- while(i<len && ch[i]!='+' && ch[i]!='-' && ch[i]!='*' && ch[i]!='/')
- {
- Num1*=10;
- Num1+=ch[i]-'0';
- i++;
- }
- Operation=ch[i++];
- while(i<len && ch[i]!='=')
- {
- Num2*=10;
- Num2+=ch[i]-'0';
- i++;
- }
- switch(Operation)
- {
- case'+':return Num1+Num2;
- case'-':return Num1-Num2;
- case'*':return Num1*Num2;
- case'/':return Num1/Num2;
- }
- return 0;
- }
- void EasyCounter(void)
- {
- unsigned char buff[30];
- unsigned char text[30];
- unsigned char key;
- unsigned char i = 0;
- do
- {
- key = keycan();//读取键值
- if(key!=0xff)//键值不等于0
- {
- text[i++]=KeyDisPlaybuf[key];//记录键值对应的字符
- text[i]='\0';
- LCD_ShowStr(0,0," ");
- LCD_ShowStr(16-strlen(text),0,text);
- }
- if(key==keyzuoyi&&i>0)//按下"<"时删除一位
- i-=2;
- }while(key!=keydeng);//按下'='时结束输入
- sprintf(buff, "%ld", Count(text, strlen(text)));
- LCD_ShowStr(0,1," ");
- LCD_ShowStr(16-strlen(buff),1,buff);
- }
- void main(void)
- {
- LCD_Init();
- while(1)
- {
- EasyCounter();
- }
- }
复制代码
所有资料51hei提供下载:
51单片机1602计算器.zip
(63.16 KB, 下载次数: 38)
|