/*************************
矩 阵 按 键
0 1 2 3
4 5 6 7
8 9 + -
/ * ~ =
*************************/
#include "reg51.h"
typedef unsigned char u8; //对数据类型进行声明定义
typedef unsigned int u16;
sbit A0=P2^2; //138译码器数码管位选
sbit A1=P2^3;
sbit A2=P2^4;
#define GPIO_KEY P1
#define GPIO_DIG P0
u16 KeyValue; //用来存放读取到的键值
u16 keyflag,i; //用来判断按下的数字还是运算符或清空键
u8 code smgduan[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40}; //共阳极 显示0~F、负号‘-’
u16 tude[8]= {0}; //用来存放每一位数码管数字的数组
void delay(u16 i)//延时函数
{
while(i--);
}
void display() //扫描显示动态数码管
{
A0=0; A1=0; A2=0; GPIO_DIG=smgduan[tude[0]]; delay(50); GPIO_DIG=0x00; //消隐
A0=1; A1=0; A2=0; GPIO_DIG=smgduan[tude[1]]; delay(50); GPIO_DIG=0x00;
A0=0; A1=1; A2=0; GPIO_DIG=smgduan[tude[2]]; delay(50); GPIO_DIG=0x00;
A0=1; A1=1; A2=0; GPIO_DIG=smgduan[tude[3]]; delay(50); GPIO_DIG=0x00;
A0=0; A1=0; A2=1; GPIO_DIG=smgduan[tude[4]]; delay(50); GPIO_DIG=0x00;
A0=1; A1=0; A2=1; GPIO_DIG=smgduan[tude[5]]; delay(50); GPIO_DIG=0x00;
A0=0; A1=1; A2=1; GPIO_DIG=smgduan[tude[6]]; delay(50); GPIO_DIG=0x00;
A0=1; A1=1; A2=1; GPIO_DIG=smgduan[tude[7]]; delay(50); GPIO_DIG=0x00;
}
void KeyDown()//检测有按键按下并读取键值
{
u16 a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//读取按键是否按下
{
delay(1000);//延时10ms进行消抖
if(GPIO_KEY!=0x0f)//再次检测键盘是否按下
{
/** 行列扫描法**/
//测试列
GPIO_KEY=0x0f;
switch(GPIO_KEY)
{
case(0X07): KeyValue=0; break;
case(0X0b): KeyValue=1; break;
case(0X0d): KeyValue=2; break;
case(0X0e): KeyValue=3; break;
}
//测试行
GPIO_KEY=0xf0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue; break;
case(0Xb0): KeyValue=KeyValue+4; break;
case(0Xd0): KeyValue=KeyValue+8; break;
case(0Xe0): KeyValue=KeyValue+12; break;
}
if(KeyValue==0 || KeyValue==1 || KeyValue==2 || KeyValue==3 || KeyValue==4 || KeyValue==5 || KeyValue==6 || KeyValue==7 || KeyValue==8 || KeyValue==9)
{
keyflag=1;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //检测按键松手检测
{
delay(1000);
a++;
}
}
}
}
void main()
{
u16 a=0,b=0,c=0;
while(1)
{
display(); /* 第一个数字输入*/
KeyDown();
if(keyflag==1)
{
for(i=7; i>0; i--) //输入一位,数字向左移动一位
{
tude[i]=tude[i-1];
}
tude[0]=KeyValue;
keyflag=0;
}
/**** 对数码管清零 ****/
else if(KeyValue==14)
{
for(i=0; i<8; i++)
tude[i]=0;
display();
}
/**** 加法运算 ****/
else if(KeyValue==10)
{
a=tude[0]+tude[1]*10+tude[2]*100+tude[3]*1000+tude[4]*10000+tude[5]*100000+tude[6]*1000000+tude[7]*10000000; //计算a的值
for(i=0; i<8; i++)
tude[i]=0;
while(1) //输入第二个数
{
display();
KeyDown();
if(KeyValue==15) break;//当读到等于号,既,KeyValue=15时,停止输入
if(keyflag==1)
{
for(i=7; i>0; i--)
{
tude[i]=tude[i-1];
}
tude[0]=KeyValue;
keyflag=0;
}
}
b=tude[0]+tude[1]*10+tude[2]*100+tude[3]*1000+tude[4]*10000+tude[5]*100000+tude[6]*1000000+tude[7]*10000000; //计算b的值
c=a+b;
tude[0]=c%10; //计算C的各个位的数字
tude[1]=c/10%10;
tude[2]=c/100%10;
tude[3]=c/1000%10;
tude[4]=c/10000%10;
tude[5]=c/100000%10;
tude[6]=c/1000000%10;
tude[7]=c/10000000%10;
display();
}
/**** 减法运算 ****/
else if(KeyValue==11)
{
a=tude[0]+tude[1]*10+tude[2]*100+tude[3]*1000+tude[4]*10000+tude[5]*100000+tude[6]*1000000+tude[7]*10000000;
for(i=0; i<8; i++)
tude[i]=0;
while(1)
{
//输入第二个数
display();
KeyDown();
if(KeyValue==15) break;
if(keyflag==1)
{
for(i=7; i>0; i--)
{
tude[i]=tude[i-1];
}
tude[0]=KeyValue;
keyflag=0;
}
}
b=tude[0]+tude[1]*10+tude[2]*100+tude[3]*1000+tude[4]*10000+tude[5]*100000+tude[6]*1000000+tude[7]*10000000; //计算b的值
if(a>=b)
{
c=a-b;
tude[0]=c%10; //计算C的各个位的数字
tude[1]=c/10%10;
tude[2]=c/100%10;
tude[3]=c/1000%10;
tude[4]=c/10000%10;
tude[5]=c/100000%10;
tude[6]=c/1000000%10;
tude[7]=c/10000000%10;
}
if(a<b)//相减得负数
{
u16 e=0;//用于判断负号的位置
c=b-a;
tude[0]=c%10; //计算C的各个位的数字
tude[1]=c/10%10;
if(tude[1]==0)
{
tude[1]=16; //
e=1;
}
tude[2]=c/100%10;
if(tude[2]==0 && e==0)
{
tude[2]=16;
e=1;
}
tude[3]=c/1000%10;
if(tude[3]==0 && e==0)
{
tude[3]=16;
e=1;
}
tude[4]=c/10000%10;
if(tude[4]==0 && e==0)
{
tude[4]=16;
e=1;
}
tude[5]=c/100000%10;
if(tude[5]==0 && e==0)
{
tude[5]=16;
e=1;
}
tude[6]=c/1000000%10;
if(tude[6]==0 && e==0)
{
tude[6]=16;
e=1;
}
tude[7]=c/10000000%10;
if(tude[7]==0 && e==0)
{
tude[7]=16;
e=1;
}
}
display();
}
/**** 除法运算 ****/
else if(KeyValue==12)
{
a=tude[0]+tude[1]*10+tude[2]*100+tude[3]*1000+tude[4]*10000+tude[5]*100000+tude[6]*1000000+tude[7]*10000000;
for(i=0; i<8; i++)
tude[i]=0;
while(1)
{
//输入第二个数
display();
KeyDown();
if(KeyValue==15) break;
if(keyflag==1)
{
for(i=7; i>0; i--)
{
tude[i]=tude[i-1];
}
tude[0]=KeyValue;
keyflag=0;
}
}
b=tude[0]+tude[1]*10+tude[2]*100+tude[3]*1000+tude[4]*10000+tude[5]*100000+tude[6]*1000000+tude[7]*1
基于STC98C51/52的矩阵按键计算器
需积分: 5 73 浏览量
2021-12-24
02:03:17
上传
评论 1
收藏 62KB RAR 举报
Major_vG
- 粉丝: 1
- 资源: 1
最新资源
- 使用 C 语言实现的计算非负整数的阶乘
- 2011-2021最新版本北京大学数字普惠金融指数(PKU-DFIIC).xlsx
- 县域数字乡村指数2018-2020(1).xlsx
- Docker容器配置进阶
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- 李慧琴C语言基础部分.zip
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0