用定时期控制一个交通灯亮c语言程序
答案:1 悬赏:20 手机版
解决时间 2021-11-10 13:09
- 提问者网友:相思似海深
- 2021-11-09 20:47
用定时期控制一个交通灯亮c语言程序
最佳答案
- 五星知识达人网友:不想翻身的咸鱼
- 2021-11-09 21:46
#include
#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 Time_Count=0,Flash_Count=0,Operation_Type=1;
//定时器 0 中断函数
void T0_INT() interrupt 1
{
TL0=-50000/256;
TH0=-50000%256;
switch(Operation_Type)
{
case 1: //东西向绿灯与南北向红灯亮 5s
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=100) return; //5s(100*50ms)切换
Time_Count=0;
Operation_Type=2;
break;
case 2: //东西向黄灯开始闪烁,绿灯关闭
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_A=~YELLOW_A;GREEN_A=0;
if(++Flash_Count!=10) return; //闪烁
Flash_Count=0;
Operation_Type=3;
break;
case 3: //东西向红灯与南北向绿灯亮 5s
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=100) return; //5s(100*50ms)切换
Time_Count=0;
Operation_Type=4;
break;
case 4: //南北向黄灯开始闪烁,绿灯关闭
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_A=0;
if(++Flash_Count!=10) return; //闪烁
Flash_Count=0;
Operation_Type=1;
break;
}
}
//主程序
void main()
{
TMOD=0x01; //T0 方式 1
IE=0x82;
TR0=1;
while(1);
}
#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 Time_Count=0,Flash_Count=0,Operation_Type=1;
//定时器 0 中断函数
void T0_INT() interrupt 1
{
TL0=-50000/256;
TH0=-50000%256;
switch(Operation_Type)
{
case 1: //东西向绿灯与南北向红灯亮 5s
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=100) return; //5s(100*50ms)切换
Time_Count=0;
Operation_Type=2;
break;
case 2: //东西向黄灯开始闪烁,绿灯关闭
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_A=~YELLOW_A;GREEN_A=0;
if(++Flash_Count!=10) return; //闪烁
Flash_Count=0;
Operation_Type=3;
break;
case 3: //东西向红灯与南北向绿灯亮 5s
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=100) return; //5s(100*50ms)切换
Time_Count=0;
Operation_Type=4;
break;
case 4: //南北向黄灯开始闪烁,绿灯关闭
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_A=0;
if(++Flash_Count!=10) return; //闪烁
Flash_Count=0;
Operation_Type=1;
break;
}
}
//主程序
void main()
{
TMOD=0x01; //T0 方式 1
IE=0x82;
TR0=1;
while(1);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯