Getting the wires right · 06 of 10

One byte, one digit

Bit 0 is the top bar, bit 7 is the dot, and the six in between go round the digit. That makes every character a number you can work out on paper — and on a common-anode digit, the same number with every bit flipped.

Which bit is which bar

The eight bits of the byte map to the eight LEDs in order, starting at the bottom:

Bit76543210
SegmentHGFEDCBA
Worth1286432168421

A is the top bar. B, C, D, E, F go round the outside clockwise from the top right. G is the bar across the middle. H is the dot.

So a 0 needs everything except the middle bar and the dot — A, B, C, D, E, F — which is 0b00111111. A 1 needs the two bars on the right, B and C, which is 0b00000110.

One byte, one digit
3 · 5161AS
Character3
Which digit
Decimal point
Segments lit
A B C D G
You write
0x4F
The 595 gets
0x4F
What you write is what goes out. A 5161AS lights a segment when its pin goes HIGH, so a 1 bit means a lit segment all the way from the table to the glass. Count the lit boxes against the lit bars: 5 segments. Blank is 0x00.

Slide through the characters and read the boxes against the bars. The drawing is made from the byte, not from a list of names, so what you see is what that number does.

A to F, and what seven bars cannot do

The sixteen characters in the figure are the hexadecimal digits, which is as far as a single seven-segment digit goes. Six of them are letters, and two are lower case — b and d — because an upper-case B on seven bars is an 8 and an upper-case D is a 0.

Everything else is a compromise or an impossibility. There is no K, no M, no V, no W and no X. Words on a seven-segment display are a party trick, and HELLO works only because those five letters happen to be among the ones that do.

The common-anode byte

Switch the figure to the 5161BS and the top row does not change. The bottom one does.

A segment table is written once, for a common-cathode digit, where a 1 bit means a lit bar. A common-anode digit lights on LOW, so the byte that goes down the wire is the same table with every bit flipped. You do not keep two tables; you keep one and invert it in the one place the byte leaves:

const uint8_t kDigits[10] = {
  0b00111111, // 0
  0b00000110, // 1
  0b01011011, // 2
  0b01001111, // 3
  0b01100110, // 4
  0b01101101, // 5
  0b01111101, // 6
  0b00000111, // 7
  0b01111111, // 8
  0b01101111, // 9
};

Adding the decimal point is a bit-or, not a second table: kDigits[3] | 0x80 shows 3., and it works before the inversion, because the inversion is the last thing that happens.

What the glass is telling you covers the shape you get when the inversion is set the wrong way, which is the easiest fault here to recognise and the easiest to fix.

When it does not work

Where do the segment letters come from?

They are the standard names for the seven bars, and they have been since long before this board: A is the top, then B, C, D, E, F go clockwise round the outside from the top right, and G is the middle. H is this board's name for the decimal point, which usually gets called DP elsewhere.

I want a letter the table does not have

Work out the byte yourself. Decide which bars the shape needs, add up the bit value of each — A is 1, B is 2, C is 4, D is 8, E is 16, F is 32, G is 64, the dot is 128 — and send the total. Seven bars cannot make a K, an M, a V, a W or an X, which is the real limit.

0b00111111 and 0x3F keep appearing for the same thing

They are the same number written two ways. Binary shows you which segments are on at a glance, which is why the tables in this book are binary; hexadecimal is shorter, which is why most people's code uses it. The compiler does not care.

My decimal point is always on

Bit 7 is set in the byte you are sending. On a common-anode digit it is the opposite: bit 7 clear in the byte on the wire lights the dot, because the whole byte is inverted. Blank on a common-anode digit is 0xFF, not 0x00, and getting that wrong lights every segment including the dot.

Where this goes next

The limit that actually gets met, and why the digit that meets it is 8.

How much current

Edit this page — content/books/seven-segment/one-byte-one-digit.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

24-Pack 0.56" 7-Segment LED with 74HC595 Driver

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum