Single chip C language programming: timer control traffic light

Publisher:Blissful567Latest update time:2013-09-05 Source: 21icKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 /* Name: Timer controlled traffic light

 

 

        Note: After the east-west green light is on for 5 seconds, the yellow light flashes, and then the red light flashes 5 times.

        After the red light comes on, the north-south direction turns from red to green, and after 5 seconds the north-south direction lights flash yellow.

        After flashing 5 times, the red light turns on, and the east-west light turns on green, and so on.

*/

#include

#define uchar unsigned char

#define uint unsigned int

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

sbit YELLOW_A=P0^1;

sbit GREEN_A=P0^2;

sbit RED_B=P0^3; //North-South direction indicator light

sbit YELLOW_B=P0^4;

sbit GREEN_B=P0^5;

//Delay multiple, number of flashes, operation type

variable

uchar Time_Count=0,Flash_Count=0,Operation_Type=1;

//Timer 0 interrupt function

void T0_INT() interrupt 1

{

TL0=-50000/256;

TH0=-50000%256;

switch(Operation_Type)

{

case 1: //The east-west green light and the north-south red light are on for 5 seconds

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

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

if(++Time_Count!=100) return; //5s (100*50ms) switch

Time_Count=0;

Operation_Type=2;

break; [page]

case 2: //The east-west yellow light starts flashing and the green light turns off

if(++Time_Count!=8) return;

Time_Count=0;

YELLOW_A=~YELLOW_A;GREEN_A=0;

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

Flash_Count=0;

Operation_Type=3;

break;  

Case 3: //The east-west red light and the north-south green light are on for 5 seconds

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

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

if(++Time_Count!=100) return; //5s (100*50ms) switch

Time_Count=0;

                                        Operation_Type=4;

break;

case 4: //The yellow light in the north-south direction starts flashing, and the green light turns off

if(++Time_Count!=8) return;

Time_Count=0;

                                        YELLOW_B=~YELLOW_B;GREEN_A=0;

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

                                        Flash_Count=0;

                                        Operation_Type=1;

                                        break;

             }

      }

//Main program

void main()

{

TMOD=0x01; //T0 mode 1

IE=0x82;

TR0=1;

while(1);

}

Single chip C language programming: timer control traffic light

 

Keywords:MCU Reference address:Single chip C language programming: timer control traffic light

Previous article:Microcontroller C language programming: alarm and rotating light
Next article:New IC and topology break through automotive data network bottleneck

Latest Microcontroller Articles
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号