找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1639|回復: 0
打印 上一主題 下一主題
收起左側

簡單十字路交通紅綠燈單片機代碼

[復制鏈接]
跳轉到指定樓層
樓主
ID:1073071 發(fā)表于 2023-5-22 18:59 | 只看該作者 |只看大圖 回帖獎勵 |正序瀏覽 |閱讀模式


源代碼
#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, 下載次數: 10, 下載積分: 黑幣 -5

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

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

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表