赞
踩
1.东西、南北两个方向都通行25s(绿灯亮),警告5s(黄灯亮),禁行30s(红灯亮),用LED模拟交通灯
2.各个方向用数码管显示剩余时间
3.考虑到异常状态
东西发生异常时,东西通行,南北禁止,东西方向绿灯闪,南北方向红灯闪60s
南北方向异常时,南北通行,东西禁行,南北方向绿灯闪,东西方向红灯闪60s
4.两种异常用外部中断0和外部中断1管理,外接开关模拟异常发生
proteus电路图如下:(如有需要原图的可评论找我拿)
代码如下:
#include<reg51.h>
sbit SN_green=P0^3;
sbit SN_yellow=P0^4;
sbit SN_red=P0^5;
sbit EW_green=P0^0;
sbit EW_yellow=P0^1;
sbit EW_red=P0^2;
//sbit SN_cg=P2^0;
//sbit SN_cs=P2^1;
//sbit EW_cg=P2^2;
//sbit EW_cs=P2^3;
unsigned char data cnt_sn,cnt_ew;
unsigned int data T1_cnt;
unsigned char data state_val_sn,state_val_ew;
//char code led_seg_code[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char code led_seg_code[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
char code init_sn[3]={24,4,29};
char code init_ew[3]={29,24,4};
void delay(unsigned int t)
{
while(–t);
}
void led_show(unsigned int u,unsigned int v)
{
unsigned char i; i=u%10; P1=led_seg_code[i]; P3=0xef; //SN_cg=0; //SN_cs=1; delay(50); P3=0xff; i=u%100/10; P1=led_seg_code[i]; P3=0xdf; //SN_cg=0; //SN_cs=1; delay(50); P3=0xff; i=v%10; P2=led_seg_code[i]; P3=0xbf; //EW_cg=0; //EW_cs=1; delay(50); P3=0xff; i=v%100/10; P2=led_seg_code[i]; P3=0x7f; //EW_cg=1; //EW_cs=0; delay(50); P3=0xff;
}
void timer1() interrupt 3
{
T1_cnt++;
if(T1_cnt>3999)
{
T1_cnt=0;
if(cnt_sn!=0) { cnt_sn--; } else { state_val_sn++; if(state_val_sn>2)state_val_sn=0; cnt_sn=init_sn[state_val_sn]; if(state_val_sn==0) { SN_green=0; SN_yellow=1; SN_red=1; } else if(state_val_sn==1) { SN_green=1; SN_yellow=0; SN_red=1; } else if(state_val_sn==2) { SN_green=1; SN_yellow=1; SN_red=0; } } if(cnt_ew!=0) { cnt_ew--; } else { state_val_ew++; if(state_val_ew>2)state_val_ew=0; cnt_ew=init_ew[state_val_ew]; if(state_val_ew==0) { EW_green=1; EW_yellow=1; EW_red=0; } else if(state_val_ew==1) { EW_green=0; EW_yellow=1; EW_red=1; } else if(state_val_ew==2) { EW_green=1; EW_yellow=0; EW_red=1; } }
}
}
void abnorma1() interrupt 0 //异常1 南北通行
{
cnt_sn=60;
cnt_ew=60;
SN_green=0;
SN_yellow=1;
SN_red=1;
EW_green=1;
EW_yellow=1;
EW_red=0;
}
void abnorma2() interrupt 2 //异常2 东西通行
{
cnt_sn=60;
cnt_ew=60;
SN_green=1;
SN_yellow=1;
SN_red=0;
EW_green=0;
EW_yellow=1;
EW_red=1;
}
main()
{
cnt_sn=init_sn[0];
cnt_ew=init_ew[0];
T1_cnt=0;
state_val_sn=0;
state_val_ew=0;
SN_green=0;
SN_yellow=1;
SN_red=1;
EW_green=1;
EW_yellow=1;
EW_red=0;
TMOD=0x20;
TH1=0x19;
TL1=0x19;
EA=1;
ET1=1;TR1=1;
IT1=1;EX1=1;
IT0=1;EX0=1;
while(1)
{ delay(10);
led_show(cnt_sn,cnt_ew);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。