|
这个是源码:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define GPIO_KEY P1
#define weixuan P2
//0~F的共阳数码管段码,最后一个是黑屏
const uchar SEG_CODE[] =
{ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF
};
sbit wei1=P2^0;
sbit wei2=P2^1;
uchar ge,shi,KeyValue,j;
uchar num;
/*****延时函数*****/
void delay1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=112;y>0;y--); //12M晶振下,延时1MS
}
/*****显示函数*****/
void xianshi(uchar k)
{
ge=k%10; //个位
shi=k/10; //十位
if(k>9)
{
weixuan=0x02;
P0=SEG_CODE[ge];//个位段选
delay1ms(10);
weixuan=0x01;
P0=SEG_CODE[shi];//十位段码,显示
delay1ms(10);
}
else
{
weixuan=0x02;
P0=SEG_CODE[ge];//个位段选
delay1ms(10);
}
}
/***键盘扫描函数***/
uchar keyscan()
{
char a=0;
GPIO_KEY=0x0f; //P1口值准备
if(GPIO_KEY!=0x0f)
{
delay1ms(10); //延时消抖
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; //高四位赋1,判断准备
switch(GPIO_KEY) //测试行
{
case(0x70):KeyValue=KeyValue;break; //0-3
case(0xb0):KeyValue=KeyValue+4;break; //4-7
case(0xd0):KeyValue=KeyValue+8;break; //8-11
case(0xe0):KeyValue=KeyValue+12;break; //12-15
}
while((GPIO_KEY!=0x0f)&&(a<10)) //判断是否松手,假若没有松手,
//一段延时后依旧判断松手完成
{
delay1ms(1);
a++;
}
a=0;
}
}
return KeyValue;
}
void count(uchar x)
{
uchar sn;
sn=x;
num=num;
if(x<10)
{
num=num*10+sn;
xianshi(num);
}
}
/*****主函数*******/
void main()
{
uchar b;
while(1)
{
b=keyscan();
count(b);
}
}
|
|