51 single chip microcomputer traffic light program and simulation

Publisher:WiseSage123Latest update time:2015-10-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#define uint unsigned int
#define uchar unsigned char
sbit red_nb = P0^0;
sbit green_nb = P0^1;
sbit yellow_nb = P0^2;

sbit red_dx = P0^3;
sbit green_dx = P0^4;
sbit yellow_dx = P0^5;

unsigned char code   Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};  //Segment code control

unsigned char code   dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F};   //bit selection control

unsigned char LedOut[4];

uchar switch_type = 1;
uchar Traffic_time = 0;
uchar ds_time = 0;
uchar i,t;

void delay(uint i)
{
    char j;
    for(i; i > 0; i--)
        for(j = 200; j > 0; j--);
}

void Traffic_Light()    // Define four status displays
{
 switch(switch_type)
 {
   case 1:
    for( i=0; i<4; i++)
      
       P1 = dispbit[i];   // Use table lookup method for bit selection
      P3 = LedOut[i];
     
      delay(100);
            // Make the digital tube scan and display continuously here

     red_nb = 0;    //North-South red light on, green light off, yellow light
     offgreen_nb = 1;
     yellow_nb = 1;
     
     red_dx = 1;     //East-West red light off, green light on, yellow light
     offgreen_dx = 0;
     yellow_dx = 1;
     if(Traffic_time != 30)
     {
    LedOut[0]=Disp_Tab[(35 - Traffic_time)0/10];    //Data displayed by digital tubeLedOut
     [1]=Disp_Tab[(35 - Traffic_time)];    
     LedOut[2]=Disp_Tab[(29 - Traffic_time)0/10];
    LedOut[3]=Disp_Tab[(29 - Traffic_time)];
        return;          //Return to case1
     }
     Traffic_time = 0;
     switch_type = 2;     //Enter the second statebreak
     ;

  case 2:
    for( i=0; i<4; i++)
      
     P1 = dispbit[i];   //Use table lookup method for bit selection
      P3 = LedOut[i];
      
      delay(100);
      
     red_nb = 0;    //North-South red light on, green light off, yellow light off
     green_nb = 1;
     yellow_nb = 1;
     
     red_dx = 1;     //East-West red light off, green light off, yellow light on
     green_dx = 1;
     
     if(Traffic_time != 6)    //Yellow light hold time
     
      if(Traffic_time != t)
      {
       t = Traffic_time;
       yellow_dx = ~yellow_dx;
        
     }
     {
    LedOut[0]=Disp_Tab[(5 - Traffic_time)0/10];
     LedOut[1]=Disp_Tab[(5 - Traffic_time)];    
     LedOut[2]=Disp_Tab[(5 - Traffic_time)0/10];
    LedOut[3]=Disp_Tab[(5 - Traffic_time)];
        return;
     }
     return; 
    }
     
       Traffic_time = 0;
    switch_type = 3;
       break;
     

  case 3:
  for( i=0; i<4; i++)
      
      P1 = dispbit[i];   //Use table lookup method for bit selection
      P3 = LedOut[i];
       
      delay(100);
      
     red_nb = 1;    //North-South red light is off, green light is on, and yellow light is off
     green_nb = 0;
     yellow_nb = 1;
     
     red_dx = 0;     //East-West red light is on, green light is off, and yellow light is off
     green_dx = 1;
     yellow_dx = 1;
     if(Traffic_time != 19)
     LedOut[0]=Disp_Tab[(24 - Traffic_time)0/10];
      LedOut[1]=Disp_Tab[(24 - Traffic_time)];    
      LedOut[2]=Disp_Tab[(18 - Traffic_time)0/10];
     LedOut[3]=Disp_Tab[(18 - Traffic_time)];
         return;
     }
     Traffic_time = 0;
     switch_type = 4;
     break;

  case 4:
  for( i=0; i<4; i++)
      
       P1 = dispbit[i];   //Use table lookup method for bit selection
      P3 = LedOut[i];
     
      delay(100);
      
     red_nb = 1;    //North-South red light is off, green light is off, and yellow light is on
     green_nb = 1;
     
     red_dx = 0;     //East-West red light is on, green light is off, and yellow light is off
     green_dx = 1;
     yellow_dx =1;

      if(Traffic_time != 6)    //黄等保持的时间
     
       if(Traffic_time != t)
       {
        t = Traffic_time;
        yellow_nb = ~yellow_nb;
        
     }
      LedOut[0]=Disp_Tab[(5 - Traffic_time)0/10];
       LedOut[1]=Disp_Tab[(5 - Traffic_time)];   
       LedOut[2]=Disp_Tab[(5 - Traffic_time)0/10];
      LedOut[3]=Disp_Tab[(5 - Traffic_time)];
      return; 
     
       Traffic_time = 0;
    switch_type = 1;
       break;   
    
     

   }
}

void main()
{
 TMOD = 0x01;
 TH0 = (65536 - 50000)/256;
 TL0 = (65536 - 50000)%6;
 EA = 1;    //Open the general interrupt
 ET0 = 1;   //Open the timer 0 interrupt
 TR0 = 1;   //Start the timer

 while(1)
 {
  Traffic_Light();


 }

}

void T0_time() interrupt 1
{
 TH0 = (65536 - 50000)/256;
 TL0 = (65536 - 50000)%6;
 ds_time++;
 if(ds_time == 20)
 {
  ds_time = 0;
  Traffic_time++; 
  
 }


}

 

 

51 MCU traffic light program and simulation
 


Reference address:51 single chip microcomputer traffic light program and simulation

Previous article:Common questions about using c8051fxxx microcontrollers are as follows
Next article:Working principle of 8051 microcontroller I/O pins

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号