Shift register (74HC595) controlling via SPI

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
charlzo
Posts: 1
Joined: Sat May 13, 2017 8:46 pm

Shift register (74HC595) controlling via SPI

Post by charlzo » Sat May 13, 2017 10:25 pm

Hi guys,

I've an issue with my current micropython project on my ESP8266. I've a 10x10 LED matrix which i would like to control via 4 shift registers.

In general 3 pins are required for the controlling DATA, LATCH and CLOCK. After some hours of internet searching the most promising solution was the usage of SPI, where also found some useful instructions for the pyboard in an older post of Dec 2015 (thank you for the code btw :) ) :

:arrow: viewtopic.php?t=1219

I tried to replace the pyboard specific librarys with the general machine module for the ESP8266 to get access to the SPI class. It worked fine till a specific point but the main issue at the moment is that it was not capable provide a binary signal at the DATA pin.

To be honest i'm a little bit confused about the write methods in the machine.SPI class. The docu says the return value is None. So in general what is the purpose of a write method with a return value of None :?: (sry for the maybe silly question)

Is there maybe another solution to get a binary signal out of the data pin? I'm not sure anymore if the usage of SPI is the best way to manage the controlling. Do you have some other examples or tutorials to get deeper into the topic?

Thank you for your kind response in advance,
BR charlzo

rosenrot
Posts: 30
Joined: Mon Dec 12, 2016 9:39 pm

Re: Shift register (74HC595) controlling via SPI

Post by rosenrot » Sun May 21, 2017 7:24 am

I checked the example from 2015.

The shiftregister class is just forwarding your byte while toggling some required pins.

Calling

Code: Select all

sr.shift(0b01010101)
does not work for you?

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

Re: Shift register (74HC595) controlling via SPI

Post by pythoncoder » Sun May 21, 2017 7:53 am

To answer your question about function return values, by default Python functions return None. A function to send data to a device takes the data as an argument and does the job; it has no need to return a value to the caller. So the caller will get None as a return value. In other cases a send() function might return a boolean (success or fail) or a count of the bytes sent.

The return value depends on the device and the design of the driver.

This function will return None:

Code: Select all

def foo():
   pass
Peter Hinch
Index to my micropython libraries.

corpse
Posts: 2
Joined: Sat Jan 20, 2018 8:13 am

Re: Shift register (74HC595) controlling via SPI

Post by corpse » Sat Jan 20, 2018 9:51 am

Hi! I'm newbie here. I'm also trying to use 74hc595 with esp8266. I spend few hours for experiments and at the moment I have two problems:

1. When esp8266 powered up, some pins on shift register has high status and some of them is low. It's randomly. Sure, I can set it to the 0x00 at start, but it can has some time lag between powering up and instructions execution from boot.py. At the moment I don't know, how to solve this.

2. Most serious problem is that after sending first byte to the shift register by write(b'\xff') (for example), I can't change register's status in future. It's holding first byte condition until powering down eps8266. I tried to use deinit for spi, send high pulse to latch to force it to apply second sended byte but without any effect. Should I use another (forth) pin to clear shift registers's cache by mr pin of IC? Also I can't set buffer lenght while init:
>>> spi.init(bits=8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given

My 74hc595 is connected to the hardware SPI pins:
MISO is GPIO12
MOSI is GPIO13
SCK is GPIO14

Initialisation code:
from machine import Pin, SPI
spi = SPI(1, baudrate=1000000, polarity=0, phase=0)
spi.write(b'\xff')

Information about SPI I found here: https://docs.micropython.org/en/latest/ ... achine-spi

chrismas9
Posts: 152
Joined: Wed Jun 25, 2014 10:07 am

Re: Shift register (74HC595) controlling via SPI

Post by chrismas9 » Thu Jan 25, 2018 12:49 pm

The initial output state will always be random as there is no reset for the output latch. The only way around this would be to use another pin to tristate the outputs. This pin would need a pullup resistor to keep the outputs tristate until the ESP code runs. if you need defined logic levels on the outputs you would need pullup or pulldown resistors to define them. If you are driving LEDs you don't need the resistors (except /G).

Another solution would be a port expander. There are plenty available in SMD, but not many through hole. They generally startup as inputs and would need resistors to define their state (eg MCP23008 or MCP23S08). The PCF8574 has 100uA weak pullups to define a logic one at reset. It also allows you to drive up to 8 devices with only two pins.

To use SPI with 74HC595 and most other SPI devices you need to use nCS (active low Chip Select) to transfer the data from the shift register to the output latch. MCUs with SPI slave mode may call it NSS, but in master mode you can use any GPIO. Just set it low before writing the SPI data, and set it high after the transfer has finished. The correct connections for 74HC595 to SPI are:

GPIO or nCS or NSS: pin12 RCK
SCK: pin11 SCK
MOSI: pin14 SER
MISO: not required
VDD (3.3V) or /RST PIN10 /SCLR
GND or GPIO pin13 /G (enable outputs always or after first data transfer)

Another interesting device is the SN74LV8153 in 20 pin DIP. It has a one wire UART protocol input, reset pin and selectable push pull or 40mA open drain outputs.

corpse
Posts: 2
Joined: Sat Jan 20, 2018 8:13 am

Re: Shift register (74HC595) controlling via SPI

Post by corpse » Sat Jan 27, 2018 12:23 am

[quote=chrismas9 post_id=24951 time=1516884551 user_id=240]
The initial output state will always be random as there is no reset for the output latch.
....
[/quote]

Oh! Thank you very much for explanation! I'll try it later. At the moment I found LCD -> I2C adapter with pcf8574 and it's amazing easy to use it! Distinct pin for interrupts - thats great! I Ordered 10 pcs of pcf8574 for 2.5$ and I'm happy now. :) Anyway, I have 10 shift registers and your explanation will be usefull.

kurt
Posts: 20
Joined: Sun Feb 11, 2018 7:45 pm

Re: Shift register (74HC595) controlling via SPI

Post by kurt » Sun Feb 11, 2018 7:59 pm

As I build a webradio using ESP07, I needed to have chip select signals for lcd, touchpad, VS1053, external RAM and to control the backlight of lcd. Without having not enough GPIO pins I used a 74HC595.
At start the project I run into the same problem as you. Solved the problem by:
Hardware:
- connect SPI as usual to 74HC595
- add 10k pullup resistors to all output lines of 74HC595 (QA to QH)
- connect one of ESP07 GPIO pin (I took GPIO16) to G input of 74HC595 - PIN 13
Software:
- set the output lines of 74HC595 to tri state by setting G (PIN13) to high
- write the data via SPI to the chip
- activate the output lines of 74HC595 by setting the G (PIN13) to low, now the new output states are valid for the devices
This works well over a year now.

Post Reply