首页 > 代码库 > 单片机第16课:简易交通灯

单片机第16课:简易交通灯

JP3接PO口。




JP1接P2口,J22接P3.0和P3.1口。

#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar smg = 0;
uchar code table[] = {0xc0, 0xf9, 0xa4, 0xb0, 
					  0x99, 0x92, 0x82, 0xf8, 
					  0x80, 0x90, 0x88, 0x83, 
					  0xc6, 0xa1, 0x86, 0x8e};
int flag_t0_green = 0;

sbit led_frag_green = P3^1;
sbit led_frag_red = P3^0;

void init_t0();
void init_traffic();

void main()
{
	init_t0();
	init_traffic();
	while(1)
	{
		
	}
}
//¶¨Ê±Æ÷³õʼ»¯
void init_t0()
{
	TMOD = 0x01;
	TH0 = (65536-50000)/256;
	TL0 = (65536-50000)%256;
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}
//³õʼ»¯½»Í¨µÆ
void init_traffic()
{
	//Ö÷¸ÉµÀÂ̵ÆÁÁ£¬Ö§Â·ºìµÆÁÁ,	Ö÷¸ÉµÀ·½ÏòÉϵÄÈËÐеÀ¿ÉÒÔͨÐÐ
	P2 = 0xb3;
	led_frag_green = 1;
	led_frag_red = 0;
	P0 = table[0];
}
//ÖжϷþÎñ³ÌÐò
void timer0() interrupt 1
{
	TH0 = (65536-50000)/256;
	TL0 = (65536-50000)%256;
	flag_t0_green++;
	if(flag_t0_green%20 == 0)
		{
			smg++;
			P0 = table[smg];
		
		}
		if(flag_t0_green == 200)
		{
			P2 = 0xab;
			smg = 0;	
		}
		if(flag_t0_green == 300)
		{	
			smg = 0;
			P2 = 0x5e;
			led_frag_green = 0;
			led_frag_red = 1;	
		}
		if(flag_t0_green == 400)
		{
			//½«Ö§Â·µÄ»ÆµÆµãÁÁ
			P2 = 0x5d;
			smg = 0;	
		}
		if(flag_t0_green == 500)
		{
			init_traffic();
			flag_t0_green = 0;
			smg = 0;	
		}
}


单片机第16课:简易交通灯