#include<iom16v.h>
#include "LCD1602.h"
#include "MatrixKey.h"
unsigned char L1[16];//显示第一行
unsigned char L2[16];//显示第二行
int result1 = 0; //存储第一个整数
int result2 = 0; //存储第二个整数
int Oper = 0; //存储运算符
unsigned int succeed_flag = 0,clear_flag=0;//计算完成标志位,清除标志位
void delay_1ms(void)
{
unsigned char i,j;
for(i=0;i<40;i++)
for(j=0;j<33;j++)
{;}
}
void delay_xms(unsigned int x)
{
unsigned int i;
for(i=0;i<x;i++)
{
delay_1ms();
}
}
void display_line1(void)//第一行输入显示
{
static unsigned int num=0;
unsigned char str = 0;
/*如果有计算完成标志,将显示内容清除,并将此标志位清零*/
if(succeed_flag == 1)
{
sprintf((char *)L1," ");
LCD_write_string(0,0,L1);
sprintf((char *)L2," ");
LCD_write_string(0,1,L2);
succeed_flag = 0;
num = 0;
}
switch(KeyResult)
{
case 0 : str = '0'; break;
case 1 : str = '1'; break;
case 2 : str = '2'; break;
case 3 : str = '3'; break;
case 4 : str = '4'; break;
case 5 : str = '5'; break;
case 6 : str = '6'; break;
case 7 : str = '7'; break;
case 8 : str = '8'; break;
case 9 : str = '9'; break;
case 10 : str = '+'; break;
case 11 : str = '-'; break;
case 12 : str = '*'; break;
case 13 : clear_flag = 1; break;
case 14 : str = '='; break;
case 15 : str = '/'; break;
default:break;
}
L1[num]=str;
num++;
if(clear_flag == 1)
{
sprintf((char *)L1," ");
LCD_write_string(0,0,L1);
sprintf((char *)L2," ");
LCD_write_string(0,1,L2);
num=0;
clear_flag=0;
}
LCD_write_string(0,0,L1);
key_flag=0;
if(num>=16)
{
num=0;
}
}
/*识别第一个数字*/
void distinguish_num1(void)
{
static int num = 0;
unsigned int key;
key=KeyResult;
/*如果被按下的是数字*/
switch(key)
{
case 0 : num = num * 10 + 0; break;
case 1 : num = num * 10 + 1; break;
case 2 : num = num * 10 + 2; break;
case 3 : 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 7 : num = num * 10 + 7; break;
case 8 : num = num * 10 + 8; break;
case 9 : num = num * 10 + 9; break;
default:break;
}
/*如果被按下的是运算符,则将之前的数字进行合成,然后保存运算符*/
if(key == 10)
{
/*加法*/
Oper = 1;
result1 = num;
num = 0; //将num清零,避免影响之后的计算
}
else if(key == 11)
{
/*减法*/
Oper = 2;
result1 = num;
num = 0; //将num清零,避免影响之后的计算
}
else if(key == 12)
{
/*乘法*/
Oper = 3;
result1 = num;
num = 0; //将num清零,避免影响之后的计算
}
else if(key == 13)
{
/*清除*/
result1 = 0;
Oper = 0;
num = 0;
}
else if(key == 15)
{
/*除法*/
Oper = 4;
result1 = num;
num = 0; //将num清零,避免影响之后的计算
}
}
/*识别第二个数字*/
void distinguish_num2(void)
{
static int num = 0;
/*如果被按下的是数字*/
unsigned int key;
key=KeyResult;
switch(key)
{
case 0 : num = num * 10 + 0; break;
case 1 : num = num * 10 + 1; break;
case 2 : num = num * 10 + 2; break;
case 3 : 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 7 : num = num * 10 + 7; break;
case 8 : num = num * 10 + 8; break;
case 9 : num = num * 10 + 9; break;
default:break;
}
if(key == 13)
{
/*清除*/
result2 = 0;
Oper = 0;
num = 0;
}
/*当等于号被按下时,识别第二个数字,并将结果计算,显示*/
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 *)L2,"%d ",result2);
LCD_write_string(0,1,L2);
/*将第一,二个数字,运算符以及保存的num清零*/
result1 = 0;
result2 = 0;
Oper = 0;
num = 0;
/*计算完成标志置1*/
succeed_flag = 1;
}
}
void main()
{
LCD1602_init();
while(1)
{
Key_Scan();
if(key_flag)
{
display_line1();
/*当第一串数字没有识别时,执行此函数,识别第一串数字以及运算符*/
if(Oper == 0)
{
distinguish_num1();
}
/*当第一串数字以及运算符已经确定的时候,执行识别第二串数字函数*/
else
{
distinguish_num2();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Atmega16的简易计算器实验设计和Proteus仿真.zip (155个子文件)
MatrixKey.c.190518104422 1KB
calculator.c.221114170142 0B
calculator.c.221114171906 223B
MatrixKey.c.221114203514 1KB
calculator.c.221114203514 435B
MatrixKey.c.221114215310 2KB
calculator.c.221114215310 461B
MatrixKey.c.221114215402 2KB
MatrixKey.c.221114215436 2KB
calculator.c.221114215436 437B
MatrixKey.c.221114215456 2KB
MatrixKey.c.221114215536 2KB
MatrixKey.c.221114215654 2KB
MatrixKey.c.221114215704 2KB
calculator.c.221114220558 446B
calculator.c.221114220624 470B
MatrixKey.c.221114220650 2KB
MatrixKey.c.221114220702 2KB
calculator.c.221114220702 446B
calculator.c.221114221120 477B
MatrixKey.h.221114221510 194B
MatrixKey.c.221114221602 2KB
calculator.c.221114221602 446B
MatrixKey.c.221114221730 2KB
MatrixKey.c.221114221800 2KB
MatrixKey.c.221114221856 2KB
calculator.c.221114221938 470B
MatrixKey.c.221114222106 2KB
calculator.c.221114222308 489B
calculator.c.221114222324 483B
calculator.c.221114222338 464B
calculator.c.221114222544 507B
MatrixKey.c.221114223038 2KB
calculator.c.221114223130 495B
calculator.c.221114223144 495B
calculator.c.221114223158 497B
calculator.c.221114223224 499B
calculator.c.221114223248 497B
calculator.c.221114223320 499B
calculator.c.221114223354 505B
calculator.c.221114223708 474B
MatrixKey.c.221114223744 2KB
calculator.c.221114223928 476B
calculator.c.221114224226 404B
calculator.c.221114224248 394B
calculator.c.221114224304 394B
calculator.c.221114224318 404B
calculator.c.221114224340 396B
calculator.c.221114224916 501B
calculator.c.221114225002 480B
MatrixKey.c.221114225136 2KB
MatrixKey.c.221114225256 2KB
MatrixKey.c.221114225310 2KB
MatrixKey.c.221114225344 2KB
calculator.c.221114225344 476B
MatrixKey.c.221114225506 2KB
calculator.c.221114230312 474B
calculator.c.221115152424 693B
MatrixKey.c.221115153006 2KB
calculator.c.221115153006 564B
calculator.c.221115153220 631B
calculator.c.221115153320 631B
calculator.c.221115153332 631B
calculator.c.221115153552 688B
calculator.c.221115153640 725B
calculator.c.221115153748 739B
calculator.c.221115153824 768B
calculator.c.221115153836 768B
calculator.c.221115154028 777B
calculator.c.221115154132 844B
calculator.c.221115154508 847B
calculator.c.221115154658 845B
calculator.c.221115154802 848B
calculator.c.221115154812 848B
MatrixKey.c.221115154926 2KB
calculator.c.221115154926 845B
calculator.c.221115155010 785B
calculator.c.221115155028 761B
calculator.c.221115155110 754B
calculator.c.221115155258 762B
calculator.c.221115155456 766B
calculator.c.221115155608 766B
calculator.c.221115155630 766B
calculator.c.221115155704 770B
calculator.c.221115155732 770B
calculator.c.221115155834 770B
calculator.c.221115155912 770B
MatrixKey.c.221115160250 2KB
calculator.c.221115160250 767B
MatrixKey.c.221115160350 2KB
MatrixKey.c.221115160418 2KB
calculator.c.221115163448 763B
MatrixKey.c.221115185958 2KB
calculator.c.221115185958 1KB
calculator.c.221115190026 1KB
MatrixKey.c.221115190110 2KB
MatrixKey.c.221115190126 2KB
MatrixKey.c.221115190136 2KB
calculator.c.221115194308 3KB
calculator.c.221115205200 3KB
共 155 条
- 1
- 2
资源评论
木贞子
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功