0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

電子工作メモ - 流れる水のように光るLED

Last updated at Posted at 2024-03-21
import time
from my74HC595 import Chip74HC595

chip = Chip74HC595(18, 20, 21)
#Chip74HC595() == Chip74HC595(18, 20, 21) 

isDirectionMSB = False
while True:
#    x = 0x01
#    for count in range(8):
#        # 最上位ビットがQ8にくるので、Q1 -> Q8 の方向でON
#        chip.shiftOut(1, x)
#        x = x<<1;
#        time.sleep_ms(300)
#    x = 0x01
#    for count in range(8):
#        # 最下位ビットがQ8にくるので、Q8 -> Q1 の方向でON
#        chip.shiftOut(0, x)
#        x = x<<1
#        time.sleep_ms(300)

    isDirectionMSB = !isDirectionMSB
    x = 0x01
    for count in range(8):
        chip.shiftOut(isDirectionMSB, x)
        x = x<<1;
        time.sleep_ms(300)
from machine import Pin

# 74HC595
# 16: VCC
# 15, 1~7: Q0~Q7
# 14: DS   --- Pico GPIO 18
# 13: OE   --- Pico GPIO 19
# 12: STCP --- Pico GPIO 20
# 11: SHCP --- Pico GPIO 21
# 9: Q7S
# 8: GND


class Chip74HC595(object):
    def __init__(self, ds: int=18, stcp: int=20, shcp: int=21, oe: int=19):
        self._ds = Pin(ds, Pin.OUT, value=0)
        self._shcp = Pin(shcp, Pin.OUT, value=0)
        self._stcp = Pin(stcp, Pin.OUT, value=0)
        self._oe = Pin(oe, Pin.OUT, value=0)
        self.enable()

    # 入力データをパラレル出力する
    # direction: データの方向(1: 上位ビット(MLB)がQ7に格納, 0: 下位ビット(LSB)がQ7に格納)
    # data: 出力データ(8bit)
    def shiftOut(self,direction,data): 
        self._shcp.on() 
        self._stcp.on()
        if direction: 
            for i in range(8):
                # データを左にシフトして最上位ビットのみ残す
                bit = data << i
                bit = bit & 0x80
                # シリアルデータをセット
                if bit == 0x80:
                    self._ds.on()
                else:
                    self._ds.off()
                # シフトレジスタをシフト
                self._shift_bit()
            # パラレルデータ出力
            self._send_data()
        if not direction: 
            for i in range(8):
                # データを右にシフトして最下位ビットのみ残す
                bit = data >> i
                bit = bit & 0x01
                # シリアルデータをセット
                if bit == 0x01:
                    self._ds.on()
                else:
                    self._ds.off()
                # シフトレジスタをシフト
                self._shift_bit()
            # パラレルデータ出力
            self._send_data()

    # データをクリアする
    def clear(self):
        for i in range(8):
            self._ds.off()
            self._shift_bit()
        self._send_data()
        self.enable()

    # シフトレジスタをシフトする(DS=>Q1, Q1=>Q2, ..., Q6=>Q7, Q7=>Q7')
    def _shift_bit(self):
        self._shcp.off()
        self._shcp.on()

    # シフトレジスタの状態を出力する。パラレル出力する。
    def _send_data(self):
        self._stcp.off()
        self._stcp.on()
        
    def disable(self):
        self._oe.on()
    
    def enable(self):
        self._oe.off()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?