Very detailed single chip traffic light control program

Publisher:bullfishLatest update time:2020-08-26 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Use the timer to count time, the annotations are very detailed, and you can change the timing time yourself.


The microcontroller source program is as follows:

#include "reg52.h"

#define GPIO_TRAFFIC P2

#define GPIO_DIG P0

typedef unsigned char u8;

typedef unsigned int u16;


//3-8 decoder pin definition

sbit LSA = P1^0;

sbit LSB = P1^1;

sbit LSC = P1^2;

//Definition of the north-south direction control pins of the traffic light

sbit GREEN10 = P2^0;

sbit RED10 = P2^1;

sbit GREEN11 = P2^2;

sbit YELLOW11 = P2^3;

sbit RED11 = P2^4;

// Traffic light east-west direction control pin definition

sbit GREEN00 = P1^6;

sbit RED00 = P1^7;

sbit GREEN01 = P2^5;

sbit YELLOW01 = P2^6;

sbit RED01 = P2^7;



u8 code smgduan[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,

                                                0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //Common cathode

u8 displaydata[8]; //Store the countdown of the signal light


//Define a global variable

u8 second;



//Declaration of various functions

void Timer0Init();

void delay(u16 i);

void Digdisplay();

void trafficpros();

/*******************************

                Main function: main

*******************************/

void main(void)

{

        second = 1;

        Timer0Init(); //Initialize timer T0;

        while(1)

        {

                 trafficpros();       

        }

}

/*******************************

Timer T0 configuration function, only select TR in TCON

0 trigger, timing 10ms.

*******************************/

void Timer0Init()

{

        TMOD |= 0x01;

        TH0 = 0xd8;

        TL0 = 0xf0; //Set the initial value to 55536 and the timing to 10ms

        IE = 0x82; // Enable T0 interrupt and total interrupt

        TR0 = 1; //Start timer T0

}

/*******************************

Interrupt handling function

*******************************/

void Time0() interrupt 1

{

        static u16 i;

        TH0 = 0xd8;

        TL0 = 0xf0; //Reload initial value

        i++; //Record the number of interruptions, 100 times per second

        if(100 == i)

        {

                 i = 0;

                second++;

        }       

}

/*******************************

Delay function, delay about 10μs

*******************************/

void delay(u16 i)

{

        while(i--);

}

/*******************************

8-bit digital tube bit selection function

*******************************/

void DigDisplay()

{

        u8 i;

        for(i = 0; i < 8; i++)

        {

                   switch(i)

                {

                        case 0:

                                LSA = 0; LSB = 0; LSC = 0; break;

                         case 1:

                                LSA = 1; LSB = 0; LSC = 0; break;

                        case 2:

                                LSA = 0; LSB = 1; LSC = 0; break;

                        case 3:

                                LSA = 1; LSB = 1; LSC = 0; break;

                        case 4:

                                LSA = 0; LSB = 0; LSC = 1; break;

                        case 5:

                                LSA = 1; LSB = 0; LSC = 1; break;

                        case 6:

                                LSA = 0; LSB = 1; LSC = 1; break;

                        case 7:

                                LSA = 1; LSB = 1; LSC = 1; break;

                }

                GPIO_DIG = displaydata[i];

                delay(100);

                GPIO_DIG = 0x00; //disappear

        }

}

/*******************************

Traffic light control function

*******************************/

void trafficpros()

{

        if(second > 74)

        {

                 second = 1;

        }

        //North-South traffic and pedestrian paths

        if(second < 32)

        {

                displaydata[0] = 0x00; //The first digit is not displayed

                displaydata[1] = 0x00; //The second digital tube does not display       

                displaydata[2] = smgduan[(31 - second)/10];

                displaydata[3] = smgduan[(31 - second)%10];

                displaydata[4] = 0x00; //The fifth digit is not displayed

                displaydata[5] = 0x00; //The sixth digit is not displayed

                displaydata[6] = displaydata[2];

                displaydata[7] = displaydata[3];

                Digdisplay();

                // Each time all the lights are turned off

                GPIO_TRAFFIC = 0xff;

                GREEN00 = 1;

                RED00 = 1;


                GREEN10 = 0; //The pedestrian crossing light is green

                GREEN11 = 0; //The lane green light is on


                RED00 = 0; //The red light on the east-west sidewalk is on

                RED01 = 0; //The red light on the east-west lane is on

        }

        //Intermediate yellow light stage

        else if(second < 38)

        {

                 displaydata[0] = 0x00; //The first digit is not displayed

                displaydata[1] = 0x00; //The second digital tube does not display       

                displaydata[2] = smgduan[(37 - second)/10];

                displaydata[3] = smgduan[(37 - second)%10];

                displaydata[4] = 0x00; //The fifth digit is not displayed

                displaydata[5] = 0x00; //The sixth digit is not displayed

                displaydata[6] = displaydata[2];

                displaydata[7] = displaydata[3];

                Digdisplay();

                // Each time all the lights are turned off

                GPIO_TRAFFIC = 0xff;

                GREEN00 = 1;

                RED00 = 1;


                RED10 = 0; //The red light on the north-south pedestrian crossing is on

                YELLOW11 = 0; //The yellow light on the north-south lane is on


                RED00 = 0; //The red light on the east-west sidewalk is on

                RED01 = 0; //The red light on the east-west lane is on               

        }

        //East-west traffic and pedestrian paths

        else if(second < 69)

        {

                 displaydata[0] = 0x00; //The first digit is not displayed

                displaydata[1] = 0x00; //The second digital tube does not display       

                displaydata[2] = smgduan[(68 - second)/10];

                displaydata[3] = smgduan[(68 - second)%10];

                displaydata[4] = 0x00; //The fifth digit is not displayed

                displaydata[5] = 0x00; //The sixth digit is not displayed

                displaydata[6] = displaydata[2];

                displaydata[7] = displaydata[3];

                Digdisplay();

                // Each time all the lights are turned off

                GPIO_TRAFFIC = 0xff;

                GREEN00 = 1;

                RED00 = 1;

               

                GREEN00 = 0; //The green light on the east-west sidewalk is on

                GREEN01 = 0; //The green light on the east-west lane is on


                RED10 = 0; //The red light on the north-south pedestrian crossing is on

                RED11 = 0; //The red light on the north-south lane is on

        }

        //Intermediate yellow light stage

        else

        {

                 displaydata[0] = 0x00; //The first digit is not displayed

                displaydata[1] = 0x00; //The second digital tube does not display       

                displaydata[2] = smgduan[(74 - second)/10];

                displaydata[3] = smgduan[(74 - second)%10];

                displaydata[4] = 0x00; //The fifth digit is not displayed

                displaydata[5] = 0x00; //The sixth digit is not displayed

                displaydata[6] = displaydata[2];

                displaydata[7] = displaydata[3];

                Digdisplay();

                // Each time all the lights are turned off

                GPIO_TRAFFIC = 0xff;

                GREEN00 = 1;

                RED00 = 1;

               

                RED00 = 0; //The red light on the east-west sidewalk is on

                YELLOW01 = 0; //The yellow light on the east-west lane is on

               

                RED10 = 0; //The red light on the north-south pedestrian crossing is on

                RED11 = 0; //The red light on the north-south lane is on       

        }

}


Reference address:Very detailed single chip traffic light control program

Previous article:Digital altitude meter program based on 51 single chip microcomputer + bmp180
Next article:Design of 51 single chip microcomputer Hall sensor motor speed measurement

Recommended ReadingLatest update time:2024-11-16 12:47

PIC timer1 timer
Function: Use Timer1 to delay the LED light to flash in a cycle Requirements: MPLAB 8.2 or above, a MEO engineering board, PIC16-MCD2 simulation download, Chip configuration word, watchdog off, power-on delay on, power-off detection off, low voltage programming off, 4MXT mode oscillation  */ #include pic18.h int mai
[Microcontroller]
STM32 uses DAC+DMA+TIMER to output sine wave
The board used is STM32F407, and the program refers to the program in the DAC_SignalsGeneration folder of the STM32F4xx firmware library. The official routines use the Escalator Wave trapezoidal wave, Sine Wave sine wave of the DAC's disabled generation wave (DAC_WaveGeneration_None) and the built-in Noise Wave (DAC_W
[Microcontroller]
STM32 uses DAC+DMA+TIMER to output sine wave
STM32 timer1 generates PWM (complementary channels)
1. Introduction This article describes how to use the TIM1_CH2N channel (PB14) of timer1 to generate PWM in the STM32 series. 2. Experimental Platform Library version: STM32F10x_StdPeriph_Lib_V3.5.0 Compiler software: MDK4.53 Hardware platform: STM32 development board (main chip stm32f103c8t6) Emulator
[Microcontroller]
Solve the problem of inaccurate timer2 interrupt configuration in stm8s
Problem Description: The project is based on the stm8s003 chip. Taking timer 2 as an example, after the timer is initialized, it is found that the time of the first interrupt entry is not the set value, and it seems that the interrupt is entered immediately. The subsequent interrupt time is normal. The actual first in
[Microcontroller]
Solve the problem of inaccurate timer2 interrupt configuration in stm8s
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号