實驗室考覈————交通燈控制器(紅綠燈)

0x00 前言

  學校實驗室的階段測試,是一個交通燈控制器,開始感覺貌似不太難,但是搞着搞着,發現情況不對,還是出現了些問題的(可能還是我菜),這週六中午放的題,下午,把大框搭好了,然後身體就開始不舒服(得好好鍛鍊身體了),等週日中午,感覺好一些了,繼續完善了一下,開始調bug,,等晚上差不多了,就交了。有些可惜的是RCTF就在這兩天,沒參加上,等復現了。

筆者水平有限,但是還是分享一下。

 

0x01 題目

有一說一,第一眼看到題目時我是懵的,拓展裏的1,2,3條我沒看懂,沒關係,還給了一個仿真圖,我們來和仿真結合分析一下

仿真:

很清晰,不過,有些小錯誤,貌似沒有綠燈,最後通過詢問,我把D3,D6設爲綠燈。

分析:

基礎要求:

爲了滿足要求在循環播放時爲:

       A路:綠燈(10s)--->  黃燈 (閃爍4s)---> 紅燈(10s)--->黃燈(閃爍4s)
       B路:綠燈(10s)--->  黃燈 (閃爍4s)---> 紅燈(10s)--->黃燈(閃爍4s)

緊急按鍵,是中斷0。

       當按下“緊急車輛”按鈕,馬上紅燈(4s)。

(基礎要求很好理解)

拓展要求:

1. 當按下A路檢查,或B路檢測,就代表A,B路來車了

2. 當在綠燈時,兩路車都有車,就循環放行:首先A路綠燈,同時B路紅燈;之後交換A路紅燈,同時B路綠燈。

3.當只有一路車,且綠燈積累10s後,若另一路沒有車,就繼續放行,這裏,的繼續放行概念很模糊,這裏我設置成:如果進入這種情況,數碼管將  黑屏,且繼續放綠燈,當另一路來車時,數碼管退出黑屏。然和進入兩路都有車的情況。

4. 修改沒啥可說的,需要注意的,就是,所有的數字要用變量代替。

 

0x02 基礎要求

先把 延時寫上:

void Delay(u16 t)		
{
	while(t--);
}

數碼管顯示:

這個時候注意,因爲在拓展要求時要進行修改還有需要的黑屏,所以數碼管的顯示一共有5種顯示

1. 正常

2.放行時間修改模式

3.黃燈時間修改模式

4.緊急時間修改模式

5.黑屏

我們用一個變量 model來選擇:

u8 code Duanxuan[]={                       //????
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

u8  WE[]={0x00,0x00,0x00,0x00};   //位選                       				
u16 numA=10, numB=10;		  //數碼管的顯示數字
u8 emergency_t = 4;	          //緊急時間
u8 Red_A_T = 10,Green_A_T = 10, Yellow_A_T =4;//A紅綠燈
u8 Red_B_T = 10,Green_B_T = 10, Yellow_B_T =4;//B紅綠燈
u8 model = 0;                     //用來判斷修改哪個時間		

void display(void)
{
	static unsigned char i = 0;
	
	P0 = 0xff;

	P3 = (0x10 << i) | 0x04;
	
	P0 = WE[i++];

	i %= 4;
}
void SMC_show(void)
{

	if(model == 0)//正常
	{
	         WE[0] = ~Duanxuan[numA/10];
	         WE[1] = ~Duanxuan[numA%10];
	         WE[2] = ~Duanxuan[numB/10];
	         WE[3] = ~Duanxuan[numB%10];
	}
	 else if(model == 1)//放行
	{
		WE[0] = ~Duanxuan[Green_A_T/10];
		WE[1] = ~Duanxuan[Green_A_T%10];
		WE[2] = ~Duanxuan[Green_B_T/10];
		WE[3] = ~Duanxuan[Green_B_T%10];
		
	}
	else if (model == 2)//黃
	{
		WE[0] = ~Duanxuan[Yellow_A_T/10];
		WE[1] = ~Duanxuan[Yellow_A_T%10];
		WE[2] = ~Duanxuan[Yellow_B_T/10];
		WE[3] = ~Duanxuan[Yellow_B_T%10];
	}
	else if (model == 3)//緊急
	{
                WE[0] = ~Duanxuan[emergency_t/1000];
		WE[1] = ~Duanxuan[emergency_t/100%10];
		WE[2] = ~Duanxuan[emergency_t/10%10];
		WE[3] = ~Duanxuan[emergency_t%10];
		}
	else if (model == 4)//黑屏
	{
                WE[0] = ~Duanxuan[16];
		WE[1] = ~Duanxuan[16];
		WE[2] = ~Duanxuan[16];
		WE[3] = ~Duanxuan[16];
	}
}					

用定時器進行倒計時 && 黃燈閃爍:

void Service_T0(void) interrupt 1
{
	TH0 = (65535-50000) / 256;
	TL0 = (65535-50000) % 256;

	if(count++ >= 20)
	{
		if(emergency == 0 && signle == 0 && both == 0 && ((come_a == 1 && come_b == 0) || (come_a == 0 && come_b == 1)) && (LedA_Select%4 ==0 || LedB_Select%4 ==0))

		count_change_t++;
		
		if(model == 0)
		{
			numA--;
			numB--;
		}
		
		count = 0;
	}
	
	//==利用定時 黃燈閃爍
	if(LedA_Select%4 == 1 && emergency == 0)
	{
		LED_A_Y = ~LED_A_Y;
	}
		if(LedB_Select%4 == 1 && emergency == 0 )
	{
		LED_B_Y = ~LED_B_Y;
	}
	if(LedA_Select%4 ==3  && emergency == 0)
	{
		LED_A_Y = ~LED_A_Y;
	}
		if(LedB_Select%4 == 3 && emergency == 0 )
	{
		LED_B_Y = ~LED_B_Y;
	}
}

裏面有一個 判斷count_change_t這變量的條件,在下文會說

判斷兩燈之間的轉換

用Led_Select來判斷到底是什麼燈

sbit LED_A_G = P1^0;
sbit LED_A_Y = P1^1;
sbit LED_A_R = P1^2;
sbit LED_B_R = P1^3;
sbit LED_B_Y = P1^4;
sbit LED_B_G = P1^5;

if(emergency == 0 && (both == 0 || signle == 0))//當拓展情況下,正常模式下的交替停止
	{
		if(numA == 0)
		{
			LedA_Select++;
			switch(LedA_Select%4)
			{
				case 0://Â̵Æ
					numA = Green_A_T;
					LED_A_G=0;
					LED_A_R=1;
					LED_A_Y=1;
				break;
				case 1://»ÆµÆ
					numA = Yellow_A_T;
					LED_A_Y= 0;
					LED_A_G = 1;
					LED_A_R=1;
					break;
				case 2://ºìµÆ
					numA = Red_A_T;
					LED_A_R = 0;
					LED_A_Y= 1;
					LED_A_G=1;
					break;
				case 3://»ÆµÆ
					numA = Yellow_A_T;
					LED_A_Y= 0;
					LED_A_G = 1;
					LED_A_R=1;
					break;
			}
		}
		
		if(numB == 0)
		{
			LedB_Select++;
			switch(LedB_Select%4)
			{
				case 0://Â̵Æ
						numB = Green_B_T;
					LED_B_Y=1;
					LED_B_G=0;
					LED_B_R=1;
				
				break;
				case 1://»ÆµÆ
					numB = Yellow_B_T;
					LED_B_R=1;
					LED_B_G=1;
					LED_B_Y=0;
					break;
				case 2://ºìµÆ
					numB = Red_B_T;
					LED_B_R=0;
					LED_B_Y=1;
					LED_B_G=1;
					break;
				case 3://»ÆµÆ
					numB = Yellow_B_T;
					LED_B_G=1;
					LED_B_Y=0;
					LED_B_R=1;
					break;
			}
		}
	}

 

緊急情況:

爲了保證緊急情況後可以復原,需要設置變量暫存當時的數碼管數字,燈的顏色,因爲由Led_Select不會改變,所以,需要再次用這個變量來還原燈

void Service_INT0() interrupt 0
{
	emergency = 1;
	temp_numA = numA;//ÔÝ´æ
	temp_numB = numB;//ÔÝ´æ
	numA = emergency_t;
	numB = emergency_t;
	LED_A_G = 1;
	LED_A_Y = 1;
	LED_A_R = 0;
	LED_B_R = 0;
	LED_B_Y = 1;
	LED_B_G = 1;
}

else if(emergency == 1 && numA == 0 && numB == 0) 
	{
		numA = temp_numA;
		numB = temp_numB;
		//ÏȹØÉÏ£¬ÈÃLED_SelectÈ¥ÅжÏÔ­À´ÎªÊ²Ã´µÆ
		emergency = 0;
		LED_A_G = 1;
		LED_A_Y = 1;
		LED_A_R = 1;
		LED_B_R = 1;
		LED_B_Y = 1;
		LED_B_G = 1;
				
		
		switch(LedA_Select%4)
		{
			case 0://Â̵Æ
				LED_A_Y=1;
				LED_A_G=0;
				LED_A_Y=1;
			break;
					
			case 1://»ÆµÆ
				LED_A_R=1;
				LED_A_Y= 0;
				LED_A_G = 1;
			break;
			case 2://ºìµÆ
				LED_A_G=1;
				LED_A_R = 0;
				LED_A_Y= 1;
			break;
			case 3://»ÆµÆ
				LED_A_G=1;
				LED_A_Y= 0;
				LED_A_R = 1;
			break;
		}
						
		switch(LedB_Select%4)
		{
			case 0://Â̵Æ
				LED_B_Y=1;
				LED_B_G=0;
				LED_B_R=1;
			break;
			case 1://»ÆµÆ
				LED_B_R=1;
				LED_B_G=1;
				LED_B_Y=0;
			break;
			case 2://ºìµÆ
				LED_B_G=1;
				LED_B_R=0;
				LED_B_Y=1;
			break;
			case 3://»ÆµÆ
				LED_B_G=1;
				LED_B_R=1;
				LED_B_Y=0;
			break;
		}
	}

 

0x03 拓展要求

先把按鍵寫好,當按下“A路檢測”,“B路檢測”則代表車來了,come_a, come_b置1,

然後通過上述的model變量來判斷到底是修改什麼時間

按鍵函數:

sbit Key_set = P2^0;		//設置按鍵用來改變model											
sbit Key_up = P2^1;		//增加												
sbit Key_down = P2^2;		//減少											
sbit Key_A = P2^3;		//A路檢測												
sbit Key_B = P2^4;	        //B路檢測
													
void key(void)//
{
	if(Key_set == 0)
	{
		Delay(100);
		if(Key_set == 0)
		{
			if(model++ == 3)
			model = 0;
		}
		while(Key_set == 0);
	}
	
	if(Key_up == 0)
	{
		Delay(100);
		if(Key_up == 0)
		{
			switch (model)
			{
					case 1:
					 Green_A_T++;
					 Green_B_T++;
					break;
									
					case 2:
					 Yellow_A_T++;
					 Yellow_B_T++;
					break;
									
					case 3:
					 emergency_t++;
					break;
			}
	  }
		while(Key_up == 0);
	}

	if(Key_down == 0)
	{
		Delay(100);
		if (Key_down == 0)
		{
				
			 switch (model)
			 {
					case 1:
						
					if(Green_A_T > 0)//Òª±È0´ó
						Green_A_T--;
					if(Green_B_T > 0)
						Green_B_T--;
					break;
						
					case 2:
						if(Yellow_A_T > 0)
						Yellow_A_T--;
						if(Yellow_B_T > 0)
						Yellow_B_T--;
					break;
						
					case 3:
						if(emergency_t>= 4)//½ô¼±time>=4
						emergency_t--;
					break;
					}
		  }
		while(Key_down == 0);
	 }
		
	if(Key_A == 0)
	{
		Delay(100);
		if (Key_A == 0)
		{
				come_a = 1;
		}	
		while(Key_A == 0);
	}
				
	if(Key_B == 0)
	{
		Delay(100);
		if (Key_B == 0)
		{
				come_b = 1;
		}	
		while(Key_B == 0);
	}
}

 拓展要求函數

1. 兩路都有車

當兩路同行,且a,b都來車,就先把此時的B燈時間暫存,將B燈時間等於A燈時間,然後A路綠燈b路紅燈,當時間減到0後,將come_A置0,將暫存的B燈放回B燈,再將A燈時間等於B燈時間,然後A路紅燈b路綠燈,當時間減到0後,come_B置0。當兩者都是0時,輪流結束。

需要注意的是當循環後直接跳到黃燈

if(come_a == 1 && come_b == 1 && LedB_Select%4== 0 && LedA_Select%4 == 0)
	{
		signle = 0;
		model= 0;
		if(both == 0)
		{
			both = 1;
			LED_A_G = 0;
			LED_A_Y = 1;
			LED_A_R = 1;
			LED_B_R = 0;
			LED_B_Y = 1;
			LED_B_G = 1;
			
			temp_numB = numB;
			numB = numA;
			
		}
	}
		if(both == 1)
		{
			if(numA == 0 && come_a != 0)
			{
				LED_A_G = 1;
				LED_A_Y = 1;
				LED_A_R = 0;
				LED_B_R = 1;
				LED_B_Y = 1;
				LED_B_G = 0;
				
				numB= temp_numB;
				numA= numB;
				//====================
				come_a = 0;

			}
			if (numB == 0 && come_a == 0)
			{	
				LED_A_G = 1;
				LED_A_Y = 1;
				LED_A_R = 1;
				LED_B_R = 1;
				LED_B_Y = 1;
				LED_B_G = 1;
				come_b = 0;
			}
                        if((come_a == 0 && come_b == 0))
			{
				both = 0;
				
				numA = 0;
				numB = 0;
				LedA_Select = 0;
				LedB_Select = 0;
				
				LED_A_G = 1;
				LED_A_Y = 1;
				LED_A_R = 1;
				LED_B_R = 1;
				LED_B_Y = 1;
				LED_B_G = 1;
			}
		}

 

一路有燈且綠燈10s

需要利用定時器積累10s,

條件

1. 不是緊急模式

2. 不是輪流模式

3. 只有一路同行,要麼A路,要麼B路,

4. 且是綠燈

if(emergency == 0 && signle == 0 && both == 0 &&
 ((come_a == 1 && come_b == 0) || (come_a == 0 && come_b == 1)) && 
(LedA_Select%4 ==0 || LedB_Select%4 ==0))
{
			count_change_t++;
}

當積累10s後進入黑屏(model 4)

if(((come_a == 1 && come_b == 0) || (come_a == 0 && come_b == 1)) && both == 0 && (LedA_Select%4== 0 || LedB_Select%4 == 0) )
		{
			if(count_change_t >= 10)
			{
				signle = 1;
				count_change_t = 0;
				LedA_Select = 0;
				LedB_Select = 0;
				model = 4;
			}
		}

 

0x04 完整代碼:
 

#include <REGX52.H>

typedef unsigned char u8;
typedef unsigned int u16;

sbit LED_A_G = P1^0;
sbit LED_A_Y = P1^1;
sbit LED_A_R = P1^2;
sbit LED_B_R = P1^3;
sbit LED_B_Y = P1^4;
sbit LED_B_G = P1^5;


u8 code Duanxuan[]={                       //????
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

u8  WE[]={0x00,0x00,0x00,0x00}; 
u16 count = 0;                        				//ÓÃÓÚ¶¨Ê±Æ÷µÄ¼ÆÊý
u16 numA=10, numB=10;													//ÊýÂë¹ÜµÄÏÔʾÊý×Ö
u8 LedA_Select = 0;														//ÅжÏAµÄLED
u8 LedB_Select = 0;														//ÅжÏBµÄLED
u8 emergency_t = 4;														//½ô¼±³µÁ¾Ê±¼ä
bit emergency = 0;														//½ô¼±³µÁ¾±ê־λ
u8 temp_numA, temp_numB;											//ÔÝ´æ
u8 Red_A_T = 10,Green_A_T = 10, Yellow_A_T =4;//AºìÂ̻ƵÆʱ¼ä
u8 Red_B_T = 10,Green_B_T = 10, Yellow_B_T =4;//BºìÂ̻ƵÆʱ¼ä
u8 count_change_t= 0;													//һ·ͨÐÐ10S£¬ÁíÍâһ·ûÓгµµÄʱºò//
u8 model = 0;																	//ÅжÏÉèÖÃÐÞ¸ÄÄĸöʱ¼ä
sbit Key_set = P2^0;													//ÉèÖð´¼ü
sbit Key_up = P2^1;														//Ôö¼Ó°´¼ü
sbit Key_down = P2^2;													//¼õÉÙ°´¼ü
sbit Key_A = P2^3;														//A·¼ì²é°´¼ü
sbit Key_B = P2^4;														//B·¼ì²é°´¼ü
bit come_a=0, come_b=0;												//A·À´³µ£¬B·À´³µ
bit both, signle;															//µ¥Â·Óгµ or ˫·Óгµ

void Delay(u16 t)		
{
	while(t--);
}

void display(void)
{
	static unsigned char i = 0;
	
	P0 = 0xff;

	P3 = (0x10 << i) | 0x04;
	
	P0 = WE[i++];

	i %= 4;
}
void SMC_show(void)
{

	if(model == 0)//Õý³£Ä£Ê½
	{
	
		WE[0] = ~Duanxuan[numA/10];
	  WE[1] = ~Duanxuan[numA%10];
	  WE[2] = ~Duanxuan[numB/10];
	  WE[3] = ~Duanxuan[numB%10];
	}
	 else if(model == 1)//Ð޸ķÅÐÐʱ¼ä
	{
		WE[0] = ~Duanxuan[Green_A_T/10];
		WE[1] = ~Duanxuan[Green_A_T%10];
		WE[2] = ~Duanxuan[Green_B_T/10];
		WE[3] = ~Duanxuan[Green_B_T%10];
		
	}
	else if (model == 2)//Ð޸ĻƵÆʱ¼ä
	{
		WE[0] = ~Duanxuan[Yellow_A_T/10];
		WE[1] = ~Duanxuan[Yellow_A_T%10];
		WE[2] = ~Duanxuan[Yellow_B_T/10];
		WE[3] = ~Duanxuan[Yellow_B_T%10];
	}
	else if (model == 3)//Ð޸Ľô¼±Ê±¼ä
	{
    WE[0] = ~Duanxuan[emergency_t/1000];
		WE[1] = ~Duanxuan[emergency_t/100%10];
		WE[2] = ~Duanxuan[emergency_t/10%10];
		WE[3] = ~Duanxuan[emergency_t%10];
		}
	else if (model == 4)//µ±Ò»Â·À´³µ£¬10sºó£¬½øÈëºÚÆÁ
	{
    WE[0] = ~Duanxuan[16];
		WE[1] = ~Duanxuan[16];
		WE[2] = ~Duanxuan[16];
		WE[3] = ~Duanxuan[16];
	}
}
//////////////////////////////////////==¶¨Ê±Æ÷0 && ÖжÏ0==///////////////////////
void Init_Int0_T0()
{
	IT0 = 1;
	EX0 = 1;
	
	TMOD = 0x01;//·Ç×Ô¶¯
	TR0 = 1;
	TH0 = (65535-50000) / 256;
	TL0 = (65535-50000) % 256;
	ET0 = 1; 
	
	EA = 1;
}

void Service_T0(void) interrupt 1
{
	TH0 = (65535-50000) / 256;
	TL0 = (65535-50000) % 256;

	if(count++ >= 20)
	{
		if(emergency == 0 && signle == 0 && both == 0 && ((come_a == 1 && come_b == 0) || (come_a == 0 && come_b == 1)) && (LedA_Select%4 ==0 || LedB_Select%4 ==0))//µ±Ò»Â·À´³µ£¬¿ªÊ¼¼Æʱ
			count_change_t++;
		
		if(model == 0)
		{
			numA--;
			numB--;
		}
		
		count = 0;
	}
	
	//==ÀûÓö¨Ê±Æ÷»ÆµÆÉÁ˸	
	if(LedA_Select%4 == 1 && emergency == 0)
	{
		LED_A_Y = ~LED_A_Y;
	}
		if(LedB_Select%4 == 1 && emergency == 0 )
	{
		LED_B_Y = ~LED_B_Y;
	}
	if(LedA_Select%4 ==3  && emergency == 0)
	{
		LED_A_Y = ~LED_A_Y;
	}
		if(LedB_Select%4 == 3 && emergency == 0 )
	{
		LED_B_Y = ~LED_B_Y;
	}
}


void Service_INT0() interrupt 0
{
	emergency = 1;
	temp_numA = numA;//ÔÝ´æ
	temp_numB = numB;//ÔÝ´æ
	numA = emergency_t;
	numB = emergency_t;
	LED_A_G = 1;
	LED_A_Y = 1;
	LED_A_R = 0;
	LED_B_R = 0;
	LED_B_Y = 1;
	LED_B_G = 1;
}

////////////////////////////////////////////////==end==////////////////////////////
void Led_Change(void)//Á½µÆÖ®¼äµÄת»» && ½ô¼±Çé¿öʱµÄ¸´Ô­
{
	if(emergency == 0 && (both == 0 || signle == 0))//µ±ÎªÍØչģʽÏÂʱ£¬Õý³£Ä£Ê½ÏµĽ»Ìæ¾ÍÍ£Ö¹
	{
		if(numA == 0)
		{
			LedA_Select++;
			switch(LedA_Select%4)
			{
				case 0://Â̵Æ
					numA = Green_A_T;
					LED_A_G=0;
					LED_A_R=1;
					LED_A_Y=1;
				break;
				case 1://»ÆµÆ
					numA = Yellow_A_T;
					LED_A_Y= 0;
					LED_A_G = 1;
					LED_A_R=1;
					break;
				case 2://ºìµÆ
					numA = Red_A_T;
					LED_A_R = 0;
					LED_A_Y= 1;
					LED_A_G=1;
					break;
				case 3://»ÆµÆ
					numA = Yellow_A_T;
					LED_A_Y= 0;
					LED_A_G = 1;
					LED_A_R=1;
					break;
			}
		}
		
		if(numB == 0)
		{
			LedB_Select++;
			switch(LedB_Select%4)
			{
				case 0://Â̵Æ
						numB = Green_B_T;
					LED_B_Y=1;
					LED_B_G=0;
					LED_B_R=1;
				
				break;
				case 1://»ÆµÆ
					numB = Yellow_B_T;
					LED_B_R=1;
					LED_B_G=1;
					LED_B_Y=0;
					break;
				case 2://ºìµÆ
					numB = Red_B_T;
					LED_B_R=0;
					LED_B_Y=1;
					LED_B_G=1;
					break;
				case 3://»ÆµÆ
					numB = Yellow_B_T;
					LED_B_G=1;
					LED_B_Y=0;
					LED_B_R=1;
					break;
			}
		}
	}
	else if(emergency == 1 && numA == 0 && numB == 0) 
	{
		numA = temp_numA;
		numB = temp_numB;
		//ÏȹØÉÏ£¬ÈÃLED_SelectÈ¥ÅжÏÔ­À´ÎªÊ²Ã´µÆ
		emergency = 0;
		LED_A_G = 1;
		LED_A_Y = 1;
		LED_A_R = 1;
		LED_B_R = 1;
		LED_B_Y = 1;
		LED_B_G = 1;
				
		
		switch(LedA_Select%4)
		{
			case 0://Â̵Æ
				LED_A_Y=1;
				LED_A_G=0;
				LED_A_Y=1;
			break;
					
			case 1://»ÆµÆ
				LED_A_R=1;
				LED_A_Y= 0;
				LED_A_G = 1;
			break;
			case 2://ºìµÆ
				LED_A_G=1;
				LED_A_R = 0;
				LED_A_Y= 1;
			break;
			case 3://»ÆµÆ
				LED_A_G=1;
				LED_A_Y= 0;
				LED_A_R = 1;
			break;
		}
						
		switch(LedB_Select%4)
		{
			case 0://Â̵Æ
				LED_B_Y=1;
				LED_B_G=0;
				LED_B_R=1;
			break;
			case 1://»ÆµÆ
				LED_B_R=1;
				LED_B_G=1;
				LED_B_Y=0;
			break;
			case 2://ºìµÆ
				LED_B_G=1;
				LED_B_R=0;
				LED_B_Y=1;
			break;
			case 3://»ÆµÆ
				LED_B_G=1;
				LED_B_R=1;
				LED_B_Y=0;
			break;
		}
	}
}

void key(void)//°´¼üº¯Êý
{
	if(Key_set == 0)
	{
		Delay(100);
		if(Key_set == 0)
		{
			if(model++ == 3)
			model = 0;
		}
		while(Key_set == 0);
	}
	
	if(Key_up == 0)
	{
		Delay(100);
		if(Key_up == 0)
		{
			switch (model)
			{
					case 1:
					 Green_A_T++;
					 Green_B_T++;
					break;
									
					case 2:
					 Yellow_A_T++;
					 Yellow_B_T++;
					break;
									
					case 3:
					 emergency_t++;
					break;
			}
	  }
		while(Key_up == 0);
	}

	if(Key_down == 0)
	{
		Delay(100);
		if (Key_down == 0)
		{
				
			 switch (model)
			 {
					case 1:
						
					if(Green_A_T > 0)//Òª±È0´ó
						Green_A_T--;
					if(Green_B_T > 0)
						Green_B_T--;
					break;
						
					case 2:
						if(Yellow_A_T > 0)
						Yellow_A_T--;
						if(Yellow_B_T > 0)
						Yellow_B_T--;
					break;
						
					case 3:
						if(emergency_t>= 4)//½ô¼±time>=4
						emergency_t--;
					break;
					}
		  }
		while(Key_down == 0);
	 }
		
	if(Key_A == 0)
	{
		Delay(100);
		if (Key_A == 0)
		{
				come_a = 1;//A·À´³µ
		}	
		while(Key_A == 0);
	}
				
	if(Key_B == 0)
	{
		Delay(100);
		if (Key_B == 0)
		{
				come_b = 1;//B·À´³µ
		}	
		while(Key_B == 0);
	}
}

void expand(void)//À©Õ¹
{
	
	if(come_a == 1 && come_b == 1 && LedB_Select%4== 0 && LedA_Select%4 == 0)
	{
		signle = 0;
		model= 0;
		if(both == 0)
		{
			both = 1;
			LED_A_G = 0;
			LED_A_Y = 1;
			LED_A_R = 1;
			LED_B_R = 0;
			LED_B_Y = 1;
			LED_B_G = 1;
			//====================òËÆÖ»ÄÜÕâôд´ýµ÷ÊÔ
			/*
			temp_numB = numB;
			temp_numA = numA;
			numB = numA;
			*/
			temp_numB = numB;
			numB = numA;
			//====================
		}
	}zhi'y
		if(both == 1)
		{
			if(numA == 0 && come_a != 0)//a == 1ÓÐʱ»á2´Î»ÆµÆ  a != 0ÎÈÍ×һЩ
			{
				LED_A_G = 1;
				LED_A_Y = 1;
				LED_A_R = 0;
				LED_B_R = 1;
				LED_B_Y = 1;
				LED_B_G = 0;
				//====================òËÆÖ»ÄÜÕâôд´ýµ÷ÊÔ
			  /*
			  numB= temp_numB;
			  numA = temp_numA;
			  */
				numB= temp_numB;
				numA= numB;
				//====================
				come_a = 0;

			}
			if (numB == 0 && come_a == 0)
			{	
				LED_A_G = 1;
				LED_A_Y = 1;
				LED_A_R = 1;
				LED_B_R = 1;
				LED_B_Y = 1;
				LED_B_G = 1;
				come_b = 0;
			}
			if((come_a == 0 && come_b == 0))
			{
				both = 0;
				//=====================ΪÁ˱£Ö¤²»¿¨¶Ù£¬ÇÒûÓбðµÄbugÖ»ÄÜÕâ¸ö˳Ðò
				//Delay(500);
				numA = 0;
				numB = 0;
				LedA_Select = 0;
				LedB_Select = 0;
				//=====================
				LED_A_G = 1;
				LED_A_Y = 1;
				LED_A_R = 1;
				LED_B_R = 1;
				LED_B_Y = 1;
				LED_B_G = 1;
			}
		}
/////////////////////////////////////////////////////////////////////////////////
		
		if(((come_a == 1 && come_b == 0) || (come_a == 0 && come_b == 1)) && both == 0 && (LedA_Select%4== 0 || LedB_Select%4 == 0) )
		{
			if(count_change_t >= 10)
			{
				signle = 1;
				count_change_t = 0;
				LedA_Select = 0;
				LedB_Select = 0;
				model = 4;
			}
		}
}

void Init_System()//³õʼ»¯
{
	WE[0] = ~Duanxuan[numA/10];
	WE[1] = ~Duanxuan[numA%10];
	WE[2] = ~Duanxuan[numB/10];
	WE[3] = ~Duanxuan[numB%10];
	
	LED_A_G = 0;
	LED_A_Y = 1;
  LED_A_R = 1;
	LED_B_G = 0;
  LED_B_Y = 1;
	LED_B_R = 1;
}

void main(void)
{
	Init_System();
	Init_Int0_T0();
	while(1)
	{
		
		if(model != 4)
			Led_Change();
		display();
		SMC_show();
		key();
		expand();
	}
}

 

0x05 總結

感覺有難度的,積累一下經驗,如有不對的地方希望路過的大佬斧正,去復現RCTF去了(逃

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章