How to Write an Embedded C Programming to Display 0-9 Numbers on a Common Cathode 7-Segment Display

30 Apr 2023 Balmiki Mandal 0 Embedded C

Writing an Embedded C Program to Display 0-9 Numbers on Common Cathode 7-Segment Display

In this tutorial, you will learn how to write a program in embedded C language to display 0-9 numbers on common cathode 7-segment display using header file.

What is a 7-Segment Display?

A 7-segment display is an electronic device used in electronic systems to display numeric characters. It contains 7 individual LCD or LED segments that can be turned ON/OFF to represent numbers, as well as a decimal point (DP). It usually has a common cathode pin which is connected to ground, and individual pins for each of the 7 segments.

Hardware Requirements for the Program

  • A Controller (AT89C51, 8051, ATMega 16)
  • 7-Segment Display (common cathode)
  • Resistors (for current limiting)
  • Connecting Wires

Writing the Program in Embedded C

The program is written in Embedded C language. Some header files are included in the program, which are defines in the header file. The header file contains the details of the port pins used for the 7-segment display. It also contains the delays used for the proper functioning of the program.

The program starts with declaring the pins of the controller which are used for the 7-segment display. For example, to represent the number “1” on the display, we require to turn the segment ‘b’ and ‘c’ of the display ON and other segments OFF. To do so, we need to set the corresponding ports of the controller high and low respectively. The code for this is given below:

	//Declaring the pins of controller used for 7-segment display
	#define seg_a P1_7
	#define seg_b P1_6
	#define seg_c P1_5
	#define seg_d P1_4
	#define seg_e P1_3
	#define seg_f P1_2
	#define seg_g P1_1
	#define seg_dp P1_0
	
	//Creating a function to represent "1" on the display
	void seg_1(void)
	{
		//Turning "b" and "c" segments ON
		seg_b = 1; 
		seg_c = 1;
		
		//Turning other segments OFF
		seg_a = 0;
		seg_d = 0;
		seg_e = 0;
		seg_f = 0;
		seg_g = 0;
		seg_dp = 0;
	}

Similarly, the functions are created for other numbers 0-9 and the main function containing a loop to continuously display the numbers is given below:

	//Main function
	void main(void)
	{
		int i;
		while(1)
		{
			//Creating a loop to display 0-9 numbers
			for(i=0;i<10;i++)
			{
				switch(i)
				{
					case 0: seg_0(); break;
					case 1: seg_1(); break;
					case 2: seg_2(); break;
					case 3: seg_3(); break;
					case 4: seg_4(); break;
					case 5: seg_5(); break;
					case 6: seg_6(); break;
					case 7: seg_7(); break;
					case 8: seg_8(); break;
					case 9: seg_9(); break;
					default: break;
				}
				delay_ms(1000); //Delay of 1 second
			}
		}	
	}

Connecting the Circuit and Testing the Program

After writing the program and compiling it, the next step is to test the program. For testing the program, we need to connect the circuit for our program as shown in the image below. After connecting the circuit, upload the compiled program to the controller and the numbers should start appearing on the display.

Conclusion

In this tutorial, we have learned how to write an embedded C program to display 0-9 numbers on common cathode 7-segment display using header file. We have also discussed the hardware requirements for the same, wrote the program and connected the circuit for the same. We hope that this tutorial has been helpful in understanding how to write and test a program for this project.

 

example 02 embedded C program that uses a header file to display numbers 0-9 on a common cathode 7-segment display:

#include <reg51.h>     // include 8051 microcontroller header file
#include "sevensegment.h"    // include header file for 7-segment display

void main() 
{
    unsigned char i;    // declare variable i

    while (1)   // infinite loop
    {
        for (i = 0; i <= 9; i++)   // display numbers 0 to 9
        {
            display7Segment(i);   // call function to display digit on 7-segment display
            delay_ms(1000);   // wait for 1 second
        }
    }
}

In this program, we have included the sevensegment.h header file which contains the necessary functions to display digits on the 7-segment display. The main() function has an infinite loop that uses a for loop to display numbers 0 to 9 on the 7-segment display.

The display7Segment() function is called with the value of i as an argument to display the digit on the 7-segment display. The delay_ms() function is used to wait for 1 second before displaying the next digit.

Here's an example implementation of the sevensegment.h header file:

#ifndef _SEVENSEGMENT_H_
#define _SEVENSEGMENT_H_

// define pins for 7-segment display
#define LED_A P1_0
#define LED_B P1_1
#define LED_C P1_2
#define LED_D P1_3
#define LED_E P1_4
#define LED_F P1_5
#define LED_G P1_6
#define LED_DP P1_7

// define 7-segment display patterns for numbers 0-9
unsigned char sevenSegmentPatterns[] = {
    0x3F,   // 0
    0x06,   // 1
    0x5B,   // 2
    0x4F,   // 3
    0x66,   // 4
    0x6D,   // 5
    0x7D,   // 6
    0x07,   // 7
    0x7F,   // 8
    0x6F    // 9
};

// function to display a digit on the 7-segment display
void display7Segment(unsigned char digit)
{
    P1 = sevenSegmentPatterns[digit];   // display the pattern for the digit on 7-segment display
}

#endif // _SEVENSEGMENT_H_

The header file contains the necessary pin definitions for the 7-segment display and an array of 7-segment patterns for each digit from 0 to 9. The display7Segment() function takes a digit as an argument and displays the corresponding pattern on the 7-segment display.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.