keil+proteus 制作计算器_【Proteus】单片机配合矩阵键盘LCD1602制作简易计算器的代码
时间: 2024-01-12 22:02:29 浏览: 159
以下是一个基于 Keil 和 Proteus 的简易计算器的示例代码。这个代码实现了基本的加、减、乘、除四则运算和清除功能。你可以根据需要进行修改和扩展。
```
#include <reg52.h>
#include <intrins.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_E = P2^7;
sbit Key_Receive = P3^2;
sbit Key_Send = P3^3;
unsigned char Key_Value, Key_Recognized;
unsigned char Key_Waiting;
unsigned char Key_Buffer[16];
unsigned char Key_Buffer_Index;
void Delay_1ms(unsigned int t) {
unsigned int i;
while (t--) {
for (i = 0; i < 125; i++);
}
}
void Delay_10us(unsigned int t) {
while (t--) {
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void LCD1602_Write_Command(unsigned char command) {
LCD1602_DB = command;
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_E = 1;
Delay_10us(1);
LCD1602_E = 0;
Delay_1ms(2);
}
void LCD1602_Write_Data(unsigned char data) {
LCD1602_DB = data;
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_E = 1;
Delay_10us(1);
LCD1602_E = 0;
Delay_1ms(2);
}
void LCD1602_Init() {
LCD1602_Write_Command(0x38);
LCD1602_Write_Command(0x0c);
LCD1602_Write_Command(0x06);
LCD1602_Write_Command(0x01);
}
void LCD1602_Clear() {
LCD1602_Write_Command(0x01);
}
void LCD1602_Display(unsigned char *str) {
while (*str) {
LCD1602_Write_Data(*str++);
}
}
void Key_Init() {
Key_Receive = 1;
Key_Send = 1;
Key_Waiting = 0;
Key_Buffer_Index = 0;
}
void Key_Send_Command() {
Key_Send = 0;
Delay_10us(10);
Key_Send = 1;
}
void Key_Scan() {
unsigned char i;
Key_Value = 0xff;
Key_Recognized = 0;
for (i = 0; i < 4; i++) {
Key_Send_Command();
if (!Key_Receive) {
Delay_10us(100);
if (!Key_Receive) {
Key_Value = i * 4 + 1;
break;
}
}
Key_Send_Command();
if (!Key_Receive) {
Delay_10us(100);
if (!Key_Receive) {
Key_Value = i * 4 + 2;
break;
}
}
Key_Send_Command();
if (!Key_Receive) {
Delay_10us(100);
if (!Key_Receive) {
Key_Value = i * 4 + 3;
break;
}
}
Key_Send_Command();
if (!Key_Receive) {
Delay_10us(100);
if (!Key_Receive) {
Key_Value = i * 4 + 4;
break;
}
}
}
if (Key_Value != 0xff) {
Key_Buffer[Key_Buffer_Index++] = Key_Value;
Key_Recognized = 1;
}
Key_Waiting = 0;
}
void Key_Wait() {
while (!Key_Recognized);
Key_Recognized = 0;
Key_Waiting = 1;
}
unsigned char Key_Get_Value() {
unsigned char i, value = 0;
for (i = 0; i < Key_Buffer_Index; i++) {
value = value * 10 + Key_Buffer[i];
}
return value;
}
void Key_Clear_Buffer() {
Key_Buffer_Index = 0;
}
void main() {
unsigned char n1, n2, op;
unsigned int result;
LCD1602_Init();
Key_Init();
while (1) {
LCD1602_Clear();
LCD1602_Display("Input n1:");
Key_Clear_Buffer();
while (!Key_Waiting) {
Key_Scan();
}
n1 = Key_Get_Value();
LCD1602_Clear();
LCD1602_Display("Input n2:");
Key_Clear_Buffer();
while (!Key_Waiting) {
Key_Scan();
}
n2 = Key_Get_Value();
LCD1602_Clear();
LCD1602_Display("Input op:");
Key_Clear_Buffer();
while (!Key_Waiting) {
Key_Scan();
}
op = Key_Buffer[0];
switch (op) {
case 1:
result = n1 + n2;
break;
case 2:
result = n1 - n2;
break;
case 3:
result = n1 * n2;
break;
case 4:
result = n1 / n2;
break;
default:
result = 0;
break;
}
LCD1602_Clear();
LCD1602_Display("Result:");
LCD1602_Write_Data(result / 10000 + '0');
LCD1602_Write_Data(result % 10000 / 1000 + '0');
LCD1602_Write_Data(result % 1000 / 100 + '0');
LCD1602_Write_Data(result % 100 / 10 + '0');
LCD1602_Write_Data(result % 10 + '0');
Delay_1ms(5000);
}
}
```
注意:以上代码只是示例,可能需要根据具体的硬件和需求进行修改。请仔细阅读代码并进行测试。
阅读全文