J015 intersection traffic light based on AT89C51 (emergency + night)

Intersection traffic light emergency + night based on AT89C51

Simulation diagram proteus 7.8

Program compiler: keil 4/keil 5

Programming language: C

No.: J015

Functional requirements

1) There are red, yellow and green signal lights in the east-west direction and the north-south direction;

2) Equipped with emergency braking button. When the button is pressed, the red light will be on in all directions; Press again to restore the normal display;

3) When the night mode button is pressed, the yellow light flashes in all directions;

4) Real time reminder of the remaining time when the green light is on.

Simulation diagram

Normal operation mode

Normal traffic light operation has four states:

1. Green light in east-west direction, red light in north-south direction

2. Yellow light in east-west direction, red light in north-south direction

3. Green light in north-south direction, red light in east-west direction

4. Yellow light in north-south direction, red light in east-west direction

Emergency mode

Red light in east-west and North-South directions, displaying EE

Normal operation mode

program

Program explanation

The main core points are countdown and LED light on and off control

Generation of countdown

Remember this to design software. First of all, there must be a time base. Where does the countdown come from?

There are generally two sources:

1. Delay

delay(1000ms);

The delay effect is achieved by running the main software of the dead cycle card, and the program execution efficiency is extremely low, which is undesirable.

2. Timing

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

	EA=1;//Total on interruption
	EX0=1;//External interrupt 0
	EX1=1;//External interrupt 1
	IT0=1;//Set falling edge trigger interrupt 0
	IT1=1;//Set falling edge trigger interrupt 1

	TMOD=0X01;//The working mode of timer 0 is mode 1
	TH0=0X4C;
	TL0=0X00;//11.0592M crystal oscillator 50ms timing initial value
	ET0=1; //Allow timer 1 to interrupt
	TR0=1;//Start timer 0 

Execute the interrupt function once every 20ms, through one_ sec_ The flag accumulated to 50 to judge that one second has passed. Set one second flag bit scan_flag is set to one.

	TH0=0XBB;
	TL0=0X00;//Reload timing initial value
	if(++one_sec_flag>=50){
		one_sec_flag=0;
		
	if(run_flag==0)//Normally, the countdown is only started
		scan_flag=1;
	}

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

	while(1)
	{	
		led_sacn();	//LED and nixie tube display, always refresh
		count_sacn();//Countdown scan
	}

Traffic light status processing

There are four modes of normal traffic light operation:

1. Green light in east-west direction, red light in north-south direction

2. Yellow light in east-west direction, red light in north-south direction

3. Green light in north-south direction, red light in east-west direction

4. Yellow light in north-south direction, red light in east-west direction

5. Perform step 1

if(run_flag==0)//0 normal operation
    {
   	if(ns_road_time==0 || we_road_time==0)//Count down to 0 and switch the status.
   	//This section of the program is executed once only when the countdown is 0. After one execution, it is executed again when the next countdown is 0. This is the core
   	{
   	if(state == 0){
   		state=1;//Switch to the next mode next time
   		ns_road_time=ns_green_cnt;//Straight green traffic time in north-south direction
   		we_road_time=ns_green_cnt+yellow_cnt;
   		//Common sense: traffic time in east-west red light direction = = straight green light in north-south direction + yellow light time in north-south direction 
   		 we_red    = ON;
   		 we_yellow = OFF;
   		 we_green  = OFF;
   		 ns_red    = OFF;
   		 ns_yellow = OFF;
   		 ns_green  = ON;
   	}else if(state == 1){
   		state=2;
   		ns_road_time = yellow_cnt;//Straight yellow light time in north-south direction
   		we_red	  = ON;
   		we_yellow = OFF;
   		we_green  = OFF;
   		ns_red	  = OFF;
   		ns_yellow = ON;
   		ns_green  = OFF;


   	}else if(state == 2){
   		state=3;
   		ns_road_time=we_green_cnt+yellow_cnt;
   		we_road_time =we_green_cnt;
   		we_red	  = OFF;
   		we_yellow = OFF;
   		we_green  = ON;
   		ns_red	  = ON;
   		ns_yellow = OFF;
   		ns_green  = OFF;


   	}else if(state==3){

   		state=0;
   		we_road_time=yellow_cnt;//Yellow light time
   		we_red	  = OFF;
   		we_yellow = ON;
   		we_green  = OFF;
   		ns_red	  = ON;
   		ns_yellow = OFF;
   		ns_green  = OFF;

   	}
   	}
   	seg_display();//Digital tube display	
   }

Countdown display processing

In fact, the countdown display is to display ns_road_time–; we_road_time–; Design function: display ns in A direction through digital tube_ road_ Time and we in direction B_ road_ Time

/*
Call this function to display 4-digit nixie tube
*/
void seg_display(void){
		seg_disp(ns_road_time/10,0);
		seg_disp(ns_road_time%10,1);
		seg_disp(we_road_time/10,2);
		seg_disp(we_road_time%10,3);
}
/*
Nixie tube dynamic display program
wei Bit selection of seg0 1, 2, 3 on behalf of nixie tube
 Select bit selection, assign it to P0, and the number will be displayed on the selected SEG position
*/
void seg_disp(uchar number,uchar wei)	
{
	P0=0XFF;//Reset to prevent ghosting
	W0=W1=W2=W3=0;//Clear bit selection	
	if(wei == 0){//Display SEGA
		W0=1;
		P0=seg_du[number];
		delay_ms(2);
	}
	if(wei == 1){//Display SEGB
		W1=1;
		P0=seg_du[number];
		delay_ms(2);
	}
	if(wei == 2){//Show SEGC
		W2=1;
		P0=seg_du[number];
		delay_ms(2);
	}
	if(wei == 3){//Display SEGD
		W3=1;	
		P0=seg_du[number];
		delay_ms(2);
	}
}

Key handling

Key interrupt processing function, press the night mode key and execute night_run() function, execute emergency after pressing the emergency key_ run();

void night_run() interrupt 0
{
	if(run_flag==0)//In case of normal mode:
	{
		run_flag=2; //Night mode
	}else if(run_flag == 2){//In case of night mode:
		run_flag=0;  //Switch back to normal mode
		ns_road_time=0;//Reproduction start
		state=0;

	}

}

void urgent_run() interrupt 2//External interrupt processing function, after pressing the emergency key, execute this
{
	if(run_flag==0){//In case of normal mode:
		run_flag=1;
		buck_led=P2;//Record the current LED status

	}else if(run_flag==1){//If it is already night mode
		run_flag=0;//Switch back to normal mode
		P2=buck_led;

	}
}

Data list

Data download

Tags: C Embedded system Single-Chip Microcomputer Proteus 8051 microcontroller

Posted by bigmichael on Sun, 04 Sep 2022 22:29:40 +0300