单片机:谁有C语言双灯点亮程序呀?

就是有两个灯同时亮的流水灯程序
为什么第一个用不得啊?
第二个也不能同时有两个灯亮,只有一个灯来回亮?
能再帮我解释一下吗?
可能是我不会弄,我按照大家给的程序原样放到keil里生成.hex文件,然后再放到单片机里,但除了第一个回答 里的第二个程序可以一个灯来回亮外,其他程序灯都不亮,?不知是为什么...
我的八个灯是从AT89s52的p2口连出来的

你是说这样? 如果要单向流动就删掉一个for循环。

#include <reg52.h> 

void Delay(unsigned int j) 

{ unsigned int i,k; 

for(i=0;i<=j;i++) 

for(k=0;k<800;k++) ; 

void main() 

unsigned char i,j,MOVE; 

while(1) 

MOVE=0x03; 

for(i=0;i<8;i++) 

P1=~MOVE; 

Delay(80); 

MOVE<<=1; 

MOVE=0xC0; 

for(j=0;j<8;j++) 

P1=~MOVE; 

Delay(80); 

MOVE>>=1; 

}; 

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-04
这个太简单了,告诉楼主,只要改变数组里的内容就可以了……
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar table[]={
0Xfc,0Xf9,0Xf3,0Xe7,
0Xcf,0X9f,0X3f, 0X7e
};
uint temp;
void delay(uint z);
void main()
{
uint num;
while(1)
{
for(num=0;num<8;num++)
{
temp=table[num];
temp=~temp;
P0=temp;
delay(3000);
}
}

}
void delay(uint z)
{ uint x,y;
for(x=300;x>0;x--)
for(y=z;y>0;y--);
}
看得懂吧!
还有这个,更好看点:#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);
}
}
一个一个来回流动!
第2个回答  2009-04-05
我的是接P1口的,换成P2就行了试试吧
低电平点亮
#include<reg51.h>
void main(void)
{
unsigned int t,i;
long int y;//改变值改变时间
P2=0xfc;
while(1)
{
t=P2>>6;
i=P2<<2;
P2=t|i;
for(y=0;y<40000;y++);
}
}本回答被提问者采纳
相似回答