ShiftOut and Shift Registers...

Hello

I am working on a nixie tube project and i am trying to figure out how shift registers work.

I am using this example to study:

I see they are setting a value of a byte in the loop, then calling the shift out function to write that data to the shift register. However, i do not understand the code in the shift out function…

Specifically this:

//for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {	
      pinState= 0;
    }

Why is there a for loop? What bitmask? Why is it comparing it to %01000000? How do the bytes correspond do the registers? I would assume for an 8 bit shift register 01010101 would result in alternating pins of the shift register being off and on? How to you change the value of one of the bits of the byte?

The comment is trying to say that if the byte = 0b11010100, then ANDing it with bitmask 1<<6 (which is = 0b01000000 ) to get the value of the 6th bit of that byte ( 0b11010100 ), then the result will be 1

Each bit of the byte is sent to the shift register, one by one, hence the for loop.

I use this little library, it’s better than shiftOut GitHub - bildr-org/Shift-Register-8-Bit-74HC595: Arduino code to control the 74HC595 8-bit Shift-Register

Thank you for your response. The library does look very easy to use.

So, through the for loop after it is done it will turn on 3 of the pins of the shift register... it will check to see if the byte sent has a high bit or low bit in each bit of the byte and then set pinState to 1 or 0. Then it will write the bit out to the shift register and continue writing bits out until it has written an entire byte. Got it.

Now I think the next step is figuring out how to set a bit in a byte, but I'm pretty sure i saw an example for that already. Thanks for your help.

You can use macros bitSet and bitClear

https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitset/

But using the library you don't need to do that, simply use the method Shifter::setPin(int index, boolean val)

Also, why they send in reverse order from bit 7 to bit 0, you may ask? It's because when a bit is sent to the shift register, it's always stored in the first slot Q0, and all bits in the other slots are shifted (hence the name "shift register") one position, in the next slot, so that Q7 is lost (or, if you have multiple devices daisy chained, it goes to Q0 of the next shift register in the chain), Q6 goes to Q7, Q5 to Q6, etc. So by sending in reverse order, bit 7 ends in slot Q7, as expected :slight_smile: