该项目是基于51单片机开发板来实现的矩阵计算器LCD(加减乘除),该项目为正整数计算器,结果均为正整数(不进行四舍五入),可以实现多位符号同时进行判断运算(例如:判断先乘除,后加减),该计算器能实现10个数字进行加减乘除运算,并且运算结果可以到五位数(最大可达65535)。该项目是用keil5写的,所以如果有其他版本不兼容问题请自己想办法。
所用的材料:LCD1602和51单片机开发板(学习板哦!)一块
接下来为主函数
#include <REGX52.H>
#include "MatrixKey.h"
#include "LCD1602.h"
// KeyNumber = 1~9 '1~9'
// KeyNumber = 10 '0'
// KeyNumber = 11 'CLEAR'
// KeyNumber = 12 '='
// KeyNumber = 13 '+'
// KeyNumber = 14 '-'
// KeyNumber = 15 '*'
// KeyNumber = 16 '/'
unsigned int res = 0; // 结果
unsigned int arr = 0; // 记录第几个整数
unsigned int row = 1; // 记录位数
unsigned int KeyNum = 0; // 矩阵键盘的输出
unsigned int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 记录整数值
unsigned int b[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 记录加减乘除的先后
unsigned int i = 0, j = 0; // 控制循环
void main ()
{
LCD_Init();
while(1) {
KeyNum = MatrixKey ();
if (KeyNum >= 1 && KeyNum <= 9) {
LCD_ShowNum(1,row,KeyNum,1);
a[arr] = a[arr] * 10 + KeyNum;
row++;
}
if (KeyNum == 10) {
a[arr] = a[arr] * 10;
LCD_ShowNum(1,row,0,1);
row++;
}
if(KeyNum == 11)
{
(*((void(code*)(void))0x0000))();
}//软复位
if (KeyNum >= 13 && KeyNum <= 16) {
LCD_Init();
b[arr] = KeyNum;
arr++;
if (KeyNum == 13)
LCD_ShowChar(1,1,'+');
if (KeyNum == 14)
LCD_ShowChar(1,1,'-');
if (KeyNum == 15)
LCD_ShowChar(1,1,'*');
if (KeyNum == 16)
LCD_ShowChar(1,1,'/');
row=1;
}
if (KeyNum == 12) {
LCD_Init();
LCD_ShowString(1,1,"ANSWER:");
for (i = 0; i < 10; i++) {
if (b[i] == 15) {
for(j = i; j < 10; j++) {
b[j] = b[j + 1];//读取一位符号后将后面的符号向前移一位
}
a[i] = a[i] * a[i + 1];
for(j = i + 1; j < 10; j++) {
a[j] = a[j + 1];//计算后将后面的数向前移
}
if (b[i] == 15) {
i--;//防止少算了符号
}
}//乘法运算
if (b[i] == 16) {
for(j = i; j < 10; j++) {
b[j] = b[j + 1];//读取一位符号后将后面的符号向前移一位
}
a[i] = a[i] / a[i + 1];
for(j = i + 1; j < 10; j++) {
a[j] = a[j + 1];//计算后将后面的数向前移
}
i--;
}//除法运算
}
res = a[0];
for(i=0;i < 10; i++){
if(b[i] == 13)
res = res + a[i+1];
if(b[i] == 14)
res = res - a[i+1];
}//加减法运算
LCD_ShowNum(2,1,res,5);
}
}
}
下面是延时函数
#include <INTRINS.H>
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while (xms--) {
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
接下来为矩阵键盘的函数,这部分是借鉴了江协科技
// KeyNumber = 1~9 '1~9'
// KeyNumber = 10 '0'
// KeyNumber = 11 'CLEAR'
// KeyNumber = 12 '='
// KeyNumber = 13 '+'
// KeyNumber = 14 '-'
// KeyNumber = 15 '*'
// KeyNumber = 16 '/'
#include <REGX52.H>
#include "Delay.h"
unsigned char MatrixKey ()
{
// KeyNumber 为输出的数字
unsigned char KeyNumber = 0;
P1 = 0xFF; //先将p1全部置高
P1_3 = 0; //扫描第一列
if (P1_7 == 0) {Delay(20);while (P1_7 == 0);Delay(20);KeyNumber = 1;}
if (P1_6 == 0) {Delay(20);while (P1_6 == 0);Delay(20);KeyNumber = 5;}
if (P1_5 == 0) {Delay(20);while (P1_5 == 0);Delay(20);KeyNumber = 9;}
if (P1_4 == 0) {Delay(20);while (P1_4 == 0);Delay(20);KeyNumber = 13;}
P1 = 0xFF; //先将p1全部置高
P1_2 = 0; //扫描第二列
if (P1_7 == 0) {Delay(20);while (P1_7 == 0);Delay(20);KeyNumber = 2;}
if (P1_6 == 0) {Delay(20);while (P1_6 == 0);Delay(20);KeyNumber = 6;}
if (P1_5 == 0) {Delay(20);while (P1_5 == 0);Delay(20);KeyNumber = 10;}
if (P1_4 == 0) {Delay(20);while (P1_4 == 0);Delay(20);KeyNumber = 14;}
P1 = 0xFF; //先将p1全部置高
P1_1 = 0; //扫描第三列
if (P1_7 == 0) {Delay(20);while (P1_7 == 0);Delay(20);KeyNumber = 3;}
if (P1_6 == 0) {Delay(20);while (P1_6 == 0);Delay(20);KeyNumber = 7;}
if (P1_5 == 0) {Delay(20);while (P1_5 == 0);Delay(20);KeyNumber = 11;}
if (P1_4 == 0) {Delay(20);while (P1_4 == 0);Delay(20);KeyNumber = 15;}
P1 = 0xFF; //先将p1全部置高
P1_0 = 0; //扫描第四列
if (P1_7 == 0) {Delay(20);while (P1_7 == 0);Delay(20);KeyNumber = 4;}
if (P1_6 == 0) {Delay(20);while (P1_6 == 0);Delay(20);KeyNumber = 8;}
if (P1_5 == 0) {Delay(20);while (P1_5 == 0);Delay(20);KeyNumber = 12;}
if (P1_4 == 0) {Delay(20);while (P1_4 == 0);Delay(20);KeyNumber = 16;}
return KeyNumber;
}
接下来为lcd1602的函数,当然这部分要感谢江协科技给出的lcd1602调试工具
#include <REGX52.H>
//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
//函数定义:
/**
* @brief LCD1602延时函数,12MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
/**
* @brief 返回值=X的Y次方
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief 在LCD1602指定位置开始显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~65535
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
这些都为模块化写法,如果不会模块化的话还是比较困难的如果自己摸索,因此我推荐先学习了模块化写法再来详细的看此文章。
还有感谢江协科技的网课教我!
这里是江协科技51单片机B站视频的链接(可以在他那学到很多!):51单片机入门教程-2020版 程序全程纯手打 从零开始入门_哔哩哔哩_bilibili