'''
python-periphery hc595 sample
2G_IOT---HC595
--------------
Pin#29---SER(SerialDataInput)
Pin#31---SRCLK
Pin#33---RCLK
---------Qa----- LED1
---------Qb----- LED2
---------Qc----- LED3
---------Qd----- LED4
---------Qe----- LED5
---------Qf----- LED6
---------Qg----- LED7
---------Qh----- LED8
---------Qh'---- Next HC595(Daisy chaining more than
74HC595s)
3.3V-----Vcc
3.3V-----BAR(SRCLR)
GND------GND
GND------BAR(OE)
'''
#!/usr/bin/python
#-*- encoding: utf-8 -*-
#import
from periphery import GPIO
from time import sleep
class ShiftRegister:
register_type = '74HC595'
"""
data_pin => pin 14 on the 74HC595
latch_pin => pin 12 on the 74HC595
clock_pin => pin 11 on the 74HC595
"""
def __init__(self, data_pin, latch_pin,
clock_pin):
self.data_pin =
data_pin
self.latch_pin
= latch_pin
self.clock_pin
= clock_pin
self.gpio_data_pin = GPIO(self.data_pin,"out")
self.gpio_latch_pin = GPIO(self.latch_pin,"out")
self.gpio_clock_pin = GPIO(self.clock_pin,"out")
self.outputs =
[0] * 8
"""
output_number => Value from 0 to 7
pointing to the output pin on the 74HC595
0 => Q0 pin 15 on the 74HC595
1 => Q1 pin 1 on the 74HC595
2 => Q2 pin 2 on the 74HC595
3 => Q3 pin 3 on the 74HC595
4 => Q4 pin 4 on the 74HC595
5 => Q5 pin 5 on the 74HC595
6 => Q6 pin 6 on the 74HC595
7 => Q7 pin 7 on the 74HC595
value => a state to pass to the pin,
could be HIGH or LOW
"""
def setOutput(self, output_number,
value):
try:
self.outputs[output_number] = value
except
IndexError:
raise ValueError("Invalid output number. Can be only an
int from 0 to 7")
def setOutputs(self, outputs):
if 8 !=
len(outputs):
raise ValueError("setOutputs must be an array with 8
elements")
self.outputs =
outputs
def showOutputs(self):
print
self.outputs
def latch(self):
self.gpio_latch_pin.write(False)
for i in
range(7, -1, -1):
self.gpio_clock_pin.write(False)
if (self.outputs[i] == 0):
self.gpio_data_pin.write(False)
if (self.outputs[i] == 1):
self.gpio_data_pin.write(True)
self.gpio_clock_pin.write(True)
self.gpio_latch_pin.write(True)
if __name__ == '__main__':
data_pin = 122 #pin 14 on the 75HC595
latch_pin = 123 #pin 12 on the 75HC595
clock_pin = 124 #pin 11 on the 75HC595
shift_register = ShiftRegister(data_pin, latch_pin,
clock_pin)
# Set all LOW
shift_register.setOutputs([0,0,0,0,0,0,0,0])
shift_register.showOutputs()
# Display
shift_register.latch()
for x in range(8):
# Set some output individually
shift_register.setOutput(x, 1)
shift_register.showOutputs()
# Display
shift_register.latch()
sleep(1)
for x in range(8):
# Set some output individually
shift_register.setOutput(x, 0)
shift_register.showOutputs()
# Display
shift_register.latch()
sleep(1)
|