|
本帖最后由 池上柳 于 2018-11-16 10:44 编辑
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit DX_LED2=P2^3;
sbit DX_LED1=P2^2;
sbit NB_LED2=P2^1;
sbit NB_LED1=P2^0;
sbit NB_Yellow=P1^6;
sbit DX_Yellow=P1^2;
sbit DX_Red=P1^3;
sbit NB_Red=P1^7;
bit Flag_SN_Yellow;
bit Flag_EW_Yellow;
char Time_EW;
char Time_SN;
sbit key0=P3^0;
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^6;
sbit key4=P3^7;
sbit led1=P3^1;
sbit led2=P3^4;
sbit led3=P3^5;
sbit led4=P2^6;
sbit led5=P2^7;
uchar EW=60,SN=40,EWL=19,SNL=19;
uchar EW1=60,SN1=40,EWL1=19,SNL1=19;
uchar code table[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uchar code S[6]={0X28,0X18,0X48,0X82,0X84,0X81};
void Delay(uchar a)
{
/*uchar i;
i=a;
while(i--){;} */
uchar i,j;
for(i=a;i>0;i--)
for(j=110;j>0;j--);
}
void Display(void)
{
char h,l;
h=Time_EW/10;
l=Time_EW%10;
P0=table[l];
DX_LED2=1;
Delay(2);
DX_LED2=0;
P0=table[h];
DX_LED1=1;
Delay(2);
DX_LED1=0;
h=Time_SN/10;
l=Time_SN%10;
P0=table[l];
NB_LED2=1;
Delay(2);
NB_LED2=0;
P0=table[h];
NB_LED1=1;
Delay(2);
NB_LED1=0;
}
void main(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
IT0=1;
EX0=1;
PX0=1;
IT1=1;
EX1=1;
PX1=1;
ET1=1;
PT1=1;
TR1=1;
led1=0;led3=0;
led2=0;led4=0;led5=0;
while(1)
{
Time_EW=EW;
Time_SN=SN;
while(Time_SN>=5)
{P1=S[0]; //南北通行,东西红灯
Display();}
P1=0x00;
while(Time_SN>=0)
{Flag_SN_Yellow=1;
DX_Red=1;Display();}
Flag_SN_Yellow=0;
Time_SN=SNL;
while(Time_SN>=5)
{P1=S[1];Display();} //南北左拐,东西红灯
while(Time_SN>=0)
{Flag_SN_Yellow=1;
DX_Red=1;Display();}
Flag_SN_Yellow=0;
Time_EW=SN;
Time_SN=EW;
while(Time_EW>=5)
{ P1=S[3];//东西通行,南北红灯
Display();}
P1=0x00;
while(Time_EW>=0)
{Flag_EW_Yellow=1;
NB_Red=1;Display();}
Flag_EW_Yellow=0;
Time_EW=EWL;
while(Time_SN>=5)
{P1=S[5];Display();} //东西左拐,南北红灯
while(Time_EW>=0)
{Flag_EW_Yellow=1;
NB_Red=1;Display();}
Flag_EW_Yellow=0;
EW=EW1;
SN=SN1;
EWL=EWL1;
SNL=SNL1;
}
}
void timer0(void)interrupt 1
{
static uchar t;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
if(t==10)
{
if(Flag_SN_Yellow==1)
{NB_Yellow=~NB_Yellow;}
if(Flag_EW_Yellow==1)
{DX_Yellow=~DX_Yellow;}
}
if(t==20)
{
Time_EW--;
Time_SN--;
if(Flag_SN_Yellow==1)
{NB_Yellow=~NB_Yellow;}
if(Flag_EW_Yellow==1)
{DX_Yellow=~DX_Yellow;}
t=0;
}
if(key0==0)
{
Delay(5);
if(key0==0)//此处按键按下,数码管端口会被阻塞,当然,这也是我要的结果以便进行下一步的操作
{
TR0=0;
P2=0x00;
P1=0x88;
led1=1;
while(!key0);
TR0=1;led1=0;
}
}
}
void ext0() interrupt 0//这些外部中断以及定时器1的中断都设置了高优先级以打断定时器0,并保持数码管处于闭塞的状态
{
if(key1==0)
{
P1=0x82;
led2=1;
while(!key1);
P1=0x88;led2=0;
}
}
void ext1() interrupt 2
{
if(key2==0)
{
P1=0x28;
led3=1;
while(!key2);
P1=0x88;led3=0;
}
}
void T1_timer() interrupt 3
{
if(key3==0)
{
P1=0x81;
led4=1;
while(!key3);
P1=0x88;led4=0;
}
if(key4==0)
{
P1=0x18;
led5=1;
while(!key4);
P1=0x88;led5=0;
}
}//原理图中的非门其实没有必要连接,可以将显示代码稍微改一下就可以不用非门了,先将位选端的电平拉低稍作软件延时后再拉高就可以了 |
评分
-
查看全部评分
|