qzc13457 发表于 2013-8-17 11:46 
这不难实现吧,如楼上思路,加去抖,还有你的按键自复位吗?自复位的要定义变量储存状态值。 ...
您看看这个
#include<reg52.h>
sbit KEY1=P3^0;
sbit KEY2=P3^1;
sbit LED2=P1^1;
void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
void main (void)
{
KEY1=1;
KEY2=1;
while (1)
{
if(!KEY1)//判断按键1是否按下
{
DelayMs(10);
if(!KEY1)//按键1延时去抖
while(1)
{if(!KEY2)
{DelayMs(10);
if(!KEY2)
LED2=0;
break;
}
}
}
while (1)
{if(LED2==0)
while(1)
{if(!KEY2)
{DelayMs(10);
if(!KEY2)
while(1)
{if(!KEY2)
{DelayMs(10);
if(!KEY2)
LED2=0;
break;
}
}
}
}
}
}
}
/*------------------------------------------------
uS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
长度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
|