Crossroad traffic light based on 51 microcontroller_5s yellow light countdown flashing

Traffic light at intersection based on 51 microcontroller_5s yellow light flashing

(Program + simulation + simulation video)

Simulation: proteus 7.8

Program compiler: keil 4/keil 5

Programming language: C language

Design number: J006

Functional requirements

Traffic light operating status:

(1) Mode 1: The east-west red light and the north-south green light are on for 5 seconds;

(2) Mode 2: The north-south green light is off and the yellow light flashes for 5 seconds (5 times);

(3) Mode 3: 5 seconds each for east-west green lights and north-south red lights;

(4) Mode 4: The east-west green light is off and the yellow light flashes for 5 seconds (5 times);

(5) The specific number of seconds can be realized by changing the number in the program.

Simulation diagram

North-south trafficimage-20220831004850875

East-west traffic

image-20220831004910205

program

image-20220831005300678

Main program code

#include<reg52.h>
#include<intrins.h>

//数据类型定义
typedef unsigned char uchar;
typedef unsigned int  uint;

void led_sacn();
void delay_ms(ms);
void seg_disp(uchar number,uchar wei);

#define ON   1    //LED给告电平亮灯
#define OFF  0    //LED给低电平灭灯

//通用IO引脚分配
sbit W0=P3^4;
sbit W1=P3^5;
sbit W2=P3^6;
sbit W3=P3^7;

sbit NS_G = P2^0;
sbit NS_Y = P2^1;
sbit NS_R = P2^2;
sbit WE_R = P2^3;
sbit WE_Y = P2^4;
sbit WE_G = P2^5;

bit flag1s;
bit half_1sflag;
uchar one_sec_flag,main_road_time,secondary_road_time,half_sec_flag;
//1秒定时标志位 南北方向显示时间 东西方向显示时间
uchar state=0;//正常模式不同状态
uchar code seg_du[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};

uchar main_green_straight_cnt=5,yellow_cnt =5,2sec_green_straight_cnt =5; 
//南北方向直行绿灯时间            _黄灯时间      _东西方向绿灯时间

void main()
{
	EA=1;	//开总中断
	TMOD=0X01;//T0的工作模式为模式1
	TH0=0X4C;
	TL0=0X00;//11.0592M晶振 50ms定时初值
	ET0=1; //允许定时器1中断
	TR0=1;//启动定时器0  
//	state=2;	
	while(1)
	{	
		led_sacn();				  		//LED和数码管显示,时刻刷新

		if(flag1s)						//一秒刷新一次
		{
			flag1s=0;
			main_road_time--;			//红绿灯倒计时时间减
			secondary_road_time--;
		}
		if (half_1sflag){
			half_1sflag = 0;
			if(state == 0){//黄灯闪烁
				WE_Y =~WE_Y;
			}else if(state == 2){
				NS_Y =~NS_Y;
			}
			
		}
	}
}

void led_sacn()
{

		if(main_road_time==0 || secondary_road_time==0)//当南北方向或者东西方向倒数到0,切换状态。
		//这一段程序只有倒计时为0才执行一次,执行完一次等下一次倒计时为0才再执行一次
		{
			switch(state)//改变红绿灯的状态
			{
				case 0:
				{
					state=1;//下次切换到下一个模式
					main_road_time=main_green_straight_cnt+yellow_cnt;//南北方向直行绿灯通行时间
					secondary_road_time=sec_green_straight_cnt;//东西方向红灯时间
					 NS_G = ON;
					 NS_Y = OFF;
					 NS_R = OFF;
					 WE_R = ON;
					 WE_Y = OFF;
					 WE_G = OFF;
				}break;
				case 1:
				{
					state=2;
//					main_road_time = yellow_cnt;//南北方向直行黄灯时间
					secondary_road_time =yellow_cnt;
					
					 NS_G = OFF;
					 NS_Y = ON;
					 NS_R = OFF;
					 WE_R = ON;
					 WE_Y = OFF;
					 WE_G = OFF;	
				}break;
				case 2:
				{
					state=3;
					main_road_time=sec_green_straight_cnt;
					secondary_road_time=sec_green_straight_cnt+yellow_cnt;
					NS_G = OFF;
					NS_Y = OFF;
					NS_R = ON;
					WE_R = OFF;
					WE_Y = OFF;
					WE_G = ON;

				}break;
				case 3:
				{
					state=0;
//					secondary_road_time=yellow_cnt;//黄灯时间
					main_road_time=yellow_cnt;
					NS_G = OFF;
					NS_Y = OFF;
					NS_R = ON;
					WE_R = OFF;
					WE_Y = ON;
					WE_G = OFF;

				}break;
				
				default:break;
			}
		}
		
		seg_disp(main_road_time/10,0);//显示W0控制的数码管 时刻刷新
		seg_disp(main_road_time%10,1);//显示W1控制的数码管
		seg_disp(secondary_road_time/10,2);//显示W2控制的数码管
		seg_disp(secondary_road_time%10,3);//显示W3控制的数码管

}

void seg_disp(uchar number,uchar wei)	//数码管动态显示程序 wei代表数码管W0 W1 W2 W3的位选
{
	P0=0XFF;//清零,防止重影
	if(wei == 0){//显示第一位
		W0=0;
		W1=1;
		W2=1;
		W3=1;	
		P0=seg_du[number];
		delay_ms(2);
		W0=1;
	}
	if(wei == 1){//显示第二位
		W0=1;
		W1=0;
		W2=1;
		W3=1;	
		P0=seg_du[number];
		delay_ms(2);
		W1=1;
	}
	if(wei == 2){//显示第三位
		W0=1;
		W1=1;
		W2=0;
		W3=1;	
		P0=seg_du[number];
		delay_ms(2);
		W2=1;
	}
	if(wei == 3){//显示第四位
		W0=1;
		W1=1;
		W2=1;
		W3=0;	
		P0=seg_du[number];
		delay_ms(2);
		W3=1;
	}
}

Program explanation

The main core point is the countdown. The green light time + yellow light time on the main road = the red light time on the secondary road.

During the red light process on the secondary road, the main road completed the two steps of green light countdown + yellow light countdown.

The creation of countdown

Keep this in mind when designing software. First of all, we need to have a time basis. Where does the countdown come from?

Generally two sources:

1. Delay

delay(1000ms);

To achieve the delay effect by running the card main software in an infinite loop, the program execution efficiency is extremely low and is not advisable.

2. Timing

The time base is generated through a timer. The software sets a timer interrupt of 50ms and counts it in the interrupt execution function.

	EA=1;	//开总中断
	TMOD=0X01;//T0的工作模式为模式1
	TH0=0X4C;
	TL0=0X00;//11.0592M晶振 50ms定时初值
	ET0=1; //允许定时器1中断
	TR0=1;//启动定时器0  

The interrupt function is executed once every 50ms, and one_sec_flag is accumulated to 20 to determine that one second has passed. Set the one-second flag flag1s to one.

void Timer0() interrupt 1
{
	TH0=0X4C;
	TL0=0X00;//11.0592M晶振 50ms定时初值
	if(++half_sec_flag>10){
		half_1sflag = 1;
		half_sec_flag = 0;

	}


	if(++one_sec_flag<20){
		return;//提前结束函数
		}
	
	one_sec_flag=0;
	flag1s=1;	
}

Judge the flag bit in the while loop of the main function. If it is 1, the countdown count value is decremented by one, which completes the countdown software design idea.

    if(flag1s)						//一秒刷新一次
    {
        flag1s=0;
        main_road_time--;			//红绿灯倒计时时间减
        secondary_road_time--;
    }
Red, yellow and green light status processing

image-20220829214546328

Traffic light status is actually divided into four states:

1. The main road has a green light and the secondary road has a red light.

2. Main roads have yellow lights and secondary roads have red lights.

3. Main roads have red lights and secondary roads have green lights.

4. Main roads have red lights and secondary roads have yellow lights.

Make a state machine and set four states. In the changes of the four states, set the on and off of the red, green and yellow lights to implement the basic traffic light operation logic.

if(main_road_time==0 || secondary_road_time==0)//当南北方向或者东西方向倒数到0,切换状态。
		//这一段程序只有倒计时为0才执行一次,执行完一次等下一次倒计时为0才再执行一次
		{
			switch(state)//改变红绿灯的状态
			{
				case 0:
				{
					state=1;//下次切换到下一个模式
					main_road_time=main_green_straight_cnt+yellow_cnt;//南北方向直行绿灯通行时间
					secondary_road_time=sec_green_straight_cnt;//东西方向红灯时间
					 NS_G = ON;
					 NS_Y = OFF;
					 NS_R = OFF;
					 WE_R = ON;
					 WE_Y = OFF;
					 WE_G = OFF;
				}break;
				case 1:
				{
					state=2;
//					main_road_time = yellow_cnt;//南北方向直行黄灯时间
					secondary_road_time =yellow_cnt;
					
					 NS_G = OFF;
					 NS_Y = ON;
					 NS_R = OFF;
					 WE_R = ON;
					 WE_Y = OFF;
					 WE_G = OFF;	
				}break;
				case 2:
				{
					state=3;
					main_road_time=sec_green_straight_cnt;
					secondary_road_time=sec_green_straight_cnt+yellow_cnt;
					NS_G = OFF;
					NS_Y = OFF;
					NS_R = ON;
					WE_R = OFF;
					WE_Y = OFF;
					WE_G = ON;

				}break;
				case 3:
				{
					state=0;
//					secondary_road_time=yellow_cnt;//黄灯时间
					main_road_time=yellow_cnt;
					NS_G = OFF;
					NS_Y = OFF;
					NS_R = ON;
					WE_R = OFF;
					WE_Y = ON;
					WE_G = OFF;

				}break;
				
				default:break;
			}
		}
		
		seg_disp(main_road_time/10,0);//显示W0控制的数码管 时刻刷新
		seg_disp(main_road_time%10,1);//显示W1控制的数码管
		seg_disp(secondary_road_time/10,2);//显示W2控制的数码管
		seg_disp(secondary_road_time%10,3);//显示W3控制的数码管

Countdown display processing

In fact, the countdown display is to display main_road_time–; secondary_road_time–; the design function can display the main_road_time of the main road and the secondary_road_time of the secondary road through the digital tube.

		seg_disp(main_road_time/10,0);
		//显示W0控制的数码管
		seg_disp(main_road_time%10,1);//显示W1控制的数码管
		seg_disp(secondary_road_time/10,2);//显示W2控制的数码管
		seg_disp(secondary_road_time%10,3);//显示W3控制的数码管

Information list

Data download link

image-20220903203349509

Guess you like

Origin blog.csdn.net/weixin_52733843/article/details/134765182