|
|
//上電南北通行30秒,5秒倒計(jì)時(shí)亮黃燈,30秒結(jié)束,東西通行,后面依次交替,
//增加了2個(gè)應(yīng)急按鍵,按下S3南北通行30秒,30秒結(jié)束時(shí)回到應(yīng)急前的狀態(tài),同理按下S2.....
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define led P0
sbit s3=P3^5;//南北應(yīng)急通行
sbit s2=P3^4;//東西應(yīng)急通行
sbit wei1=P2^1;sbit wei0=P2^0;//數(shù)碼管位選
sbit red0=P2^5;sbit green0=P2^6;sbit yellow0=P2^7;//南北紅綠黃燈
sbit red1=P2^4;sbit green1=P2^2;sbit yellow1=P2^3;//東西紅綠黃燈
//uchar code display[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0 ---- 9,共陽(yáng)
uchar code display[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};//共陰數(shù)碼管0---9,a---f,滅
uchar buf[2]={3,0};//初始顯示30
uchar num0,num1;//1s計(jì)時(shí)
uchar counter0=30;//30s倒計(jì)時(shí)
uchar counter1=30;//30s倒計(jì)時(shí)
bit flag1=0;//正常南北東西切換
bit flag2=0;//應(yīng)急南北東西切換
bit flag3=0;//正常與應(yīng)急切換,初始為正常
uchar temp;//緩存標(biāo)記
//==============================================================================================
void time0_init()
{
TMOD=0x11;//定時(shí)器0,1均工作于方式1
TH0=(65535-50000)/256;//定時(shí)50ms
TL0=(65535-50000)%256;
TH1=(65535-50000)/256;//定時(shí)50ms
TL1=(65535-50000)%256;
ET0=ET1=EA=TR0=1;TR1=0;
}
void delay1m(uint x)//毫秒級(jí)延時(shí)函數(shù)
{
uint i,j;
for(i=0;i<x;i++) //連數(shù)x次,約 x ms
for(j=0;j<120;j++); //數(shù)120 次,約1 ms
}
void scan()//數(shù)碼管掃描函數(shù)
{
wei1=1;wei0=0;led=display[buf[1]];
delay1m(5);led=0x00;
wei1=0;wei0=1;led=display[buf[0]];
delay1m(5);led=0x00;
}
void convert(uchar dat)//將數(shù)據(jù)轉(zhuǎn)成適合數(shù)碼管顯示的數(shù)
{
buf[0]=dat/10;
buf[1]=dat%10;
}
void main()//主函數(shù)
{
time0_init();//初始化
while(1)
{
if(flag3==0){convert(counter0);}//正常數(shù)據(jù)轉(zhuǎn)換
else if(flag3==1){{convert(counter1);}}//應(yīng)急數(shù)據(jù)轉(zhuǎn)換
scan();//倒計(jì)時(shí)顯示
//===================================================================================
if((flag1==0)&&(flag3==0))//南北通行
{
red0=green1=yellow0=yellow1=1;green0=red1=0;
if(counter0<=5){yellow0=red1=0;red0=green0=yellow1=green1=1;}
}
else if((flag1==1)&&(flag3==0))//東西通行
{
red1=yellow1=green0=yellow0=1;green1=red0=0;
if(counter0<=5){yellow1=red0=0;red1=green1=yellow0=green0=1;}
}
//===================================================================================
if(s3==0)//南北應(yīng)急按鍵
{
delay1m(3);
if(s3==0)
{
TR0=0;flag3=1;flag2=0;
TR1=1;counter1=30;
}while(!s3);
}
if(s2==0)//東西應(yīng)急按鍵
{
delay1m(3);
if(s2==0)
{
TR0=0;flag3=flag2=1;
TR1=1;counter1=30;
}while(!s2);
}
//====================================================================================
if((flag2==0)&&(flag3==1))//南北應(yīng)急通行
{
red0=yellow0=green1=yellow1=1;green0=red1=0;
if(counter1<=5){yellow0=red1=0;red0=green0=yellow1=green1=1;}
}
else if((flag2==1)&&(flag3==1))//東西應(yīng)急通行
{
red1=yellow1=green0=yellow0=1;green1=red0=0;
if(counter1<=5){yellow1=red0=0;red1=green1=yellow0=green0=1;}
}
}
}
void time0_interrupt()interrupt 1
{
TH0=(65535-50000)/256;//定時(shí)50ms
TL0=(65535-50000)%256;
num0++;
if(num0==20)//1s時(shí)間
{
num0=0;counter0--;
if(counter0==0){counter0=30;flag1=~flag1;}
}
}
void time1_interrupt()interrupt 3
{
TH1=(65535-50000)/256;//定時(shí)50ms
TL1=(65535-50000)%256;
num1++;
if(num1==20)//1s時(shí)間
{
num1=0;counter1--;
if(counter1==0){TR0=1;flag3=0;}
}
}
|
-
交通燈.png
(96.71 KB, 下載次數(shù): 42)
下載附件
2019-11-25 23:37 上傳
|