sbit LED_1 =P1^4;
sbit LED_2 =P1^5;
sbit LED_3 =P1^6;
sbit LED_4 =P1^7;
#define KEY P2
unsigned char scan_key(); //掃描鍵盤,返回鍵值(高四位代表行,低四位代表列)
void delay(unsigned int N);
//-----------------------變量聲明---------------------------------------------------------------------
main()
{
int ge=0,j=0,shi=0,bai=0,qian=0,key_value,key_flag,key_flash_num;
char leddisplay[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
while(1)
{
key_value=scan_key();
if (key_value==0x18)//按下1鍵
{
key_flag=1;
key_flash_num=0;
}
if (key_value==0x28)//按下2鍵
key_flag=0;
if (key_value==0x48&&key_flag==1)//按下3鍵,3鍵的功能,用于增加閃爍位的值
{
ge++;
if (ge==10)
ge=0;
}
if (key_value==0x14&&key_flag==1)//按下4鍵,4鍵的功能,用于減小閃爍位的值
{
ge--;
if (ge==0)
ge=0;
}
if (key_flag==0)
{
if (j==100)
{
ge++;
if(ge==10)
{
ge=0;
shi++;
if(shi==6)
{
shi=0;
bai++;
if (bai==10)
{
bai=0;
qian++;
if (qian==6)
{
ge=0;
shi=0;
bai=0;
qian=0;
}
}
}
}
j=0;
}
j++;
}
if (key_flag==0)
{
LED_1=0;
P0=leddisplay[ge];
delay(100);
LED_1=1;
}
else
{
if (key_flash_num==8)
{
LED_1=0;
P0=leddisplay[ge];
delay(100);
LED_1=1;
key_flash_num=0;
}
}
key_flash_num++;
LED_2=0;
P0=leddisplay[shi];
delay(100);
LED_2=1;
LED_3=0;
P0=leddisplay[bai];
delay(100);
LED_3=1;
LED_4=0;
P0=leddisplay[qian];
delay(100);
LED_4=1;
}
}
/*A---88H 3---48H 2---28H 1---18H
B---84H 6---44H 5---24H 4---14H
C---82H 9---42H 8---22H 7---12H
D---81H E---41H 0---21H F---11H
*/
//--------------------------------------------------------------------------------------------------
// 函數名稱: scan_key
// 函數功能: //掃描鍵盤,返回鍵值(高四位代表行,低四位代表列)
//--------------------------------------------------------------------------------------------------
unsigned char scan_key() //掃描鍵盤,返回鍵值(高四位代表行,低四位代表列)
{
unsigned char scancode,keycode,keycode_nd;
scancode=0xef; //鍵盤掃描碼,采用逐行掃描的方法
while(scancode!=0xff)
{
KEY=scancode; //輸入掃描碼,掃描P2.4對應的行
keycode=KEY; //讀出數據,看是否在此行上的某列鍵盤被按下
if((keycode&0x0f)!=0x0f)
{
while (((keycode_nd=KEY)&0x0f)!=0x0f);
break; //掃描到按下的鍵,則退出
}
scancode=(keycode<<1)|0x0f; //否則,更新掃描碼繼續掃描
}
keycode=~keycode;
return(keycode);
}
void delay(unsigned int N)
{
int i;
for (i=0;i }