stc89c52rc单片机左右跑马灯程序问题求解
时间:10-02
整理:3721RD
点击:
我想问一下下边这个程序
main ()
{
uint i;
while(1)
{
LED = 0xfe;
for (i = 0;i<8;i++)
{
P1 = LED;
delay (600);
LED = LED <<1;
LED = LED|0X01;
if (LED == 0x7f) break;
}
for (i = 0;i<8;i++)
{
P1 = LED;
delay (600);
LED = LED >> 1;
LED = LED |0x80;
}
}
}
可以正常运行,但是改成
main ()
{
uint i;
while(1)
{
LED = 0xfe;
for (i = 8;i>0;i--)
{
P1 = LED;
delay (600);
LED = LED <<1;
LED = LED|0X01;
if (LED == 0x7f) break;
}
for (i = 8;i>0;i--)
{
P1 = LED;
delay (600);
LED = LED >> 1;
LED = LED |0x80;
}
}
}
就只能正序跑马。求大师讲解
main ()
{
uint i;
while(1)
{
LED = 0xfe;
for (i = 0;i<8;i++)
{
P1 = LED;
delay (600);
LED = LED <<1;
LED = LED|0X01;
if (LED == 0x7f) break;
}
for (i = 0;i<8;i++)
{
P1 = LED;
delay (600);
LED = LED >> 1;
LED = LED |0x80;
}
}
}
可以正常运行,但是改成
main ()
{
uint i;
while(1)
{
LED = 0xfe;
for (i = 8;i>0;i--)
{
P1 = LED;
delay (600);
LED = LED <<1;
LED = LED|0X01;
if (LED == 0x7f) break;
}
for (i = 8;i>0;i--)
{
P1 = LED;
delay (600);
LED = LED >> 1;
LED = LED |0x80;
}
}
}
就只能正序跑马。求大师讲解
把 for (i = 8;i>0;i--)改成 for (i = 8;i>=0;i--)就可以了,求解
不应该啊,改成i>=0才不对的,因为i是无符号整型的,期待高手来解答
这个循环的中止不是靠 i,而是靠if (LED == 0x7f) break;
我是没看出来哪里有什么不同。
第二个for语句
P1 = LED;
delay (600);
LED = LED >> 1;
LED = LED |0x80;
你这段程序写错了,把第一句改成P1=0x7f;这就对了
你让P1=0xfe;当它右移一位成为0x7f,你又或上0x80,就变成0xff,灯肯定不亮啊。
施主,你是不是在这闲的没事乱发帖啊,这个程序是没错的,我都试过了