C51模拟交通灯程序
来源:网络收集 点击: 时间:2024-02-11打开proteus仿真软件,绘制交通灯仿真电路图,如下:

打开Keil4编程软件,根据刚刚绘制的交通灯仿真电路图编写相应的程序,程序如下:
3/4#include reg52.h
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P0^0;
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3;
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
uchar Flash_Count = 0;
Operation_Type = 1;
void DelayMS(uint x)
{
uchar t;
while(x--)
{
for(t=120;t0;t--);
}
}
void Traffic_lignt()
{
switch(Operation_Type)
{
case 1:
RED_A=1;YELLOW_A=1;GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1;
DelayMS(2000);
Operation_Type = 2;
break;
case 2:
DelayMS(200);
YELLOW_A=~YELLOW_A;
if(++Flash_Count !=10)
return;
Flash_Count=0;
Operation_Type = 3;
break;
case 3:
RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0;
DelayMS(2000);
Operation_Type = 4;
break;
case 4:
DelayMS(200);
YELLOW_B=~YELLOW_B;
if(++Flash_Count !=10)
return;
Flash_Count=0;
Operation_Type = 1;
break;
}
}
void main()
{
while(1)
{
Traffic_lignt();
}
}

将Keil4编写好的程序导入proteus仿真软件中,效果如下:



版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.ff371.cn/art_22369.html