Diode (STC89C52): Write a program to implement a traffic light

Publisher:悠闲之旅Latest update time:2016-03-08 Source: eefocusKeywords:diode Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Hardware Design
Diode (STC89C52): Write a program to implement a traffic light
 
2. Software Design
1. Program Features
Write a program to implement a traffic light:  the east-west green light is on for a few seconds, the yellow light flashes 5 times and then the red light is on. After the red light is on, the north-south light changes from red to green. After a few seconds, the north-south yellow light flashes 5 times and then turns red. The east-west light changes to green, and this repeats.
 
2. Program source code
#include
 
#define uchar unsigned char
#define uint unsigned int
 
sbit red_east_west = P1 ^ 0;
sbit yellow_east_west = P1 ^ 1;
sbit green_east_west = P1 ^ 2;
 
sbit red_south_north = P1 ^ 3;
sbit yellow_south_north = P1 ^ 4;
sbit green_south_north = P1 ^ 5;
 
float flash_count = 0;
uchar operation_type = 1;
 
void delayms(uint xms);
void traffic_light();
 
void main()
{
    while(1)
        traffic_light();
}
 
void traffic_light()
{
    switch(operation_type)
    {
    case 1: //green between east and west, red between south and north
        red_east_west = 1; yellow_east_west = 1; green_east_west = 0;
        red_south_north = 0; yellow_south_north = 1; green_south_north = 1;
        delayms(2000);
        operation_type = 2;
        break;
    case 2: //yellow flash between east and west, green close between east and west
        delayms(300);
        yellow_east_west = ~yellow_east_west; green_east_west = 1;
        if (++flash_count != 10)
            return;
        flash_count = 0;
        operation_type = 3;
       break;
    case 3:  //red between east and west, green between south and north
        red_east_west = 0; yellow_east_west = 1; green_east_west = 1;
        red_south_north = 1; yellow_south_north = 1; green_south_north = 0;
       delayms(2000);
        operation_type = 4;
       break;
    case 4: //yellow flash between south and north
       delayms(300);
        yellow_south_north = ~yellow_south_north; green_south_north = 1;
       if (++flash_count != 10)
           return;
       flash_count = 0;
        operation_type = 1;
       break;
    }
}
 
void delayms(uint xms)
{
    uint i, j;
 
    for (i = xms; i > 0; i--)
        for (j = 110; j > 0; j--)
            ;
}

Keywords:diode Reference address:Diode (STC89C52): Write a program to implement a traffic light

Previous article:Diode (STC89C52): Write a program to control the first diode to flash according to the time
Next article:Serial port communication AD collects signals, displays them on the digital tube, and sends them to the host computer

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号