新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)信號交通燈系統(tǒng)源代碼

51單片機(jī)信號交通燈系統(tǒng)源代碼

作者: 時(shí)間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
本程序所用的原理圖下載:點(diǎn)這里,單片機(jī)芯片使用的stc89c52

/*項(xiàng)目:交通燈控制系統(tǒng)
*項(xiàng)目設(shè)計(jì)交通燈用于控制行人和車輛依 次通過十字路口。
*其設(shè)計(jì)要求 如 下: 1)、首先車行道亮綠燈 45s,同時(shí)人行道亮紅 45s;
*45s 后,2)、車行道黃燈閃爍 3 次,亮、滅各 1s,
*此時(shí)人行道 仍維持紅燈;
*3)、6s 后,轉(zhuǎn)為人行道亮綠燈 20s,車行道亮 紅燈 20s;
*4)、20s 后,再轉(zhuǎn)到 第 1步,如此循環(huán)往復(fù); 5)
*51 系列單片機(jī)控制整個(gè)系統(tǒng)工 作; 6)交通信號燈用 LED 代 替;
*/
/**
*項(xiàng)目名稱:信號交通燈控制系統(tǒng);
*作者:徐冉;
*日期:2013-06-01-10:00-13:45;
*說明:開源項(xiàng)目,基于AT89C52-RC單片機(jī),51hei開發(fā)板;
*備注:系統(tǒng)已經(jīng)順利通過調(diào)試,完美通過測試并成功實(shí)現(xiàn)系統(tǒng)功能,耗時(shí)3小時(shí)45分鐘;
**/
/*******************AT89C52-RC單片機(jī)****************************/
/*************************HL-1開發(fā)板***************************/
#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; //定時(shí)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;
}
}

本文引用地址:http://m.butianyuan.cn/article/201612/324635.htm


評論


技術(shù)專區(qū)

關(guān)閉