#include <reg52.h>
#include <intrins.h>
sbit datacs = P2 ^ 6;
sbit chipcs = P2 ^ 7;
char i = 100;
unsigned char j, k;
sbit row1 = P2 ^ 5; //行端口定义
sbit row2 = P2 ^ 4;
sbit row3 = P2 ^ 3;
sbit row4 = P2 ^ 2;
sbit col1 = P1 ^ 7; //列端口定义
sbit col2 = P1 ^ 6;
sbit col3 = P2 ^ 0;
sbit col4 = P2 ^ 1;
long x = 0, y = 0, z = 0; //z=x+y or z=x-y
unsigned char oper;//+-*/
bit flag = 0;
unsigned char code table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
}; //共阴数码管显示段码0~9
void add()
{
z = x + y;
}
void sub()
{
z = x - y;
}
long pw(unsigned char n,unsigned char m)
{
long x=1;
while(m--)
{
x*=n;
}
return x;
}
void display(long num)
{
unsigned char l = 0x7f;
unsigned char n = 0;
unsigned char temp;
long x = 0;
/*
如何判断数字位数?
*/
if(num == 0)
{
n = 1;
}
else
{
x = num;
while (x)
{
x = x / 10;
n++;
}
}
for (i = n - 1; i >= 0; i--)
{
long p = pw(10, i + 1);
long q = pw(10 , i);
temp = num % p / q;
P0 = table[temp];
//P1=0xff;
datacs = 1;
datacs = 0;
P0 = _cror_(l, i);
chipcs = 1;
chipcs = 0;
}
}
void display_num()
{
if (flag)
{
display(z);//当摁下了等于号,显示Z值
}
else
{
if (!oper)
{
display(x);//当没有摁下等于号,也没摁下运算符,显示x
}
else
{
display(y);//当没摁下等于号,但是摁下了运算符,显示y
}
}
}
/*数码管显示函数*/
/*
例:num = 123
bai = num % 1000 / 100 = 1;
shi = num % 100 / 10 = 2;
ge = num % 10 = 3;
例2:num = 1234;
qian = num % 10000/ 1000 = 1;
bai = num % 1000 / 100 = 2;
shi = num % 100 / 10 = 3;
ge = num % 10 / 1= 4;
例3:num = 12345;
wan = num % 100000/ 10000 = 1;
qian = num % 10000 / 1000 = 2;
bai = num % 1000 / 100 = 3;
shi = num % 100 / 10 = 4;
ge = num % 10 / 1= 5;
*/
void fuzhi(unsigned char num)
{
//判断条件是什么呢?
//是否有操作符,也就是oper是否为0
//如果oper为0,则没有摁下操作符,则将值赋给x
//如果oper不为0,则摁下操作符,将值赋给x
if (!oper)
{
//为什么是x*10+7
//第一次摁7键,x=7
//第二次摁7键,x=77,即7*10+7
//第三次摁7键,x=777,即77*10+7
x = x * 10 + num;//
}
else
{
y = y * 10 + num;
}
while ((P2 & 0x3c) != 0x3c);
}
/*按键扫描函数*/
void keyScan()
{
unsigned char temp;
//P3=0x00;
col1 = 0; col2 = 1; col3 = 1; col4 = 1;
temp = P2 & 0x3f;
if (col1 == 0)
{
//假设第一列有按键
if (temp != 0x3f)
{
//没有按键退出这次对第一列键盘扫描
temp = P2 & 0x3C;
switch (temp)
{
case 0x38:
fuzhi(0);
break; //’0’键被按下时导通,则对应端口变为低电平
case 0x34:
fuzhi(1);
return;
break;
case 0x2C:
fuzhi(4);
return;
break;
case 0x1C:
fuzhi(7);
return;
break;
}
}
}
col1 = 1; col2 = 0; col3 = 1; col4 = 1;
temp = P2;
temp = temp & 0x3f;
if (col2 == 0)
{
if (temp != 0x3f)
{
temp = P2 & 0x3C;
switch (temp)
{
case 0x38:
return;
break;
case 0x34:
fuzhi(2);
return;
break;
case 0x2C:
fuzhi(5);
return;
break;
case 0x1C:
fuzhi(8);
return;
break;
}
}
}
col1 = 1; col2 = 1; col3 = 0; col4 = 1;
temp = P2;
temp = temp & 0x3f;
if (temp != 0x3f)
{
temp = P2 & 0x3C;
switch (temp)
{
case 0x38:
//为什么用switch?
//判断是加号还是减号
switch (oper)
{
case 1:
add();
return;
break;
case 2:
sub();
return;
break;
default:
return;
break;
}
break; //’0’键被按下时导通,则对应端口变为低电平
case 0x34:
fuzhi(3);
return;
break;
case 0x2C:
fuzhi(6);
return;
break;
case 0x1C:
fuzhi(9);
return;
break;
}
}
col1 = 1; col2 = 1; col3 = 1; col4 = 0;
temp = P2;
temp = temp & 0x3f;
if (temp != 0x3f)
{
temp = P2 & 0x3C;
switch (temp)
{
case 0x38:
oper = 1;//操作符为+
return;
break; //’0’键被按下时导通,则对应端口变为低电平
case 0x34:
oper = 2;
return;
break;
case 0x2C:
oper = 3;
return;
break;
case 0x1C:
return;
break;
}
}
}
void main()
{
datacs = 0;
chipcs = 0;
// P1=0xff;
while (1)
{
P3 = 0xff;
keyScan();
display_num();
}
}
|