模拟交通灯控制(单片机C程序)
模拟交通灯控制
P0口段码
P1口位吗
用数码管动态显示剩余时间;
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);
}
}
模拟交通灯单片 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)