Go Down

### Topic: Hexadecimal encoding in 7 segment Display (Read 17619 times)previous topic - next topic

#### anilkunchalaece

##### May 30, 2015, 12:30 pm
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 ?

#### Riva

#1
##### May 30, 2015, 12:52 pmLast Edit: May 30, 2015, 12:56 pm by Riva
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?
Don't PM me for help as I will ignore it.

#### PaulRB

#2
##### May 30, 2015, 01:04 pmLast Edit: May 30, 2015, 01:05 pm by PaulRB
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:

Code: [Select]
` aaaf   bf   bf   b ggg e   ce   ce   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

#### anilkunchalaece

#3
##### May 30, 2015, 01:08 pm
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?

#### anilkunchalaece

#4
##### May 30, 2015, 01:14 pm
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

#### PaulRB

#5
##### May 30, 2015, 01:28 pmLast Edit: May 30, 2015, 01:31 pm by PaulRB
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.)

#### anilkunchalaece

#6
##### May 30, 2015, 01:42 pm
sketch

Code: [Select]
`/*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 initializationint pin[] = {  2,3,4,5,6,7,8};//calculating the no of pinsint 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 setupvoid 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 loopvoid 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
Code: [Select]
`/*  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 setupvoid 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`

#### PaulRB

#7
##### May 30, 2015, 01:50 pmLast Edit: May 30, 2015, 01:53 pm by PaulRB
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.

#### PaulRB

#8
##### May 30, 2015, 02:09 pmLast Edit: May 30, 2015, 02:14 pm by PaulRB
Can I suggest instead of coding the patterns like this:
Code: [Select]
`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;`

Code: [Select]
`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:

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

#### anilkunchalaece

#9
##### May 30, 2015, 03:10 pmLast Edit: May 30, 2015, 03:13 pm by anilkunchalaece
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.

#### Paul__B

#10
##### May 30, 2015, 03:12 pm
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.

#### anilkunchalaece

#11
##### May 30, 2015, 03:26 pm
0b1101011, // FIVE

changed to

0b1011011 // 0x5B

#### anilkunchalaece

#12
##### May 30, 2015, 03:27 pm
Frankly, you would probably be better off to wait until you do.

May I know Why?

#### PaulRB

#13
##### May 30, 2015, 06:13 pm
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.

#### Paul__B

#14
##### May 31, 2015, 12:25 am
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.

Go Up