标题: 单片机LED模拟交通灯 [打印本页]

作者: ZQ01    时间: 2021-10-27 16:08
标题: 单片机LED模拟交通灯
      说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮, 红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。



#include<reg51.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)

{

       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();

}

1.png (37.64 KB, 下载次数: 98)

1.png





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1