当前位置:网站首页>解答私信@田田WX //2022-6-12 C语言 51单片机LED模拟交通灯
解答私信@田田WX //2022-6-12 C语言 51单片机LED模拟交通灯
2022-06-13 03:47:00 【Navigator_Z】
名称:LED模拟交通灯
说明:
东西向绿灯亮若干秒后,黄灯闪烁,闪烁5次后亮红灯,
红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁,
闪烁5次后亮红灯,东西向绿灯亮,如此往复.
本例将时间设得较短是为了调试的时候能较快的观察到运行结果.
代码块:
#include <reg51.h>
#define INT8U unsigned char
#define INT16U 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;
//闪烁次数及操作类型变量定义
INT8U Flash_Count = 0, Operation_Type = 1;
//-----------------------------------------
//延时函数
//-----------------------------------------
void delay_ms(INT16U x)
{
INT8U t;
while(x--)
for(t=0; t<120; t++);
}
//-----------------------------------------
//交通灯切换子程序
//-----------------------------------------
void Traffic_Light()
{
switch(Operation_Type)
{
case 1: //东西向绿灯与南北向红灯亮
RED_A=1; YELLOW_A=1; GREEN_A=0;
RED_B=0, YELLOW_B=1; GREEN_B=1;
delay_ms(5000);
Operation_Type=2;
break;
case 2: //东西向黄灯开始闪烁,绿灯关闭
for(; Flash_Count<6; Flash_Count++)
{
RED_A=1; YELLOW_A=~YELLOW_A; GREEN_A=1;
delay_ms(300);
}
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;
delay_ms(5000);
Operation_Type=4;
break;
case 4: //南北向黄灯开始闪烁
for(; Flash_Count<6; Flash_Count++)
{
RED_B=1; YELLOW_B=~YELLOW_B; GREEN_B=1;
delay_ms(300);
}
Operation_Type=1;
break;
}
}
//------------------------------------------
//主程序
//------------------------------------------
void main()
{
while(1){
Traffic_Light();
}
}
边栏推荐
猜你喜欢
随机推荐
Meaning of different values of margin and padding
单片机:A/D 和 D/A 的基本概念
[test development] installation of test management tool Zen path
LVS four layer load balancing cluster (3) cluster function classification - HPC
H5 jump to mobile app store
单片机:PCF8591硬件接口
[test development] file compression project practice
ROS话题与节点
SQL injection case demonstration and preventive measures
[Yugong series] June 2022 Net architecture class 080 master cluster and database switching of distributed middleware schedulemaster
On the value of line height
Student management system
Binocular vision -- creating an "optimal solution" for outdoor obstacle avoidance
try-catch finally执行顺序的例题
[test development] automated test selenium (III) -- unittest framework analysis
[test development] automated test selenium (II) -- common APIs for webdriver
单片机信号发生器程序
基于华为云物联网设计的浇花神器(STM32+ESP8266)
Lambda termination operation Max & min
[test development] use case