#include 'reg52.h'
typedef unsigned char u8;
typedef unsigned int u16;
sbit ledD1=P2^0; //P20
sbit ledD2=P2^1;
sbit ledD3=P2^2;
sbit ledD4=P2^3;
sbit ledD5=P2^4;
sbit ledD6=P2^5;
sbit ledD7=P2^6;
sbit ledD8=P2^7;//P27
void delay(u16 i) //延时函数
{
while(i--)
{
//什么也不做
};
}
void main()
{
while(1)
{
ledD1=0;
delay(60000);
ledD1=1;
delay(60000);
ledD2=0;
delay(60000);
ledD2=1;
delay(60000);
ledD3=0;
delay(60000);
ledD3=1;
delay(60000);
ledD4=0;
delay(60000);
ledD4=1;
delay(60000);
ledD5=0;
delay(60000);
ledD5=1;
delay(60000);
ledD6=0;
delay(60000);
ledD6=1;
delay(60000);
ledD7=0;
delay(60000);
ledD7=1;
delay(60000);
ledD8=0;
delay(60000);
ledD8=1;
delay(60000);
}
}
我这个好象不对哈,哈哈。
刚根据视频画了一个流水灯的示意图。
代码我再琢磨琢磨
其实改进也很简单,让D7到D2依次亮就可以了。
#include 'reg52.h'
#include 'intrins.h'
typedef unsigned char u8;
typedef unsigned int u16;
sbit ledD1=P2^0;
sbit ledD2=P2^1;
sbit ledD3=P2^2;
sbit ledD4=P2^3;
sbit ledD5=P2^4;
sbit ledD6=P2^5;
sbit ledD7=P2^6;
sbit ledD8=P2^7;
void delay(u16 i) //延时函数
{
while(i--)
{
//什么也不做
};
}
void main()
{
//D1到D8依次亮
while(1)
{
ledD1=0;
delay(60000);
ledD1=1;
delay(60000);
ledD2=0;
delay(60000);
ledD2=1;
delay(60000);
ledD3=0;
delay(60000);
ledD3=1;
delay(60000);
ledD4=0;
delay(60000);
ledD4=1;
delay(60000);
ledD5=0;
delay(60000);
ledD5=1;
delay(60000);
ledD6=0;
delay(60000);
ledD6=1;
delay(60000);
ledD7=0;
delay(60000);
ledD7=1;
delay(60000);
ledD8=0;
delay(60000);
ledD8=1;
delay(60000);
//以上的代码就是D1到D8依次亮
//D7到D2依次亮
ledD7=0;
delay(60000);
ledD7=1;
delay(60000);
ledD6=0;
delay(60000);
ledD6=1;
delay(60000);
ledD5=0;
delay(60000);
ledD5=1;
delay(60000);
ledD5=0;
delay(60000);
ledD5=1;
delay(60000);
ledD4=0;
delay(60000);
ledD4=1;
delay(60000);
ledD3=0;
delay(60000);
ledD3=1;
delay(60000);
ledD2=0;
delay(60000);
ledD2=1;
delay(60000);
}
}
两个灯一起亮的流水灯
#include 'reg52.h'
#include 'intrins.h'
typedef unsigned char u8;
typedef unsigned int u16;
sbit ledD1=P2^0;
sbit ledD2=P2^1;
sbit ledD3=P2^2;
sbit ledD4=P2^3;
sbit ledD5=P2^4;
sbit ledD6=P2^5;
sbit ledD7=P2^6;
sbit ledD8=P2^7;
void delay(u16 i) //延时函数
{
while(i--)
{
//什么也不做
};
}
void main()
{
//D1到D8依次亮
while(1)
{
//D1 D2一起亮
ledD1=0;
ledD2=0;
delay(60000);
ledD1=1;
ledD2=1;
delay(60000);
//D3 D4一起亮
ledD3=0;
ledD4=0;
delay(60000);
ledD3=1;
ledD4=1;
delay(60000);
//D5 D6一起亮
ledD5=0;
ledD6=0;
delay(60000);
ledD5=1;
ledD6=1;
delay(60000);
//D7 D8一起亮
ledD7=0;
ledD8=0;
delay(60000);
ledD7=1;
ledD8=1;
delay(60000);
//D6 D5一起亮
ledD5=0;
ledD6=0;
delay(60000);
ledD5=1;
ledD6=1;
delay(60000);
//D4 D3
ledD3=0;
ledD4=0;
delay(60000);
ledD3=1;
ledD4=1;
delay(60000);
}
}
给自己留了一个作业,当D1到D8的时候灯移动的速度越来越快,而从D8到D2的时候是灯移动越来越慢。
#include 'reg52.h'
#include 'intrins.h'
typedef unsigned char u8;
typedef unsigned int u16;
sbit ledD1=P2^0;
sbit ledD2=P2^1;
sbit ledD3=P2^2;
sbit ledD4=P2^3;
sbit ledD5=P2^4;
sbit ledD6=P2^5;
sbit ledD7=P2^6;
sbit ledD8=P2^7;
void delay(u16 i) //延时函数
{
while(i--)
{
//什么也不做
};
}
void main()
{
//D1到D8依次亮
while(1)
{
ledD1=0;
delay(60000);
ledD1=1;
delay(60000);
ledD2=0;
delay(55000);
ledD2=1;
delay(55000);
ledD3=0;
delay(50000);
ledD3=1;
delay(50000);
ledD4=0;
delay(45000);
ledD4=1;
delay(45000);
ledD5=0;
delay(40000);
ledD5=1;
delay(40000);
ledD6=0;
delay(35000);
ledD6=1;
delay(35000);
ledD7=0;
delay(30000);
ledD7=1;
delay(30000);
ledD8=0;
delay(25000);
ledD8=1;
delay(25000);
//以上的代码就是D1到D7依次亮
//D7到D2依次亮
ledD7=0;
delay(30000);
ledD7=1;
delay(30000);
ledD6=0;
delay(35000);
ledD6=1;
delay(35000);
ledD5=0;
delay(40000);
ledD5=1;
delay(40000);
ledD4=0;
delay(45000);
ledD4=1;
delay(45000);
ledD3=0;
delay(50000);
ledD3=1;
delay(50000);
ledD2=0;
delay(55000);
ledD2=1;
delay(55000);
}
}