基于51单片机的模拟交通灯
时间:12-02
来源:互联网
点击:
/*-----------------------------------------------
注:P1口连接至LED用于显示红绿灯,P0口接数码管段码,用于显示时间;
两个74HC573控制段码和位码。段码接P20,位码接P21
------------------------------------------------*/
#include
#define uchar unsigned char
#define uint unsigned int
sbit DUAN=P2^0;
sbit WEI=P2^1;
/*------------------------------------------------
全局变量
------------------------------------------------*/
bit red,green,yellow,turnred;//定义红绿黄灯标志位
code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00} ;
//共阴数码管0-9
uchar shi,ge;
/*------------------------------------------------
------------------------------------------------*/
void delay(uint t)
{
while(t--);
}
/*------------------------------------------------
主程序
------------------------------------------------*/
main()
{
TMOD |=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;//开中断
ET0=1;
TR0=1;//开定时器
P1=0xfc;//红灯亮
red =1;
while(1)
{
WEI=1;
P0=0xfe;
WEI=0;
DUAN=1;
P0=shi;//显示十位
DUAN=0;
delay(300);
WEI=1;
P0=0xfd;
WEI=0;
DUAN=1;
P0=ge;//显示十位
DUAN=0;
delay(300);
}
}
/*------------------------------------------------
定时器0中断函数
------------------------------------------------*/
void tim(void) interrupt 1
{
static uchar second=60,count;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
count++;
if (count==20)
{
count=0;
second--;//秒减1
if(second==0)
{
if(red)
{
red=0;yellow=1;
second=5;
P1=0xF3;//黄灯亮5秒
}
else if(yellow && !turnred)
{
yellow=0;green=1;
second=50;
P1=0xCF;//绿灯亮50秒
}
else if(green)
{
yellow=1;green=0;
second=5;
P1=0xF3;//黄灯亮5秒
turnred=1;
}
else if(yellow && turnred)
{
red=1;yellow=0;
P1=0xFC;//红灯亮60秒
second=60;
turnred=0;
}
}
shi=tab[second/10];
ge=tab[second%10];
}
}
注:P1口连接至LED用于显示红绿灯,P0口接数码管段码,用于显示时间;
两个74HC573控制段码和位码。段码接P20,位码接P21
------------------------------------------------*/
#include
#define uchar unsigned char
#define uint unsigned int
sbit DUAN=P2^0;
sbit WEI=P2^1;
/*------------------------------------------------
全局变量
------------------------------------------------*/
bit red,green,yellow,turnred;//定义红绿黄灯标志位
code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00} ;
//共阴数码管0-9
uchar shi,ge;
/*------------------------------------------------
------------------------------------------------*/
void delay(uint t)
{
while(t--);
}
/*------------------------------------------------
主程序
------------------------------------------------*/
main()
{
TMOD |=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;//开中断
ET0=1;
TR0=1;//开定时器
P1=0xfc;//红灯亮
red =1;
while(1)
{
WEI=1;
P0=0xfe;
WEI=0;
DUAN=1;
P0=shi;//显示十位
DUAN=0;
delay(300);
WEI=1;
P0=0xfd;
WEI=0;
DUAN=1;
P0=ge;//显示十位
DUAN=0;
delay(300);
}
}
/*------------------------------------------------
定时器0中断函数
------------------------------------------------*/
void tim(void) interrupt 1
{
static uchar second=60,count;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
count++;
if (count==20)
{
count=0;
second--;//秒减1
if(second==0)
{
if(red)
{
red=0;yellow=1;
second=5;
P1=0xF3;//黄灯亮5秒
}
else if(yellow && !turnred)
{
yellow=0;green=1;
second=50;
P1=0xCF;//绿灯亮50秒
}
else if(green)
{
yellow=1;green=0;
second=5;
P1=0xF3;//黄灯亮5秒
turnred=1;
}
else if(yellow && turnred)
{
red=1;yellow=0;
P1=0xFC;//红灯亮60秒
second=60;
turnred=0;
}
}
shi=tab[second/10];
ge=tab[second%10];
}
}
51单片机模拟交通 相关文章:
- 51单片机模拟交通灯(11-29)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)