Design and Implementation of a Key Increment/Decrement Digital Tube Display System Based on AT89C52 Microcontroller

Abstract

This article designs a key increment/decrement digital tube display system based on the AT89C52 microcontroller. The system achieves numerical increment and decrement control through independent keys and drives the digital tube display using a dynamic scanning method. The paper details the hardware circuit design, software programming implementation, and anti-interference measures of the system. Test results indicate that the system operates stably, responds quickly, and possesses high reliability and practicality, making it suitable as a basic teaching experimental platform for microcontrollers.

Keywords

AT89C52; key detection; digital tube display; dynamic scanning; debouncing

1. Introduction

The microcontroller, as the core component of embedded systems, is widely used in industrial control, smart instruments, smart homes, and other fields. The AT89C52 is a classic 8-bit microcontroller with rich I/O interfaces and good cost performance, making it suitable as a teaching and basic development platform. Keys and digital tubes are the most basic input and output devices in microcontroller systems, and controlling the digital tube display through keys is a typical experimental project in microcontroller education. This article provides a detailed introduction to the design and implementation process of the key increment/decrement digital tube display system based on the AT89C52, which has significant educational value for understanding the principles of input and output control in microcontrollers.

2. Overall System Design

2.1 System Functional Requirements

The system needs to achieve the following functions:

  1. Implement increment and decrement operations through two independent keys
  2. Real-time display of the current value on the digital tube
  3. Limit the value range between 0 and 9
  4. Include key debouncing functionality to prevent false triggering
  5. Ensure stable digital tube display without flickering

2.2 System Architecture Design

The system adopts a modular design concept, mainly consisting of the following modules:

  • Main Controller Module

: AT89C52 microcontroller

  • Key Input Module: Two independent keys for increment and decrement control
  • Digital Tube Display Module: Uses common cathode digital tubes with dynamic scanning display
  • Power Module: Provides the necessary power for the system

3. Hardware Circuit Design

3.1 Minimum System Design of the Microcontroller

The minimum system of the AT89C52 microcontroller includes the microcontroller chip, clock circuit, and reset circuit. The clock circuit uses a 12MHz crystal oscillator to provide precise clock signals for the microcontroller. The reset circuit combines power-on reset and manual reset to ensure reliable system startup and reset.

3.2 Key Input Circuit Design

The key input circuit design is shown in Figure 3. Keys K1 and K2 are connected to the P3.3 and P3.4 pins of the microcontroller, respectively, using pull-up resistors to ensure that the pins are high when the keys are not pressed. When a key is pressed, the corresponding pin is pulled low, and the microcontroller detects the key operation by monitoring the pin level changes.

3.3 Digital Tube Display Circuit Design

The digital tube display circuit uses common cathode digital tubes, controlled by a 74HC573 latch for segment selection and digit selection signals. The segment selection signal controls the numerical content displayed on the digital tube, while the digit selection signal controls which digital tube is selected for display. The dynamic scanning method drives the digital tube, reducing I/O port usage and lowering system power consumption.

4. Software Design

4.1 Software Development Environment

The software development for this system uses the Keil C51 integrated development environment, programming in C language. Keil C51 provides a rich set of library functions and debugging tools, facilitating code writing, compilation, and debugging.

4.2 System Software Architecture

The system software adopts a modular design, mainly including the following functional modules:

  • System Initialization Module

  • Key Detection and Processing Module
  • Digital Tube Display Driver Module
  • Delay Module

4.3 Core Code Implementation

The following is the core code implementation of the system:

#include<reg52.h>
sbit KEY_ADD=P3^3; sbit KEY_DEC=P3^4;
#define DataPort P0 
sbit LATCH1=P2^2;sbit LATCH2=P2^3;
unsigned char code dofly_DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code dofly_WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char TempData[8]; 
void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
void Display(unsigned char FirstBit,unsigned char Num);
void main (void){unsigned char num=0;                   
	KEY_ADD=1;  
	KEY_DEC=1;
while (1)            {
		if(!KEY_ADD)       	{DelayMs(10);       	if(!KEY_ADD)     			{         
			while(!KEY_ADD);
				{
					if(num<9)   					num++;
				}
			}
		}
		if(!KEY_DEC)       	{DelayMs(10);      	if(!KEY_DEC)      			{         
			while(!KEY_DEC);
				{
					if(num>0)   					num--;
				}
			}
		}
		TempData[0]=dofly_DuanMa[num%10];
		Display(0,1);
	}
}
void DelayUs2x(unsigned char t){    while(--t);}
void DelayMs(unsigned char t){
	while(t--)  	{     DelayUs2x(245); 	 DelayUs2x(245);  
}}
void Display(unsigned char FirstBit,unsigned char Num){    unsigned char i;
for(i=0;i<Num;i++)  	{  		DataPort=0;           LATCH1=1;             LATCH1=0;
		DataPort=dofly_WeiMa[i+FirstBit];         LATCH2=1;            LATCH2=0;
		DataPort=TempData[i];         LATCH1=1;            LATCH1=0;
		DelayMs(2); 
	}
}

4.4 Key Debouncing Processing

Keys may produce mechanical bounce during pressing and releasing, which can lead to misjudgment by the microcontroller. To eliminate the impact of bouncing, the system adopts a software delay debouncing method. When a key state change is detected, a delay of 10ms is applied, and if the key state remains pressed, it is considered a valid key operation.

4.5 Dynamic Scanning Display of Digital Tubes

The dynamic scanning display of digital tubes is achieved by time-sharing control of each digital tube’s display, utilizing the visual persistence effect of the human eye to create the effect of simultaneously displaying multiple digital tubes. In this system, one digital tube is scanned at a time, with a delay of 2ms before scanning the next, ensuring that all digital tubes can display stably.

5. System Testing and Analysis

5.1 Test Environment Setup

To verify the correctness and stability of the system design, the following test environment was established:

  • AT89C52 microcontroller development board

  • Keil C51 development environment
  • STC-ISP programmer

5.2 Functional Testing

Functional testing mainly verifies whether the basic functions of the system are implemented correctly, including:

  • Key detection function test: Pressing the increment and decrement keys to observe whether the digital tube display increases or decreases accordingly

  • Value range limit test: Testing the decrement key when the value is 0 and the increment key when the value is 9 to verify that the value remains unchanged

  • Display stability test: Observing whether the digital tube display is clear, stable, and free of flickering

5.3 Performance Testing

Performance testing mainly evaluates the system’s response speed and anti-interference capability, including:

  1. Key response time test: Using an oscilloscope to measure the time interval from key press to digital tube display update

  2. Anti-interference capability test: Applying electromagnetic interference near the keys to observe whether the system can operate normally

5.4 Test Result Analysis

After testing, all functions of the system were implemented correctly, with rapid key response and stable digital tube display. In the key response time test, the average response time was measured at 15ms, meeting general application requirements. In the anti-interference test, the system demonstrated good stability, with no false triggering or display anomalies.

6. Conclusion

This article designed and implemented a key increment/decrement digital tube display system based on the AT89C52 microcontroller. Through reasonable hardware circuit design and software programming, the system achieved key detection, dynamic digital tube display, and numerical increment/decrement control functions. Test results indicate that the system has advantages such as stable operation, rapid response, and strong anti-interference capability, making it a typical case for microcontroller teaching experiments and applicable in some simple digital control scenarios.

In future work, the system’s functionality can be further expanded, such as adding multi-digit digital tube displays, incorporating value storage functions, and implementing more complex numerical control algorithms to meet the needs of more application scenarios.

References

[1] Zhang Yigang. Principles and Applications of Microcontrollers [M]. Higher Education Press, 2016.[2] Li Zhaoqing. Principles and Interface Technology of Microcontrollers [M]. Beihang University Press, 2013.[3] Zhou Hangci. Microcontroller Application Program Design Technology [M]. Beihang University Press, 2008.

Leave a Comment