AT89C51: Interfacing with 7-Segment Display


The AT89C51 is a low-power, high-performance CMOS 8-bit microcontroller that is often used in embedded systems applications.

A 7-segment display is a commonly used electronic component for visualizing numerical data.

By combining individual segments, it can display numbers from 0 to 9 and a few additional characters.

The AT89C51 microcontroller, a popular choice for many embedded systems projects, can be easily integrated with a 7-segment display to showcase numeric information.

Hardware Connection

When interfacing a 7-segment display with Port 0 of the AT89C51, you need to use pull-up resistors.

Common Cathode Display

Connect each of the seven segments (A-G) and the decimal point (DP) of the display to a separate I/O pin on the microcontroller. The common cathode pin should be connected to the ground (0V).

Common Anode Display

Connect each of the seven segments (A-G) and the decimal point (DP) of the display to a separate I/O pin on the microcontroller. The common anode pin should be connected to the power supply (Vcc).

Programming AT89C51 for 7-Segment Display

Connect each of the seven segments (A-G) and the decimal point (DP) of the display to a separate I/O pin on the microcontroller. The common cathode pin should be connected to the ground (0V).

AT89C51 Interfacing with single digit common cathode 7-segment display

For example, you could connect segment A to P0.0, segment B to P0.1, segment C to P0.2, and so on, up to segment G on P0.6. If you're using the decimal point, you could connect it to P0.7.

Initialize the I/O pins connected to the display as output pins.

To display a digit or character, send the appropriate signals to the segment pins. For a common cathode display, a 'HIGH' signal turns a segment on, and a 'LOW' signal turns it off.

For a common anode display, it's the opposite: a 'LOW' signal turns a segment on, and a 'HIGH' signal turns it off.

Here's an example with bit size -

#include <reg51.h>
// Define the segment pins
sbit segA = P2^0;
sbit segB = P2^1;
sbit segC = P2^2;
sbit segD = P2^3;
sbit segE = P2^4;
sbit segF = P2^5;
sbit segG = P2^6;

void delay(unsigned int time) {
    // Simple delay function
    unsigned int i, j;
    for(i=0; i<time; i++)
        for(j=0; j<1275; j++);
}

void main() {
    while(1) {
        // Display the digit '1'
        segA = 0;
        segB = 1;
        segC = 1;
        segD = 0;
        segE = 0;
        segF = 0;
        segG = 0;
        delay(1000);

        // Display the digit '2'
        segA = 1;
        segB = 1;
        segC = 0;
        segD = 1;
        segE = 1;
        segF = 0;
        segG = 1;
        delay(1000);

        // Continue for other digits...
    }
}

Here's an example with byte size-

 #include <reg51.h>
//define segment port
#define segPort  P0

void delay(unsigned int time) {
    // Simple delay function
    unsigned int i, j;
    for(i=0; i<time; i++)
        for(j=0; j<1275; j++);
}

void main() {
    while(1) {
        // Display the digit '1'
        segPort = 0x06;
        delay(1000);

        // Display the digit '2'
         segPort = 0x5b ;
         delay(1000);

        // Continue for other digits...
    }
} 

In a common cathode 7-segment display, a segment is turned on by applying a 'HIGH' signal and turned off by applying a 'LOW' signal.
To display the digit '1', you would turn on segments B and C, and turn off all the other segments. This gives us the following pattern:

Segment A: off
Segment B: on
Segment C: on
Segment D: off
Segment E: off
Segment F: off
Segment G: off

If we represent 'on' as '1' and 'off' as '0', we get the binary number 0000110. If we convert this binary number to hexadecimal, we get 0x06.Get all hexadecimal codes from here - Digits And Characters Code Table for common cathode

Here's an example with byte array


 
#include <reg51.h>
//define segment port
#define segPort  P0

// digit mapping for common cathode 7-segment display
unsigned char seg_code[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

void delay(unsigned int time) {
    // Simple delay function
    unsigned int i, j;
    for(i=0; i<time; i++)
        for(j=0; j<1275; j++);
}

void main() {
    int digit;
    while(1) {
        for(digit = 0; digit < 10; digit++) {
            segPort = seg_code[digit];
            delay(1000);
        }
    }
} 

In this example, we have an array named seg_code[] which is storing the 7-segment codes for displaying the digits from 0-9 on a common cathode 7-segment display.

In the main function, we are running a loop from 0 to 9 and selecting each digit's 7-segment code from the array using the digit as the index.

After setting the port to the 7-segment code, we wait for a delay before moving on to the next digit. This process continues indefinitely.

Loading...

Search