hex文件及其工程文件下载:http://www.51hei.com/f/pingfc.rar 下面是程序源码:
/*
*功能:使用矩阵按键使得按键按下时数码管上显示各自对应的数字的立方数
* 优化显示,只显示有效位;
* 且使用独立按键实现key1清零,key2加数,key3减数;
*日期:2013-05-07-10:20
*作者:徐冉
*特别说明:本程序代码已经通过调试,仅供学习使用;
*
*/
/***********AT89C52-RC 单片机-51hei实验板***********/
/*****************51hei开发板*********************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
sbit key1 = P3^4;
sbit key2 = P3^5;
sbit key3 = P3^6;
sbit fm = P2^3;
uint temp;
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
// uchar key; Error!!! 此时的key值大于258 超出了uchar 故错误!!
uint key;
void display(num);
void delay(uint xms);
void max_kanscan();
void keyscan();
void main(void)
{
while(1)
{
keyscan();
max_kanscan();
display(key);
}
}
void display(num)
{
uchar qian, bai, shi, ge;
qian = num / 1000 % 10;
bai = num / 100 % 10;
shi = num / 10 % 10;
ge = num % 10;
if(num < 10)
{
dula = 1;
P0 =table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
}
else if(num < 100)
{
dula = 1;
P0 =table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 =table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(1);
}
else if(num < 1000)
{
dula = 1;
P0 =table[bai];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 =table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 =table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
P0 = 0x00;
delay(1);
}
else if(num < 10000)
{
dula = 1;
P0 =table[qian];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 =table[bai];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 =table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 =table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xf7;
wela = 0;
P0 = 0x00;
delay(1);
}
}
[page]
void delay(uint xms)
{
uint i, j;
for(i = xms; i > 0; i--)
for(j = 125; j > 0; j--);
}
void keyscan()
{
if(key1 == 0)
{
delay(5);
if(key1 == 0)
{
fm = 0;
key = 0;
while(!key1);
delay(5);
while(!key1);
fm = 1;
}
}
if(key2 == 0)
{
delay(5);
if(key2 == 0)
{
fm = 0;
key++;
while(!key2);
delay(5);
while(!key2);
fm = 1;
}
}
if(key3 == 0)
{
delay(5);
if(key3 == 0)
{
fm = 0;
key--;
while(!key3);
delay(5);
while(!key3);
fm = 1;
}
}
}
void max_kanscan()
{
P3 = 0xfe;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{ //按键检测
temp = P3;
fm = 0;
switch(temp)
{
case 0xee: key = 1 * 1 *1;
break;
case 0xde: key = 2 * 2 *2;
break;
case 0xbe: key = 3 * 3 *3;
break;
case 0x7e: key = 4 * 4 *4;
break;
default:
break;
}
//按键释放
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
fm = 1;
}
}
P3 = 0xfd;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{ //按键检测
temp = P3;
fm = 0;
switch(temp)
{
case 0xed: key = 5 * 5 *5;
break;
case 0xdd: key = 6 * 6 *6;
break;
case 0xbd: key = 7 * 7 *7;
break;
case 0x7d: key = 8 * 8 *8;
break;
default:
break;
}
//按键释放
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
fm = 1;
}
}
P3 = 0xfb;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{ //按键检测
temp = P3;
fm = 0;
switch(temp)
{
case 0xeb: key = 9 * 9 *9;
break;
case 0xdb: key = 10 * 10 *10;
break;
case 0xbb: key = 11 * 11 *11;
break;
case 0x7b: key = 12 * 12 *12;
break;
default:
break;
}
//按键释放
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
fm = 1;
}
}
P3 = 0xf7;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{ //按键检测
temp = P3;
fm = 0;
switch(temp)
{
case 0xe7: key = 13 * 13 *13;
break;
case 0xd7: key = 14 * 14 *14;
break;
case 0xb7: key = 15 * 15 *15;
break;
case 0x77: key = 16 * 16 *16;
break;
default:
break;
}
//按键释放
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
fm = 1;
}
}
}
上一篇:51单片机矩阵键盘与左右流水灯控制C程序
下一篇:单片机I2C通信及LCD1602显示C程序
推荐阅读最新更新时间:2024-03-16 14:00