这个代码设置的是两个按键对LED移位,分别是P3_1向右移,P3_0向左移。
#include <REGX52.H>
unsigned char LEDnum=0;
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
P2=~0x01; //定义LED初始状态
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--;
P2=~(0x01<<LEDnum); //向左移相对应的单位
}
}
}