Created
December 14, 2023 13:23
-
-
Save TakuroFukamizu/214bcf74985c93d0c5c86c24d39c9518 to your computer and use it in GitHub Desktop.
MicroPython driver for 74HC595 Shift Register IC work with Raspberry Pi Pico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import utime | |
from sr74hc595 import SR74hc595 | |
dataPIN=8 # 74HC595 > 14 SER | |
latchPIN=6 # 74HC595 > 12 RCLK | |
clockPIN=7 # 74HC595 > 11 SRCLK | |
sr = SR74hc595(dataPIN, clockPIN, latchPIN, sr_size) | |
sr_size = 8 # 8, 16, 24... | |
bit = [0,0,0,0,0,0,0,0] | |
def fill(val: int): | |
for i in range(sr_size): | |
bit[i] = val | |
# ---------------------------------------------------------------- | |
while True: | |
for i in range(sr_size): | |
fill(0) | |
bit[i] = 1 | |
sr.shiftOut(bit) # LSBFIRST | |
utime.sleep(0.5) | |
utime.sleep(1) | |
fill(0) | |
for i in range(sr_size): | |
bit[i] = 1 | |
sr.shiftOut(bit) # LSBFIRST | |
utime.sleep(0.5) | |
utime.sleep(1) | |
for i in range(sr_size): | |
fill(0) | |
bit[i] = 1 | |
sr.shiftOut(bit, SR74hc595.MSBFIRST) | |
utime.sleep(0.5) | |
utime.sleep(1) | |
fill(0) | |
for i in range(sr_size): | |
bit[i] = 1 | |
sr.shiftOut(bit, SR74hc595.MSBFIRST) | |
utime.sleep(0.5) | |
utime.sleep(1) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from micropython import const | |
from machine import Pin | |
""" | |
MicroPython driver for 74HC595 Shift Register IC using GPIOs. | |
""" | |
class SR74hc595: | |
LSBFIRST = const(0) | |
MSBFIRST = const(1) | |
def __init__(self, dataPin:int, clockPin:int, latchPin:int, size:int): | |
self.data = Pin(dataPin, Pin.OUT) # 74HC595 > 14 SER | |
self.latch = Pin(latchPin, Pin.OUT) # 74HC595 > 12 RCLK | |
self.clock = Pin(clockPin, Pin.OUT) # 74HC595 > 11 SRCLK | |
self.size = size | |
def shiftOut(self, val:list[int], order = LSBFIRST): | |
#put latch down to start data sending | |
self.clock.low() | |
self.latch.low() | |
self.clock.high() | |
if order == SR74hc595.LSBFIRST: | |
#load data in reverse order | |
for i in range(self.size-1, -1, -1): | |
self.clock.low() | |
self.data.value(val[i]) | |
self.clock.high() | |
else: # MSBFIRST | |
for i in range(self.size): | |
self.clock.low() | |
self.data.value(val[i]) | |
self.clock.high() | |
#put latch up to store data on register | |
self.clock.low() | |
self.latch.high() | |
self.clock.high() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example https://twitter.com/uzuki_aoba/status/1735280994490749387