Hi, I am trying to make a common anode 7 segment and shift register display show numbers 1-9. I used the code from: 7 Segment Display using Shift Register and Arduino – Shashank Mehta
However, it lit up the LEDs, but not the right ones. How can I change "the byte sequence" so that it will work properly?
Thanks!
//The byte sequence
int seq[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
The point is that you have not wired it up in the same way as that article.
If you want to try by trial and error, make all the bytes 0x00 that is all off. Then make one 0x01 which is just one segment on and see which it is.
If 0x00 makes all the segments light up then you are using current sinking not sourcing.
int seq[14] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
Because those define what data is sent to your shift register, and so what LEDs are switched off (instead of on).
I can't tell you exactly how to change it, because i don't know how to convert this into the bits that are set.
I can tell you that the value 0x06 makes an E and switches off the two right hand vertical LEDs, as they form the number 1.
And 0x6F will make the left hand lower vertical LED light up (an inverse 9) and 0x3F will make the center horizontal LED light up (an inverse 0).
I can't tell you exactly how to change it, because i don't know how to convert this into the bits that are set.
It's rather simple:
there are the bit values 1 2 4 and 8
every hex digit is a combination of those:e.g. 6 is 2 + 4
hex digits have a range 0 to F ( A == 10 == 8+2 , B == 11, ... , F = 15 = 8+4+2+1 )
now you invert a digit by exchanging the set bit values: 6 = 4+2 --> inverts to 8+1 = 9
a byte consists of 2 digts, both are inverted: 0x3F 3 = 2+1 --> inverts to 8+4 = 12 = C , F = 8+4+2+1 inverts to 0 ==> 0x3F --> 0xC0
As a check: original + inverted sums up to 0xFF, (why?) because every bit is set in either the original or in the inverse, never both and never none
--> you can invert a byte by subtracting it from 0xFF ( if that's easier )
const int seq_inv[14] = {0xFF-0x3F,0xFF-0x06,0xFF-0x5B,0xFF-0x4F,0xFF-0x66,0xFF-0x6D,0xFF-0x7D,0xFF-0x07,0xFF-0x7F,0xFF-0x6F};
And, as GM said, the "exclusive or" ( xor , ^ ) can be seen as an invert operator : a byte xor'ed with a byte with all bits set (0xFF) results in the inverted byte . ( Original XOR Inverted results in 0xFF )
Thank you for all the help, I really appreciate it guys. CrossRoads, using those values lit up the leds, but not as a number. What I really want to know is what each number in the byte sequence (when in binary, not hex format) does. For example, does the value B11000000 make 2 leds HIGH, (the two 1s) and the rest LOW? I was trying to find a pattern by putting different numbers in for the zeroes and ones, but I couldn't find one. Sorry again that I am not getting this, but am eager to learn!
Here's what I usually have set up:
Shift register output bits 7-6-5-4-3-2-1-0 connect to LED segments DP-G-F-E-D-C-B-A
Then shifting out the data below:
0 = 0x3f = B00111111 = use B11000000
1 = 0x06 = B00000110 = B11111001 a
2 = 0x5b = B01011011 = B10100100 f b
3 = 0x4f = B01001111 = B10110000 g
4 = 0x66 = B01100110 = B10011001 e c
5 = 0x6d = B01101101 = B10010010 d dp
6 = 0x7d = B01111101 = B10000010
7 = 0x07 = B00000111 = B11111000
8 = 0x7f = B01111111 = B10000000
9 = 0x6f = B01101111 = B10010000
lights up the LEDs properly. Make sure you have a 0.1uF cap on your Vcc pin to Gnd.
I use B11000000 format to make it easier to visualize.
The compiler doesn't care if you use 0b11000000 or B11000000 or 0xC0 or 192, it all looks like the same number when compiled.
hockeystar56:
What I really want to know is what each number in the byte sequence (when in binary, not hex format) does.
Try not to be afrade of hex format, it is a lot easer to handle than binary because it is more compact. At the end of the day they both show the same thing, the bit pattern.
By the way don't put the capacitor on any signal line, that is an error in some tutorials. It goes between the power and ground.