流水灯

#include <reg51.h>
#include <delay.h>


sbit P1_0 = P1^0;
sbit key1 = P0^0;

unsigned char state = 1;

void main()
{

	unsigned char i = 0,a = 1;
	while(1)
	{
		
		if(state == 1)
		{
			for(i = 0;i < 8;i++)
			{
			    if(key1 == 0)
				{
					delay_ms(1);//延时消抖
					if(key1 == 0)
					{
						state = 0;
						while(!key1);//等待按键松开
						break;
					}
				}
							
				P1<<=1;      
				P1|=0x01;    
				if(P1 == 0xff)
                    P1=0xfe;
				delay_ms(500);
				
			}
        }
		else if(state == 0)
		{
			for(i = 0;i < 8;i++)
			{
				if(key1 == 0)
				{
					delay_ms(1);
					if(key1 == 0)
					{
						state = 1;
						while(!key1);
						break;
					}
								
				}
							
				P1>>=1;
				P1|=0x80;
				if(P1 ==0xff)
				    P1=0x7f;
				delay_ms(500);
			}
		}
	}
}

写的好像有一点复杂,慢慢改进

猜你喜欢

转载自blog.csdn.net/cxycj123/article/details/81148650