使用Raspberry Pi Python使用74HC595控制8位7段显示

2024-09-30 08:27:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个8位数的LED显示板,但我得到了奇怪的结果。第21行定义了哪些线段点亮,哪些显示被使用。因此[1,0,0,0,1,0,0,0]关闭第1和第5段,并点亮第4和第8个显示器(显示器的顺序为4,3,2,1,8,7,6,5)。因此,第24行列表中的第一个1将关闭小数点并点亮第四个显示屏。第5个1关闭底部区段并点亮第8个显示屏

8 digit seven-segment display

我希望能够指定要使用的显示器以及要亮起/不亮起显示器的哪些部分

以下是Python 3代码:

import RPi.GPIO as IO
import time

# Ignore warnings
IO.setwarnings(False)

# Set pinouts
dataPin  = 11
latchPin = 15
clockPin = 13

IO.setmode (IO.BOARD)
IO.setup(dataPin, IO.OUT)
IO.setup(clockPin, IO.OUT)
IO.setup(latchPin, IO.OUT)

# 7-segment displays are in the following
# order:   4 3 2 1 8 7 6 5

# Segments to light (0 = on / 1 = off)
segsLit = [1,0,0,0,1,0,0,0] # Line 21

# Iterate through 7-seg displays
for j in range(8):
  IO.output(latchPin, 0)

  # Iterate through list1 to light segments
  for i in range(8):
    IO.output(clockPin, 0)
    IO.output(dataPin, segsLit[i])
    IO.output(clockPin, 1)
  IO.output(latchPin, 1)

IO.cleanup()

print("Done")

我找到了一些指南,但它们只用于驱动单个显示器或8个LED。有很多关于Arduino的指南,我已经试着转换了,但是我只是不断地遇到类似的问题,显示错误的数字


Tags: toinioimportoutputledsetupout
2条回答

需要向电路板发送16位数据。前8位用于照亮特定段,随后的8位用于指示要使用的显示器

在下面更新的代码中,第8个(最右侧)显示屏将显示字母“A”

import RPi.GPIO as IO
import time

# Ignore warnings
IO.setwarnings(False)

# Set pinouts
dataPin  = 11
latchPin = 15
clockPin = 13

IO.setmode (IO.BOARD)
IO.setup(dataPin, IO.OUT)
IO.setup(clockPin, IO.OUT)
IO.setup(latchPin, IO.OUT)

# 7-segment displays are in the following
# order:   4 3 2 1 8 7 6 5

# First 8 bits = segments to light // Second 8 bits = position
segsLit = [1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0] # Line 21

# Iterate through 7-seg displays
IO.output(latchPin, 0)

# Iterate through list1 to light segments
for i in range(16):
  IO.output(clockPin, 0)
  IO.output(dataPin, segsLit[i])
  IO.output(clockPin, 1)
IO.output(latchPin, 1)

IO.cleanup()

print("Done")

根据我为你的电路板找到的原理图,看起来要驱动8个LED,你需要一个类似的程序。其主要思想是led必须不断刷新以显示多个数字。每个换档周期只启用一个区段模式(尽管您可以使其显示在多个数字上,这肯定是一个不寻常的应用。)

我没有费心去试着让数字位置和片段位置都正确,这取决于你。此外,数字选择极性可能需要反转。但对代码的编辑应该会让您更接近:

import RPi.GPIO as IO

REFRESH_NS = 1_000_000_000 / 30 / 8  # refresh 30 times per second, 8 digits

# Ignore warnings
IO.setwarnings(False)

# Set pinouts
dataPin  = 11
latchPin = 15
clockPin = 13

IO.setmode (IO.BOARD)
IO.setup(dataPin, IO.OUT)
IO.setup(clockPin, IO.OUT)
IO.setup(latchPin, IO.OUT)

# 7-segment displays are in the following
# order:   4 3 2 1 8 7 6 5

# Segments to light (0 = on / 1 = off)
segsLit = 
[
[1,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0],
[0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0],
[0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,1],
]

update_time = time.monotonic_ns()

# Iterate through 7-seg displays (8 bits with decimal point)
while True:
    IO.output(latchPin, 0) # start shift operation
    
    for digit in range(8):

        # digit select shifts out first
        for pos in range(8):
            IO.output(clockPin, 0)
            IO.output(dataPin, 1 if pos==digit else 0)
            IO.output(clockPin, 1)

        # Iterate through to light segments
        for seg in range(8):
            IO.output(clockPin, 0)
            IO.output(dataPin, segsLit[digit][seg])
            IO.output(clockPin, 1)


    IO.output(latchPin, 1) # end shift operation
    
    while time.monotonic_ns() < update_time + REFRESH_NS: # delay to energize led
        pass # spin loop
    
    update_time = time.monotonic_ns() # prepare for next update
    
IO.cleanup()

print("Done")

相关问题 更多 >

    热门问题