Lesson 8: Independent Key Control of LED Display in Binary Using the Puzhong A2 Microcontroller

Core Principle Overview

The essence of displaying binary with an independent key is to use a variable to record the number of times the key is pressed, and to display the binary value of this variable through 8 LEDs. Each LED represents a binary bit, with the LED on indicating 1 and off indicating 0.

Detailed Principle Breakdown

1. Hardware Principle

  • Independent Key: Connected to P3.1, active low
  • 8 LEDs: Connected to port P2 (P2.0~P2.7), common anode connection
    • P2.0 → Represents the least significant bit (bit 0, 2⁰)
    • P2.1 → bit 1 (2¹)
    • P2.2 → bit 2 (2²)
    • P2.7 → Most significant bit (bit 7, 2⁷)

2. Software Principle

Core Idea:

  1. Define a counter variable, initialized to 0
  2. Each time the key is pressed, increment the counter by 1
  3. Directly assign the value of the counter to port P2, and the LEDs will automatically display the corresponding binary number

Complete Code Implementation

#include <REGX52.H>

sbit KEY1 = P3^1;  // Independent key connected to P3.1

void Delay(unsigned int t) {
    while(t--);
}

void main() {
    unsigned char count = 0;  // Counter, range 0-255
    
    while(1) {
        // Detect key press
        if(KEY1 == 0) {
            Delay(10);        // Debounce on press
            if(KEY1 == 0) {
                count++;      // Increment counter by 1
                P2 = ~count;  // Important! Invert the count value before sending to P2
                
                // Wait for key release
                while(KEY1 == 0);
                Delay(10);    // Debounce on release
            }
        }
    }
}

Code Principle Deep Analysis

Key Statement:<span>P2 = ~count;</span>

Why is inversion necessary?

Because our LEDs are connected in a common anode configuration:

  • LED on requires <span>P2.x = 0</span> (low level)
  • LED off requires <span>P2.x = 1</span> (high level)

However, the logic for binary display is:

  • When the binary bit is 1, the LED should be on
  • When the binary bit is 0, the LED should be off

Thus, the inversion operation is necessary:

count = 5 (binary: 0000 0101)
~count = 1111 1010

P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
  1    1    1    1    1    0    1    0   (P2 output)
 Off  Off  Off  Off  Off  On   Off  On   (LED state)

Actual Display Effects

Key Press Count Count Value Binary LED Display (P2.7~P2.0) Notes
0 0 0000 0000 Off Off Off Off Off Off Off Off All Off
1 1 0000 0001 Off Off Off Off Off Off Off On Only D1 On
2 2 0000 0010 Off Off Off Off Off On Off Only D2 On
3 3 0000 0011 Off Off Off Off Off On On D1, D2 On
4 4 0000 0100 Off Off Off Off On Off Off Only D3 On
255 255 1111 1111 On On On On On On On On All On
256 0 0000 0000 Off Off Off Off Off Off Off Off Restart

Visual Demonstration

Let’s take a look at the display effects for the first few key presses:

Key Press Count   Binary      LED Display (D8...D1)
  0      0000 0000   Off Off Off Off Off Off Off Off
  1      0000 0001   Off Off Off Off Off Off Off On
  2      0000 0010   Off Off Off Off Off Off On Off  
  3      0000 0011   Off Off Off Off Off Off On On
  4      0000 0100   Off Off Off Off Off On Off Off
  5      0000 0101   Off Off Off Off Off On Off On
  6      0000 0110   Off Off Off Off Off On On Off
  7      0000 0111   Off Off Off Off Off On On On
  8      0000 1000   Off Off Off Off On Off Off Off

Advanced Function: Adding Decrement Functionality

If you need key K1 to increment and key K2 to decrement:

#include <REGX52.H>

sbit KEY1 = P3^1;  // Increment key
sbit KEY2 = P3^0;  // Decrement key

void Delay(unsigned int t) {
    while(t--);
}

void main() {
    unsigned char count = 0;
    
    while(1) {
        // Detect KEY1 (increment)
        if(KEY1 == 0) {
            Delay(10);
            if(KEY1 == 0) {
                count++;      // Increment counter by 1
                P2 = ~count;  // Display binary
                while(KEY1 == 0);
                Delay(10);
            }
        }
        
        // Detect KEY2 (decrement)
        if(KEY2 == 0) {
            Delay(10);
            if(KEY2 == 0) {
                count--;      // Decrement counter by 1
                P2 = ~count;  // Display binary
                while(KEY2 == 0);
                Delay(10);
            }
        }
    }
}

Key Points Summary

  1. Binary Mapping: Each bit of the counter directly corresponds to an LED
  • bit 0 → P2.0 → D1
  • bit 1 → P2.1 → D2
  • bit 7 → P2.7 → D8
  • Necessity of Inversion: Due to the common anode connection, the binary 1/0 needs to be inverted to low/high level
  • Automatic Overflow: When count reaches 255, adding 1 will reset it to 0, achieving cyclic counting
  • Real-time Display: The display updates immediately after each key press, without the need for additional display programs
  • Practical Learning Significance

    This experiment helps you understand:

    • The concept and representation of binary numbers
    • The relationship between bits and bytes
    • How variables are stored in memory
    • Conversion between binary and decimal
    • Bit manipulation of microcontroller I/O ports

    Conclusion

    The principle of independent key control of binary display using the Puzhong A2 microcontroller is to use a counter to record the number of key presses, and to send the inverted binary value of the counter to port P2, using 8 LEDs to visually display each bit of the binary number.

    Leave a Comment