Shift register missing bits

Hey friends,

I have a two daisy chained shift registers (74AHC595) which are controlled via an ESP32. I want to set one output to high at a time before switching to the next.

The code seems to work, but the outputs O_9 and O_10 are not staying high (zoom) after setting them, whereas all the other ones are working fine. This is the used code snipped:


<span style="color:#323232;">pinMode(SHIFT_OUT_DATA, OUTPUT);
</span><span style="color:#323232;">pinMode(SHIFT_OUT_CLK, OUTPUT);
</span><span style="color:#323232;">pinMode(SHIFT_OUT_N_EN, OUTPUT);
</span><span style="color:#323232;">pinMode(SHIFT_OUT_LATCH, OUTPUT);
</span><span style="color:#323232;">
</span><span style="color:#323232;">digitalWrite(SHIFT_OUT_N_EN, LOW);
</span><span style="color:#323232;">
</span><span style="color:#323232;">uint16_t input_bin = 0b1000000000000000;
</span><span style="color:#323232;">
</span><span style="color:#323232;">for(int i=0; i&lt;17; i++){
</span><span style="color:#323232;">
</span><span style="color:#323232;">    byte upper_byte = input_bin >> 8;
</span><span style="color:#323232;">    byte lower_byte = input_bin &amp; 0x00FF;
</span><span style="color:#323232;">
</span><span style="color:#323232;">    digitalWrite(SHIFT_OUT_LATCH, LOW);
</span><span style="color:#323232;">    shiftDataOut(SHIFT_OUT_DATA, SHIFT_OUT_CLK, MSBFIRST, lower_byte);
</span><span style="color:#323232;">    shiftDataOut(SHIFT_OUT_DATA, SHIFT_OUT_CLK, MSBFIRST, upper_byte);
</span><span style="color:#323232;">    usleep(10);
</span><span style="color:#323232;">    digitalWrite(SHIFT_OUT_LATCH, HIGH);
</span><span style="color:#323232;">
</span><span style="color:#323232;">    delay(10)
</span><span style="color:#323232;">    input_bin = input_bin>>1;
</span><span style="color:#323232;">} 
</span>

Is there anything I’m doing wrong, or any idea on where the problem may lie? I’ve already tried looking for shorts and other error sources, but the design was manufactured on a PCB and no assembly issues are noticeable.

FuzzChef,

What does shiftDataOut do? You loop over it but you give the whole byte to it anyway in each loop.

mvirts,

Would it work if you made that delay 1000?

mvirts,

Also try upping the usleep call?

mvirts,

Hmmmm do you want to write to both shift register at the same time? I say this because you’re looping 16 times, but seem to be sending the high and low bytes out 16 times over rather than one bit each time, although you are shifting the input.

mvirts,

Maybe I’m getting ahead of myself, but maybe try using digitalWrite for a single bit instead of shiftDataOut?

quiescentcurrent,

Good idea, I’ve tried usleep after all lines, but no change…

Kalcifer, (edited )

The first two lines of the for loop,


<span style="color:#323232;">byte upper_byte = input_bin >> 8;
</span><span style="color:#323232;">byte lower_byte = input_bin &amp; 0x00FF;
</span>

don’t really accomplish anything. The first line is bit shifting to the right 8, and then you just bitwise and it resulting in the same thing. For example, starting with input_bin:


<span style="color:#323232;">1000 0000 0000 0000
</span><span style="color:#323232;">>> 8
</span><span style="color:#323232;">0000 0000 1000 0000
</span><span style="color:#323232;">&amp; 0xFF
</span><span style="color:#323232;">0000 0000 1000 0000
</span>

So, every time you go through a cycle of the for loop, you’ll just start with the same values in upper_byte, and lower_byte. To sequentially output each shifted value, you’ll instead want something like:


<span style="color:#323232;">output_value = 0b1
</span><span style="color:#323232;">for i = 1 to 16:
</span><span style="color:#323232;">    latch(low)
</span><span style="color:#323232;">    shift_out(output_value)
</span><span style="color:#323232;">    latch(high)
</span><span style="color:#323232;">    output_value = output_value &lt;&lt; 1
</span>

That is, if I interpereted correctly that you want the shift registers to output the following:


<span style="color:#323232;">output_count, upper_shift_register, lower_shift_register
</span><span style="color:#323232;">1, 00000000, 00000001
</span><span style="color:#323232;">2, 00000000, 00000010
</span><span style="color:#323232;">3, 00000000, 00000100
</span><span style="color:#323232;">.
</span><span style="color:#323232;">.
</span><span style="color:#323232;">.
</span><span style="color:#323232;">16, 10000000, 00000000
</span>

Note: Lemmy has a bug where it doesn’t format some symbols correctly, so the left angle bracket gets formatted as &lt;. The same issue exists for the right angle bracket, the ampersand, and I would presume others.

quiescentcurrent,

You’re 100% right, I’ve lost ‘i’ somewhere in my debugging process

byte upper_byte = input_bin >> (8+i) ; byte lower_byte = (input_bin >> i) & 0x00FF;

mvirts,

I think you got and and or switched, first two lines should be fine for shifting the top 8 bits down.

Kalcifer,

I don’t follow what you mean.

FuzzChef,

I think what he refers to is that you seem to do a bitwise or for the second line instead of the bitwise and.

Kalcifer,

2nd line of what? Oh you are completely right. My bad. Idk why I wrote that. I’ll fix my comment.

mvirts,

Yes that’s what I was thinking

Kalcifer,

Would you not want to shift out the upper byte first? I could be misinterpreting your setup.

quiescentcurrent,

You’re probably right, but that should only change the order of the outputs right?

  • All
  • Subscribed
  • Moderated
  • Favorites
  • askelectronics@discuss.tchncs.de
  • localhost
  • All magazines
  • Loading…
    Loading the web debug toolbar…
    Attempt #