单片机源程序如下:
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#define uchar unsigned char
#define uint unsigned int
sbit we = P2^7;
sbit du = P2^6;
sbit start=P3^0;
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
uchar code leddata[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x40, //"-"
};
uchar KeyScan() //带返回值的子函数
{
uchar cord_l,cord_h;//声明列线和行线的值的储存变量
P3 = 0x0f;//0000 1111
if( (P3 & 0x0f) != 0x0f)//判断是否有按键按下
{
delay(5);//软件消抖
if( (P3 & 0x0f) != 0x0f)//判断是否有按键按下
{
cord_h = P3 & 0x0f;// 储存行线值
P3 = cord_l | 0xf0;
cord_l = P3 & 0xf0;// 储存列线值
while( (P3 & 0xf0) != 0xf0 );//松手检测
return (cord_l + cord_h);//返回键值码
}
}
}
void main(void)
{
bit Flag;
bit flag;
while(!Flag)//执行一次就停止了 先检测到的相应 复位后有效
{
we = 1;//打开位选
P0 = 0;//八位数码管全显示
we = 0;//锁存位选
du = 1;//打开段选端
P0 = leddata[9];
while(!flag)
{
if(!start){ P0 = leddata[0];flag=1;}
}
switch( KeyScan() )
{
//第一行键值码
case 0xee: P1=0xFE;P0 = leddata[1];Flag=1; break;
case 0xde: P1=0xFD;P0 = leddata[2];Flag=1; break;
case 0xbe: P1=0xFB;P0 = leddata[3];Flag=1; break;
case 0x7e: P1=0xF7;P0 = leddata[4];Flag=1; break;
//第二行键值码
case 0xed: P1=0xef;P0 = leddata[5];Flag=1; break;
case 0xdd: P1=0xdf;P0 = leddata[6];Flag=1;; break;
case 0xbd: P1=0xbf;P0 = leddata[7];Flag=1; break;
case 0x7d: P1=0x7f;P0 = leddata[8];Flag=1; break;
}
}
while(Flag);
}
|