2017年3月2日 星期四

樹莓派3 Raspberry Pi 3 - 使用2 顆 74HC595 及 3 個 GPIO 控制 2 顆七段數字顯示器








接駁圖:



電子零件:
  • 麵包板 x 1
  • GPIO Board x 1
  • 74HC595 x 2
  • 七段數字顯示器 x 2
  • 公對公杜邦線 N 條


Python 程式碼:

#!/usr/bin/python
import RPi.GPIO as gpio
import time

# Setup 7 segment display
seg = []
seg.append([0,0,0,0,0,0,1])    # 0
seg.append([1,0,0,1,1,1,1])    # 1
seg.append([0,0,1,0,0,1,0])    # 2
seg.append([0,0,0,0,1,1,0])    # 3
seg.append([1,0,0,1,1,0,0])    # 4
seg.append([0,1,0,0,1,0,0])    # 5
seg.append([0,1,0,0,0,0,0])    # 6
seg.append([0,0,0,1,1,1,1])    # 7
seg.append([0,0,0,0,0,0,0])    # 8
seg.append([0,0,0,0,1,0,0])    # 9

gpio.setmode(gpio.BCM)

# Setup 74HC595 Shift Registor pins
DS   = 12     # Serial Data
SHCP = 21     # Clock
STCP = 20     # Latch
gpio.setup(DS, gpio.OUT)
gpio.setup(SHCP, gpio.OUT)
gpio.setup(STCP, gpio.OUT)

# Send Clock signal
def clock(pin):
    gpio.output(pin, 1)
    gpio.output(pin, 0)
    
# Send Latch signal
def latch(pin):
    gpio.output(pin, 1)
    gpio.output(pin, 0)

def display(index, dot):
    tenDigit = int(index / 10)
    unitDigit = index % 10
    if tenDigit == 0:
        digits = [[1,1,1,1,1,1,1], seg[unitDigit]]
    else:
        digits = [seg[tenDigit], seg[unitDigit]]

    # Send signal to 74HC595 and drive two 7 segment display
    clock(SHCP)
    for x in range(2):
        for bit in digits[x]:
            gpio.output(DS, bit)
            latch(SHCP)
        gpio.output(DS, dot)
        latch(SHCP)
    
    clock(STCP)


try:
    # display 0-99 on two 7 segemnt display
    for i in range(100):
        display(i, 1)
        time.sleep(0.3)
        
    time.sleep(10)

finally:
    gpio.cleanup()

實驗短片:

沒有留言:

張貼留言