MCU C language programming: LED simulated traffic light

Publisher:水手谷水手Latest update time:2013-06-06 Source: 单片机C语言程序设计:LED 模拟交通灯 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/* Name: LED simulated traffic light

Note: The east-west direction green light is on for a few seconds, the yellow

The light flashes 5 times and then turns red.

The northbound traffic light changes from red to green, and after a few seconds the northbound

The yellow light flashes 5 times and then turns red.

Green light, repeat.

*/

#include

#define uchar unsigned char

#define uint unsigned int

sbit RED_A=P0^0; //East-West direction light

sbit YELLOW_A=P0^1;

sbit GREEN_A=P0^2;

sbit RED_B=P0^3; //North-South lights

sbit YELLOW_B=P0^4;

sbit GREEN_B=P0^5;

uchar Flash_Count=0,Operation_Type=1; //Number of flashes, operation type variable

 

 

 

 

//Delay

void DelayMS(uint x)

{

uchar i;


}

while(x--) for(i=0;i<120;i++);

 

//Traffic light switching

void Traffic_Light()

{

switch(Operation_Type)

{

case 1: //The east-west direction is green and the north-south direction is red

RED_A=1;YELLOW_A=1;GREEN_A=0;

RED_B=0;YELLOW_B=1;GREEN_B=1;

DelayMS(2000);

Operation_Type=2;

break;

case 2: //The yellow light flashes in the east-west direction, and the green light is off

DelayMS(300);

YELLOW_A=~YELLOW_A;GREEN_A=1;

if(++Flash_Count!=10) return; // Flash 5 times

Flash_Count=0;

Operation_Type=3;

break; [page]

case 3: //The east-west direction is red, and the north-south direction is green

RED_A=0;YELLOW_A=1;GREEN_A=1;

RED_B=1;YELLOW_B=1;GREEN_B=0;

DelayMS(2000);

Operation_Type=4;

break;

Case 4: //The yellow light flashes 5 times in the north-south direction

DelayMS(300);

YELLOW_B=~YELLOW_B;GREEN_B=1;

if(++Flash_Count!=10) return;

Flash_Count=0;

Operation_Type=1;

}

}

//Main program

void main()

{

while(1)Traffic_Light();

}

MCU C language programming: LED simulated traffic light

Keywords:MCU Reference address:MCU C language programming: LED simulated traffic light

Previous article:Single-chip C language programming: a single digital tube cyclically displays 0~9
Next article:Common delay and interrupt problems of single chip microcomputer and their solutions

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号