From 9b6920bf90b5983ce7fa00672b35ac56614378ef Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 30 Jan 2018 00:49:59 +0100 Subject: [PATCH] More Verbose --- 10_74HC595_LED.py | 60 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/10_74HC595_LED.py b/10_74HC595_LED.py index 7f45632..31f408a 100755 --- a/10_74HC595_LED.py +++ b/10_74HC595_LED.py @@ -2,6 +2,7 @@ #================================================ # # This program is for SunFounder SuperKit for Rpi. +# or for the Velleman VMS-502 # # Extend use of 8 LED with 74HC595. # @@ -10,12 +11,18 @@ # #================================================= -import RPi.GPIO as GPIO +import RPi.GPIO as GPIO #if you use a standard Raspberry PI +#import RTk.GPIO as GPIO #if you use a RTk.GPIO USB Board for PC or Mac +#import ASUS.GPIO as GPIO #if you use a ASUS TinkerBoard import time +import sys #for displaying without newline -SDI = 11 -RCLK = 12 -SRCLK = 13 +#Datasheet: http://www.ti.com/lit/ds/symlink/sn74hc595.pdf + +#name = GPIO Pin +SDI = 11 #75hc595 PIN 14: Serial Data IN +RCLK = 12 #74hc595 PIN 12: storage Register CLocK +SRCLK = 13 #74hc595 PIN 11: Shift Register CLocK #=============== LED Mode Defne ================ # You can define yourself, in binay, and convert it to Hex @@ -33,41 +40,62 @@ def print_msg(): print 'Program is running...' print 'Please press Ctrl+C to end the program...' + def setup(): - GPIO.setmode(GPIO.BOARD) # Number GPIOs by its physical location - GPIO.setup(SDI, GPIO.OUT) - GPIO.setup(RCLK, GPIO.OUT) - GPIO.setup(SRCLK, GPIO.OUT) - GPIO.output(SDI, GPIO.LOW) - GPIO.output(RCLK, GPIO.LOW) - GPIO.output(SRCLK, GPIO.LOW) + GPIO.setmode(GPIO.BOARD) # Number GPIOs by its physical location + GPIO.setup(SDI, GPIO.OUT) # Define Pin as OUTPUT + GPIO.setup(RCLK, GPIO.OUT) # Define Pin as OUTPUT + GPIO.setup(SRCLK, GPIO.OUT) # Define Pin as OUTPUT + GPIO.output(SDI, GPIO.LOW) # Set Pin to 0 + GPIO.output(RCLK, GPIO.LOW) # Set Pin to 0 + GPIO.output(SRCLK, GPIO.LOW) # Set Pin to 0 + + +#write as Serial Bits to HC595 by waking bitwise through the byte def hc595_in(dat): - for bit in range(0, 8): - GPIO.output(SDI, 0x80 & (dat << bit)) + sys.stdout.write( "\r" + '{:08b}'.format(dat) ) # display on screen + sys.stdout.flush() # + sys.stdout.write( "\n" ) # + sys.stdout.flush() # + for bit in range(0, 8): + GPIO.output(SDI, 0x80 & (dat << bit)) #<< is the bitwise shift operator GPIO.output(SRCLK, GPIO.HIGH) - time.sleep(0.001) + time.sleep(0.1) + sys.stdout.write( "x" ) # + sys.stdout.flush() # display on screen GPIO.output(SRCLK, GPIO.LOW) -def hc595_out(): + +#send an impulse to RCLK that will load the data from the Shift Register in the Chip to the Display Register in the Chip + +def hc595_out(): #pin 12: storage Register CLocK GPIO.output(RCLK, GPIO.HIGH) time.sleep(0.001) GPIO.output(RCLK, GPIO.LOW) + sys.stdout.write( " (X) Loading to DisplayRegister ") + sys.stdout.flush() + def loop(): WhichLeds = LED0 # Change Mode, modes from LED0 to LED3 - sleeptime = 0.1 # Change speed, lower value, faster speed + sleeptime = 1 # Change speed, lower value, faster speed while True: + #walk from bit 1 to bit 8 for i in range(0, len(WhichLeds)): + #write data serial to 74hc595 hc595_in(WhichLeds[i]) + #adopt the data in the shift register hc595_out() time.sleep(sleeptime) + #reverse direction for i in range(len(WhichLeds)-1, -1, -1): hc595_in(WhichLeds[i]) hc595_out() time.sleep(sleeptime) + def destroy(): # When program ending, the function is executed. GPIO.cleanup()