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

来源:百度知道 编辑:UC知道 时间:2024/05/06 01:50:08
用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*/
}
}

#include <REGX52.H>
void Delay1ms(unsigned int count) count毫秒的延时函数,count=1,2,3,4…………
{
unsigned int i,