當前位置:編程學習大全網 - 編程語言 - 求高人幫忙單片機交通燈編程

求高人幫忙單片機交通燈編程

//以下代碼經過Keil C51軟件調試通過

#include "reg51.h" //頭文件包含

//宏定義

#define uint unsigned int

#define uchar unsigned char

#define ON 0 //0的時候燈亮

#define OFF 1 //1的時候燈亮

//管腳定義

//南北道 黃燈亮3秒 紅燈亮 30秒 黃燈3秒 綠燈38秒

//東西道 黃燈亮3秒 綠燈亮 28秒 黃燈3秒 紅燈40秒

sbit PortDX_Yellow = P1^0; //東西方向黃燈

sbit PortDX_Red = P1^1; //東西方向紅燈

sbit PortDX_Green = P1^2; //東西方向綠燈

sbit PortNB_Yellow = P1^3; //南北方向黃燈

sbit PortNB_Red = P1^4; //南北方向紅燈

sbit PortNB_Green = P1^5; //南北方向綠燈

void delay(uint ms) // 毫秒級延時(AT89C52 @ 11.0592MHz)

{

uint ti;

while(ms--)

{

for(ti=0;ti<112;ti++){}//112

}

}

/*==================== 主函數 ====================*/

void main(void)

{

delay(400); //開機延時400ms

while(1)

{

PortNB_Yellow = ON ;PortNB_Red = OFF;PortNB_Green = OFF;//南北黃燈亮3秒

PortDX_Yellow = ON ;PortDX_Red = OFF;PortDX_Green = OFF;//東西黃燈亮3秒

delay(3000);

PortNB_Yellow = OFF;PortNB_Red = ON ;PortNB_Green = OFF;//南北紅燈亮28秒

PortDX_Yellow = OFF;PortDX_Red = OFF;PortDX_Green = ON ;//東西綠燈亮28秒

delay(28000);

PortNB_Yellow = OFF;PortNB_Red = ON ;PortNB_Green = OFF;//南北紅燈亮2秒

PortDX_Yellow = ON ;PortDX_Red = OFF;PortDX_Green = OFF;//東西黃燈亮2秒

delay(2000);

PortNB_Yellow = ON ;PortNB_Red = OFF;PortNB_Green = OFF;//南北黃燈亮1秒

PortDX_Yellow = ON ;PortDX_Red = OFF;PortDX_Green = OFF;//東西黃燈亮1秒

delay(1000);

PortNB_Yellow = ON ;PortNB_Red = OFF;PortNB_Green = OFF;//南北黃燈亮2秒

PortDX_Yellow = OFF;PortDX_Red = ON ;PortDX_Green = OFF;//東西紅燈亮2秒

delay(2000);

PortNB_Yellow = OFF;PortNB_Red = OFF;PortNB_Green = ON ;//南北綠燈亮38秒

PortDX_Yellow = OFF;PortDX_Red = ON ;PortDX_Green = OFF;//東西紅燈亮38秒

delay(3800);

}

}

1.當為黃燈時南北、東西兩道同時為黃燈;以提示行人或車輛下壹個燈色即將到來時間 3秒。

2.當南北為紅燈,南北道車輛禁止通行,時間30秒;南北道為綠燈,南北道車輛通過時間38秒 。

3.當東西道綠燈,東西道車輛通行,時間為28秒;東西道為紅燈,東西道車輛禁止通過時間為40秒。

4.這樣如上表的時間和紅、綠、黃出現的順序依次出現這樣行人和車輛就能安全暢通的通行。

  • 上一篇:月亮獅子女是狠角色,十二月亮星座的崽崽要這樣養
  • 下一篇:產品經理常用的軟件可推薦的有哪些
  • copyright 2024編程學習大全網