晚上随手写,也未调试,权当看看
程序有不严谨地方还得调试修改
#include <REGX51H>
#define STATUS_1S 1
#define STATUS_2S 2
#define STATUS_3S 3
#define STATUS_4S 4
#define STATUS_5S 5
#define STATUS_15S 15
#define STATUS_28S 28
#define STATUS_29S 29
#define STATUS_30S 30
unsigned char flag,status,status_temp=1,status_temp_;
sbit K1 = P0^0;
sbit K2 = P0^1;
sbit RLED = P0^2;
sbit YLED = P0^3;
sbit GLED = P0^4;
void Init(void)
{
ET0=1;
TR0=1;
RLED=0; //亮
YLED=0;
GLED=0;
while(status_temp)
{
switch (status)
{
case STATUS_1S: //1S
{
RLED=1; //灭,闪一次
YLED=1;
GLED=1;
} break;
case STATUS_2S: //1S
{
RLED=0; //亮,闪一次
YLED=0;
GLED=0;
} break;
case STATUS_3S: //1S
{
RLED=1; //灭
YLED=1;
GLED=1;
status_temp=0;
} break;
}
}
status_temp=1;
ET0=0;
TR0=0;
}
void Init_Timer0(void)
{
TMOD=0x01;
// ET0=1;
// TR0=1;
EA=1;
TH0=0X3C;
TL0=0XB0; // 未计算,偷楼上的
}
void M_Start(void)
{
TH0=0X3C;
TL0=0XB0;
ET0=1;
TR0=1;
RLED=0; //红亮
while(status_temp)
{
switch (status)
{
case STATUS_1S:
{
if(status_temp_==1)
{
YLED=0;
}
} break;
case STATUS_2S:
{
if(status_temp_==1)
{
YLED=1;
}
} break;
case STATUS_3S:
{
if(status_temp_==1)
{
YLED=0;
status_temp_=0;
}
} break;
case STATUS_15S: //15S灭,绿亮
{
RLED=1;
GLED=0;
} break;
case STATUS_28S:
{
GLED=1;
} break;
case STATUS_29S:
{
GLED=0;
} break;
case STATUS_30S:
{
GLED=1;
status=0;
status_temp_=1;
} break;
}
}
ET0=0;
TR0=0;
status=0;
status_temp=1;
YLED=1;
}
void M_End(void)
{
TH0=0X3C;
TL0=0XB0;
ET0=1;
TR0=1;
RLED=0; //亮 1次
YLED=0;
GLED=0;
while(status_temp)
{
switch (status)
{
case STATUS_1S: //1S
{
RLED=1; //灭一次
YLED=1;
GLED=1;
} break;
case STATUS_2S: //1S
{
RLED=0; //亮二次
YLED=0;
GLED=0;
} break;
case STATUS_3S: //1S
{
RLED=1; ////灭二次
YLED=1;
GLED=1;
} break;
case STATUS_4S: //1S
{
RLED=0; ////亮三次
YLED=0;
GLED=0;
} break;
case STATUS_5S: //1S
{
RLED=1; ////灭三次
YLED=1;
GLED=1;
status_temp=0;
} break;
}
}
status_temp=1;
ET0=0;
TR0=0;
}
main()
{
Init();//单片机初始化
Init_Timer0();//定时器初始化
while(1)
{
if(K1==0)
M_Start();//模拟开始
if(K2==0)
M_End();//模拟结束
}
}
void interrupt_time0() interrupt 1 //核对头文件,一致就好
{
TH0=0X3C;
TL0=0XB0;
flag++;
if(flag==20)
{
status++;
flag=0;
}
}
#include <reg51h>
#include <stdioh>
#define uchar unsigned char
#define uint unsigned int
uchar h;
uint r=0;
uint s;
uchar code dis[]={ 0Xc0,0Xf9, 0Xa4,0Xb0,0X99,0X92,0X82,0Xf8,0X80,0X90,0Xff,};
/0//1//2//3//4//5//6//7//8//9//NULL/
void delay(uint c)
{
uint i,j;
for(i=0;i<c;i++)
for(j=0;j<100;j++);
}
void t0(void) interrupt 0 using 1 //interrupt 1
{
delay(2);
if(INT0==0)
{
r++;
if(r==2)
{
r=0;
}
r%=2;
if(r==0)
{
P0=0xeb;
P1=0x00;
P2=0xff;
}while(1)
{
if(INT0==0|INT1==0|T0==0)
{
delay(2);
if(INT0==0|INT1==0|T0==0)
break;
}
};
}
}
void t1(void) interrupt 2 using 1 //interrupt 2
{
delay(2);
if(INT1==0)
{
r++;
if(r==2)
{
r=0;
}
r%=2;
if(r==0)
{
P0=0xdd;
P1=0x00;
P2=0xff;
}while(1)
{
if(INT1==0|INT0==0|T0==0)
{
delay(2);
if(INT1==0|INT0==0|T0==0)
break;
}
};
}
}
void t2(void) interrupt 1 using 1 //interrupt 3
{
TH0=0xff;
TL0=0xff;
delay(2);
if(T0==0)
{
P0=0xdb;
P1=0x00;
P2=0xff;
}while(1)
{
if(INT0==0|INT1==0)
{
delay(2);
if(INT0==0|INT1==0)
break;
}
};
}
void main(void)
{
TMOD=0x06;
TH0=0xff;
TL0=0xff;
TR0=1;
EA=1;
EX0=1;
EX1=1;
ET0=1;
while(1)
{
P0=0xeb;
for(h=28;h>0;h--)
{
for(s=248;s>0;s--)
{
P1=0x00;
P2=0xff;
P1=0x01;
P2=dis[h/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x02;
P2=dis[h%10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x04;
P2=dis[(h+2)/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x08;
P2=dis[(h+2)%10];
delay(1);
}
}
delay(248);
P0=0xf3;
for(h=3;h>0;h--)
{
delay(392);
P0=0xf3;
delay(1);
P1=0x00;
P2=0xff;
P1=0xaa;
P2=dis[(h-1)%10];
delay(600);
P0=0xfb;
}
delay(248);
P0=0xdd;
for(h=38;h>0;h--)
{
for(s=248;s>0;s--)
{
P1=0x00;
P2=0xff;
P1=0x01;
P2=dis[(h+2)/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x02;
P2=dis[(h+2)%10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x04;
P2=dis[h/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x08;
P2=dis[h%10];
delay(1);
}
}
delay(248);
P0=0xde;
for(h=3;h>0;h--)
{
delay(392);
P0=0xde;
delay(1);
P1=0x00;
P2=0xff;
P1=0xaa;
P2=dis[(h-1)%10];
delay(600);
P0=0xdf;
}
delay(248);
}
}
#include<reg51h>
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P3^0; //东西向指示灯
sbit YELLOW_A=P3^1;
sbit GREEN_A=P3^2;
sbit RED_B=P3^3; //南北向指示灯
sbit YELLOW_B=P3^4;
sbit GREEN_B=P3^5;
sbit KEY1=P1^0;
sbit KEY2=P1^1;
sbit KEY3=P1^2;
//延时倍数,闪烁次数, *** 作类型变量
uchar Flash_Count=0,Operation_Type=1,LEDsng,LEDsns,LEDewg,LEDews,discnt;
uint Time_Count=0,time;
uchar ledtab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
void displaysn()
{
LEDsng=((time-Time_Count)/20)%10;
LEDsns=((time-Time_Count)/20)/10;
LEDewg=0x10;
LEDews=0x10;
}
void displayew()
{
LEDewg=((time-Time_Count)/20)%10;
LEDews=((time-Time_Count)/20)/10;
LEDsng=0x10;
LEDsns=0x10;
}
//定时器0 中断函数
void T0_INT() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
switch(Operation_Type)
{
if((Time_Count%20)==0)displayew();
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=time) return;
Time_Count=0;
Operation_Type=2;
break;
case 2: //东西向黄灯开始闪烁,绿灯关闭
LEDewg=0x0;
LEDews=0x0;
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: //东西向红灯与南北向绿灯亮
if((Time_Count%20)==0)displaysn();
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=time) return;
Time_Count=0;
Operation_Type=4;
break;
case 4: //南北向黄灯开始闪烁,绿灯关闭
LEDsng=0x0;
LEDsns=0x0;
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 t1_isr() interrupt 3
{
TR1=0;
TH1=(65536-3000)/256;
TL1=(65536-3000)%256;
TR1=1;
switch(discnt)
{
case 0:
P2=0x02;
P0=ledtab[LEDewg];
break;
case 1:
P2=0x01;
P0=ledtab[LEDews];
break;
case 2:
P2=0x08;
P0=ledtab[LEDsng];
break;
case 3:
P2=0x04;
P0=ledtab[LEDsns];
break;
default:discnt=0;break;
}
discnt++;
discnt&=0x03;
}
void delay()
{
uint i;
for(i=0;i<1000;i++);
}
//主程序
void main()
{
TMOD=0x11; //T0 方式1
EA=1;
ET0=1;
TR0=1;
TH1=(65536-3000)/256;
TL1=(65536-3000)%256;
TR1=1;
ET1=1;
time=100;
Time_Count=100;
Time_Count=0;
Operation_Type=1;
while(1)
{
if(KEY1==0) //按一下加1S
{
delay();
if(KEY1==0)
{
while(KEY1==0);
TR0=0;
time+=20;
LEDsng=(time/20)%10;
LEDsns=(time/20)/10;
LEDewg=0x10;
LEDews=0x10;
}
}
if(KEY2==0) //按一下减1S
{
delay();
if(KEY2==0)
{
while(KEY2==0);
TR0=0;
time-=20;
if(time==0)time=20;
LEDewg=(time/20)%10;
LEDews=(time/20)/10;
LEDsng=0x10;
LEDsns=0x10;
}
}
if(KEY3==0) //启动
{
delay();
if(KEY3==0)
{
while(KEY2==0);
TR0=1;
Time_Count=0;
}
}
}
}
东西和南北的时间不同步,楼主没有说得很清楚
你把时间流程说清楚我可以把代码写出来
//以下代码经过Keil C51软件调试通过
#include "reg51h" //头文件包含
//宏定义
#define uint unsigned int
#define uchar unsigned char
#define ON 0 //0的时候灯亮
#define OFF 1 //1的时候灯亮
//管脚定义
//南北道 黄灯亮3秒 红灯亮 30秒 黄灯3秒 绿灯38秒
//东西道 黄灯亮3秒 绿灯亮 28秒 黄灯3秒 红灯40秒
sbit PortDX_Yellow = P1^0; //东西方向黄灯
sbit PortDX_Red = P1^1; //东西方向红灯
sbit PortDX_Green = P1^2; //东西方向绿灯
sbit PortNB_Yellow = P1^3; //南北方向黄灯
sbit PortNB_Red = P1^4; //南北方向红灯
sbit PortNB_Green = P1^5; //南北方向绿灯
void delay(uint ms) // 毫秒级延时(AT89C52 @ 110592MHz)
{
uint ti;
while(ms--)
{
for(ti=0;ti<112;ti++){}//112
}
}
/==================== 主函数 ====================/
void main(void)
{
delay(400); //开机延时400ms
while(1)
{
PortNB_Yellow = ON ;PortNB_Red = OFF;PortNB_Green = OFF;//南北黄灯亮3秒
PortDX_Yellow = ON ;PortDX_Red = OFF;PortDX_Green = OFF;//东西黄灯亮3秒
delay(3000);
PortNB_Yellow = OFF;PortNB_Red = ON ;PortNB_Green = OFF;//南北红灯亮28秒
PortDX_Yellow = OFF;PortDX_Red = OFF;PortDX_Green = ON ;//东西绿灯亮28秒
delay(28000);
PortNB_Yellow = OFF;PortNB_Red = ON ;PortNB_Green = OFF;//南北红灯亮2秒
PortDX_Yellow = ON ;PortDX_Red = OFF;PortDX_Green = OFF;//东西黄灯亮2秒
delay(2000);
PortNB_Yellow = ON ;PortNB_Red = OFF;PortNB_Green = OFF;//南北黄灯亮1秒
PortDX_Yellow = ON ;PortDX_Red = OFF;PortDX_Green = OFF;//东西黄灯亮1秒
delay(1000);
PortNB_Yellow = ON ;PortNB_Red = OFF;PortNB_Green = OFF;//南北黄灯亮2秒
PortDX_Yellow = OFF;PortDX_Red = ON ;PortDX_Green = OFF;//东西红灯亮2秒
delay(2000);
PortNB_Yellow = OFF;PortNB_Red = OFF;PortNB_Green = ON ;//南北绿灯亮38秒
PortDX_Yellow = OFF;PortDX_Red = ON ;PortDX_Green = OFF;//东西红灯亮38秒
delay(3800);
}
}
以上就是关于C51单片机程序设计题,要求用C语言做,模拟交通灯控制单片机电路,答案要详细具体。全部的内容,包括:C51单片机程序设计题,要求用C语言做,模拟交通灯控制单片机电路,答案要详细具体。、基于51单片机控制交通灯的电路图与C语言程序、用51单片机编写城市道路交通灯c语言程序,有左转右转等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)