微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机信号交通灯系统源代码

51单片机信号交通灯系统源代码

时间:12-02 来源:互联网 点击:
本程序所用的原理图下载:点这里,单片机芯片使用的stc89c52

/*项目:交通灯控制系统
*项目设计交通灯用于控制行人和车辆依 次通过十字路口。
*其设计要求 如 下: 1)、首先车行道亮绿灯 45s,同时人行道亮红 45s;
*45s 后,2)、车行道黄灯闪烁 3 次,亮、灭各 1s,
*此时人行道 仍维持红灯;
*3)、6s 后,转为人行道亮绿灯 20s,车行道亮 红灯 20s;
*4)、20s 后,再转到 第 1步,如此循环往复; 5)
*51 系列单片机控制整个系统工 作; 6)交通信号灯用 LED 代 替;
*/
/**
*项目名称:信号交通灯控制系统;
*作者:徐冉;
*日期:2013-06-01-10:00-13:45;
*说明:开源项目,基于AT89C52-RC单片机,51hei开发板;
*备注:系统已经顺利通过调试,完美通过测试并成功实现系统功能,耗时3小时45分钟;
**/
/*******************AT89C52-RC单片机****************************/
/*************************HL-1开发板***************************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
//车行道:信号交通灯
sbit cled_hong = P1^0; // 红灯
sbit cled_lv = P1^2; //绿灯
sbit cled_huang = P1^4; //黄灯
//人行道:信号交通灯
sbit rled_hong = P1^1; //红灯
sbit rled_lv = P1^3; //绿灯
//table:code
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
uchar sec, counter, counter1, flag, flag1;
//timer1&timer0:init
void init()
{
P1 = 0xff;
TMOD = 0x11;//T0,T1 工作方式1
TH0 = 0xB8; //定时20ms
TL0 = 0x00;
TH1 = 0xB8;
TL1 = 0x00;
TR0 = 1;
EA = 1;
ET0 = 1;
ET1 = 1;
}
//delay:xms
void delay(uint xms)
{
uchar i, j;
for(i = 0; i < xms; i++)
for(j = 0; j < 110; j++);
}
//display:
void display(uchar sec)
{
uchar cshi, cge;
cshi = sec / 10 % 10;
cge = sec % 10;
dula = 1;
P0 = table[cshi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 = table[cge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(1);
}
//main:
void main(void)
{
init();
while(1)
{
display(sec);
rled_lv = 1;
cled_hong = 1;
rled_hong = 0;
cled_lv = 0;
if(flag == 1)
{
TR1 = 1;
rled_hong = 0;
cled_lv = 1;
}
if(flag1 == 1)
{
TR1 = 0;
cled_huang = 1;
rled_hong = 1;
cled_lv = 1;
rled_lv = 0;
cled_hong = 0;
}
}
}
//timer0:int
void timer0_int() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
if(counter == 50)
{
counter = 0;
sec++;
if(sec >= 45)
{
flag = 1;
}
if(sec >= 51)
{
flag1 = 1;
flag = 0;
}
if(sec == 71)
{
sec = 0;
flag1 = 0;
P1 = 0xff;
rled_hong = 0;
cled_lv = 0;
rled_lv = 1;
cled_hong = 1;
cled_huang = 1;
}
}
}
//timer1:int
void timer1_int() interrupt 3
{
TH1 = 0XB8;
TL1 = 0X00;
counter1++;
if(counter1 == 50)
{
counter1 = 0;
cled_huang = ~cled_huang;
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top