|
這個是源碼:
#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);
}
}
|
|