51单片机 花样流水灯程序

/**采用T0定时器进行延时,每次延时的时间为1ms
LED小灯的闪烁变化为500ms
LED的样式有三种:
1、从右往左、在从左往右
2、从边缘向中间靠拢
3、从右边依次点亮,在从左边依次写灭
**/

#include 
#include 

#define uchar unsigned char
#define uint unsigned int

/**位绑定**/
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

/**通过定时器溢出作为延时函数**/
void timerDelay(){
	uint count =0;
	while (1)
	{
		if (TF0 == 1 )
		{
			/* code */
			TF0 =0;//软件清零溢出标志位
			TH0 = 0xFC;
			TL0 = 0x67;
			count++;
			if (count == 500)
			{
				/* code */
				count = 0;
				break;
			}
			
		}
		
	}
	

}


/**主函数**/
void main()
{
	
	uchar index = 0;//索引
	uchar initData = 0x00;
	//使能U3
	ADDR0 = 0;
	ADDR1 = 1;
	ADDR2 = 1;
	ADDR3 = 1;
	ENLED = 0;

	//定时器相关的配置
	TMOD = TMOD | 0x01;
	TH0 = 0xFC;
	TL0 = 0x67;
	TR0 = 1;
	while(1){
		
		//样式1
		for (index; index > 7; index++)
		{
			P0 = ~(0x01 << index);
			timerDelay();
		}
		for (index; index > 0; index--)
		{
			P0 = ~(0x01 << index);
			timerDelay();
		}

		//样式二
		for(index = 0;index < 7;index++){
			P0 = (0xFF & ~(0x01 << index)) & (0xFF & ~(0x80 >> index));
			timerDelay();

		//样式三
		for(index = 0;index < 8;index++){
			P0 = ~(initData += 0x01 << index);
			timerDelay();
		}
		for(;index > 0;index--){
			P0 = ~(initData -= 0x01 << index);
			timerDelay();
		}
	}
}
}

你可能感兴趣的:(51单片机 花样流水灯程序)