# How to convert decimal values for the 7 segments ?

#### John99407

##### Junior Member level 1
Hi!, I'm trying to build a basic counter as a test for figuring out how to interface with multiple 7-segment LEDs. I have a 4-digit cc(common anode)-type 7-segment multiplexed display unit. I'm working displaying numbers on 7 segment LED display by multiplexing. I want to convert decimal values to 7 segments patterns

Code:
void display (int i)
{

}

void main ()
{
unsigned int i = 0;

for (i= 0; i<10000; i++)
{
display (i);
}
}

Which device? - if it a device with configuration bits, then what are the configuration bits and fosc?
Which compiler?
How is 7-seg connected?

#### betwixt

##### Super Moderator
Staff member
hexreader is right of course but the general principle is to convert the actual digit into a pattern of segments. There are seven segments so if you represent each segment as one bit in a byte, you can use a simple look up table to make the conversion. Start by assigning the segments a bit number, for example from segment A to segment G might be bit 0 to bit 6. Then for each digit shape on the LEDS, write down the combination of 1s and 0s that correspond with the lit up and blank segments. Combine them into a byte and that will be the value in the look up table.

I'll guide you but I'm not going to do it for you.
Hint: The entire look up routine can be done in one short line.

Brian.

#### KlausST

##### Super Moderator
Staff member
Hi.
The problem is not new. Instead has been solved many thousand times before.
Many documents, descriptions, code examples can be found in the internet.

There are many solutions. Instead of one counter from 0 to 9999 you could use 4 cascaded counters from 0 to 9.

Klaus

#### dick_freebird

I guess when you say "build" it means "write a
verilog module". There are 74xx-series BCD to
7 segment decoders, if I recall... yep, 74141
through 74147.

Seems to me that a simple lookup table, 7x10,
would do it most easily.

Treating each segment as a unique logical
equation, acting on Booleans, is probably
more like the 74143 BCD-to-7-segment
decoder/LED driver and if you cracked TI's
_The TTL Databook For Design Engineers_
open to page 7-138 or 7-147 you will see
two logic-gate implementations of the
decode.

#### paulfjujo

hello ,

you need to define all the hardware links before begin to write some code ...

here is an example to drive 6 common Anode 7seg display
use of buffer to drive segments and digits ( because big digit size =5")

Once hardwra is defined , you can build the correspondance table for Segments and digits

Code:
// Hardware
/*      RB7 ----------x  ----> ULN --->  pin 1 circuit M948A-2  Melodie
Pin 28  RB7  dispo  --x  x----ICSP Data
Pin 27  RB6  seg g  --x  x-----ICSP Clock
Pin 26  RB5  seg f
Pin 25  RB4  seg e
Pin 24  RB3  seg d
Pin 23  RB2  seg c
Pin 22  RB1  Seg b
Pin 21  RB0  Seg a    un 1 allume le segment

Pin  7  RA5 Digit 6  H  un zero allume le digit
Pin  6  RA4 Digit 5  H  sortie port A
Pin  5  RA3 Digit 4  M  UDN collecteur transistor  PNP -> Anode commune Afficheur
Pin  4  RA2 Digit 3  M
Pin  3  RA1 Digit 2  S
Pin  2  RA0 Digit 1  S

#define SegA  LATB.B0
#define SegB  LATB.B1
#define SegC  LATB.B2
#define SegD  LATB.B3
#define SegE  LATB.B4
#define SegF  LATB.B5
#define SegG  LATB.B6

#define AFF_S  LATA.B0
#define AFF_DS LATA.B1
#define AFF_M  LATA.B2
#define AFF_DM LATA.B3
#define AFF_H  LATA.B4
#define AFF_DH LATA.B5

// cde directe via ULN2083 inverseurs pour les segments   niveau 1 = Allume segment
const code unsigned char  E878_Segments[]={63,6,91,79,102,109,125,7,127,111,0};
// Cde via ULN2981 pour les digits  1=allume digit
const code unsigned char Afficheur[]={1,2,4,8,16,32,0 };
Timer2 based on 4mS
to get a refresh time of 6x4=24mS for the 6 digits
so no flikering effect..

example of use in a Clock with 6 digits ( Time or temperature display)
with all display treatment inside Timer2 interrupt

Code:
  if  ( (TMR2IE_bit==1) && ( TMR2IF_bit==1))
{
TMR2IF_bit=0;
//U1TXB='?';   // for debugging only
Index_Digit++;
// version 6 digits
if (Index_Digit>5) Index_Digit=0;

// ....
switch (Index_Digit)
{
case 0:
LATA.B5=0;
Temp = second & 0x0F ;
LATB=E878_Segments[Temp]|  Melodie;
LATA.B0=1;
break;
case 1:
LATA.B0=0;
Temp = second >>4 ;
LATB=E878_Segments[Temp]|  Melodie;
LATA.B1=1;
break;
case 2:
LATA.B1=0;
Temp = minute & 0x0F ;
LATB=E878_Segments[Temp]|  Melodie;
LATA.B2=1;
break;
case 3:
LATA.B2=0;
Temp = minute >>4 ;
LATB=E878_Segments[Temp]|  Melodie;
LATA.B3=1;
break;
case 4:
LATA.B3=0;
Temp = heure & 0x0F  ;
LATB=E878_Segments[Temp]|  Melodie;
LATA.B4=1;
break;
case 5:
LATA.B4=0;
Temp = heure >>4 ;
LATB=E878_Segments[Temp]|  Melodie;;
LATA.B5=1;
break;
default:
break;
} //switch
}
PIR1.TMR2IF=0;
}