#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P1^0; //ä¸è¥¿åç¯
sbit YELLOW_A=P1^1;
sbit GREEN_A=P1^2;
sbit RED_B=P1^5; //åååç¯
sbit YELLOW_B=P1^6;
sbit GREEN_B=P1^7;
uchar Flash_Count=0,Operation_Type=1; //éªç次æ°ï¼æä½ç±»ååé
//延æ¶
void DelayMS(uint x)
{
uchari;
while(x--)for(i=0;i<120;i++);
}
//交éç¯åæ¢
void Traffic_Light()
{
switch(Operation_Type)
{
case1: //ä¸è¥¿å绿ç¯ä¸ååå红ç¯äº®
RED_A=1;YELLOW_A=1;GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1;
DelayMS(2000);
Operation_Type=2;
break;
case2: //ä¸è¥¿åé»ç¯éªçï¼ç»¿ç¯å
³é
DelayMS(300);
YELLOW_A=~YELLOW_A;GREEN_A=1;
if(++Flash_Count!=10)return; //éªç5次
Flash_Count=0;
Operation_Type=3;
break;
case3: //ä¸è¥¿å红ç¯ï¼ååå绿ç¯äº®
RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0;
DelayMS(2000);
Operation_Type=4;
break;
case4: //åååé»ç¯éªç5次
DelayMS(300);
YELLOW_B=~YELLOW_B;GREEN_B=1;
if(++Flash_Count!=10)return;
Flash_Count=0;
Operation_Type=1;
}
}
//主ç¨åº
void main()
{
while(1)Traffic_Light();
}
温馨提示:答案为网友推荐,仅供参考