程序
#include <REGX52.H>
void main()
{
while(1)
{
if(P3_1 == 0) //DOWN
{
P2_0 = 0xFE;
}
if(P3_1 == 1) //UP
{
P2_0 = 0XFF;
}
};
}
按键的抖动问题
软件实现解决-按键的抖动问题
通过延时解决
#include <REGX52.H>
void Delay(unsigned int xms)
{
while(xms)
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
};
}
void main()
{
while(1)
{
if(P3_1 == 0) //down
{
Delay(20);
while(P3_1 == 0); //check down
Delay(20);
P2_0 = ~P2_0; //change
}
};
}
按键控制LED左移右移示例
#include <REGX52.H>
void Delay(unsigned int xms)
{
while(xms)
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
};
}
unsigned char LedNum = 0;
void main()
{
P2 = ~0x01;
while(1)
{
if(P3_1 == 0)
{
Delay(20);
while(P3_1 == 0);
Delay(20);
LedNum++;
if(LedNum >= 8)
{
LedNum = 0;
}
P2 = ~(0x01 << LedNum);
}
if(P3_0 == 0)
{
Delay(20);
while(P3_0 == 0);
Delay(20);
if(LedNum == 0)
LedNum = 7;
else
LedNum--;
if(LedNum >= 8)
{
LedNum = 0;
}
P2 = ~(0x01 << LedNum);
}
};
}