0

I have just started using a 74HC595 IC and want to be able to change a single value (bit) of the byte value without changing any other value in the byte, for example:

const uint8_t LED_0 =   0b00000001;

This will turn of all LED's except LED 0 (depending on the Most / Least Significant Bit order).

What I was thinking of doing in order to retain each bit value, is to place every bit value into an array e.g:

int seq[] = {0b,0,0,0,0,0,0,0,1};

If and when a pin on the 74HC595 changes I can easily change the value in the array to either 1 or 0.

The following procedure writes the byte to the IC:

void updateShiftRegister( const uint8_t value )
{
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, value );
    digitalWrite(latchPin, HIGH);
}

The function is then called in this manner:

updateShiftRegister( LED_0 ); // led 0 on, other leds off

How can I combine the seq array above, to be able to pass all the values in the array to my procedure ?

I have looked at sprintf and String.concat but I am not entirely sure that is the way to go. I honestly doubt if the sprintf function should even be a consideration.

If a better / simplified method exist, please do not hesitate to educate me on that.

Regards, Tino

  • I guess this post will help you https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit – Michael Heil Mar 05 '20 at 21:15
  • @mike Thank you for your reply. After working through the link I realised that bit manipulation is not going to work in this case because, the 8bit value representing each of the 8 pins on the IC needs to be sent to the IC every time the status of a pin changes. – Tino Fourie Mar 05 '20 at 22:06
  • @mike: Unless you are referring to a variable containing the bit data (0b00000000) and then manipulate that before sending it to the IC. I will look into that for sure!! – Tino Fourie Mar 05 '20 at 22:17
  • That's exactly what you want to do. Keep one byte and use bit manipulation to modify whichever bits you want and send the byte out to the shifter. – Delta_G Mar 05 '20 at 23:42
  • @mike Thank you once again for steering me in the right direction. @ Delta_G Thank you for confirming my thoughts on what Mike put forward. Bit manipulation is not as difficult as it looked. I used the Arduino pages dealing with BitMath in specific Forcing a bit value. I am not sure if I am supposed to post my code here, very happy to do so if someone else needs the information - but I guess that will be doing their work for them. – Tino Fourie Mar 06 '20 at 08:44

0 Answers0