51单片机-交通灯-红绿灯

51单片机-交通灯-红绿灯


1. 视频

bilibili视频地址:

https://www.bilibili.com/video/av91934816

51单片机-交通灯-红绿灯

2. 文件

文件下载地址:

https://download.csdn.net/download/weixin_43130546/12194351

3. 说明

绿灯亮10秒;黄灯亮5秒;红灯亮10秒

4. 代码


#include 

sbit DULA = P2^6;
sbit WELA = P2^7;

unsigned char code DUTable[16] = 
{
	0x3F,0x06,0x5B,0x4F,
	0x66,0x6D,0x7D,0x07,
	0x7F,0x6F,0x77,0x7C,
	0x39,0x5E,0x79,0x71
};

unsigned char code WETable[8] = 
{
	~0x80,~0x40,~0x20,~0x10,
	~0x08,~0x04,~0x02,~0x01
};

unsigned char LedBuff[8] = 
{
	0x3F,0x3F,0x00,0X00,
	0X00,0X00,0X00,0X00
};

unsigned char T0RH,T0RL;
bit flag1s = 0;

void ConfigTimer0(unsigned char ms);
void TransLED(void);

void main(void)
{
	ConfigTimer0(1);
	while(1)
	{
		if(flag1s)
		{
			flag1s = 0;
			TransLED();
		}
	}
}

void TransLED(void)
{
	static unsigned char num = 0;
	static unsigned char led = 0;

	if(0 == num)
	{
		switch(led)
		{
			case 0 : led = 1; num = 9; P1 = ~0x04; break;
			case 1 : led = 2; num = 4; P1 = ~0x02; break;
			case 2 : led = 0; num = 9; P1 = ~0x01; break;
		}
	}
	else
	{
		num--;
	}

	LedBuff[0] = DUTable[num/1%10];
	LedBuff[1] = DUTable[num/10%10];
}

void ConfigTimer0(unsigned char ms)
{
	unsigned long tmp;

	tmp = 11059200 / 12;
	tmp = tmp * ms / 1000;
	tmp = 65536 - tmp;
	tmp += 12;

	T0RH = (unsigned char)(tmp>>8);
	T0RL = (unsigned char)tmp;

	TH0 = T0RH;
	TL0 = T0RL;
	TMOD &= 0xF0;
	TMOD |= 0x01;
	EA = 1;
	ET0 = 1;
	TF0 = 0;
	TR0 = 1;
}

void LedScan(void)
{
	static unsigned char i = 0;

	P0 = 0x00;
	DULA = 1;
	DULA = 0;
	P0 = WETable[i];
	WELA = 1;
	WELA = 0;
	P0 = LedBuff[i];
	DULA = 1;
	DULA = 0;

	i++;
	if(i>=8)
	{
		i = 0;
	}
}

void InterruptTimer0(void) interrupt 1
{
	static unsigned int tmr1s = 0;

	TH0 = T0RH;
	TL0 = T0RL;

	LedScan();

	tmr1s++;
	if(tmr1s>=1000)
	{
		tmr1s = 0;
		flag1s = 1;
	}
}

你可能感兴趣的:(51单片机-交通灯-红绿灯)