Hexadecimal encoding in 7 segment Display

Can anyone explain me what is Hexadecimal Encoding in 7 Segment Display?

for digit 0

gfedcba - 0x3f - 0011 1111 --> g and f are off
abcdefg - 0x7e - 0111 1110 --> a is off ?

is this assumption of mine is right ?

anilkunchalaece:
Can anyone explain me what is Hexadecimal Encoding in 7 Segment Display?
Hexidecimal is just a method of displaying a number, it uses base 16. Decimal uses base 10 & binary uses base 2.
Take the decimal number 63, if you display it using hexidecimal it's 0x3F (you put the 0x on the front to let people/programs know it's in base 16) to display in binary it would be 0b111111 (0b in front denoted base 2)

for digit 0

gfedcba - 0x3f - 0011 1111 --> g and f are off < Wrong: a-f are on
abcdefg - 0x7e - 0111 1110 --> a is off ? < Wrong: a-f are on, the segment order has reversed

is this assumption of mine is right ?
Nearly.

Maybe your refering to a way of displaying hexidecimal numbers on a 7 segment display? 0123456789AbCdEF

To display upper case A you light segments ABCEFG
Lower case b =segments CDEFG
and so on?

Hi,

This is a slightly confusing subject.

In hexadecimal, we use the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

When using 7 segment displays, the segments are referred to as a, b, c, d, e, f, g, dp

But there is no true relationship between the hex digits A to F and the segments a to f!

7 seg displays are usually laid out with this convention:

 aaa
f   b
f   b
f   b
 ggg 
e   c
e   c
e   c
 ddd    dp

As for how you convert the segment patterns into binary patterns and then into hexadecimal representations, that depends entirely on how you wire up your digits.

Sorry if I have repeated what Riva says... I though I would post it anyway!

Paul

what's the difference between those two encodings?

if i connect my digital pins 2,3,4,5,6,7,8 to g,f,e,d,c,b,a which encoding i have to use?

if i write hexnumber 0x7E(0b0111 1110) to pins 2,3,4,5,6,7,8 then i get the display 0
But it is giving something else for 0x3F

Either your wiring is incorrect, or your sketch is incorrect or your display is faulty. If you write HIGH/1 to each of pins 2, 3, 4, 5, 6, 7, 8 then you should see digit "8" not "0". (Assuming you are not using the dp segment.)

Post your sketch (use code tags) and your wiring schematic.

sketch

/*
hEXA DECIMAL ENCODING FOR DISPLAYING NUMBERS 0 TO 9
 FOR COMBINATION - GFEDCBA to pins 2,3,4,5,6,7,8 
 */
/*
 Hex codes for displayiing respective values
 */
const int ZERO = 0X7E;
const int ONE = 0X30;
const int TWO = 0X6D;
const int  THREE = 0X79;
const int  FOUR = 0X33;
const int  FIVE = 0X5B;
const int  SIX = 0X5F;
const int  SEVEN = 0X70;
const int   EIGHT = 0X7F;
const int  NINE = 0X7B;

//pins initialization
int pin[] = {
  2,3,4,5,6,7,8};

//calculating the no of pins
int no_pins = sizeof(pin)/sizeof(pin[0]);


void setup(){
  Serial.begin(9600);
  //setting all pins as Outputs
  for(int i = 0; i < no_pins;i++)
  {
    pinMode(pin[i],OUTPUT);

  }//end of for loop
}//end of setup


void loop(){
  for(int i=0;i<=9;i++)
  {
    switch(i)
    {
    case 0:
      display_it(ZERO);
      break;

    case 1:
      display_it(ONE);
      break;

    case 2:
      display_it(TWO);
      break;

    case 3:
      display_it(THREE);
      break;

    case 4:
      display_it(FOUR);
      break;

    case 5:
      display_it(FIVE);
      break;

    case 6:
      display_it(SIX);
      break;

    case 7:
      display_it(SEVEN);
      break;

    case 8:
      display_it(EIGHT);
      break;

    case 9:
      display_it(NINE);
      break;

    default :
      Serial.println("something went wrong");

    }//end of switch
  }//end of for loop()
}//end of loop


void display_it(const int value)
{
  for(int i=2,j=0;i<=8;i++,j++)
  {
    digitalWrite(i,bitRead(value,j));

  }
  delay(1000);
}

i check the respective led pin numbers using this sketch

/*
 
 It is a common cathode 7 Segment Display.
 connect the common cathode to display and all the remaining pins to the Arduino Digital pins 2 to 8
 */

int pin = 2;
int pin_dummy = 2;
void setup()
{
  Serial.begin(9600);
  Serial.println("Hello World");
  for(int i = 2; i <= 8; i++)
  {
    pinMode(i,OUTPUT);
    Serial.print("pin \t");
    Serial.print(i);
    Serial.println("\t is set as Output");
  }//end of for loop

}//end of setup

void loop()
{
  Serial.println("Please enter y");
  while(!Serial.available())
  {
    //wait until user enters the data
  }//end of while
  if(Serial.available())
  {
    if(Serial.read() == 'y')
    {
      if(pin_dummy <= 8)
      {
        digitalWrite(pin_dummy,LOW);
        Serial.println("you entered y");
        digitalWrite(pin,HIGH);
        Serial.print("pin ");
        Serial.print(pin);
        Serial.println(" is activated");
        pin_dummy = pin; // pin_dummy is used to OFF the LED before ON next led in display
        pin++;
      }
      else{
        Serial.println("Maximum pin number is reached");
        pin = 2;
        pin_dummy =2;
        Serial.println("Pin values are RESET");
      }//end of If else

    }
    else{
      Serial.println("you entered wrong character");
    }//end of If Else 
  }//end of If 

}//end of loop

Your diagram does not help very much because the pins on the 7-seg display are not marked "a" to "f". Normally, individual digit displays have 10 pins in two rows of 5 pins. You must understand the use of the 10 pins to make the display show any pattern correctly.

Question: do you wish to display hexadecimal numbers on your display, or only decimal numbers?

If your original question was about how to encode the binary patterns for each digit using hexadecimal in your sketch, why do you wish to do that? There is no advantage to encoding in hex versus binary in your sketch. The advantage of using binary over hex is that a programmer reading the sketch can see the individual patterns of zeroes and ones.

Can I suggest instead of coding the patterns like this:

const int ZERO = 0X7E;
const int ONE = 0X30;
const int TWO = 0X6D;
const int  THREE = 0X79;
const int  FOUR = 0X33;
const int  FIVE = 0X5B;
const int  SIX = 0X5F;
const int  SEVEN = 0X70;
const int   EIGHT = 0X7F;
const int  NINE = 0X7B;

Instead, code them like this:

const byte pattern[10] = {
  //abcdefg
  0b1111110, // ZERO
  0b0110000, // ONE
  0b1101101, // TWO
  0b1111001, // THREE
  0b0110011, // FOUR
  0b1101011, // FIVE
  0b1011111, // SIX
  0b1110000, // SEVEN
  0b1111111, // EIGHT
  0b1111011  // NINE
};

(I hope I did not make any mistakes!)

This will make your sketch much shorter. Your entire "switch" statement can then be deleted and your loop() becomes:

void loop(){
  for(int i=0;i<=9;i++)
  {
    display_it(pattern[i]);
  }//end of for loop()
}//end of loop

PaulRB:
This will make your sketch much shorter.

Thanks..

i am trying the code in proteus. I didn't have 7 segment display right now...

It is not showing any pin labels as i said before small sketch helped me to get the pin labels.

anilkunchalaece:
I am trying the code in proteus. I didn't have 7 segment display right now...

Frankly, you would probably be better off to wait until you do.

0b1101011, // FIVE

changed to

0b1011011 // 0x5B

Paul__B:
Frankly, you would probably be better off to wait until you do.

May I know Why?

anilkunchalaece:
0b1101011, // FIVE

changed to

0b1011011 // 0x5B

You are correct about the binary, I should have put 0 for segment b. However, although it makes no difference to the sketch when it runs, your " // 0x5B" comment is less useful than the comment "// FIVE" when you or someone else reads it in the future.

anilkunchalaece:
May I know Why?

Because notwithstanding the elegance of a good simulator, it seems to me that you are going to be doing much the same work twice. If you have the hardware, then on each "run" you can observe the effects of both software and hardware (wiring) problems.

When you do get to making this for real, be sure to include resistors in series with each one of those segment driver lines.