村网通总站 姬家寨村 455496.nync.com 欢迎您!
《姬家寨村智能交通灯》程序代码
硬件电路设计:姬学瑞;软件程序编写:姬学瑞;
版权所有,侵权必究!姬家寨村电子科技研究所研制!
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit nb_red=P2^6;
sbit nb_blue=P2^4;
sbit nb_yellow=P2^5;
sbit dx_red=P2^7;
sbit dx_blue=P3^7;
sbit dx_yellow=P3^6;
sbit wei1=P3^2;
sbit wei2=P3^3;
sbit wei3=P3^4;
sbit wei4=P3^5;
sbit qiehuan=P2^0;
sbit shezhi=P2^1;
sbit bujinjia=P2^2;
sbit bujinjian=P2^3;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar count,count1,nanbei,dongxi,nan_bei,nan_bei1,dong_xi,dong_xi1,zhuangtai,
zhuangtai1,nanbei_num,dongxi_num,shezhi_count,qiehuan_count,
q_nbred,q_nbblue,q_dxred,q_dxblue;
uint num;
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display()
{
wei1=0;
P1=~table[nanbei/10];
delayms(1);
wei1=1;
P1=0xff;
wei2=0;
P1=~table[nanbei%10];
delayms(1);
wei2=1;
P1=0xff;
wei3=0;
P0=~table[dongxi/10];
delayms(1);
wei3=1;
P0=0xff;
wei4=0;
P0=~table[dongxi%10];
delayms(1);
wei4=1;
P0=0xff;
}
void key()
{
if(!shezhi)
{
delayms(10);
{
if(!shezhi)
{
if(shezhi_count<2)
{
shezhi_count++;
if(shezhi_count==1)
{
nanbei=nan_bei;
dongxi=dong_xi1;
}
if(shezhi_count==2)
{
nanbei=nan_bei1;
dongxi=dong_xi;
}
}
else
{
shezhi_count=0;
nanbei=nan_bei;
dongxi=dong_xi1;
TR0=1;
TR1=1;
}
while(!shezhi)display();
}
}
}
if(!bujinjia)
{
delayms(10);
{
if(!bujinjia)
{
switch(shezhi_count)
{
case 1: if(nanbei<100){nan_bei++;dong_xi1++;nanbei=nan_bei;dongxi=dong_xi1;}break;
case 2: if(nanbei<100){nan_bei1++;dong_xi++;nanbei=nan_bei1;dongxi=dong_xi;}break;
}
while(!bujinjia)display();
}
}
}
if(!bujinjian)
{
delayms(10);
{
if(!bujinjian)
{
switch(shezhi_count)
{
case 1: if(nanbei>0){nan_bei--;dong_xi1--;nanbei=nan_bei;dongxi=dong_xi1;}break;
case 2: if(nanbei>0){nan_bei1--;dong_xi--;nanbei=nan_bei1;dongxi=dong_xi;}break;
}
while(!bujinjian)display();
}
}
}
}
void she_zhi()
{
key();
while(shezhi_count)
{
TR0=0;
TR1=0;
key();
display();
}
}
void qiehuan_key()
{
if(!qiehuan)
{
delayms(10);
{
if(!qiehuan)
{
if(qiehuan_count<2)
{
qiehuan_count++;
switch(qiehuan_count)
{
case 1:nb_red=1;nb_yellow=0;nb_blue=1;dx_red=1;dx_yellow=0;dx_blue=1;break;
case 2:nb_red=0;nb_yellow=1;nb_blue=1;dx_red=1;dx_yellow=1;dx_blue=0;break;
}
}
else
{
qiehuan_count=0;
TR0=1;
TR1=1;
}
while(!qiehuan);
}
}
}
}
void qie_huan()
{
qiehuan_key();
while(qiehuan_count)
{
TR0=0;
TR1=0;
qiehuan_key();
}
}
void time_init()
{
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
EA=1;
}
void init()
{
time_init();
nanbei=22;
dongxi=22;
nan_bei=22;
nan_bei1=10;
dong_xi1=22;
dong_xi=10;
nb_red=0;
nb_blue=1;
nb_yellow=1;
dx_blue=0;
dx_red=1;
dx_yellow=1;
}
void main()
{
init();
while(1)
{
display();
she_zhi();
qie_huan();
}
}
void t0()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(!nanbei_num)
{
if(count==20)
{
count=0;
if(nanbei>2)
nanbei--;
else
{
nanbei_num=1;
nb_red=1;
nb_blue=1;
}
}
}
if(nanbei_num)
{
if(count<10)
nb_yellow=0;
if(count>10)
nb_yellow=1;
if(count>20)
{
count=0;
if(nanbei>0)
nanbei--;
else
{
nanbei_num=0;
zhuangtai=~zhuangtai;
if(zhuangtai)
{
nb_blue=0;nb_red=1;
nanbei=nan_bei1;
}
else
{
nb_red=0;nb_blue=1;
nanbei=nan_bei;
}
}
}
}
}
void t1()interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
count1++;
if(!dongxi_num)
{
if(count1==20)
{
count1=0;
if(dongxi>2)
dongxi--;
else
{
dongxi_num=1;
dx_red=1;
dx_blue=1;
}
}
}
if(dongxi_num)
{
if(count1<10)
dx_yellow=0;
if(count1>10)
dx_yellow=1;
if(count1>20)
{
count1=0;
if(dongxi>0)
dongxi--;
else
{
dongxi_num=0;
zhuangtai1=~zhuangtai1;
if(zhuangtai1)
{
dx_blue=1;dx_red=0;
dongxi=dong_xi;
}
else
{
dx_red=1;dx_blue=0;
dongxi=dong_xi1;
}
}
}
}
}