文章目录
- 一、功能简介
- 二、软件设计
- 三、实验现象
- 联系作者
一、功能简介
本项目使用Proteus8仿真Arduino单片机控制器,使用PCF8574、LCD1602液晶、4*4矩阵键盘等。
主要功能:
系统运行后,操作矩阵按键可实现简单四则运算。
二、软件设计
/*
作者:嗨小易(QQ:3443792007)*/#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>long x,time,timex,a,b,c;
int menu;/*定义变量*/LiquidCrystal_I2C lcd(0x27,16,2);/*定义LCD地址*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{{'1','2','3','+'},{'4','5','6','-'},{'7','8','9','*'},{'C','0','=','/'}
};
byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2}; /*初始化4*4小键盘*/Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );/*定义4*4小键盘*/
void cleanxy(int x,int y)
{for(int i=0;i<x;i++){lcd.setCursor(i,y);lcd.print(" ");}
}
void clean()
{b=0;a=0;c=0;Serial.println("clean");x=0;lcd.setCursor(0,0);lcd.print(" ");lcd.setCursor(0,1);lcd.print(" ");
}
void compute(char key)
{if(key>='0'&&key<='9'){a=a*10+(key-'0');Serial.println(a);lcd.setCursor(0,0);lcd.print(a);}if(key=='+'){b=a;a=0;Serial.println("+");x=1;lcd.setCursor(12,0);lcd.print("+");cleanxy(11,0);}if(key=='-'){b=a;a=0;Serial.println("-");x=2;lcd.setCursor(12,0);lcd.print("-");cleanxy(11,0);}if(key=='*'){b=a;a=0;Serial.println("*");x=3;lcd.setCursor(12,0);lcd.print("*");cleanxy(11,0);}if(key=='/'){b=a;a=0;Serial.println("/");x=4;lcd.setCursor(12,0);lcd.print("/");cleanxy(11,0);}if(key=='='){cleanxy(16,0);Serial.println("=");lcd.setCursor(0,1);lcd.print("=");if(x==1){Serial.println(a+b);lcd.setCursor(1,1);lcd.print(a+b);}if(x==2){Serial.println(b-a);lcd.setCursor(1,1);lcd.print(b-a);}if(x==3){Serial.println(a*b);lcd.setCursor(1,1);lcd.print(a*b);}if(x==4){Serial.println(b/a);lcd.setCursor(1,1);lcd.print(b/a);}a=0;b=0;x=0;}if(key=='C'){clean();lcd.setCursor(0,0);lcd.print("clean");delay(1000);clean();}
}
void compute2(char key)
{if(key>='0'&&key<='9'){a=a*10+(key-'0');Serial.println(a);lcd.setCursor(0,0);lcd.print(a);}if(key=='+'){b=a;a=0;Serial.println("^");x=1;lcd.setCursor(12,0);lcd.print("^");cleanxy(11,0);}if(key=='-'){b=a;a=0;Serial.println("sqrt");x=2;lcd.setCursor(0,0);lcd.print("stop input please");delay(2000);cleanxy(12,0);lcd.setCursor(12,0);lcd.print("sqrt");}if(key=='*'){b=a;a=0;Serial.println("sin");x=3;lcd.setCursor(0,0);lcd.print("stop input please");delay(2000);cleanxy(13,0);lcd.setCursor(13,0);lcd.print("sin");}if(key=='/'){b=a;a=0;Serial.println("tan");x=4;lcd.setCursor(0,0);lcd.print("stop input please");delay(2000);cleanxy(13,0);lcd.setCursor(13,0);lcd.print("tan");}if(key=='='){cleanxy(16,0);Serial.println("=");lcd.setCursor(0,1);lcd.print("=");if(x==1){Serial.println(pow(b,a));lcd.setCursor(1,1);lcd.print(pow(b,a));}if(x==2){double sum;sum=sqrt(b);Serial.println(sum);lcd.setCursor(1,1);lcd.print(sum);}if(x==3){Serial.println(sin(b));lcd.setCursor(1,1);lcd.print(sin(b));}if(x==4){Serial.println(tan(b));lcd.setCursor(1,1);lcd.print(tan(b));}a=0;b=0;x=0;}if(key=='C'){clean();lcd.setCursor(0,0);lcd.print("clean");delay(1000);clean();}
}
三、实验现象
B站演示视频:https://space.bilibili.com/444388619
联系作者
视频地址:https://space.bilibili.com/444388619/video
专注于51单片机、STM32、国产32、DSP、Proteus、arduino、ESP32、物联网软件开发,PCB设计,视频分享,技术交流。