-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmy74HC595.py
More file actions
71 lines (61 loc) · 1.69 KB
/
Copy pathmy74HC595.py
File metadata and controls
71 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from machine import Pin
# 74HC595
# 16: VCC
# 15, 1~7: Q0~Q7
# 14: DS
# 13: OE
# 12: STCP
# 11: SHCP
# 9: Q7S
# 8: GND
class Chip74HC595(object):
def __init__(self, ds: int=12, stcp: int=13, shcp: int=14, oe: int=5):
self._ds = Pin(ds, Pin.OUT, value=0)
self._shcp = Pin(shcp, Pin.OUT, value=0)
self._stcp = Pin(stcp, Pin.OUT, value=0)
if oe==-1:
self._oe = -1
else:
self._oe = Pin(oe, Pin.OUT, value=0)
self.enable()
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()
def _shift_bit(self):
self._shcp.off()
self._shcp.on()
def _send_data(self):
self._stcp.off()
self._stcp.on()
def disable(self):
if self._oe != -1:
self._oe.on()
def enable(self):
if self._oe != -1:
self._oe.off()