#include "reg52.h"
#include<intrins.h> //此文件中定義了單片機的一些特殊功能寄存器
#define led P2
#define uchar unsigned char
typedef unsigned int u16; //對數據類型進行聲明定義
typedef unsigned char u8;
#define uint unsigned int
uchar table[]={0x7e,0xbd,0xdb,0xe7};
#define GPIO_KEY P1
u8 KeyValue;
void delay_ms(uint z)
{
uint i,j;
for(i=0;i<z;i++)
for(j=0;j<110;j++);
}
void delay(u16 i)
{
while(i--);
} //用來存放讀取到的鍵值
void KEY_Scan(void)
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//讀取按鍵是否按下
{
delay(1000);//延時10ms進行消抖
if(GPIO_KEY!=0x0f)//再次檢測鍵盤是否按下
{
//測試列
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=1;break;
case(0X0b): KeyValue=2;break;
case(0X0d): KeyValue=3;break;
case(0X0e): KeyValue=4;break;
}
//測試行
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
{
delay(1000);
a++;
}
}
}
}
void liu()
{ u16 i;
if(KeyValue==1)
{
led =0xfe;
for(i=0;i<7;i++) //從左至右亮,每次一盞
{ delay(50000); //大約延時450ms
led=_crol_(led,1);
Int0Init();
}
}
if(KeyValue==2)
{
KeyValue=0;
led=0x7f;
for(i=0;i<7;i++) //將led右移一位
{ delay(50000); //大約延時450ms
led=_cror_(led,1);
}
delay(50000);
led=0xff;
}
}
void time0init()
{
TMOD=0X01; //定時器0工作方式
TH0=0XFc;
TL0=0X18;
TR0=1;
ET0=1;
EA=1;
}
void Int0Init()
{
//設置INT0
IT0=1;//跳變沿出發方式(下降沿)
EX0=1;//打開INT0的中斷允許。
EA=1;//打開總中斷
}
void Int0() interrupt 0 //外部中斷1的中斷函數
{
KEY_Scan();
if(KeyValue==3)
{
led=0x00;
KeyValue=0;
}
}
void main()
{
time0init();
while(1);
}
void tim0() interrupt 1
{
TH0=0XFc;
TL0=0X18;
KEY_Scan();
liu();
}
|