|
#include "reg51.h"
#define GPIO_KEY P1
typedef unsigned int u16;
typedef unsigned char u8;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
u8 KeyDown(void)//注意:只是提供思路
{
u8 KeyValueH = 0;
u8 KeyValueL = 0;
static u8 state_k;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f&&!state_k){state_k=1;return;}//消抖
if(GPIO_KEY!=0x0f)
{
KeyValueH=GPIO_KEY;
GPIO_KEY = 0xf0;
KeyValueL=GPIO_KEY;
}
GPIO_KEY=0xff;
while(GPIO_KEY!=0xff) ;//可以照著按下消抖的方法自己改
return (KeyValueH |KeyValueL);
}
u8 keyscan()
{
switch(KeyDown())
{
鍵值處理……
}
return 鍵值;
}
void timer1Init(void)
{
TMOD |= 0x10;
TH1 =( 65536-20000)/256;
TL1 =( 65536-20000)%256;
TR1 = 1;
ET1 = 1;
EA = 1;
}
void timer1() interrupt 3 using 3
{
static u16 i = 0;
static u8 key = 0;
TH1 =( 65536-20000)/256;
TL1 =( 65536-20000)%256;
GPIO_KEY = 0x0f;
if (GPIO_KEY != 0x0f)
{
key = keyscan();
}
P0 = ~smgduan[key];
}
int mian(void)
{
timer1Init();
while (1) ;
}
|
|