Skip to content

Commit 4b389e6

Browse files
profbradytannewt
authored andcommitted
Update simpleio.py
Update for timing of clock toggle in shift_in and shift_out. Add bitWrite function similar to Arduino.
1 parent 49e181e commit 4b389e6

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

simpleio.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ def tone(pin, frequency, duration = 1):
5858
pwm.duty_cycle = 0x8000
5959
time.sleep(duration)
6060

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+
6179
def shift_in(dataPin, clock, msb_first=True):
6280
"""
6381
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):
7795
i = 0
7896

7997
for i in range(0, 8):
80-
clock.value = True
8198
if msb_first:
8299
value |= ((dataPin.value) << (7-i))
83100
else:
84101
value |= ((dataPin.value) << i)
102+
# toggle clock True/False
103+
clock.value = True
85104
clock.value = False
86105
i+=1
87106
return value
@@ -108,27 +127,45 @@ def shift_out(dataPin, clock, value, msb_first=True):
108127
from board import *
109128
clock = digitalio.DigitalInOut(D12)
110129
dataPin = digitalio.DigitalInOut(D11)
130+
latchPin = digitalio.DigitalInOut(D10)
111131
clock.direction = digitalio.Direction.OUTPUT
112132
dataPin.direction = digitalio.Direction.OUTPUT
133+
latchPin.direction = digitalio.Direction.OUTPUT
113134
114135
while True:
115136
valueSend = 500
116137
# 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
117141
simpleio.shift_out(dataPin, clock, (valueSend>>8), msb_first = False)
142+
latchPin.value = True
143+
time.sleep(1.0)
144+
latchPin.value = False
118145
simpleio.shift_out(dataPin, clock, valueSend, msb_first = False)
146+
latchPin.value = True
147+
time.sleep(1.0)
148+
119149
# shifting out most significant bits
150+
latchPin.value = False
120151
simpleio.shift_out(dataPin, clock, (valueSend>>8))
152+
latchPin.value = True
153+
time.sleep(1.0)
154+
latchpin.value = False
121155
simpleio.shift_out(dataPin, clock, valueSend)
156+
latchpin.value = True
157+
time.sleep(1.0)
122158
"""
123159
value = value&0xFF
124160
for i in range(0, 8):
125-
clock.value = True
126161
if msb_first:
127162
tmpval = bool(value & (1 << (7-i)))
128163
dataPin.value = tmpval
129164
else:
130165
tmpval = bool((value & (1 << i)))
131166
dataPin.value = tmpval
167+
# toggle clock pin True/False
168+
clock.value = True
132169
clock.value = False
133170

134171
class Servo:

0 commit comments

Comments
 (0)