模擬交通燈控制(單片機C程序)
模擬交通燈控制
P0口段碼
P1口位嗎
用數(shù)碼管動態(tài)顯示剩余時間;
P2口顯示紅綠黃燈;
東西方向燈分別用P00東西綠,P01東西黃,P02東西紅;
P05南北綠,P06南北黃,P07南北紅。
*/
#include
#define uint unsigned int
#define uchar unsigned char
uchar code dula[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
uchar code wula[]={0xfe,0xfd};
sbit ds_green=P2^0;
sbit ds_yellou=P2^1;
sbit ds_red=P2^2;
sbit nb_green=P2^5;
sbit nb_yellou=P2^6;
sbit nb_red=P2^7;
uchar flag1s,num1,num2,green,yellou,temp;
bit flag500ms,flag1;
void delay(uint z);
void disp();
void main()
{
green=30;
yellou=5;
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
EA=1;
TR0=1;
while(1)
disp();
}
//--------------------
void delay(uint z)
{
uint x,y;
for(x=0;x
}
//----------------------
void timer0() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
num1++;
if(num1==50)
{num1=0;
num2++;
flag500ms=~flag500ms;
if(num2==2)
{num2=0;
flag1s++;flag1=1;
if(flag1s>70)
flag1s=0;
}
}
if(flag1s<=30)
{ds_green=0;ds_red=1;ds_yellou=1;
nb_red=0;nb_yellou=1;nb_green=1;
}
if((flag1s>30)&&(flag1s<=35))
{ds_green=1;ds_red=1;
nb_red=0;nb_yellou=1;
nb_green=1;
ds_yellou=flag500ms;
}
if((flag1s>35)&&(flag1s<=65))
{ds_green=1;ds_red=0;ds_yellou=1;
nb_red=1;nb_yellou=1;nb_green=0;}
if((flag1s>65)&&(flag1s<=70))
{ds_green=1;ds_red=0;ds_yellou=1;
nb_red=1;nb_green=1;
nb_yellou=flag500ms;
}
}
//---------------------------
void disp()
{ uchar i;
if((flag1s<=30)||((flag1s>35)&&(flag1s<=65)))
{yellou=5;
if(flag1==1)
{flag1=0;green--;}
P0=dula[green/10];
P1=wula[0];
delay(5);
P0=dula[green%10];
P1=wula[1];
delay(5);
}
if(((flag1s>30)&&(flag1s<=35))||((flag1s>65)&&(flag1s<=70)))
{green=30;
if(flag1==1)
{flag1=0;yellou--;}
P0=dula[yellou/10];
P1=wula[0];
delay(5);
P0=dula[yellou%10];
P1=wula[1];
delay(5);
}
}
評論