在Keil 5利用矩阵按键和LCD1602实现计算器功能(加减除、十进制和二进制转换) 注:除法运算结果显示保留两位小数
时间: 2024-02-15 12:01:54 浏览: 166
按键中断加减计数器_keil_dozenf5p_加减中断器_按键中断计数_
5星 · 资源好评率100%
好的,这是一个关于Keil 5的问题。以下是实现计算器功能(加减除、十进制和二进制转换)并保留除法运算结果两位小数的一些步骤和代码示例:
1. 确定硬件连接
您需要将LCD1602和矩阵按键与STC89C52单片机连接起来。具体连接方式可以参考相关的电路图和资料。
2. 编写代码
您需要编写C语言代码来实现计算器功能。以下是一个示例代码:
```
#include <reg52.h>
#include <stdio.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P1^2;
uchar code table[]={"0123456789ABCDEF"};
void delay(uint i)
{
while(i--);
}
void write_com(uchar com)
{
RS=0;
RW=0;
P2=com;
EN=1;
delay(10);
EN=0;
}
void write_data(uchar date)
{
RS=1;
RW=0;
P2=date;
EN=1;
delay(10);
EN=0;
}
void init()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void display(uchar *p)
{
while(*p!='\0')
{
write_data(*p++);
}
}
void clear()
{
write_com(0x01);
}
uchar get_key()
{
uchar key=0xff;
uchar key_code[]={0xfe,0xfd,0xfb,0xf7};
uchar i,j;
for(i=0;i<4;i++)
{
P3=key_code[i];
for(j=0;j<4;j++)
{
if(P3&(1<<j))
{
key=i*4+j;
while(P3&(1<<j));
return key;
}
}
}
return key;
}
void write_num(uint num)
{
uchar buf[16];
uchar i=0,j=0;
while(num)
{
buf[i++]=table[num%10];
num/=10;
}
if(i==0) buf[i++]='0';
buf[i]='\0';
for(j=strlen(buf)-1;j>=0;j--)
{
write_data(buf[j]);
}
}
uint read_num()
{
uint num=0;
uchar key=get_key();
while(key!=0xff)
{
if(key>=0&&key<=9)
{
num=num*10+key;
write_data(table[key]);
}
key=get_key();
}
return num;
}
void main()
{
init();
while(1)
{
clear();
write_com(0x80);
write_data('A');
write_data('+');
write_data('B');
write_data('=');
uint a=read_num();
write_data('+');
uint b=read_num();
write_data('=');
write_num(a+b);
clear();
write_com(0x80);
write_data('A');
write_data('-');
write_data('B');
write_data('=');
a=read_num();
write_data('-');
b=read_num();
write_data('=');
write_num(a-b);
clear();
write_com(0x80);
write_data('A');
write_data('*');
write_data('B');
write_data('=');
a=read_num();
write_data('*');
b=read_num();
write_data('=');
write_num(a*b);
clear();
write_com(0x80);
write_data('A');
write_data('/');
write_data('B');
write_data('=');
a=read_num();
write_data('/');
b=read_num();
write_data('=');
if(b==0)
{
display("ERROR");
}
else
{
float c=(float)a/b;
uchar buf[16];
sprintf(buf,"%.2f",c);
display(buf);
}
clear();
write_com(0x80);
write_data('A');
write_data('H');
write_data('B');
write_data('L');
write_data('=');
a=read_num();
write_data('H');
write_data('=');
write_num(a>>8);
write_data('L');
write_data('=');
write_num(a&0xff);
clear();
write_com(0x80);
write_data('B');
write_data('i');
write_data('n');
write_data('a');
write_data('r');
write_data('y');
write_data('A');
write_data('=');
a=read_num();
write_data('=');
write_num(a);
write_com(0xc0);
write_data('D');
write_data('e');
write_data('c');
write_data('i');
write_data('m');
write_data('a');
write_data('l');
write_data('=');
uchar buf[16];
sprintf(buf,"%d",a);
display(buf);
clear();
write_com(0x80);
write_data('D');
write_data('e');
write_data('c');
write_data('i');
write_data('m');
write_data('a');
write_data('l');
write_data('A');
write_data('=');
a=read_num();
write_data('=');
write_num(a);
write_com(0xc0);
write_data('B');
write_data('i');
write_data('n');
write_data('a');
write_data('r');
write_data('y');
write_data('=');
for(uchar i=0;i<16;i++)
{
buf[i]=(a>>i)&0x01;
}
for(uchar i=15;i>=0;i--)
{
write_data(table[buf[i]]);
}
}
}
```
3. 测试和调试
在编写代码后,您需要进行测试和调试。您可以使用Keil 5的模拟器来模拟单片机的运行情况,以确保代码的正确性。
总之,实现一个计算器功能需要您熟悉Keil 5和STC89C52单片机的使用,并且需要编写正确的代码来实现所需的功能。这段示例代码可以实现加减除、十进制和二进制转换,并保留除法运算结果两位小数。您可以根据自己的需求和实际情况进行修改和优化。
阅读全文