@@ -58,6 +58,24 @@ def tone(pin, frequency, duration = 1):
58
58
pwm .duty_cycle = 0x8000
59
59
time .sleep (duration )
60
60
61
+ def bitWrite (x ,n ,b ):
62
+ """
63
+ Based on the Arduino bitWrite function, changes a specific bit of a value to 0 or 1.
64
+ The return value is the original value with the changed bit.
65
+ This function is written for use with 8-bit shift registers
66
+
67
+ :param x: numeric value
68
+ :param n: position to change starting with least-significant (right-most) bit as 0
69
+ :param b: value to write (0 or 1)
70
+ """
71
+ if b == 1 :
72
+ x |= 1 << n & 255
73
+ else :
74
+ x &= ~ (1 << n ) & 255
75
+ return x
76
+
77
+
78
+
61
79
def shift_in (dataPin , clock , msb_first = True ):
62
80
"""
63
81
Shifts in a byte of data one bit at a time. Starts from either the LSB or
@@ -77,11 +95,12 @@ def shift_in(dataPin, clock, msb_first=True):
77
95
i = 0
78
96
79
97
for i in range (0 , 8 ):
80
- clock .value = True
81
98
if msb_first :
82
99
value |= ((dataPin .value ) << (7 - i ))
83
100
else :
84
101
value |= ((dataPin .value ) << i )
102
+ # toggle clock True/False
103
+ clock .value = True
85
104
clock .value = False
86
105
i += 1
87
106
return value
@@ -108,27 +127,45 @@ def shift_out(dataPin, clock, value, msb_first=True):
108
127
from board import *
109
128
clock = digitalio.DigitalInOut(D12)
110
129
dataPin = digitalio.DigitalInOut(D11)
130
+ latchPin = digitalio.DigitalInOut(D10)
111
131
clock.direction = digitalio.Direction.OUTPUT
112
132
dataPin.direction = digitalio.Direction.OUTPUT
133
+ latchPin.direction = digitalio.Direction.OUTPUT
113
134
114
135
while True:
115
136
valueSend = 500
116
137
# shifting out least significant bits
138
+ # must toggle latchPin.value before and after shift_out to push to IC chip
139
+ # this sample code was tested using
140
+ latchPin.value = False
117
141
simpleio.shift_out(dataPin, clock, (valueSend>>8), msb_first = False)
142
+ latchPin.value = True
143
+ time.sleep(1.0)
144
+ latchPin.value = False
118
145
simpleio.shift_out(dataPin, clock, valueSend, msb_first = False)
146
+ latchPin.value = True
147
+ time.sleep(1.0)
148
+
119
149
# shifting out most significant bits
150
+ latchPin.value = False
120
151
simpleio.shift_out(dataPin, clock, (valueSend>>8))
152
+ latchPin.value = True
153
+ time.sleep(1.0)
154
+ latchpin.value = False
121
155
simpleio.shift_out(dataPin, clock, valueSend)
156
+ latchpin.value = True
157
+ time.sleep(1.0)
122
158
"""
123
159
value = value & 0xFF
124
160
for i in range (0 , 8 ):
125
- clock .value = True
126
161
if msb_first :
127
162
tmpval = bool (value & (1 << (7 - i )))
128
163
dataPin .value = tmpval
129
164
else :
130
165
tmpval = bool ((value & (1 << i )))
131
166
dataPin .value = tmpval
167
+ # toggle clock pin True/False
168
+ clock .value = True
132
169
clock .value = False
133
170
134
171
class Servo :
0 commit comments