找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1755|回復(fù): 0
收起左側(cè)

簡單十字路交通紅綠燈單片機(jī)代碼

[復(fù)制鏈接]
ID:1073071 發(fā)表于 2023-5-22 18:59 | 顯示全部樓層 |閱讀模式
屏幕截圖 2023-05-22 185510.png

源代碼
#include <reg51.h>
sbit EastWestGreen = P1^2;
sbit EastWestYellow = P1^1;
sbit EastWestRed = P1^0;
sbit NorthSouthGreen = P1^3;
sbit NorthSouthYellow = P1^4;
sbit NorthSouthRed = P1^5;

#define TL0_INIT_VALUE 0x1F3F

void timer0_isr() interrupt 1
{
  char i;
  static unsigned char count = 0;
  TH0 = TL0_INIT_VALUE >> 8;
  TL0 = TL0_INIT_VALUE & 0xFF;
  i++;
  if(i==20)
   {
    i=0;
    count++;
        }


  if (count <= 15)
  {
    EastWestGreen = 0;
    EastWestYellow = 0;
    EastWestRed = 1;
  }
  else if (count <= 25)
  {  
    EastWestGreen = 1;
    EastWestYellow = 0;
    EastWestRed = 0;
  }
  else if (count <= 28) {  // 綠燈閃爍3s
    EastWestGreen = !EastWestGreen;
    EastWestYellow = 0;
    EastWestRed = 0;
  } else if (count <= 30) {  // 黃燈亮2s
    EastWestGreen = 0;
    EastWestYellow = 1;
    EastWestRed = 0;
  }
  else
  {  
        count = 0;
  }

  if (count <= 10)
  {
    NorthSouthGreen = 1;
    NorthSouthYellow = 0;
    NorthSouthRed = 0;
  }
  else if (count <= 13)
  {  
    NorthSouthGreen = !NorthSouthGreen;
    NorthSouthYellow = 0;
    NorthSouthRed = 0;
  }
  else if (count <= 15)
  {  
    NorthSouthGreen = 0;
    NorthSouthYellow = 1;
    NorthSouthRed = 0;
  }
  else {  
    NorthSouthGreen = 0;
    NorthSouthYellow = 0;
    NorthSouthRed = 1;
  }
}

void main()
{
  TMOD = 0x01;  
  TH0 = TL0_INIT_VALUE >> 8;  
  TL0 = TL0_INIT_VALUE & 0xFF;
  ET0 = 1;  
  EA = 1;  
  TR0 = 1;  

  while (1)
  {

  }
}

紅綠燈.7z

59.81 KB, 下載次數(shù): 10, 下載積分: 黑幣 -5

回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表