Traffic light based on 51 microcontroller_emergency switch + yellow light countdown + adjustable time


Based on 51 microcontroller traffic light_emergency switch + yellow light countdown + adjustable time

Simulation diagram proteus 7.8

Program compiler: keil 4/keil 5

Programming language: C language

Design number: J000

Function Description:

Using 51 microcontroller and the minimum microcontroller system and transistor drive circuit as well as peripheral buttons and digital tube display components, a traffic light design based on the microcontroller is designed.

1. Four-way traffic light with traffic light countdown. After the green light countdown ends, it turns to yellow for 5 seconds and then to red.

2. In an emergency, press the emergency button and the four yellow lights will flash quickly.

3. The traffic light time can be dynamically set.

Knowledge points involved: key scanning, timer, digital tube driver.

Opening report

Content requirements:

1. Task

In recent years, with the rapid development of science and technology, the application of microcontrollers has been deepening, and at the same time, traditional control and detection technology has been increasingly updated. In real-time detection and automatic control microcontroller application systems, the microcontroller is often used as a core component. Knowledge of the microcontroller alone is not enough. It should also be improved based on the specific hardware structure by combining software and hardware.

The intersection is full of vehicles and pedestrians, and the vehicle lanes and pedestrian walkways are orderly. So what can we rely on to achieve this order? It relies on the automatic command system of traffic lights. There are many ways to control traffic lights. This system uses the STC89C52 microcontroller and the minimum microcontroller system and transistor drive circuit as well as peripheral buttons and digital tube display components to design a traffic light design based on the microcontroller. The design is displayed through a two-in-one common cathode digital tube, and the timing can be set through buttons. This system has strong practicability, simple operation and strong expansion functions. The design uses the STC89C52 microcontroller and the minimum system of the microcontroller and the triode to drive the digital tube (the digital tube is brighter and can be seen clearly during the day) as well as peripheral buttons and digital tube display components. The digital tube counts down to display the time.

image-20220827223845964

Simulation diagram (source file provided):

image-20220903202521141

image-20220903202531050

System hardware design

According to the above functional requirements, the hardware system mainly includes a microcontroller module, an indicator light module and a countdown display module. The selection of each module is as follows

Main controller selection

Microcontrollers are an integrated circuit chip that uses very large-scale integrated circuit technology to combine a central processing unit (CPU) with data processing capabilities, random access memory (RAM), read-only memory (ROM), various I/O ports, interrupt systems, and timers. /Counter and other functions (which may also include display driver circuits, pulse width modulation circuits, analog multiplexers, A/D converters and other circuits) are integrated into a silicon chip to form a small and complete microcomputer system. Widely used in control fields. From the 1980s, the 4-bit and 8-bit microcontrollers developed at that time to the current 300M high-speed microcontrollers. The microcontroller in this article specifically refers to the 51 microcontroller, and the specific chip model is STC89C52RC. It should be noted that STC89C51, STC89C52, AT89C51, and AT89C52 are all specific chip models of the 51 microcontroller.

AT89C51 is a typical product of the AT89C5X series of microcontrollers. It is manufactured using ATMEL's high-density non-volatile storage technology and is compatible with the industry standard MCS-51 instruction set and pin structure. By combining universal CPL1 and flash memory on a single chip, the AT89C52 produced by ATMEL is a powerful microcomputer that provides a highly flexible and low-cost solution for many embedded control applications. The chip AT89C52 is an 8-bit microcontroller with 2K bytes of flash memory produced by ATMEL.

​ This microcontroller also has the characteristics of small size and low price. AT89C51 microcontroller includes several major units such as central processing unit, program memory (ROM), data memory (RAM), timer/counter, parallel interface, serial interface and interrupt system, as well as three major buses including data bus, address bus and control bus.

System hardware structure diagram

The central processing unit (CPU) is the core component of the entire microcontroller. It is an 8-bit data width processor and can process 8-bit binary data or codes. The CPU is responsible for controlling, directing and scheduling the coordinated work of the entire unit system, completing calculations and controlling input Output functions and other operations.

There are 128 8-bit user data storage units and 128 special register units inside the AT89C51. They are uniformly addressed. The special registers can only be used to store control instruction data. Users can only access them, but cannot be used to store user data, so , there are only 128 RAMs available to users, which can store read and write data, intermediate results of operations or user-defined font tables.

AT89C51 has a total of 4096 8-bit mask ROM, which is used to store user programs, original data or tables. AT89C51 has two 16-bit programmable timer/counters to implement timing or counting to generate interrupts for controlling program steering.

AT89C51 has a total of 4 groups of 8-bit I/O ports (P0, P1, P2 or P3), which are used for transmitting external data. AT89C51 has a built-in full-duplex serial communication port for serial data transmission with other devices. The serial port can be used as an asynchronous communication transceiver or a synchronous shifter.

AT89C51 has a relatively complete interrupt function, including two external interrupts, two timer/counter interrupts and a serial interrupt, which can meet different control requirements and has 2-level priority selection.

Clock and reset circuit

AT89C51 has a built-in clock circuit with a maximum frequency of 12MHz, which is used to generate the pulse timing for the operation of the entire microcontroller. However, the AT89C51 microcontroller requires an external oscillation capacitor.

In this design, the microcontroller is made to run in the smallest system.

The clock circuit consists of two 20nF trimmer capacitors and a crystal oscillator. There is a high-gain inverting amplifier inside the AT89C51 to form an oscillator. Its input terminal is the chip pin XTAL1 and the output terminal is the pin XTAL2. These two pins are connected to the quartz crystal oscillator and trimmer capacitor to form a stable self-excited oscillator. This system uses a 12Hz oscillator with a machine cycle of 1us. There are three reset operations: automatic reset on power on, key level reset and external pulse reset.

Indicator light and countdown module

(1) Indicator module:

There should be three control lights at each intersection in the designed circuit, namely one red light, one yellow light, and one green light. Therefore, four red lights, yellow lights, and green lights are used in the design of this circuit. The same color indicator lights at two intersections in the same direction turn on and off at the same time. To simplify the circuit, these two lights can be connected to the same pin. In this way, the P1 port can be used to control all indicator lights.

According to the design, the indicator light uses three types of red/green/yellow light-emitting diodes. The component and its connection to the microcontroller pins are as follows:

Countdown module:

Two 74HC573 latches are used to drive four two-digit digital tubes. Because the countdown time in the north-south direction and the northeast direction are the same, there are actually only two displays for the countdown.

img

program

image-20220903202729067

Software main process framework

image-20220828100640968

​ It will be in normal operation when powered on. After 30 seconds of north-south traffic, it will change to east-west traffic for 30 seconds. This will cycle once every 60 seconds, causing alternating traffic of east-west and north-south directions.

The display system displays the time remaining until the next change of traffic direction, which is helpful for the driver to adjust the vehicle status. Whenever the traffic direction changes, the green light in the current direction goes out and turns into a flashing yellow light to remind the driver to pay attention to the change in traffic direction and avoid unnecessary danger.

​ This design supports dynamic modification of traffic light times in the east, west, north and south directions. Short press Set/Exit Design to enter modification mode. First modify the green light time in the north-south direction, then short press to modify the red light time in the east-west direction, and press the fourth button to save the set time.

In an emergency, four-way yellow lights can be set by pressing the button.

There are two delay methods. One is to use the MCS-51 internal timer to generate an overflow interrupt to determine the time of 1 second, and the other is to use the soft delay method. The countdown of this program uses software delay.

main function

The following is the main function

#define _MAIN_C
#include "use.h"
#include "main.h"
#include "time.h"
#include "key.h"
bit flag1s;	 
uchar count1s,NS_time,WE_time;
uchar GreenTime=30,RedTime=35;//绿灯时间和红灯时间,红灯要比绿灯时间长
uchar index=0;
enum eStaSysterm StaSysterm=NormalRun;

void main()
{
	EA=1;						  		//开总中断
	InintTimer0();				  		//初始化定时器0
	while(1)
	{	
		LEDScan();				  		//数码管即LED扫描
		KeyDriver();			  		//键盘驱动扫描
		if(flag1s)
		{
			flag1s=0;
			NS_time--;			  		//红绿灯倒计时时间减
			WE_time--;
		}	
	}
}
void InintTimer0()
{
	TMOD=0X01;
	TH0=0X4C;
	TL0=0X00;
	ET0=1;
	TR0=1;
}
void Timer0() interrupt 1
{
	TH0=0XBB;
	TL0=0X00;
	KeyScan();
	if(++count1s<50)	return;
	count1s=0;
	if(StaSysterm==NormalRun) flag1s=1;	 //非正常运行时,不红绿灯数值不减一	
}
void LEDScan()
{
	 if(StaSysterm==NormalRun)			 //正常运行
	 {
		if(NS_time==0 || WE_time==0)//当东西或者南北方向通行时间倒数到0,切换状态
		{
			switch(index)				 //改变红绿灯的状态,状态机
			{
				case 0:
				{
					index=1;
					NS_time=GreenTime;//南北方向绿灯通行时间
					WE_time=RedTime;//东西绿灯方向通行时间
					P2=0X09;	//需要亮的灯转换为二进制位0000 1001,对应P2口的P2^0和P2^3,
					          //对应仿真图的绿灯L0和红灯L3亮。
				}break;
				case 1:
				{
					index=2;
					NS_time=RedTime-GreenTime;
					P2=0X0A;	
				}break;
				case 2:
				{
					index=3;
					NS_time=RedTime;
					WE_time=GreenTime;
					P2=0X24;	
				}break;
				case 3:
				{
					index=0;
					WE_time=RedTime-GreenTime;
					P2=0X14;	
				}break;
				default:break;
			}
		}
		Seg_Display(NS_time/10,0);
		Seg_Display(NS_time%10,1);
		Seg_Display(WE_time/10,2);
		Seg_Display(WE_time%10,3);
	}
	else if(StaSysterm==UrgentRun)
	{								   //紧急情况运行
		P2=0x12;
		Delayms(200);
		P2=0x00;
		Delayms(200);
	}
	else							   //设置调整红绿灯时间
	{
		index=0;
		NS_time=0;
		WE_time=0;	 //重新初始化红绿灯并运行
		UrgentScan();
	}
}

design report

image-20220903202739452

Information list

image-20220903202745825

Data download link

Guess you like

Origin blog.csdn.net/weixin_52733843/article/details/134765136