Skip to content

Commit c841c35

Browse files
committed
Add MIP package
1 parent 3fe566c commit c841c35

File tree

8 files changed

+154
-55
lines changed

8 files changed

+154
-55
lines changed

examples/basic_bitbang.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: 2021 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
MicroPython 74HC595 8-Bit Shift Register
6+
https://github.com/mcauser/micropython-74hc595
7+
"""
8+
9+
from machine import Pin
10+
from sr74hc595 import SR74HC595_BITBANG
11+
12+
ser = Pin(23, Pin.OUT)
13+
rclk = Pin(5, Pin.OUT)
14+
srclk = Pin(18, Pin.OUT)
15+
16+
# construct without optional pins
17+
sr = SR74HC595_BITBANG(ser, srclk, rclk)
18+
19+
sr.clear() # raises RuntimeError because you haven't provide srclr pin
20+
sr.enable() # raises RuntimeError because you haven't provide oe pin
21+
22+
# reconstruct with all pins
23+
oe = Pin(33, Pin.OUT, value=0) # low enables output
24+
srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data
25+
26+
sr = SR74HC595_BITBANG(ser, srclk, rclk, srclr, oe)
27+
28+
sr.bit(1) # send high bit, do not latch yet
29+
sr.bit(0) # send low bit, do not latch yet
30+
sr.latch() # latch outputs, outputs=0000_0010
31+
32+
sr.bit(1, 1) # send high bit and latch, outputs=0000_0101
33+
sr.bit(0, 1) # send low bit and latch, outputs=0000_1010
34+
35+
sr.bits(0xff, 4) # send 4 lowest bits of 0xff (sends 0x0f), outputs=1010_1111
36+
37+
sr.clear(0) # clear the memory but don't latch yet
38+
sr.latch() # next latch shows the outputs have been reset
39+
40+
sr.bits(0b1010_1010, 8) # write some bits
41+
sr.clear() # clear the memory and latch, outputs have been reset
42+
43+
sr.enable() # outputs enabled
44+
sr.enable(0) # outputs disabled

examples/basic_spi.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2021 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
MicroPython 74HC595 8-Bit Shift Register
6+
https://github.com/mcauser/micropython-74hc595
7+
"""
8+
9+
from machine import Pin, SPI
10+
from sr74hc595 import SR74HC595_SPI
11+
12+
spi = SPI(1, 100000)
13+
rclk = Pin(5, Pin.OUT)
14+
15+
oe = Pin(33, Pin.OUT, value=0) # low enables output
16+
srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data
17+
18+
sr = SR74HC595_SPI(spi, rclk, 2) # chain of 2 shift registers
19+
20+
sr.pin(2,1) # set pin 2 high of furthest shift register
21+
sr.pin(2) # read pin 2
22+
sr.pin(2,0) # set pin 2 low
23+
24+
sr.toggle(8) # toggle first pin of closest shift register
25+
26+
sr[0] = 0xff # set all pins high on furthest shift register
27+
sr[1] = 240 # set half pins high on closest shift register
28+
sr[1] # read pins
29+
30+
oe.value(0) # disable outputs
31+
oe.value(1) # enable outputs
32+
33+
# pulse to clear shift register memory
34+
srclr.value(1)
35+
srclr.value(0)

examples/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"urls": [
3+
["sr74hc595/examples/basic_bitbang.py", "github:mcauser/micropython-74hc595/examples/basic_bitbang.py"],
4+
["sr74hc595/examples/basic_spi.py", "github:mcauser/micropython-74hc595/examples/basic_spi.py"]
5+
],
6+
"deps": [],
7+
"version": "1.0.0"
8+
}

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"urls": [
3+
["sr74hc595/__init__.py", "github:mcauser/micropython-74hc595/src/__init__.py"],
4+
["sr74hc595/sr74hc595_bitbang.py", "github:mcauser/micropython-74hc595/src/sr74hc595_bitbang.py"],
5+
["sr74hc595/sr74hc595_spi.py", "github:mcauser/micropython-74hc595/src/sr74hc595_spi.py"]
6+
],
7+
"deps": [],
8+
"version": "1.0.0"
9+
}

readme.md

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,54 @@
33
A MicroPython library for 74HC595 8-bit shift registers.
44

55
There's both an SPI version and a bit-bang version, each with a slightly
6-
different interface.
6+
different interface. See below.
77

88
![demo](docs/demo.jpg)
99

10+
11+
### Installation
12+
13+
Using mip via mpremote:
14+
15+
```bash
16+
$ mpremote mip install github:mcauser/micropython-74hc595
17+
$ mpremote mip install github:mcauser/micropython-74hc595/examples
18+
```
19+
20+
Using mip directly on a WiFi capable board:
21+
22+
```python
23+
>>> import mip
24+
>>> mip.install("github:mcauser/micropython-74hc595")
25+
>>> mip.install("github:mcauser/micropython-74hc595/examples")
26+
```
27+
28+
Manual installation:
29+
30+
Copy `src/sr74hc595_bitbang.py` and `src/sr74hc595_spi.py` to the root directory of your device.
31+
32+
1033
## SPI Version
1134

1235
You can use either HSPI or SPI. This version is significantly faster than the
1336
bit-bang version, but is limited to writing whole bytes of data.
1437

38+
1539
### SPI Example
1640

1741
**Basic Usage**
1842

1943
```python
2044
from machine import Pin, SPI
21-
from sr_74hc595_spi import SR
45+
from sr74hc595 import SR74HC595_SPI
2246

2347
spi = SPI(1, 100000)
2448
rclk = Pin(5, Pin.OUT)
2549

2650
oe = Pin(33, Pin.OUT, value=0) # low enables output
2751
srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data
2852

29-
sr = SR(spi, rclk, 2) # chain of 2 shift registers
53+
sr = SR74HC595_SPI(spi, rclk, 2) # chain of 2 shift registers
3054

3155
sr.pin(2,1) # set pin 2 high of furthest shift register
3256
sr.pin(2) # read pin 2
@@ -120,14 +144,14 @@ at the expense of the performance you get using SPI.
120144

121145
```python
122146
from machine import Pin
123-
from sr_74hc595_bitbang import SR
147+
from sr74hc595 import SR74HC595_BITBANG
124148

125149
ser = Pin(23, Pin.OUT)
126150
rclk = Pin(5, Pin.OUT)
127151
srclk = Pin(18, Pin.OUT)
128152

129153
# construct without optional pins
130-
sr = SR(ser, srclk, rclk)
154+
sr = SR74HC595_BITBANG(ser, srclk, rclk)
131155

132156
sr.clear() # raises RuntimeError because you haven't provide srclr pin
133157
sr.enable() # raises RuntimeError because you haven't provide oe pin
@@ -136,7 +160,7 @@ sr.enable() # raises RuntimeError because you haven't provide oe pin
136160
oe = Pin(33, Pin.OUT, value=0) # low enables output
137161
srclr = Pin(32, Pin.OUT, value=1) # pulsing low clears data
138162

139-
sr = SR(ser, srclk, rclk, srclr, oe)
163+
sr = SR74HC595_BITBANG(ser, srclk, rclk, srclr, oe)
140164

141165
sr.bit(1) # send high bit, do not latch yet
142166
sr.bit(0) # send low bit, do not latch yet
@@ -209,6 +233,7 @@ the current state of the `ser` pin and copy it to the shift register memory.
209233
_clock()
210234
```
211235

236+
212237
## Chaining
213238

214239
You can connect multiple 74HC595 shift registers together to form a chain.
@@ -225,11 +250,13 @@ The `QH\`` output pin on the first shift register goes into the next shift regis
225250
When clocking in data, the values appear on the closest shift register to the
226251
micro controller first, before overflowing into each chained shift register.
227252

253+
228254
## Parts
229255

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
256+
* [TinyPICO](https://www.tinypico.com/)
257+
* [74HC595 DIP-16](https://s.click.aliexpress.com/e/_DnLBdpL)
258+
* [74HC595 breakout](https://s.click.aliexpress.com/e/_Der4vyZ)
259+
233260

234261
## Connections
235262

@@ -253,12 +280,14 @@ SRCLR | Shift Register Clear | Active low. Drive high to clear contents.
253280
QA-QH | Outputs | 8 output pins
254281
QH\` | Serial Output | Connect to the next 74HC595 SER pin
255282

283+
256284
## Links
257285

258286
* [TinyPICO Getting Started](https://www.tinypico.com/gettingstarted)
259287
* [micropython.org](http://micropython.org)
260288
* [74HC595 datasheet](docs/sn74hc595n.pdf)
261289

290+
262291
## License
263292

264293
Licensed under the [MIT License](http://opensource.org/licenses/MIT).

src/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-FileCopyrightText: 2021 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
MicroPython 74HC595 8-Bit Shift Register
6+
https://github.com/mcauser/micropython-74hc595
7+
"""
8+
9+
from .sr74hc595_bitbang import SR74HC595_BITBANG
10+
from .sr74hc595_spi import SR74HC595_SPI

sr_74hc595_bitbang.py renamed to src/sr74hc595_bitbang.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1+
# SPDX-FileCopyrightText: 2021 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
14
"""
25
MicroPython 74HC595 8-Bit Shift Register
36
https://github.com/mcauser/micropython-74hc595
4-
5-
MIT License
6-
Copyright (c) 2021 Mike Causer
7-
8-
Permission is hereby granted, free of charge, to any person obtaining a copy
9-
of this software and associated documentation files (the "Software"), to deal
10-
in the Software without restriction, including without limitation the rights
11-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
copies of the Software, and to permit persons to whom the Software is
13-
furnished to do so, subject to the following conditions:
14-
15-
The above copyright notice and this permission notice shall be included in all
16-
copies or substantial portions of the Software.
17-
18-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
SOFTWARE.
257
"""
268

27-
__version__ = '0.0.1'
9+
__version__ = '1.0.0'
2810

29-
class SR:
11+
class SR74HC595_BITBANG:
3012
def __init__(self, ser, srclk, rclk, srclr=None, oe=None):
3113
self.ser = ser
3214
self.srclk = srclk

sr_74hc595_spi.py renamed to src/sr74hc595_spi.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1+
# SPDX-FileCopyrightText: 2021 Mike Causer <https://github.com/mcauser>
2+
# SPDX-License-Identifier: MIT
3+
14
"""
25
MicroPython 74HC595 8-Bit Shift Register
36
https://github.com/mcauser/micropython-74hc595
4-
5-
MIT License
6-
Copyright (c) 2021 Mike Causer
7-
8-
Permission is hereby granted, free of charge, to any person obtaining a copy
9-
of this software and associated documentation files (the "Software"), to deal
10-
in the Software without restriction, including without limitation the rights
11-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
copies of the Software, and to permit persons to whom the Software is
13-
furnished to do so, subject to the following conditions:
14-
15-
The above copyright notice and this permission notice shall be included in all
16-
copies or substantial portions of the Software.
17-
18-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
SOFTWARE.
257
"""
268

27-
__version__ = '0.0.1'
9+
__version__ = '1.0.0'
2810

29-
class SR:
11+
class SR74HC595_SPI:
3012
def __init__(self, spi, rclk, len=1, srclr=None, oe=None):
3113
self.spi = spi
3214
self.rclk = rclk

0 commit comments

Comments
 (0)