|
# include "reg52.h"
sbit GK1=P3^0;
sbit GK2=P3^1;
sbit GK3=P3^2;
sbit GK4=P3^3;
static unsigned char keyvalue = 0xFF;
static unsigned char keystatus = 0;
void delay(unsigned int i)
{
while(i--);
}
void keyScan(void)
{
static unsigned char counter = 0;
static unsigned char keytmp = 0;
switch(keystatus)
{
case 0:
if((GK1 == 0) || (GK2 == 0) || (GK3 == 0) || (GK4 == 0))
{
keystatus = 1;
}
break;
case 1:
if(GK1 == 0)
{
keytmp = 1;
keystatus = 2;
}
else if(GK2 == 0){
keytmp = 2;
keystatus = 2;
}
else if(GK3 == 0){
keytmp = 3;
keystatus = 2;
}
else if(GK4 == 0){
keytmp = 4;
keystatus = 2;
}
else
{
keystatus = 0;
}
break;
case 2:
if((GK1 == 1) && (GK2 == 1) && (GK3 == 1) && (GK4 == 1))
{
if(counter < 40)
{
counter = 0;
keystatus = 0;
keyvalue = keytmp;
}
else
{
counter = 0;
keystatus = 0;
keyvalue = 4 + keytmp ; // keyval 5-8
}
}
else
{
if(counter < 40)
{
++counter;
}
}
break;
}
}
void taskProcessKey(void)
{
unsigned char value = keyvalue;
if(value != 255)
{
switch(keyvalue)
{
case 1:P2=0x00; break;
case 5:P2=0xab; break;
default:break;
}
}
}
int main(void)
{
while(1)
{
keyScan();
delay(2000);
taskProcessKey();
}
}
這個(gè)是我控制單片機(jī)上長(zhǎng)按和短按的一個(gè)程序,希望對(duì)你有用。
|
|