[solved] can only toggle one output on 74hc595n shift register?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
DMWidgets
Posts: 8
Joined: Fri Feb 16, 2018 4:28 pm

[solved] can only toggle one output on 74hc595n shift register?

Post by DMWidgets » Mon Mar 12, 2018 10:56 pm

After much wrangling, I have my esp8266 operating my shift register...mostly. My issue at the moment is I can only toggle the first input (Qa) on the chip AND the first input toggles on the last byte sent (which makes sense now that I think about it). As far as I can tell, input is a simple "drive latch low, spi.write(buffer), drive latch high," no? It's tough to find a clear tutorial on this kind of thing because either the code is doing something fancier than "turn on an LED" or it's written/explained with a micro:bit or pyboard. Using a pyboard example, I've modified it to work with my ESP8266 thusly:

from machine import SPI, Pin

class ShiftRegister:
def __init__(self, spi, rclk):
self.spi = spi
self.rclk = rclk

def shift(self, buf):
buf = [int(i) for i in list(buf)]
self.rclk.value(0)
self.spi.write(bytes(buf))
self.rclk.value(1) # latch data to output


# create SPI bus and pin objects
spi = SPI(1, baudrate=1000000)
rclk = Pin(15, Pin.OUT)
ser = Pin(13, Pin.OUT)
sck = Pin(14, Pin.OUT)
sr = ShiftRegister(spi, rclk)
spi.init()


I'm thinking it has something to do with the sck pin never being used, but again, tutorials are iffy on how they use it. I'm not sure whether sck is used within the spi class somehow or I need to explicitly call it myself to get this working. does sck need to blip after every byte sent to shift it all down the line?
Last edited by DMWidgets on Wed Mar 14, 2018 9:56 pm, edited 1 time in total.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: can only toggle one output on 74hc595n shift register?

Post by pythoncoder » Tue Mar 13, 2018 9:43 am

If you look at the hardware SPI bus section in the docs I don't think you should explicitly specify the pins for hardware SPI. The pins are 13 for data out (MOSI), 14 for clock (SCK), and 12 for data in (MISO which you can leave unconnected or link to 0V). So I'd have

Code: Select all

# create SPI bus and pin objects
spi = SPI(1, baudrate=1000000)
rclk = Pin(15, Pin.OUT)
sr = ShiftRegister(spi, rclk)
spi.init()
I'm puzzled that you say SCK is unused. You should have pin 14 (SCK) connected to SRCLK on the shift register, pin 13 (MOSI) connected to SER and pin 15 (rclk) to RCLK.
Peter Hinch
Index to my micropython libraries.

DMWidgets
Posts: 8
Joined: Fri Feb 16, 2018 4:28 pm

Re: can only toggle one output on 74hc595n shift register?

Post by DMWidgets » Tue Mar 13, 2018 3:12 pm

Latch was specifically called out and so often SS is chosen for that pin that I thought it might help to explcitly set the others as OUT. I've removed those lines but the issue persists. I know the other side's outputs work, as on reset they go floating and the LED on that side lights on/off/flickers and then stays off when a command is sent. What I've also noticed is that the 8th bit output (the only pin on the right side of the chip) responds to whatever the last bit sent is, whether I only send 1, 3 or 8 bits. I was under the impression that it would only respond to whatever the last bit in an 8 bit string is OR respond to whatever shifts into that slot from 7th...

I'll switch the inputs over to an arduino to figure out if it's on the ESP8266 side or if it's in my wiring later today when I have a chance.

DMWidgets
Posts: 8
Joined: Fri Feb 16, 2018 4:28 pm

Re: can only toggle one output on 74hc595n shift register?

Post by DMWidgets » Tue Mar 13, 2018 8:44 pm

Ok, wired up everything with a nano and followed the basic tutorial here: https://www.arduino.cc/en/Tutorial/ShiftOut

Everything works as intended. if I transfer mosi/sck/latch from the nano to the ESP8266, the problem reverts back to where I started (only 8th bit output works). I've replicated this problem on both of my Wemos d1 minis now so either the problem is in my code, or there's something going on with this specific dev board...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: can only toggle one output on 74hc595n shift register?

Post by pythoncoder » Wed Mar 14, 2018 9:13 am

DMWidgets wrote:
Tue Mar 13, 2018 3:12 pm
... the 8th bit output (the only pin on the right side of the chip) responds to whatever the last bit sent is, whether I only send 1, 3 or 8 bits. ...
The self.spi.write(bytes(buf)) command can't send less than 8 bits. It always sends an integer number of bytes. If you're sending multiple bytes the shift register will only display the last byte sent. Earlier bytes will have been shifted in, then out again. With only one 74HC595N it doesn't make sense to send anything other than a single byte. Sending multiple bytes caters for the case where you cascade multiple 74HC595N's. The first byte sent will end up in the last device in the chain.

Pin 9 is the output of bit 7 prior to the latch. The fact that the other outputs aren't changing suggests either that the output buffer is never getting loaded or that the OE/ pin (pin 13) is not tied low.
Peter Hinch
Index to my micropython libraries.

DMWidgets
Posts: 8
Joined: Fri Feb 16, 2018 4:28 pm

Re: can only toggle one output on 74hc595n shift register?

Post by DMWidgets » Wed Mar 14, 2018 9:48 pm

That seems kind of self-evident now that you mention it... In which case what I'm trying to send is not bytes but 8 arbitrary bits.

Sending a byte array to the spi now lets me toggle the outputs as needed so that's good but there must be a more pythonic way of sending my bit string. I'm currently just sending "spi.write(bytearray[int(arg, 2)])". Convert arg into binary number, create a byte array with that number, send that to the buffer. Just looking to pass a str as literal bytes to the spi in the simplest way possible. But! this works in the mean-time and I've had a trying week so if it works, good enough for me. At least, until something starts breaking due to my implementation here.

Thanks for for your help.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: [solved] can only toggle one output on 74hc595n shift register?

Post by pythoncoder » Thu Mar 15, 2018 9:58 am

I'd write the function to accept an integer between 0 and 255:

Code: Select all

def shift(self, num):
    buf = num.to_bytes(1, 'little')
    self.rclk.value(0)
    self.spi.write(bytes(buf))
    self.rclk.value(1) # latch data to output
There are other ways to convert an integer to a bytes object, notably using the ustruct module.
Peter Hinch
Index to my micropython libraries.

Post Reply