单片机交通灯东西绿灯亮 南北红灯亮 东西黄灯闪 南北红灯亮 东西红灯亮 南北绿灯亮 东西红灯亮 南北黄灯闪

2024-12-25 08:32:18
推荐回答(1个)
回答1:

#include

sbit RED_EW = P0^0;//东西向红灯
sbit YELLOW_EW = P0^1;//东西向黄灯
sbit GREEN_EW = P0^2;//东西向绿灯

sbit RED_SN = P0^3;//南北向红灯
sbit YELLOW_SN = P0^4;//南北向黄灯
sbit GREEN_SN = P0^5;//南北向绿灯

unsigned char OperationType = 2,FlashTime = 5;
//延时
void DelayMS(int x)
{
unsigned char i;
while(x--)
for(i=0;i<120;i++);
}

//交通灯切换
void Traffic_Light(void)
{
//东西向绿灯,南北向红灯
RED_EW = 1;
YELLOW_EW = 1;
GREEN_EW = 0;

RED_SN = 0;
YELLOW_SN = 1;
GREEN_SN = 1;
DelayMS(20000);

//东西向黄灯闪烁,绿灯关闭
RED_EW = 1;
GREEN_EW = 1;
while(FlashTime--)
{
YELLOW_EW = !YELLOW_EW;
DelayMS(100);
}
FlashTime = 5;

//南北向绿灯,东西向红灯
RED_EW = 0;
YELLOW_EW = 1;
GREEN_EW = 1;

RED_SN = 1;
YELLOW_SN = 1;
GREEN_SN = 0;
DelayMS(20000);

//南北向黄灯闪烁,绿灯关闭
RED_SN = 1;
GREEN_SN = 1;
while(FlashTime--)
{
YELLOW_SN = !YELLOW_EW;
DelayMS(100);
}
FlashTime = 5;

}
void main(void)
{
while(1)
Traffic_Light();
}