Skip to content

More Verbose #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions 10_74HC595_LED.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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
Expand All @@ -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()

Expand Down