Skip to content

Instantly share code, notes, and snippets.

@buty4649
Created March 31, 2018 15:51
Show Gist options
  • Save buty4649/5517542ce0919058d858a92e4a20f790 to your computer and use it in GitHub Desktop.
Save buty4649/5517542ce0919058d858a92e4a20f790 to your computer and use it in GitHub Desktop.
@DATA_PIN = 2
@RATCH_PIN = 3
@CLOCK_PIN = 4
class SN74HC595N
attr_accessor :data_pin, :ratch_pin, :clock_pin, :mode
def initialize(data_pin, ratch_pin, clock_pin)
@data_pin = data_pin
@ratch_pin = ratch_pin
@clock_pin = clock_pin
@mode = MSBFIRST
# ピンを初期化
[@data_pin, @ratch_pin, @clock_pin].each do |p|
pinMode(p, OUTPUT)
digitalWrite(p, 0)
end
end
def output(value)
# ラッチをHIGHにすると書き込める
digitalWrite(@ratch_pin, 1)
# 書き込み
shiftOut(@data_pin, @clock_pin, @mode, value)
# ラッチをLOWに戻す
digitalWrite(@ratch_pin, 0)
end
def clear
output(0x00)
end
end
sr = SN74HC595N.new(@DATA_PIN, @RATCH_PIN, @CLOCK_PIN)
sr.clear
256.times do |i|
sr.output(i)
delay(50)
end
# シフトレジスタなので次のデータを入れないと最後のデータが出力されない
sr.clear()
delay(250)
# MSB -> LSB
sr.mode = LSBFIRST
256.times do |i|
sr.output(i)
delay(50)
end
# シフトレジスタなので次のデータを入れないと最後のデータが出力されない
sr.clear()
delay(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment