Created
October 19, 2020 01:00
-
-
Save ffund/89aef05907cf14e2c3dcc9ea6bf67e4a to your computer and use it in GitHub Desktop.
CompE DP1 parallel communication source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
import sys | |
GPIO.setmode(GPIO.BCM) | |
DATA = [5, 6, 7, 8, 9, 10, 11, 12] | |
CLK = 18 | |
def arr_to_char(a): | |
bitstr = ''.join(str(b) for b in a) | |
c = format(int(bitstr, 2), 'c') | |
return c | |
def read_char(pins, clk, debug=True): | |
bits = [] | |
GPIO.setup(clk, GPIO.IN) | |
GPIO.wait_for_edge(clk, GPIO.FALLING) | |
for p in pins: | |
GPIO.setup(p, GPIO.IN) | |
b = GPIO.input(p) | |
bits.append(b) | |
if debug: | |
print(bits) | |
return (arr_to_char(bits)) | |
def read_char_4bit(pins, clk, debug=True): | |
bits = [] | |
GPIO.setup(clk, GPIO.IN) | |
GPIO.wait_for_edge(clk, GPIO.FALLING) | |
for p in pins: | |
GPIO.setup(p, GPIO.IN) | |
b = GPIO.input(p) | |
bits.append(b) | |
GPIO.wait_for_edge(clk, GPIO.FALLING) | |
for p in pins: | |
GPIO.setup(p, GPIO.IN) | |
b = GPIO.input(p) | |
bits.append(b) | |
if debug: | |
print(bits) | |
return (arr_to_char(bits)) | |
try: | |
while True: | |
c = read_char_4bit(DATA[4:], CLK) | |
print(c) | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
sys.exit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
DATA = [5, 6, 7, 8, 9, 10, 11, 12] | |
CLK = 18 | |
PULSE_LOW = 0.001 | |
PULSE_HIGH = 0.001 | |
def char_to_arr(c): | |
return [int(b) for b in format(ord(c), '08b')] | |
def write_char(c, pins, clk, debug=True): | |
GPIO.setup(clk, GPIO.OUT) | |
GPIO.output(clk, GPIO.LOW) | |
bits = char_to_arr(c) | |
if debug: | |
print(bits) | |
# set bits on data lines | |
for p, b in zip(pins, bits): | |
GPIO.setup(p, GPIO.OUT) | |
GPIO.output(p, b) | |
# pulse clock (rx on falling edge) | |
GPIO.output(clk, GPIO.HIGH) | |
time.sleep(PULSE_HIGH) | |
GPIO.output(clk, GPIO.LOW) | |
time.sleep(PULSE_LOW) | |
# reset pins to 0 | |
for p in pins: | |
GPIO.output(p, GPIO.LOW) | |
def write_char_4bit(c, pins, clk, debug=True): | |
GPIO.setup(clk, GPIO.OUT) | |
GPIO.output(clk, GPIO.LOW) | |
bits = char_to_arr(c) | |
if debug: | |
print(bits) | |
# set most significant bits on data lines | |
for p, b in zip(pins, bits[:4]): | |
GPIO.setup(p, GPIO.OUT) | |
GPIO.output(p, b) | |
# pulse clock (rx on falling edge) | |
GPIO.output(clk, GPIO.HIGH) | |
time.sleep(PULSE_HIGH) | |
GPIO.output(clk, GPIO.LOW) | |
time.sleep(PULSE_LOW) | |
# set least significant bits on data lines | |
for p, b in zip(pins, bits[4:]): | |
GPIO.setup(p, GPIO.OUT) | |
GPIO.output(p, b) | |
# pulse clock (rx on falling edge) | |
GPIO.output(clk, GPIO.HIGH) | |
time.sleep(PULSE_HIGH) | |
GPIO.output(clk, GPIO.LOW) | |
time.sleep(PULSE_LOW) | |
# reset pins to 0 | |
for p in pins: | |
GPIO.output(p, GPIO.LOW) | |
#write_char('H', DATA, CLK, debug=True) | |
#write_char('e', DATA, CLK, debug=True) | |
#write_char('l', DATA, CLK, debug=True) | |
#write_char('l', DATA, CLK, debug=True) | |
#write_char('o', DATA, CLK, debug=True) | |
time.sleep(0.005) | |
write_char_4bit('H', DATA[4:], CLK, debug=True) | |
write_char_4bit('e', DATA[4:], CLK, debug=True) | |
write_char_4bit('l', DATA[4:], CLK, debug=True) | |
write_char_4bit('l', DATA[4:], CLK, debug=True) | |
write_char_4bit('o', DATA[4:], CLK, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment