找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2125|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机交通灯控制电路

[复制链接]
跳转到指定楼层
楼主
#include<reg52.h>
#define  uint unsigned int;
#define  uchar unsigned char;
void  DigitalTube();   
void  InterruptT0();
sbit  P10 = P1^0;   
sbit  P11 = P1^1;
sbit  P12 = P1^2;
sbit  P13 = P1^3;
sbit  P14 = P1^4;
sbit  P15 = P1^5;
sbit  P16 = P1^6;  
sbit  P17 = P1^7;
sbit  P20 = P2^0;
sbit  P21 = P2^1;
sbit  K22 = P2^2;   
sbit  K23 = P2^3;
sbit  K24 = P2^4;
sbit dula = P2^5;
uint LedBuf[10] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};  
uint Kflag = 1;   
uint LedNumNS = 27;   
uint LedNumEW = 30;  
uint i = 0;
uint j = 0;
uint j1 = 0;
uint k = 0;
uint g = 0;
uint z = 0;   
uint LedTime1 = 30;
uint LedTime2 = 27;
uint LedTime3 = 30;
uint LedTime4 = 27;
uint Kf22 = 1;
uint Kf23 = 1;
uint Kf24 = 1;
uint KSta24 = 1;
uint KSta23 = 1;
uint KSta22 = 1;   
uchar Kflag22 = 0;  
void main()
{
EA = 1;   
ET0 = 1;   
TR0 = 1;
TMOD = 0x01;   
TH0 = 0xFC;
TL0= 0x67;
P1 = 0xEE;  
  while(1)
  {
  DigitalTube();  
  }
}

void InterruptT0() interrupt 1
{
    TH0 = 0xFC;
    TL0 = 0x67;
    i++;
    k++;
   
    KSta24 = K24;      
    if(Kf24 != KSta24)  
    {
     if(Kf24 == 1)   
     {
      Kflag = !Kflag;  
     }
     Kf24 = KSta24;
    }
   

   KSta23 = K23;      
   if(Kf23 != KSta23)  
   {
    if(Kf23 == 1)  
    {
     LedTime1 = LedTime1 +5;  
     LedTime2 = LedTime2 +5;  
     LedTime3 = LedTime3 +5;
     LedTime4 = LedTime4 +5;
     if(LedTime1 >=60)
      {
       LedTime1 = 10;
       LedTime2 = 7;
       LedTime3 = 10;
       LedTime4 = 7;
      }
     LedNumEW = LedTime1;
     LedNumNS = LedTime2;
    }
    Kf23= KSta23;
   }
  

   KSta22 = K22;      
   if(Kf22 != KSta22)  
   {
    if(Kf22 == 1)   
    {
     Kflag22 ++;
     if(Kflag22>= 3)
      {
      Kflag22 = 0;
      }
     switch(Kflag22)
     {
      case 1:
       LedTime1 = 50;
             LedTime2 = 47;
       LedTime3 = 20;
       LedTime4 = 17;
       LedNumEW = LedTime1;
       LedNumNS = LedTime2;

          break;
      case 2:
       LedTime1 = 20;
             LedTime2 = 17;   
       LedTime3 = 50;
       LedTime4 = 47;  
       LedNumEW = LedTime1;
       LedNumNS = LedTime2;
       break;
      case 0:
     
       LedTime1 = 30;
             LedTime2 = 27;
       LedTime3 = 30;
       LedTime4 = 27;
       LedNumEW = LedTime1;
       LedNumNS = LedTime2;
       break;
     }
    }
    Kf22 = KSta22;
   }
   
if(j >= 200)
  {
   j  = 0;
   j1++;
  }
   
if(i >= 1000)
{
  i = 0;
  LedNumNS--;  
  LedNumEW--;  
  switch(g)
  {     
   case 0:           
    if(LedNumEW == 3)      
    {
      P14 = ! P14;   
    if(Kf24 == Kflag)  P15 = ! P15;  
      LedNumNS = 3;
    }
    if(LedNumEW == 0)
    {
     LedNumNS =  LedTime3;
     LedNumEW =  LedTime4;
     P12 = ! P12;   
     P10 = ! P10;   
     P13 = ! P13;   
        if(Kf24 == Kflag)
     P15 = ! P15;   
     g = 1;
    }
    break;
   case 1:         
    if(LedNumNS == 3)   
     {
               P12 = !P12;
         P11 = !P11;
         LedNumEW = 3;
     }
    if(LedNumNS == 0)        
    {
     LedNumEW = LedTime1;
     LedNumNS = LedTime2;
    if(Kf24 == Kflag) P11 = !P11;   
     P10 = !P10;   
     P14 = !P14;
     P13 = !P13;
     g = 0;
    }
    break;
     }
  
     if(Kf24 != Kflag)   
  {
   if(j1 % 2 == 0)
   {
    P11 = ! P11;
    P15 = ! P15;
      }      
  }   
  }      
}

90~TA6%VB8TRFXOC{R(LI0Z.png (38.07 KB, 下载次数: 44)

90~TA6%VB8TRFXOC{R(LI0Z.png

51交通灯.rar

50.77 KB, 下载次数: 24, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表