Writing Embedded C Programming to Display 0-99 on 7-Segment Display

30 Apr 2023 Balmiki Mandal 0 Embedded C

Embedded C Programming to Display 0-99 Numbers on Multiplied Common Cathod 7-Segment Display

This guide will show you how to make use of the delay.h header file in order to display numbers from 0 to 99 on a multiplied common cathod seven segment display using embedded C programming.

Step 1: Connect the Display to the Microcontroller

Connect the display to the microcontroller in an appropriate sequence. Make sure that the positive and negative pins are connected correctly and the microcontroller is powered properly.

Step 2: Configure Clock Frequency

You need to configure the clock frequency of the microcontroller so that the timing of the output display is correct. You can adjust the frequency by using the internal clock generator or by using an external clock.

Step 3: Write the Code

Create a program in embedded C that includes the delay.h header file and uses a “for” loop to iterate through the 0-99 range of numbers. The code should include logic for each of the seven segments in the display. Using the delay.h header file, you can set up the timing so that the numbers are displayed at a speed that can be clearly seen.

Step 4: Run the Code

Once the code is written, you can compile the program and run it on the microcontroller. If everything was connected properly and coded correctly, you should see the numbers 0-99 display on the multiplied common cathod seven segment display.

 

Here is an example code for an Embedded C programming to display 0-99 numbers on a multiplexed common cathode 7-segment display using a delay header file:

#include <reg52.h> // include header file for 8051 microcontroller
#include <delay.h> // include delay header file

sbit digit1 = P1^0; // define pins for digits of the 7-segment display
sbit digit2 = P1^1;
sbit a = P2^0;
sbit b = P2^1;
sbit c = P2^2;
sbit d = P2^3;
sbit e = P2^4;
sbit f = P2^5;
sbit g = P2^6;

// function to display a single digit on the 7-segment display
void display_digit(unsigned char digit, unsigned char position)
{
    unsigned char data_segments;
    
    // assign segments of the digit to data_segments variable
    switch(digit)
    {
        case 0: data_segments = 0x3F; break;
        case 1: data_segments = 0x06; break;
        case 2: data_segments = 0x5B; break;
        case 3: data_segments = 0x4F; break;
        case 4: data_segments = 0x66; break;
        case 5: data_segments = 0x6D; break;
        case 6: data_segments = 0x7D; break;
        case 7: data_segments = 0x07; break;
        case 8: data_segments = 0x7F; break;
        case 9: data_segments = 0x6F; break;
        default: data_segments = 0x00; break;
    }
    
    // select the appropriate digit position
    switch(position)
    {
        case 1: digit1 = 0; digit2 = 1; break;
        case 2: digit1 = 1; digit2 = 0; break;
        default: digit1 = 1; digit2 = 1; break;
    }
    
    // assign segments to the port pins of the 7-segment display
    a = ~((data_segments & 0x01) >> 0);
    b = ~((data_segments & 0x02) >> 1);
    c = ~((data_segments & 0x04) >> 2);
    d = ~((data_segments & 0x08) >> 3);
    e = ~((data_segments & 0x10) >> 4);
    f = ~((data_segments & 0x20) >> 5);
    g = ~((data_segments & 0x40) >> 6);
}

// main function
void main()
{
    unsigned char i, j;
    
    while(1) // infinite loop
    {
        for(i=0; i<10; i++) // loop to display the tens digit
        {
            for(j=0; j<10; j++) // loop to display the units digit
            {
                display_digit(i, 1); // display the tens digit in the first position
                display_digit(j, 2); // display the units digit in the second position
                delay_ms(500); // delay for 500ms between digits
            }
        }
    }
}

In this code, we have defined the pins for the digits and segments of the 7-segment display using the sbit keyword. We have also defined a function display_digit()

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.