简易模拟交通灯
1、proteus仿真图
2、要求
东西向绿灯亮10秒,黄灯闪烁3次后红灯亮, 红灯亮后,南北向由红灯变为绿灯,15秒后南北向黄灯闪烁3次后变红灯,东西向变绿灯,如此重复。
3、代码
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit eastWest_Yellow = P0^1;
sbit southNorth_Yellow = P0^4;
void delay()
{
uint i;
uchar j;
for(i = 0;i < 1000;i++)
{
for(j = 0;j < 125;j++)
{
}
}
}
void eastWest_YellowAndYellowLight()
{
uchar i;
P0 = 0xF3;
for(i = 0;i <10;i++)
delay();
P0 = 0xF7;
for(i = 0;i < 6;i++)
{
eastWest_Yellow = !eastWest_Yellow;
delay();
}
}
void southNorth_GreenAndYellowLIght()
{
uchar i;
P0 = 0xDE;
for(i = 0;i <15;i++)
delay();
P0 = 0xFE;
for(i = 0;i < 6;i++)
{
southNorth_Yellow = !southNorth_Yellow;
delay();
}
}
void main()
{
while(1)
{
eastWest_YellowAndYellowLight();
southNorth_GreenAndYellowLIght() ;
}
}