设计一个基于STC89C52单片机的计算器的程序
时间: 2023-08-11 21:05:02 浏览: 99
基于89C52的计算器设计.doc
好的,下面是一个简单的基于STC89C52单片机的计算器的程序,可以进行加、减、乘、除四则运算:
```c
#include <reg52.h>
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit LCD_D4 = P2^4;
sbit LCD_D5 = P2^5;
sbit LCD_D6 = P2^6;
sbit LCD_D7 = P2^7;
void delay(unsigned int ms) // 延时函数
{
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 114; j++);
}
void LCD_Write_Command(unsigned char cmmd) // 写入指令
{
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 1;
LCD_D7 = (cmmd & 0x80) >> 7;
LCD_D6 = (cmmd & 0x40) >> 6;
LCD_D5 = (cmmd & 0x20) >> 5;
LCD_D4 = (cmmd & 0x10) >> 4;
LCD_EN = 0;
delay(1);
LCD_EN = 1;
LCD_D7 = (cmmd & 0x08) >> 3;
LCD_D6 = (cmmd & 0x04) >> 2;
LCD_D5 = (cmmd & 0x02) >> 1;
LCD_D4 = (cmmd & 0x01);
LCD_EN = 0;
delay(1);
}
void LCD_Write_Data(unsigned char data) // 写入数据
{
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 1;
LCD_D7 = (data & 0x80) >> 7;
LCD_D6 = (data & 0x40) >> 6;
LCD_D5 = (data & 0x20) >> 5;
LCD_D4 = (data & 0x10) >> 4;
LCD_EN = 0;
delay(1);
LCD_EN = 1;
LCD_D7 = (data & 0x08) >> 3;
LCD_D6 = (data & 0x04) >> 2;
LCD_D5 = (data & 0x02) >> 1;
LCD_D4 = (data & 0x01);
LCD_EN = 0;
delay(1);
}
void LCD_Init() // 初始化LCD显示器
{
LCD_Write_Command(0x02); // 返回Home
LCD_Write_Command(0x28); // 4位数据线,2行显示,5x8字符
LCD_Write_Command(0x0c); // 显示器开,不显示光标
LCD_Write_Command(0x06); // 光标右移,不移动显示
LCD_Write_Command(0x01); // 清屏
}
void LCD_Display_Char(unsigned char c) // 显示字符
{
LCD_Write_Data(c);
}
void LCD_Display_String(unsigned char *str) // 显示字符串
{
while (*str != '\0')
{
LCD_Display_Char(*str++);
}
}
unsigned char Get_Key() // 获取按键值
{
unsigned char key = 0xff;
P1 = 0xff;
if (P1 != 0xff)
{
delay(5);
if (P1 != 0xff)
{
switch (P1)
{
case 0xfe: key = 0; break;
case 0xfd: key = 1; break;
case 0xfb: key = 2; break;
case 0xf7: key = 3; break;
case 0xef: key = 4; break;
case 0xdf: key = 5; break;
case 0xbf: key = 6; break;
case 0x7f: key = 7; break;
}
while (P1 != 0xff);
}
}
return key;
}
void main()
{
unsigned char key, op = '+', flag = 0;
unsigned int num1 = 0, num2 = 0, result = 0;
LCD_Init();
while (1)
{
key = Get_Key();
if (key <= 9)
{
if (flag == 0)
{
num1 = num1 * 10 + key;
LCD_Display_Char(key + '0');
}
else
{
num2 = num2 * 10 + key;
LCD_Display_Char(key + '0');
}
}
else if (key == 10)
{
if (op == '+')
{
result = num1 + num2;
}
else if (op == '-')
{
result = num1 - num2;
}
else if (op == '*')
{
result = num1 * num2;
}
else if (op == '/')
{
result = num1 / num2;
}
num1 = result;
num2 = 0;
flag = 0;
LCD_Write_Command(0xc0);
LCD_Display_String("=");
LCD_Display_String(result);
}
else if (key == 11)
{
op = '+';
flag = 1;
LCD_Display_Char('+');
}
else if (key == 12)
{
op = '-';
flag = 1;
LCD_Display_Char('-');
}
else if (key == 13)
{
op = '*';
flag = 1;
LCD_Display_Char('*');
}
else if (key == 14)
{
op = '/';
flag = 1;
LCD_Display_Char('/');
}
else if (key == 15)
{
num1 = 0;
num2 = 0;
result = 0;
op = '+';
flag = 0;
LCD_Write_Command(0x01);
LCD_Write_Command(0x02);
}
}
}
```
该程序使用了4x4矩阵按键进行输入,LCD显示器进行输出。程序中定义了几个函数用于控制LCD显示器和获取按键值,并实现了加、减、乘、除四则运算。
阅读全文