Skip to main content

Program to create Up counter on three 7-Segment LED display

7-Segment LED display is the combination of 8 LEDs in a special digital display format. I have explained the 7-segement LED displays in earlier post and simple digit display on it. Now, i want to discuss the interfacing of different 7-segment displays with 8051 microcontroller.

Interfacing:


interfacing of three 7-segment with 8051

- As you can see above circuit diagram in which three 7-segments are connected to port P1, P2, P3 and the common cathode are connected to the port P0.1, P0.2 and P0.3. Now, we have to run a counter from 0-999. So, the basic concept is that you implement binary to hexadecimal conversion code in which MSB and LSB are extracted. Now, take MSB as hundredth digit and LSB as ones place digit.

Code:

#include"REGx52.h"
#define seg_data P1
#define seg_data1 P2
#define seg_data2 P3
sbit seg1=P0^1;
sbit seg2=P0^2;
sbit seg3=P0^3;
int num=0;
int ones=0,tens=0,hundreds=0;
void display_digit(int );
void delay();
void display_digit1(int );
void display_digit2(int );

void main()
{
while(num<=999)
{
ones=num%10;
tens=(num/10)%10;
hundreds=(num/100);
seg1=seg2=seg3=1;

display_digit(ones);
seg1=0;
delay();
seg1=1;

display_digit1(tens);
seg2=0;
delay();
seg2=1;

display_digit2(hundreds);
seg3=0;
delay();
seg3=1;
num++;
}
}
void delay()
{ 
long int k;
for(k=0;k<10000;k++};
}
void display_digit(int a)
{
switch(a)
{
case 0:
seg_data=0x00;
break;

case 1:
seg_data=0x06;
break;

case 2:
seg_data=0x5b;
break;

case 3:
seg_data=0x4f;
break;

case 4:
seg_data=0x66;
break;

case 5:
seg_data=0x6d;
break;

case 6:
seg_data=0x7d;
break;

case 7:
seg_data=0x07;
break;

case 8:
seg_data=0x7f;
break;

case 9:
seg_data=0x6f;
break;
}
}

// for tens place

void display_digit1(int b)
{
switch(b)
{
case 0:
seg_data1=0x00;
break;

case 1:
seg_data1=0x06;
break;

case 2:
seg_data1=0x5b;
break;

case 3:
seg_data1=0x4f;
break;

case 4:
seg_data1=0x66;
break;

case 5:
seg_data1=0x6d;
break;

case 6:
seg_data1=0x7d;
break;

case 7:
seg_data1=0x07;
break;

case 8:
seg_data1=0x7f;
break;

case 9:
seg_data1=0x6f;
break;
}
}

//for hundredth place 

void display_digit2(int c)
{ 
switch(c)
{

case 0:
seg_data2=0x00;
break;

case 1:
seg_data2=0x06;
break;

case 2:
seg_data2=0x5b;
break;

case 3:
seg_data2=0x4f;
break;

case 4:
seg_data2=0x66;
break;

case 5:
seg_data2=0x6d;
break;

case 6:
seg_data2=0x7d;
break;

case 7:
seg_data2=0x07;
break;

case 8:
seg_data2=0x7f;
break;

case 9:
seg_data2=0x6f;
break;
}
}

Explanation of code:

- In code, we implemented the different function for different display so that individual counter can be run. But, when ones place digit number reaches to 9 from 0, the tens place digit gets incremented automatically and so on. For this, create a while() loop to 999 and implement.
Now, write hexa value for each character. Example, when '1' is called from main program it goes to switch() case where it picks up the hexa value to switch ON that particular LEDs in order to display numeric '1'. Now, implement the other characters in similar way.

Comments

  1. How can an 8051 port with a pull up resistor of 100K possibly source the necessary current to light up an LED. The LEDs need to be common anode, with segement resistors, so the 8051 can pull down a segment (because the port can sink up to 15ma but source very little)

    ReplyDelete
  2. It is indeed a challenge for an 8051 microcontroller with a pull-up resistor of 100K to directly source enough current to light up an LED, especially if the LEDs require higher current levels than what the microcontroller can provide when sourcing current. The 8051 is generally not designed to handle high-current outputs when sourcing.

    To overcome this limitation and still drive the LEDs effectively, you can use a technique called "charlieplexing." Charlieplexing is a method that allows you to control multiple LEDs using fewer pins than traditional methods.

    Here's how you can do it:

    Common Anode LEDs: Since you mentioned that the LEDs are common anode, this means that the anodes of all the LEDs are connected together.

    Segment Resistors: Each LED segment requires a current-limiting resistor when connecting to the microcontroller's pins. Make sure you use appropriate resistors to limit the current through each segment.

    Charlieplexing Principle: Charlieplexing works on the principle of selectively enabling specific LED segments by manipulating the state of the microcontroller pins. You can switch between driving a segment as an anode (HIGH) or sinking current as a cathode (LOW).

    Pins Combination Formula: To determine the number of LEDs you can control, use the formula n*(n-1), where n is the number of available microcontroller pins. This formula calculates the number of unique pin combinations possible.

    For example, if you have three microcontroller pins (P1, P2, P3), you can control six LEDs (3*(3-1)) using charlieplexing.

    Dynamic Control: To light up a particular LED segment, you need to dynamically set the microcontroller pins as either OUTPUT and HIGH or OUTPUT and LOW based on the LED you want to activate. The others should be set as INPUT.

    Timing Considerations: You need to take care of timing when switching between states to avoid creating a short circuit. Proper software implementation is crucial for successful charlieplexing.

    Charlieplexing can be a bit tricky to implement, especially if you have many LEDs to control, but it offers a way to overcome the limited sourcing capabilities of the 8051 microcontroller. Always ensure to calculate the appropriate current-limiting resistors for each segment to protect the microcontroller and the LEDs from excess current.

    As a final note, if you find charlieplexing too complex or if you need to control a significant number of LEDs, you can use external driver ICs, such as shift registers or LED driver chips, to simplify the circuit and handle the current requirements effectively.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Switch OFF Citizen Calculator

You might be confused on seeing no OFF button on your calculator. Actually, these are general calculator available at low cost in the market. They process simple calculations like division, subtraction, Addition, Multiplication. But, how will you OFF that calculator having no option on them. There is always a trick which OFF them. It depends on the company calculator and their series to which they belong. I can tell you about Citizen Calculator CT-500 (as you can see in the image), how can you switch OFF it and can save battery too. If you leave calculator idle for 6-7 minutes, it will goes OFF automatically. My calculator goes OFF in 6.50 minutes from the point of leaving it idle. But, this is not the power saving trick for these types of calculator having not much backup battery. To save the power of your normal calculator, here is the trick to switch OFF it. Press ' /(divide sign)', 'x(multiply sign)', '%(percentage sign)', 'autoreplay option(

Buzzer interface with 8051 microcontroller

Buzzer  is a electronic device that converts the electronic signal into buzzing noise, that is applied to it. It can be used as electronic bell or as quiz buzzer in many applications around us. Here, i world like to discuss the interfacing of a small buzzer with 8051 microcontroller and how different projects can be constructed. Buzzer Interfacing: This project shows the interface with AT89S52 microcontroller to a buzzer. When a push button is pressed, the buzzer will get ON and OFF ( number of times set in the code ) and then stops. Circuit Diagram: - The port P1 of the microcontroller is connected to buzzer. This type of connection is possible, if the current requirements of the buzzer is not more than 20mA. The output is in current source mode so that buzzer will turn ON when the output of the port is logic LOW. Switch is connected to port P3 which remains at logic HIGH by pull up resistor.  Code: #include "REG52.h" #define buz P1 sbit SW=P3^0; long int i; voi

Different ways to generate delays in 8051

The delay length in 8051 microcontroller depends on three factors: The crystal frequency the number of clock per machine the C compiler. The original 8051 used 1/12 of the crystal oscillator frequency as one machine cycle. In other words, each machine cycle is equal to 12 clocks period of the crystal frequency connected to X1-X2 pins of 8051. To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one. The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051, regardless of the 8051 version. In other words, AT89C51, DS5000, and DS89C4x0 the duration of the time to execute an instruction varies, but they all use 1/12th of the crystal's oscillator frequency for the clock source. 8051 has two different ways to generate time delay using C programming, regardless of 8051 version. The first method is simply using Loop   program function in which Delay() function i