当前位置:网站首页>Write a program to simulate the traffic lights in real life.

Write a program to simulate the traffic lights in real life.

2022-07-06 13:38:00 axu_ nine hundred and ninety thousand seven hundred and seven

Programming , Simulate traffic lights in real life .

The source program and simulation have been packaged You can download what you need ( Pure originality Please cherish the fruits of your labor Welcome to forward )

link : Click to download

Traffic light test requirements :

1、 The normal operation of traffic lights can be divided into 4 Status :

state 1: The green light in the east-west direction is on , thing Blue Nixie tube countdown display ; The red light is on in the north-south direction , North South Red nixie tube countdown display ( Time is up to you );
state 2: The remaining 3 second , The yellow light flashes in the East, West, North and south directions , Final steering state 3;
state 3: The red light in the east-west direction is on , Things red digital tube countdown display ; The green light is on in the north-south direction , north and south Blue Nixie tube countdown display ( Time is up to you );
state 4: The remaining 3 second , The yellow light flashes in the East, West, North and south directions , Final steering state 1.

2. Abnormal state ( Interrupt implementation ):

When something is abnormal , The yellow light flashes in the east-west direction , Flash 60 second ;
When anomalies occur in the north and South , The yellow light flashes in the north-south direction , Flash 60 second .
( notes : Simulate the occurrence of abnormal conditions through interruption .)

Code implementation

1. The core idea of the program

①: This experiment adds header files (.h file ). All functions used in the experiment ( The time delay function void delay(int j), Judge button function int key(), Two digit nixie tube display function void showNumByCA(int num)). By setting , It can minimize the coupling degree of the main program , The structure is clear .
②: In the header file showNumByCA There are more than one... In the function , The difference lies in different showNumByCA Control different nixie tubes .
③: Principle of traffic light program : All two digital tubes use the same port (P0). Display the same number , By using different showNumByCA function (showNumByCA1,showNumByCA2…) Control different nixie tubes to achieve the function of switching between red and blue nixie tubes .
④: About interruptions 0: interrupt 0 Its function is to make things dim , The traffic lights flash yellow , North South normal count . So a global variable is set in the program repair=0, When the button is pressed repair Into a 10. In the main program , Each state switch will be checked repair Value , On duty 0 when , Means the button is not pressed , Perform normal display . When the button is pressed ,repair=10. Represents abnormal state , At this time, different showNumByCA function (showNumByCA1,showNumByCA2…) Turn off the corresponding nixie tube , Adjust the color rendering of the corresponding traffic lights . After each number displayed ,repair Will reduce itself 1, therefore , When it comes to repair=0 when , The program returns to normal . The abnormal state is repair=10 Time .
⑤: About interruptions 1: Analogy to ④, The only difference is when the button is pressed ,repair=-10, After each number displayed ,repair Will be on the 1, therefore , When added to repair=0 when , The program returns to normal . The abnormal state is repair=-10 Time .
⑥: The traffic lights in this experiment are accurate 1S timing ( Timing interrupt implementation )

2. Program source code

// Header file section   Create additional  .h  file   See the main function introduction for the file name 
void delay(unsigned char k){
    
	unsigned char i,j,h;
	for(h=0;h<k;h++){
    
		for(i=0;i<25;i++){
    
			for(j=0;j<20;j++);
		}
	}
}
void twoDigitDisplay(unsigned char num,unsigned char time,unsigned char portNumber){
    
	unsigned char box[] = {
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
	unsigned char k;
	if(portNumber==0){
    
		if(num<0||num>99||time<0){
    
			for(k=0;k<40;k++){
    
				P2=0x00;
			}	
		}else{
    		
			for(k=0;k<time;k++){
    
				unsigned char numR = num%10;
				unsigned char numL = num/10;
				P2=0x11;
				P0=box[numL];
				delay(12);
				P2=0x22;
				P0=box[numR];
				delay(12);
			}
		}
	}	
}
void twoDigitDisplay1(unsigned char num,unsigned char time,unsigned char portNumber){
    
	unsigned char box[] = {
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
	unsigned char k;
	if(portNumber==0){
    
		if(num<0||num>99||time<0){
    
			for(k=0;k<40;k++){
    
				P2=0x00;
			}	
		}else{
    		
			for(k=0;k<time;k++){
    
				unsigned char numR = num%10;
				unsigned char numL = num/10;
				P2=0x44;
				P0=box[numL];
				delay(12);
				P2=0x88;
				P0=box[numR];
				delay(12);
			}
		}
	}	
}
void twoDigitDisplay2(unsigned char num,unsigned char time,unsigned char portNumber){
    
	unsigned char box[] = {
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
	unsigned char k;
	if(portNumber==0){
    
		if(num<0||num>99||time<0){
    
			for(k=0;k<40;k++){
    
				P2=0x00;
			}	
		}else{
    		
			for(k=0;k<time;k++){
    
				unsigned char numR = num%10;
				unsigned char numL = num/10;
				P2=0x10;
				P0=box[numL];
				delay(12);
				P2=0x20;
				P0=box[numR];
				delay(12);
			}
		}
	}	
}
void twoDigitDisplay3(unsigned char num,unsigned char time,unsigned char portNumber){
    
	unsigned char box[] = {
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
	unsigned char k;
	if(portNumber==0){
    
		if(num<0||num>99||time<0){
    
			for(k=0;k<40;k++){
    
				P2=0x00;
			}	
		}else{
    		
			for(k=0;k<time;k++){
    
				unsigned char numR = num%10;
				unsigned char numL = num/10;
				P2=0x40;
				P0=box[numL];
				delay(12);
				P2=0x80;
				P0=box[numR];
				delay(12);
			}
		}
	}	
}
void twoDigitDisplay4(unsigned char num,unsigned char time,unsigned char portNumber){
    
	unsigned char box[] = {
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
	unsigned char k;
	if(portNumber==0){
    
		if(num<0||num>99||time<0){
    
			for(k=0;k<40;k++){
    
				P2=0x00;
			}	
		}else{
    		
			for(k=0;k<time;k++){
    
				unsigned char numR = num%10;
				unsigned char numL = num/10;
				P2=0x01;
				P0=box[numL];
				delay(12);
				P2=0x02;
				P0=box[numR];
				delay(12);
			}
		}
	}	
}
void twoDigitDisplay5(unsigned char num,unsigned char time,unsigned char portNumber){
    
	unsigned char box[] = {
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
	unsigned char k;
	if(portNumber==0){
    
		if(num<0||num>99||time<0){
    
			for(k=0;k<40;k++){
    
				P2=0x00;
			}	
		}else{
    		
			for(k=0;k<time;k++){
    
				unsigned char numR = num%10;
				unsigned char numL = num/10;
				P2=0x04;
				P0=box[numL];
				delay(12);
				P2=0x08;
				P0=box[numR];
				delay(12);
			}
		}
	}	
}
//  The main function part 
#include<reg51.h>
#include"ShowNum.h"
char repair=0;
void main(){
    
	char num;
	IT0=1;EA=1;EX0=1;
	IT1=1;EX1=1;
	P1=0x00;
	while(1){
    
		for(num=10;num>=0;num--){
    
			if(num<=3&&num>=0){
    
				if(repair==0){
    
					P1=0x22;twoDigitDisplay(num,8,0);
				}
				if(repair<=10&&repair>=1){
    
					P1=0x22;twoDigitDisplay2(num,8,0);
					repair--;
				}
				if(repair<=-1&&repair>=-10){
    
					P1=0x22;twoDigitDisplay4(num,8,0);
					repair++;
				}
				P1=0x00;
				if(repair==0){
    
					twoDigitDisplay(num,8,0);
				}
				if(repair<=10&&repair>=1){
    
					twoDigitDisplay2(num,8,0);
					repair--;
				}
				if(repair<=-1&&repair>=-10){
    
					twoDigitDisplay4(num,8,0);
					repair++;
				}
				continue;
			}			
			if(repair==0){
    
				P1=0x41;twoDigitDisplay(num,15,0);
			}
			if(repair<=10&&repair>=1){
    
				P1=0x42;
				twoDigitDisplay2(num,8,0);
				P1=0x40;
				twoDigitDisplay2(num,8,0);
				repair--;
			}
			if(repair<=-1&&repair>=-10){
    
				P1=0x21;
				twoDigitDisplay4(num,8,0);
				P1=0x01;
				twoDigitDisplay4(num,8,0);
				repair++;
			}
		}
		for(num=10;num>=0;num--){
    
			if(num<=3&&num>=0){
    
				if(repair==0){
    
					P1=0x22;twoDigitDisplay1(num,8,0);
				}
				if(repair<=10&&repair>=1){
    
					P1=0x22;twoDigitDisplay3(num,8,0);
					repair--;
				}
				if(repair<=-1&&repair>=-10){
    
					P1=0x22;twoDigitDisplay5(num,8,0);
					repair++;
				}
				P1=0x00;
				if(repair==0){
    
					twoDigitDisplay1(num,8,0);
				}
				if(repair<=10&&repair>=1){
    
					twoDigitDisplay3(num,8,0);
					repair--;
				}
				if(repair<=-1&&repair>=-10){
    
					twoDigitDisplay5(num,8,0);
					repair++;
				}
				continue;
			}			
			if(repair==0){
    
				P1=0x14;twoDigitDisplay1(num,15,0);
			}
			if(repair<=10&&repair>=1){
    
				P1=0x42;
				twoDigitDisplay2(num,8,0);
				P1=0x40;
				twoDigitDisplay2(num,8,0);
				repair--;
			}
			if(repair<=-1&&repair>=-10){
    
				P1=0x21;
				twoDigitDisplay4(num,8,0);
				P1=0x01;
				twoDigitDisplay4(num,8,0);
				repair++;
			}
		}
	}
}
void int0() interrupt 0 {
    
	// Things are abnormal   The yellow light in the east-west direction flashes 10 Normal after times   North South normal timing 
	repair=10;
}
void int1() interrupt 2 {
    
	// North South anomaly   The yellow light flashes in the north-south direction 10 Normal after times   North South normal timing 
	repair=-10;
}

Two 、 Simulation demonstration

Simulation demonstration timing 10S Round Parameters can be adjusted in the program ! Simulation demonstration timing 10S Round Parameters can be adjusted in the program

The source program and simulation have been packaged You can download what you need

link : Click to download

Friends who like SCM can communicate together

原网站

版权声明
本文为[axu_ nine hundred and ninety thousand seven hundred and seven]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916479993.html