⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 交通灯控制的简单程序
💻 C
字号:
#include"at89x52.h"


#define uchar unsigned char
#define uint unsigned int

uchar one_second,ch,rec[2];
uchar red_light,green_light,yellow_light,red_time,green_time,yellow_time;
uchar give_red=10,give_green=15,give_yellow=3;

void initial(void)
{ 
	P0=P1=P2=P3=0xff;
	SCON=0x50;			// UART_mode=1,REN=1
	RCAP2H=0xff; 
	RCAP2L=0xdc;		//9600
	T2MOD=0;  
	T2CON=0x34;        // Set T2 to baud rate register  
	TMOD=0x01;			//T0 工作方式1 50ms
	TH0=0x4b;
	TL0=0xff;
	ET0=1;
	ES=1;             
	EA=1;
}

void int_T0(void) interrupt 1
{
	uchar i;
	TH0=0x4b;
	TL0=0xff;
	i++;
	if(i==20)
	{
		i=0;
		one_second=1;
	}
}

void int_232(void) interrupt 4
{
	if(RI)
	{  
		RI=0;
		rec[ch]=SBUF;
		ch++;
	}
	else return;
}

void main(void)
{
	initial();
	while(1)
	{
		if(one_second)
		{
			one_second=0;
			if(red_light)
			{
				red_time--;
				if(!red_time)
				{
					red_light=0;	//0表示不亮;1表示亮
									//加上控制红灯的引脚跳变
					red_time=give_red;
					green_light=1;
									//加上控制绿灯的引脚跳变
				}
			}
			else if(green_light)
			{
				green_time--;
				if(!green_time)
				{
					green_light=0;	

					green_time=give_green;
					yellow_light=1;
	
				}
			}
			else if(yellow_light)
			{
				yellow_time--;
									//加上黄灯闪烁控制;
				if(!yellow_time)
				{
					yellow_light=0;
		
					yellow_time=give_yellow;
					red_light=1;
				}
			}
		}
		if(rec[0])
		{
			ET0=0;
			red_light=green_light=yellow_light=0;
			if(rec[0]==0x01)
			{
				give_red=rec[1];
				rec[0]=rec[1]=0;
				ch=0;
			}
			else if(rec[0]==0x02)
			{
				give_green=rec[1];
				rec[0]=rec[1]=0;
				ch=0;
			}
			else if(rec[0]==0x03)
			{
				give_yellow=rec[1];
				rec[0]=rec[1]=0;
				ch=0;
			}
			red_light=1;
			ET0=1;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -