|
| 1 | +# MicroPython 74HC595 |
| 2 | + |
| 3 | +A MicroPython library for 74HC595 8-bit shift registers. |
| 4 | + |
| 5 | +There's both an SPI version and a bit-bang version, each with a slightly |
| 6 | +different interface. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## SPI Version |
| 11 | + |
| 12 | +You can use either HSPI or SPI. This version is significantly faster than the |
| 13 | +bit-bang version, but is limited to writing whole bytes of data. |
| 14 | + |
| 15 | +### SPI Example |
| 16 | + |
| 17 | +**Basic Usage** |
| 18 | + |
| 19 | +```python |
| 20 | +from machine import Pin, SPI |
| 21 | +from sr_74hc595_spi import SR |
| 22 | + |
| 23 | +spi = SPI(1, 100000) |
| 24 | +rclk = Pin(5, Pin.OUT) |
| 25 | + |
| 26 | +oe = Pin(33, Pin.OUT, value=0) # low enables output |
| 27 | +srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data |
| 28 | + |
| 29 | +sr = SR(spi, rclk, 2) # chain of 2 shift registers |
| 30 | + |
| 31 | +sr.pin(2,1) # set pin 2 high of furthest shift register |
| 32 | +sr.pin(2) # read pin 2 |
| 33 | +sr.pin(2,0) # set pin 2 low |
| 34 | + |
| 35 | +sr.toggle(8) # toggle first pin of closest shift register |
| 36 | + |
| 37 | +sr[0] = 0xff # set all pins high on furthest shift register |
| 38 | +sr[1] = 240 # set half pins high on closest shift register |
| 39 | +sr[1] # read pins |
| 40 | + |
| 41 | +oe.value(0) # disable outputs |
| 42 | +oe.value(1) # enable outputs |
| 43 | + |
| 44 | +# pulse to clear shift register memory |
| 45 | +srclr.value(1) |
| 46 | +srclr.value(0) |
| 47 | +``` |
| 48 | + |
| 49 | +### SPI Methods |
| 50 | + |
| 51 | +Construct with a reference to `spi` and the `rclk` pin used for latching and an |
| 52 | +optional number of cascading shift registers. |
| 53 | + |
| 54 | +Pins `srclr` and `oe` are optional. |
| 55 | +If you don't need to clear the outputs, connect `srclr` to vcc. |
| 56 | +If you don't need to disable the outputs, connect `oe` to gnd. |
| 57 | + |
| 58 | +```python |
| 59 | +__init__(spi, rclk, len=1, srclr=None, oe=None) |
| 60 | +``` |
| 61 | + |
| 62 | +Read the boolean value of a pin. First pin is index `0`. If you are cascading shift |
| 63 | +registers, the first pin of the second shift register is index `8` and so on. Index |
| 64 | +`0-7` are the furthest away shift register from the serial data in. |
| 65 | + |
| 66 | +```python |
| 67 | +pin(index) |
| 68 | +``` |
| 69 | + |
| 70 | +Writes a boolean value to a pin. This updates the internal buffer of pin values then |
| 71 | +writes all of the values to each shift register in the chain. |
| 72 | + |
| 73 | +```python |
| 74 | +pin(index, value, latch=True) |
| 75 | +``` |
| 76 | + |
| 77 | +This toggles a single pin by index. Helper for reading a pin then writing the opposite |
| 78 | +value. |
| 79 | + |
| 80 | +```python |
| 81 | +toggle(index, latch=True) |
| 82 | +``` |
| 83 | + |
| 84 | +This lets you treat the class like a list, where each index represents a whole shift |
| 85 | +register. Returns an 8-bit value for the shift register by index, where lowest index |
| 86 | +is furthest away. |
| 87 | + |
| 88 | +```python |
| 89 | +__getitem__(index) |
| 90 | +``` |
| 91 | + |
| 92 | +Write an 8-bit value to a shift register at the given index. |
| 93 | + |
| 94 | +```python |
| 95 | +__setitem__(index, value) |
| 96 | +``` |
| 97 | + |
| 98 | +Private method for sending the entire internal buffer over SPI. |
| 99 | + |
| 100 | +```python |
| 101 | +_write(latch=False) |
| 102 | +``` |
| 103 | + |
| 104 | +Private method for pulsing the `rclk` pin, which latches the outputs from the shift |
| 105 | +register to the storage register and makes the outputs appear. |
| 106 | + |
| 107 | +```python |
| 108 | +_latch() |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +## Bit Bang Version |
| 113 | + |
| 114 | +This version lets you have greater control over sending individual bits of data |
| 115 | +at the expense of the performance you get using SPI. |
| 116 | + |
| 117 | +### Bit Bang Example |
| 118 | + |
| 119 | +**Basic Usage** |
| 120 | + |
| 121 | +```python |
| 122 | +from machine import Pin |
| 123 | +from sr_74hc595_bitbang import SR |
| 124 | + |
| 125 | +ser = Pin(23, Pin.OUT) |
| 126 | +rclk = Pin(5, Pin.OUT) |
| 127 | +srclk = Pin(18, Pin.OUT) |
| 128 | + |
| 129 | +# construct without optional pins |
| 130 | +sr = SR(ser, srclk, rclk) |
| 131 | + |
| 132 | +sr.clear() # raises RuntimeError because you haven't provide srclr pin |
| 133 | +sr.enable() # raises RuntimeError because you haven't provide oe pin |
| 134 | + |
| 135 | +# reconstruct with all pins |
| 136 | +oe = Pin(33, Pin.OUT, value=0) # low enables output |
| 137 | +srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data |
| 138 | + |
| 139 | +sr = SR(ser, srclk, rclk, srclr, oe) |
| 140 | + |
| 141 | +sr.bit(1) # send high bit, do not latch yet |
| 142 | +sr.bit(0) # send low bit, do not latch yet |
| 143 | +sr.latch() # latch outputs, outputs=0000_0010 |
| 144 | + |
| 145 | +sr.bit(1, 1) # send high bit and latch, outputs=0000_0101 |
| 146 | +sr.bit(0, 1) # send low bit and latch, outputs=0000_1010 |
| 147 | + |
| 148 | +sr.bits(0xff, 4) # send 4 lowest bits of 0xff (sends 0x0f), outputs=1010_1111 |
| 149 | + |
| 150 | +sr.clear(0) # clear the memory but don't latch yet |
| 151 | +sr.latch() # next latch shows the outputs have been reset |
| 152 | + |
| 153 | +sr.bits(0b1010_1010, 8) # write some bits |
| 154 | +sr.clear() # clear the memory and latch, outputs have been reset |
| 155 | + |
| 156 | +sr.enable() # outputs enabled |
| 157 | +sr.enable(0) # outputs disabled |
| 158 | +``` |
| 159 | + |
| 160 | +### Bit Bang Methods |
| 161 | + |
| 162 | +Construct with references to each of the pins needed to write to the shift register(s). |
| 163 | + |
| 164 | +Pins `ser`, `srclk` and `rclk` are required. Pins `srclr` and `oe` are optional. |
| 165 | +If you don't need to clear the outputs, connect `srclr` to vcc. |
| 166 | +If you don't need to disable the outputs, connect `oe` to gnd. |
| 167 | + |
| 168 | +```python |
| 169 | +__init__(ser, srclk, rclk, srclr=None, oe=None) |
| 170 | +``` |
| 171 | + |
| 172 | +Writes a single value and can optionally latch to make it visible. |
| 173 | + |
| 174 | +```python |
| 175 | +bit(value, latch=False) |
| 176 | +``` |
| 177 | + |
| 178 | +Write multiple (`num_bits`) values from the supplied value and optionally can latch. |
| 179 | + |
| 180 | +```python |
| 181 | +bits(value, num_bits, latch=False) |
| 182 | +``` |
| 183 | + |
| 184 | +Pulses the `rclk` pin to latch the outputs. Without this, all of the bits you have |
| 185 | +written are remain hidden. |
| 186 | + |
| 187 | +```python |
| 188 | +latch() |
| 189 | +``` |
| 190 | + |
| 191 | +Clears the shift register memory by pulsing the `srclr` pin. You will get a runtime |
| 192 | +error unless you have provided this pin on construct. |
| 193 | + |
| 194 | +```python |
| 195 | +clear(latch=True) |
| 196 | +``` |
| 197 | + |
| 198 | +Toggles the output of the shift register by toggling the output enable (`oe`) pin. |
| 199 | +You will get a runtime error unless you have provided this pin on construct. |
| 200 | + |
| 201 | +```python |
| 202 | +enable(enabled=True) |
| 203 | +``` |
| 204 | + |
| 205 | +Private method for pulsing the `srclk` pin, which tells the shift register to read |
| 206 | +the current state of the `ser` pin and copy it to the shift register memory. |
| 207 | + |
| 208 | +```python |
| 209 | +_clock() |
| 210 | +``` |
| 211 | + |
| 212 | +## Chaining |
| 213 | + |
| 214 | +You can connect multiple 74HC595 shift registers together to form a chain. |
| 215 | + |
| 216 | +Connect each shift registers `rclk`, `srclk`, `oe` and `srclr` together. |
| 217 | +If you don't need to disable outputs, you can tie each `oe` to ground. |
| 218 | +If you don't need to clear any outputs, you can tie each `srclr` to vcc. |
| 219 | + |
| 220 | +Your micro controller provides data to just the first shift registers `ser` pin. |
| 221 | + |
| 222 | +The `QH\`` output pin on the first shift register goes into the next shift register |
| 223 | +`ser` pin and so on. |
| 224 | + |
| 225 | +When clocking in data, the values appear on the closest shift register to the |
| 226 | +micro controller first, before overflowing into each chained shift register. |
| 227 | + |
| 228 | +## Parts |
| 229 | + |
| 230 | +* [TinyPICO](https://www.tinypico.com/) $20.00 USD |
| 231 | +* [74HC595 DIP-16](https://www.aliexpress.com/item/32834183196.html) $0.77 AUD - 10pcs |
| 232 | +* [74HC595 breakout](https://www.aliexpress.com/item/32807747744.html) $0.88 AUD |
| 233 | + |
| 234 | +## Connections |
| 235 | + |
| 236 | +TinyPICO | 74HC595 |
| 237 | +-------------- | ------- |
| 238 | +3V3 | VCC |
| 239 | +G | GND |
| 240 | +G (or a pin) | OE |
| 241 | +23 MOSI | SER |
| 242 | +18 SCK | SRCLK |
| 243 | +5 | RCLK |
| 244 | +3V3 (or a pin) | SRCLR |
| 245 | + |
| 246 | +Pin | Name | Description |
| 247 | +----- | ---------------------- | ----------- |
| 248 | +OE | Output Enable | Active low. Drive high to disable outputs. |
| 249 | +SER | Serial Input | Serial data sent LSB first. |
| 250 | +RCLK | Storage Register Clock | Pulse to latch data to outputs. |
| 251 | +SRCLK | Shift Register Clock | Serial input is read on rising edge. |
| 252 | +SRCLR | Shift Register Clear | Active low. Drive high to clear contents. |
| 253 | +QA-QH | Outputs | 8 output pins |
| 254 | +QH\` | Serial Output | Connect to the next 74HC595 SER pin |
| 255 | + |
| 256 | +## Links |
| 257 | + |
| 258 | +* [TinyPICO Getting Started](https://www.tinypico.com/gettingstarted) |
| 259 | +* [micropython.org](http://micropython.org) |
| 260 | +* [74HC595 datasheet](docs/sn74hc595n.pdf) |
| 261 | + |
| 262 | +## License |
| 263 | + |
| 264 | +Licensed under the [MIT License](http://opensource.org/licenses/MIT). |
| 265 | + |
| 266 | +Copyright (c) 2021 Mike Causer |
0 commit comments