Simulate traffic light control (MCU C program)

Publisher:SereneSerenityLatest update time:2016-09-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*

Simulate traffic light control
P0 port segment code
P1 port position
Use digital tube to dynamically display remaining time;
P2 port displays red, green and yellow lights;
east and west direction lights use P00 east and west green, P01 east and west yellow, and P02 east and west red respectively;

   P05 is green in the north and south, P06 is yellow in the north and south, and P07 is red in the north and south.
 

*/

#include
#define uint unsigned int 
#define uchar unsigned char
uchar code dula[]={0x3f,0x06,0x5b,0x4f,0x66,
     0x6d,0x7d,0x07,0x7f,0x6f};
uchar code wula[] ={0xfe,0xfd};
sbit ds_green=P2^0;
sbit ds_yellou=P2^1;
sbit ds_red=P2^2;
sbit nb_green=P2^5;
sbit nb_yellou=P2^6;
sbit nb_red=P2^7;
uchar flag1s,num1,num2,green,yellow,temp;
bit flag500ms,flag1;
void delay(uint z);
void disp();
void main()
{
green=30;
yellou=5;

TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
EA=1;
TR0=1;

while(1)
disp();


}
//--------------------
void delay(uint z)
{
 uint x,y;
   for(x=0;x  for(y =0;y<112;y++);
}
//----------------------
void timer0() interrupt 1
{
TH0=(65536-10000)/ 256;
TL0=(65536-10000)%256;
num1++;
 if(num1==50)
 {num1=0;
 num2++;
 flag500ms=~flag500ms;
 if(num2==2)
  {num2=0;
  flag1s++;flag1=1 ;
         if(flag1s>70)
          flag1s=0;
  }
 }
  if(flag1s<=30)
     {ds_green=0;ds_red=1;ds_yellou=1;
  nb_red=0;nb_yellou=1;nb_green=1;
  }
    if((flag1s>30)&&(flag1s<=35))
     { ds_green=1;ds_red=1;
  nb_red=0;nb_yellou=1;
  nb_green=1;
  ds_yellou =flag500ms;
  }
    if((flag1s>35)&&(flag1s<=65))
     {ds_green=1;ds_red=0;ds_yellou=1;
  nb_red=1;nb_yellou=1;nb_green=0;}
 if((flag1s> 65)&&(flag1s<=70))
     {ds_green=1;ds_red=0;ds_yellou=1;
  nb_red=1;nb_green=1;
  nb_yellou=flag500ms;
      }
  
    
}
//---------------------------
void disp()
{ uchar i;
 if((flag1s<=30 )||((flag1s>35)&&(flag1s<=65)))
     {yellou=5;
       if(flag1==1)
        {flag1=0;green--;}
        P0=dula[green/10];
   P1 =wula[0];
        delay(5);
  P0=dula[green%10];
   P1=wula[1];
        delay(5);
      }
 if(((flag1s>30)&&(flag1s<=35))| |((flag1s>65)&&(flag1s<=70)))
  {green=30;
       if(flag1==1)
        {flag1=0;yellou--;}
      P0=dula[yellou/10];
   P1=wula[0];
        delay(5);
  P0=dula[yellou%10];
   P1=wula[1];
        delay(5);
      }

}

Reference address:Simulate traffic light control (MCU C program)

Previous article:24C02 storage and reading
Next article:Simple addition calculator single chip c programming

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号