Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Custom 3-Digit Display with C51 Microcontroller

Tech 1

C51 Microcontroller 3-Digit Display Implementation

Code Example:

#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

sbit DIGIT_SELECT = P2^7;  // Digit selection latch
sbit SEGMENT_SELECT = P2^6; // Segment selection latch

const uchar digit_patterns[] = {
    0x3F,  // "0"
    0x06,  // "1"
    0x5B,  // "2"
    0x4F,  // "3"
    0x66,  // "4"
    0x6D,  // "5"
    0x7D,  // "6"
    0x07,  // "7"
    0x7F,  // "8"
    0x6F   // "9"
};

void delay_ms(uint ms) {
    uint i, j;
    for(i = ms; i > 0; i--)
        for(j = 114; j > 0; j--);
}

void show_number(uint num) {
    uchar hundreds = num / 100;
    uchar tens = (num % 100) / 10;
    uchar units = num % 10;

    // Display hundreds digit
    P0 = 0xFF;
    DIGIT_SELECT = 1;
    P0 = 0xFE;
    DIGIT_SELECT = 0;

    SEGMENT_SELECT = 1;
    P0 = digit_patterns[hundreds];
    SEGMENT_SELECT = 0;
    delay_ms(5);

    // Display tens digit
    P0 = 0xFF;
    DIGIT_SELECT = 1;
    P0 = 0xFD;
    DIGIT_SELECT = 0;

    SEGMENT_SELECT = 1;
    P0 = digit_patterns[tens];
    SEGMENT_SELECT = 0;
    delay_ms(5);

    // Display units digit
    P0 = 0xFF;
    DIGIT_SELECT = 1;
    P0 = 0xFB;
    DIGIT_SELECT = 0;

    SEGMENT_SELECT = 1;
    P0 = digit_patterns[units];
    SEGMENT_SELECT = 0;
    delay_ms(5);
}

void main() {
    while(1) {
        show_number(123); // Change this value to display different numbers
    }
}

Implementation Notes:

  1. The pin definitionss (DIGIT_SELECT and SEGMENT_SELECT) should be adjusted accroding to your specific hardware configuration.
  2. The show_number() function parameter determines the displayed value (must be a 3-digit number).
  3. This implementation uses a common cathode 7-segment display with the specified digit paterns.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.