用AT89C51单片机的P2口控制8个Led的流水灯的C程序看不懂?

用AT89C51单片机的P2口控制8个Led的流水灯的C程序看不懂?
从P2-0到P2-7灯依次单独点亮后,又从P2-7到P2-0灯依次单独点亮
如此反复循环,程序如下(低电平被点亮):
#include <REGX52.H>
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}

main()
{
unsigned char LEDIndex = 0;
bit LEDDirection = 1;
while(1)
{
if(LEDDirection)
P2 = ~(0x01<<LEDIndex);
else
P2 = ~(0x80>>LEDIndex);
if(LEDIndex==7)
LEDDirection = !LEDDirection;
LEDIndex = (LEDIndex+1)%8;
Delay1ms(500);
}
}
我这程序我真的看不懂,
这个bit LEDDirection = 1;是什么意思?
从主函数开始,麻烦高手帮我解释每一句的意思以及它有用途?
越详细越好!
在下在此先谢谢了!

#include <REGX52.H>
void Delay1ms(unsigned int count) /*延时函数,延迟时间为count×1ms)*/
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}

main()
{
unsigned char LEDIndex = 0; /*声明8位变量LEDIndex,用来指示8个LED中哪个被点亮,初始值为00000000*/
bit LEDDirection = 1; /*声明位变量LEDDirection,用来指示点亮的方向,初始值为1*/
while(1) /*一直循环执行大括号里面的语句*/
{
if(LEDDirection) /*当LEDDirection=1时,0000 0001左移LEDIndex位,取反后送给P2端口,点亮一个LED*/
P2 = ~(0x01<<LEDIndex);
else /*当LEDDirection=0时,1000 0000右移LEDIndex位,取反后送给P2端口,点亮一个LED*/

P2 = ~(0x80>>LEDIndex);
if(LEDIndex==7) /*当一次循环结束时,把LEDDirection取反,下次循环时将以相反的顺序点亮*/
LEDDirection = !LEDDirection;
LEDIndex = (LEDIndex+1)%8; /*LEDIndex+1对8取余,保证LEDIndex在0~7之间*/
Delay1ms(500); /*延时500ms,即LED的点亮间隔为0.5s*/
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-07-07
#include
<REGX52.H>
void
Delay1ms(unsigned
int
count)
/*延时函数,延迟时间为count×1ms)*/
{
unsigned
int
i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}
main()
{
unsigned
char
LEDIndex
=
0;
/*声明8位变量LEDIndex,用来指示8个LED中哪个被点亮,初始值为00000000*/
bit
LEDDirection
=
1;
/*声明位变量LEDDirection,用来指示点亮的方向,初始值为1*/
while(1)
/*一直循环执行大括号里面的语句*/
{
if(LEDDirection)
/*当LEDDirection=1时,0000
0001左移LEDIndex位,取反后送给P2端口,点亮一个LED*/
P2
=
~(0x01<<LEDIndex);
else
/*当LEDDirection=0时,1000
0000右移LEDIndex位,取反后送给P2端口,点亮一个LED*/
P2
=
~(0x80>>LEDIndex);
if(LEDIndex==7)
/*当一次循环结束时,把LEDDirection取反,下次循环时将以相反的顺序点亮*/
LEDDirection
=
!LEDDirection;
LEDIndex
=
(LEDIndex+1)%8;
/*LEDIndex+1对8取余,保证LEDIndex在0~7之间*/
Delay1ms(500);
/*延时500ms,即LED的点亮间隔为0.5s*/
}
}
第2个回答  2019-08-22
1000
0000右移LEDIndex位,延迟时间为count×1ms)*/j++),点亮一个LED*/,把LEDDirection取反;*延时函数;REGX52;LEDIndex);j<
/
for(i=0;
}
main()
{
unsigned
char
LEDIndex
=
0.H>*声明位变量LEDDirection;>
P2
=
~(0x80>.5s*/<*当一次循环结束时,用来指示点亮的方向;count,下次循环时将以相反的顺序点亮*/i++)
for(j=0,即LED的点亮间隔为0;*LEDIndex+1对8取余,保证LEDIndex在0~7之间*/120;
{
if(LEDDirection)
/*延时500ms,初始值为00000000*/*当LEDDirection=1时,取反后送给P2端口;
void
Delay1ms(unsigned
int
count)
/,初始值为1*/*声明8位变量LEDIndex;
{
unsigned
int
i;
while(1)
/
/
P2
=
~(0x01<
bit
LEDDirection
=
1;
/
if(LEDIndex==7)
/*一直循环执行大括号里面的语句*/
Delay1ms(500);
LEDIndex
=
(LEDIndex+1)%8,j;
else
/,点亮一个LED*/!LEDDirection;*当LEDDirection=0时,取反后送给P2端口;
LEDDirection
=
;LEDIndex),0000
0001左移LEDIndex位;i<,用来指示8个LED中哪个被点亮;
/#include
<
第3个回答  2009-03-23
看不懂别人编写的C程序,最好的办法是自己动手编写。
第4个回答  2009-03-22
#include <REGX52.H>
void Delay1ms(unsigned int count) count毫秒的延时函数,count=1,2,3,4…………
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++); 一毫秒
}

main()
{
unsigned char LEDIndex = 0; 声明一个实数变量LEDIndex,初值为0
bit LEDDirection = 1; 声明一个位变量 LEDDirection,初值为1
while(1) 循环语句
{
if(LEDDirection) 如果LEDDirection=1,
P2 = ~(0x01<<LEDIndex); 则 P2口每隔500毫秒从P2.0---P2.7输出低电平
else 如果LEDDirection不等于1,
P2 = ~(0x80>>LEDIndex); 则P2口每隔500毫秒从P2.7---P2.0输出低电平
if(LEDIndex==7) 如果测试到LEDIndex的值为7
LEDDirection = !LEDDirection; LEDDirection取反(是1就为0,是0就为1)
LEDIndex = (LEDIndex+1)%8; LEDIndex从0开始加1,一直加到8,再从0开始加1加到8,一直循环下去
Delay1ms(500); 每加一次1,延时 500毫秒
}
}
回去多看几遍书就明白了,努力吧!!