用15单片机点亮流水灯

电路和用15单片机点亮一个LED没有区别。

代码如下

#include <STC15F2K60S2.h>
void delay(int x)
{
	int i,j,k;
	for(i = 0; i < x; i++)
		for(j = 0; j<256; j++)
			for(k = 0; k<256; k++);
}
void main()
{
	P2=(P2&0x1f)|0x80;//设置P2高位为100,其他位不变
	while (1)
	{		
		P0 = 0xFE;  //1111 1110
		delay(20);  
		P0 = 0xFD;  //1111 1101
		delay(20);
		P0 = 0xFB;  //1111 1011
		delay(20);
		P0 = 0xF7;  //1111 0111
		delay(20);
		P0 = 0xEF;  //1110 1111
		delay(20);
		P0 = 0xDF;  //1101 1111
		delay(20);
		P0 = 0xBF;  //1011 1111
		delay(20);
		P0 = 0x7F;  //0111 1111
		delay(20);
	}
}

精简代码

#include <STC15F2K60S2.h>
void delay(int x)
{
	int i,j,k;
	for(i = 0; i < x; i++)
		for(j = 0; j<256; j++)
			for(k = 0; k<256; k++);
}
void main()
{
	char P = 0x01;
	int i;
	P2=(P2&0x1f)|0x80;
	while (1)
	{		
		for(i= 0;i<8;i++)
		{
			P0 = ~(P<<i); 
			delay(20);  
		}
	}
}

成功后,会看到8个灯依次亮

============================找指导老师布置任务吧==============================