Rasberry Pi 使用 74595 與 7447 範例與程式




這邊的情境是使用 74595驅動一個由八顆led組成的陣列,7447控制一個七段顯示器
IC的控制邏輯封裝在class內並且獨立成為一個檔案(iclib.py)

main.py

import RPi.GPIO as gpio
from iclib import IC74595,IC7447
import time

#
gpio.setmode(gpio.BCM)

#
DS = 16     # Serial Data
STCP = 20  # Latch
SHCP = 21  # Clock

IC_7447_A = 2
IC_7447_B = 3
IC_7447_C = 4
IC_7447_D = 17

#
LED_STATE = [0,128,192,224,240,248,252,254,255] #0 1 2 3 4 5 6 7 8

#
sevenSegmentDisplay = IC7447(IC_7447_A,IC_7447_B,IC_7447_C,IC_7447_D)
ledArray = IC74595(DS,STCP,SHCP)

#self test
for x in range(9):
    ledArray.shiftout(LED_STATE[x]) #led array will light 0 to 8
    sevenSegmentDisplay.show(x+1) #seven segment display will show 1 go 9
    time.sleep(0.05)

for x in range(9):
    ledArray.shiftout(LED_STATE[8-x]) #led array will off from 8 to 0
    sevenSegmentDisplay.show(9-x) #seven segment display will show 9 go 1
    time.sleep(0.05)

sevenSegmentDisplay.off()

iclib.py

import RPi.GPIO as gpio

class IC74595():
    def __init__(self,DS,STCP,SHCP):
        self.DS = DS
        self.STCP = STCP
        self.SHCP = SHCP
        gpio.setup(self.DS, gpio.OUT)
        gpio.setup(self.SHCP, gpio.OUT)
        gpio.setup(self.STCP, gpio.OUT)

    def shiftout(self,byte):
        gpio.output(self.STCP, 0)
        b = ''
        for x in range(8):
            bit = ((byte >> x) & 1)
            b = b + str(bit)
            gpio.output(self.DS, bit)
            gpio.output(self.SHCP, 1)
            gpio.output(self.SHCP, 0)

        gpio.output(self.STCP, 1)

class IC7447():
    def __init__(self,A,B,C,D):
        self.A = A # mapping to 7447 input A - D
        self.B = B
        self.C = C
        self.D = D
        gpio.setup(self.A,gpio.OUT)
        gpio.setup(self.B,gpio.OUT)
        gpio.setup(self.C,gpio.OUT)
        gpio.setup(self.D,gpio.OUT)

    def off(self):
        gpio.output(self.A,1)
        gpio.output(self.B,1)
        gpio.output(self.C,1)
        gpio.output(self.D,1)

    def show(self,num):
        if(num == 0):
            gpio.output(self.A,0)
            gpio.output(self.B,0)
            gpio.output(self.C,0)
            gpio.output(self.D,0)
        elif(num == 1):
            gpio.output(self.A,1)
            gpio.output(self.B,0)
            gpio.output(self.C,0)
            gpio.output(self.D,0)
        elif(num == 2):
            gpio.output(self.A,0)
            gpio.output(self.B,1)
            gpio.output(self.C,0)
            gpio.output(self.D,0)
        elif(num == 3):
            gpio.output(self.A,1)
            gpio.output(self.B,1)
            gpio.output(self.C,0)
            gpio.output(self.D,0)
        elif(num == 4):
            gpio.output(self.A,0)
            gpio.output(self.B,0)
            gpio.output(self.C,1)
            gpio.output(self.D,0)
        elif(num == 5):
            gpio.output(self.A,1)
            gpio.output(self.B,0)
            gpio.output(self.C,1)
            gpio.output(self.D,0)
        elif(num == 6):
            gpio.output(self.A,0)
            gpio.output(self.B,1)
            gpio.output(self.C,1)
            gpio.output(self.D,0)
        elif(num == 7):
            gpio.output(self.A,1)
            gpio.output(self.B,1)
            gpio.output(self.C,1)
            gpio.output(self.D,0)
        elif(num == 8):
            gpio.output(self.A,0)
            gpio.output(self.B,0)
            gpio.output(self.C,0)
            gpio.output(self.D,1)
        elif(num == 9):
            gpio.output(self.A,1)
            gpio.output(self.B,0)
            gpio.output(self.C,0)
            gpio.output(self.D,1)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料