Which bit goes first
shiftOut sends the byte one bit at a time, and the first bit sent is pushed furthest, to Q7, the dot. MSBFIRST sends bit 7 first, which is how the table is written. LSBFIRST sends bit 0 first and mirrors every digit.
First in goes furthest
Every clock edge pushes the bits one place along, from Q0 towards Q7. So the first bit you send is pushed eight times and ends in Q7, and the last bit sent stays in Q0. On this board Q7 is the dot and Q0 is segment a.
shiftOut(dataPin, clockPin, order, value) takes the order as its third
argument:
- MSBFIRST sends bit 7 first. Bit 7 ends on Q7, the dot; bit 0 ends on Q0, segment a. That is the book's table.
- LSBFIRST sends bit 0 first. Bit 0 ends on the dot, and every other bit lands one place mirrored.
One word, every digit
Mixed up, the byte for 2, 0x5B, arrives as 0xDA: the dot lights and the shape is not a 2. Every digit comes out wrong in its own fixed way, and that consistency is the clue. Random segments that change from one attempt to the next are wiring; the same wrong shape every time is bit order.
A few bytes read the same both ways: 4 is 0x66, 0b01100110, a palindrome. So a sketch that shows 4 correctly can still have the order backwards. Check with 2.
The page this book replaces shipped a sketch with LSBFIRST and a table written backwards to match, segment a in bit 7. It worked, and it is the reason copying half of it into another sketch fails. The book's sketch uses the ordinary table and MSBFIRST.
When it does not work
The byte is arriving reversed: segment a's bit is on the dot and the dot's bit is on segment a. Change LSBFIRST to MSBFIRST in the shiftOut call. The table is not the problem.
Its table was written the other way round, with segment a in bit 7. Reversed once in the table and once in the sending, it came out right. Either pairing works; the book's table and MSBFIRST are the pair to keep together.
Whichever order the loop counts. The book's loop runs i from 7 down to 0 and sends bit i each time, which is MSBFIRST. Counting up from 0 would be LSBFIRST.
How much each segment takes at 3.3 V and at 5 V, and what the chip can give.
VCC sets the current →Edit this page — content/books/hc595-segment-led/which-bit-goes-first.mdx
Questions about this product
See what other owners have asked, and read their solutions.
74HC595 Segment LED
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.