Single chip traffic light C language program

Publisher:EtherealEssenceLatest update time:2015-10-26 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
   This is a C language program for a single-chip traffic light. The single-chip is AT89C51. Two common cathode two-in-one digital tubes display the countdown of two intersections respectively. 12 light-emitting diodes are used to simulate red, yellow and green lights. The circuit is simple and easy to make. Small electronic production .

My traffic light

#include
#define uchar unsigned char
bit flag;
sbit red1_1=P1^0;   //North-South red light D1
sbit yel1_1=P1^1;  //North-South yellow light D2
sbit gre1_1=P1^2;  //North-South green light D3
sbit red1_2=P1^3;  //North-South red light D4
sbit yel1_2=P1^4;  //North-South yellow light D5
sbit gre1_2=P1^5;  //North-South green light D6

sbit red2_1=P1^6;  //East-West red light D7
sbit yel2_1=P1^7;  //East-West yellow light D8
sbit gre2_1=P0^0;  //East-West green light D9
sbit red2_2=P0^1;  //East-West red light D10
sbit yel2_2=P0^2;  //East-West yellow light D11
sbit gre2_2=P0^3;  //East-West green light D12

flying code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};
fly n,n1,aa=0,shi,ge,shi1,ge1;
void init();
void xianshi1(fly shi,fly ge);
void xianshi2(flying shi1,flying ge1);
void delay(fly z)
{
 fly x,y;
 for(x=z;x>0
   ;x--) for(y=110;y>0;y--);
}
main()
{
 n=30;
   sh=n/10;
 ge=n;
   n1=30;
   shi1=n1/10;
 ge1=n1;

    init();
  while(1)
     {
     if(flag!=1)
      xianshi1(shi,ge);
   else
           xianshi2(shi1,ge1);
  
  }
}

void xianshi1(uchar shi,uchar ge)
{
    P3=0x02;
    P2=tab[ge];
    delay(200);
    P3=0x01;
    P2=tab[shi];
    delay(200);
}

//  http://www.duankudp.com/
void xianshi2(uchar shi1,uchar ge1)
{
 P3=0x08;
 P2=tab[ge1];
    delay(200);
    P3=0x04;
    P2=tab[shi1];
    delay(200);

}

void init()

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

void timer0() interrupt 1
{
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%6;
 aa++;
   if(flag!=1)
     
   if(aa==20)
     {
        aa=0;
                    red1_1=0;red1_2=0;
                    gre2_1=0;gre2_2=0;  //Start the north-south red light 1 and the east-west green light 2    

        n--;
                 shi=n/10;
        ge=n;

               if(n<=3)
                  {
      red1_1=0;red1_2=0;
      gre2_1=1;gre2_2=1;
                    yel2_1=0;yel2_2=0;//Start the north-south red light 1 and the east-west yellow light 2
     
                   if(n<=0)
          {
                      n=30;          
                         red1_1=1;red1_2=1;
       gre1_1=0;gre1_2=0;
       yel2_1=1;yel2_2=1;
       red2_1=0;red2_2=0;//Start the north-south green light 1 and the east-west red light 2 http://www.5imcu.net/
 
                       TR0=0;
                         TH0=(65536-50000)/256;
                      TL0=(65536-50000)%6;
          TR0=1;
                         aa++;
          flag=1;

                }
                   }
               }
     }
   else
   {
      if(flag==1)
         
          //   TH0=(65536-50000)/256;
       //   TL0=(65536-50000)%6;
       //   TR0=1;
          //   aa++;
          if(aa==20)
     {
           aa=0;                       
                 n1--;
                 shi1=n1/10;
                   ge1=n1;
        if(n1<=3)
         {
   gre1_1=1;gre1_2=1;
            yel1_1=0;yel1_2=0;
           red2_1=0;red2_2=0;//Start the north-south yellow light 1 and the east-west red light 2
                                 
                if(n1<=0)
       {
          n1=30;
                         yel1_1=1;yel1_2=1;
                         red2_1=1;red2_2=1;

                 TR0=0;
               TH0=(65536-50000)/256;
               TL0=(65536-50000)%6;
               TR0=1;
                 flag=0;
               }
                   }
                }
         }
     }
}

Keywords:MCU Reference address:Single chip traffic light C language program

Previous article:Ultrasonic ranging obstacle avoidance program
Next article:Single chip microcomputer 2408 dot matrix clock electronic production

Recommended ReadingLatest update time:2024-11-16 14:35

GigaDevice Innovation launches the GD32F5 series of Cortex®-M33 core MCUs, providing new options for industrial high-performance applications
Beijing, China (March 7, 2024) - GigaDevice (stock code 603986), the industry's leading semiconductor device supplier, today announced the official launch of the GD32F5 series of high-performance microcontrollers based on the Arm® Cortex®-M33 core. It is fully adapted to application scenarios such as energy and power,
[Embedded]
GigaDevice Innovation launches the GD32F5 series of Cortex®-M33 core MCUs, providing new options for industrial high-performance applications
Macro definition of EMC microcontroller EM78P260
(1) The most commonly used PAGE and BANK    EMC ICs are divided into several pages and banks. Low-end EM78P156 and others only have one bank and one page, so there is no need to switch. Newer  ICs basically need to switch. This frequently used bank is most suitable to be made into a macro. The code is as follows: 
[Microcontroller]
How to select PIC microcontroller memory bank 0 or 1
The data memory of the intermediate product PIC16C6X of PIC microcontroller is usually divided into two banks, namely, bank 0 (Bank0) and bank 1 (Bank1). Each bank is composed of two parts: dedicated registers and general registers. Some register units in the two banks are actually the same register units, but have dif
[Microcontroller]
Reset circuit of 89c51 microcontroller, detailed explanation of 89c51 reset circuit diagram
  The purpose of the reset circuit is to provide a level opposite to the normal working state at the moment of power-on. Generally, the principle that the capacitor voltage cannot change suddenly is used to connect the capacitor and the resistor in series. When the power is turned on, the capacitor is not charged and
[Microcontroller]
Reset circuit of 89c51 microcontroller, detailed explanation of 89c51 reset circuit diagram
The use of other peripheral devices of single chip microcomputer
Use of buzzer: The buzzer frequency is roughly set to 20ms or 50hz. #include reg52.h #define uint unsigned int #define uchar unsigned char sbit fen = P2^6; void delay(uint z); main() {     while(1)     {         delay(1); //Delay 10 milliseconds Note that the output here is a 20 millisecond square wave          
[Microcontroller]
AVR MCU PCF8591 conversion example source program operation library
Target system: Based on AVR microcontroller                                                  Application software: ICCAVR                                                       Version: Version 1.0                                                        Experimental content: Initialize, read AD, output DA, use the LED o
[Microcontroller]
Fast Algorithm for Binary-to-Decimal Conversion of 51 Single-Chip Microcomputer Integers
After more than 20 years of development, the 89C5l series of microcontrollers are still thriving and widely used in industrial control and instrumentation. The Keil C51 compiler system used for 89C5l microcontroller software development has also matured and become the preferred choice for 89C5l series microcontrolle
[Microcontroller]
Fast Algorithm for Binary-to-Decimal Conversion of 51 Single-Chip Microcomputer Integers
Summary of six common problems in single chip alarm system
What are the common methods for using microcontroller application systems to generate alarms? The commonly used method is to send the collected data to the microcontroller application system for processing, and compare it with the upper and lower limit given values ​​of the parameter. If it exceeds the given value,
[Microcontroller]
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号