MCU traffic light with interrupt

Publisher:BlissfulHeartLatest update time:2020-12-05 Source: 51heiKeywords:Interrupt Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


MCU traffic light source program with interrupt:

#include

#include

#define uchar unsigned char

sbit c=P1^1;

sbit d=P1^4;

uchar led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0 to 9 digital tube display

void delay1(uchar j) //delay several ms

{ fly i;

while(j--)

{for(i=0;i<125;i++)

for(i=0;i<125;i++);}

}

void delay(uchar i) //delay 1 second

{

    unsigned char a,b,c;

                while(i--)

    {for(c=167;c>0;c--)

        for(b=171;b>0;b--)

            for(a=16;a>0;a--);

    _nop_; }

}

void nanbei(uchar a) //North-South traffic lights

{ fly j = 100;

P1=0X24;

for(a;a>0;a--)

{

while(j--)

{

P0=0XFE;

P2=led[a/10]; //display the ten-digit countdown

delay1(5); // Delay 5 milliseconds to give the machine some time to react

P0=0XFd;

P2=led[a%10]; //display countdown

delay1(5); // Delay 5 milliseconds to give the machine some time to react

if(a<=3) //If a is less than 3, the yellow light starts flashing

{

P1=0X12;               

delay1(10);

P1=0X00;

delay1(10);

}

}

}

}

void dongxi(uchar a)//East and West display traffic lights

{ fly j = 100;

P1=0X09;

for(a;a>0;a--)

{

while(j--)

{

P0=0XFB;

P2=led[a/10]; //display the ten-digit countdown

delay1(5); // Delay 5 milliseconds to give the machine some time to react

P0=0XF7;

P2=led[a%10]; //display countdown

delay1(5); // Delay 5 milliseconds to give the machine some time to react

if(a<=3) //If a is less than 3, the yellow light starts flashing

{

P1=0X12;               

delay1(10);

P1=0X00;

delay1(10);

}

}

}

}

main()

{EA=1; //Enable interrupt

EX0=1; //Enable external interrupt 0

IT0=1; //Edge trigger

while(1)

{dongxi(15); //East-west travel time 15 seconds

nanbei(30); //North-South travel time: 30 seconds

}

}

void into(void) interrupt 0 //External interrupt 0 function, used to forcibly change the direction of the traffic light

{fly i,j=1000,b;

P0=0XFF;

P1=P1&0X01; //If the last digit of P1 is 1, the east-west direction is green and the north-south direction is red

if(P1==0X01)

{P1=0X08;

for(b=0;b<3;b++)

{{for(i=0;i<10;i++)//Number of times the yellow light flashes

{delay1(100);

c=~c;

delay1(100);

}}}

P1=0Xe4;

delay(10); //The time delay after the change is 10 seconds

}

else

{P1=0X04;

for(b=0;b<3;b++)

{{for(i=0;i<10;i++)

{delay1(100);

d=~d;

delay1(100);

}}}

P1=0X09;

delay(10); //The time delay after the change is 10 seconds

}}   


Keywords:Interrupt Reference address:MCU traffic light with interrupt

Previous article:STC15W4K60S2 MCU 2-way SPWM source program can drive the motor
Next article:MCU + UA741 amplification shaping 1~10K frequency meter

Recommended ReadingLatest update time:2024-11-25 05:55

Using Proteus to learn 51 single chip microcomputer interrupt
For 52 single-chip microcomputer, there are 6 interrupt sources, which are described as follows (the serial number is used for writing interrupt programs): Interrupt source name Default Level Serial number illustrate INT0 Highest 0 External interrupt 0, introduced by P3.2 port line, caused by low level or falling e
[Microcontroller]
Based on 51 single chip microcomputer serial port control LED light source program
/*Note that the baud rate is set to 4800, and the LED can be turned on and off by continuously inputting 0 to the serial port*/ #include "reg52.h" //This file defines some special function registers of the microcontroller typedef unsigned int u16; //Declare and define the data type typedef unsigned char u8; sbit led0
[Microcontroller]
51 MCU - Multi-file Programming First Test
Although the delay function is rarely used, it sometimes comes in handy when you need to test the code, so it is also put into the "function.c" file. 1. The code of function.c #include reg52.h   #include function.h      u8 code LedChar ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x
[Microcontroller]
51 MCU - Multi-file Programming First Test
4-Design of wireless bed call system based on 51 single chip microcomputer
Specific implementation functions The system consists of STC89C51 microcontroller + 315M wireless communication module + SC2262/SC2272 encoding and decoding module + LCD1602 liquid crystal display + button + buzzer + power supply. Specific functions: 1. When the patient presses the button on the remote control pan
[Microcontroller]
4-Design of wireless bed call system based on 51 single chip microcomputer
51 MCU C language learning 3
Lesson 3 Generate HEX file and minimize the system   Before we begin the main content of C language, let's first look at how to use KEIL uVISION2 to compile and generate HEX files for burning chips. The HEX file format is data information arranged by address proposed by Intel. The data width is bytes
[Microcontroller]
Development of Single Chip Microcomputer Control System for Beverage Filling Machine
1. Introduction: There are many types of beverage filling machines today. They can be divided into two categories according to their scale. One is a large-scale filling production line, which is suitable for large enterprises, with large investments, high automation and complex control systems. The other is a s
[Microcontroller]
Why does the microcontroller have multiple sets of VDD?
We all know that general IC components usually only have two power supply pins, one is Vcc or Vdd, and the other is Gnd or Vss. However, today's MCUs usually have multiple sets of power pins. What is the reason for this? Taking a certain 100-pin MCU as an example, as shown in the figure below, we can see that it has 5
[Microcontroller]
Internal hardware resources of PIC microcontroller 16F84
The source program example of the PIC16F84 microcontroller lighting up a light-emitting diode introduced by the author in the previous issue is to illustrate the basic format of the specific application of the PIC microcontroller 16F84. Now we already know that for the microcontroller to work, we need to compile the
[Microcontroller]
Internal hardware resources of PIC microcontroller 16F84
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号