为什么按下key3键,松开手时不能使LED3亮。
#include<reg51.h>
#include<intrins.h>
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit key4=P3^5;
sbit LED1=P1^0;
sbit LED2=P1^1;
sbit LED3=P1^2;
sbit LED4=P1^3;
void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
//函数声明;
#define uchar unsigned char
#define uint unsigned int
void main()
{
key1=key2=key3=key4=1;
while(1)
{
if(!key1)
{
DelayMs(10);//消抖
while(!key1)
{
LED1=0;
// DelayMs(10);//消抖
while(key1)
{
LED1=1;
}
}
}
if(!key2)
{
DelayMs(10);
while(!key2)
{
LED2=0;
while(key2)
{
LED2=1;
}
}
}
if(!key3)
{
DelayMs(10);
if(!key3)
{
while(!key3)
{
LED3=!LED3;
}
}
}
}
}
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
我打算控制LED3灯按一下熄灭,再按一下亮起。
图片已上传