Go Down

Topic: Inverting Shift Register outputs in Arduino IDE. (Read 1 time) previous topic - next topic


So finally i hooked up a 74HC595 Shift Register, to a 7 Segment display, but it is a common Anode type, i hooked it up like normal LEDs and used the following code to display the letter "A":

Code: [Select]
int dataPin = 2;        //Define which pins will be used for the Shift Register control
int latchPin = 3;
int clockPin = 4;

void setup()
    pinMode(dataPin, OUTPUT);       //Configure each IO Pin
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);

void loop()
        digitalWrite(latchPin, LOW);          //Pull latch LOW to start sending data
        shiftOut(dataPin, clockPin, MSBFIRST, 0x11 );         //Send the data
        digitalWrite(latchPin, HIGH);         //Pull latch HIGH to stop sending data
       delay (1000);

As you can see Hex 0x11 means 00010001, i had to invert in manually, is there anyway i can do it in software, ie the 0's will become 1's and vice versa.


As you can see Hex 0x11 means 00010001

Nope, I don't see. The binary pattern for 'A' is 01000001 which inverted is 10111110.
If you want to invert everything, all you have to do is exclusively OR it with 11111111 (= 0xFF).
I think this will compile (not tested):
Code: [Select]
shiftOut(dataPin, clockPin, MSBFIRST, 'A' ^ 0xFF );

Don't send me technical questions via Private Message.


Maybe it's wired up with
bits 0-7 of the shift register being dp, a, b, c, d, e, f, g

Then 00010001 would be dp off and d off which is a letter 'A' on the
seven segment display.

adilmalik, is that how it is wired up?

To flip invert the bits you will take what you have and exclusive or with with 0xff.

So in your case
it would be:
Code: [Select]
shiftOut(dataPin, clockPin, MSBFIRST, 0x11 ^ 0xff);         //Send the data

But you may be doing a table lookup. In that case
you can then perform the operation on your variable.

You could even have a flag that determines whether or not to do the inversion.
So you could then

Code: [Select]
val = val ^ invert;

If invert is 0 then there will be no inversion, if it is 0xff all the bits will be inverted.

Same could be done in line:

Code: [Select]
shiftOut(dataPin, clockPin, MSBFIRST, 0x11 ^ invert);         //Send the data

if invert is 0 no invert, if 0xff, it inverts the bits.

--- bill

Go Up