1

我是 Python 新手,主要做硬件。我正在尝试使用移位寄存器和 Raspberry PI 2 创建一个 Nixie 时钟库。我发现一个类似的项目是作者包含示例代码,这里http://www.smbaker.com/raspberry-pi-nixie-tube-时钟原型

硬件已设置并运行,但是我在网上找到的测试代码中有一些东西阻止了 Pi 显示时间。作为测试脚本的代码,它将循环出来以显示时间。正如我所说,我是 Pi 和 Python 的新手,我相信我遇到的障碍是在代码的末尾。数字以模式序列显示数字 0.1.2.4.8,但从不显示时间。我已经包含了下面的代码。

import RPi.GPIO as GPIO
import time
import datetime

PIN_DATA = 23
PIN_LATCH = 24
PIN_CLK = 25

类 Nixie: def init (self, pin_data, pin_latch, pin_clk, digits): self.pin_data = pin_data self.pin_latch = pin_latch self.pin_clk = pin_clk self.digits = digits

    GPIO.setmode(GPIO.BCM)

    # Setup the GPIO pins as outputs
    GPIO.setup(self.pin_data, GPIO.OUT)
    GPIO.setup(self.pin_latch, GPIO.OUT)
    GPIO.setup(self.pin_clk, GPIO.OUT)

    # Set the initial state of our GPIO pins to 0
    GPIO.output(self.pin_data, False)
    GPIO.output(self.pin_latch, False)
    GPIO.output(self.pin_clk, False)

def delay(self):
    # We'll use a 10ms delay for our clock
    time.sleep(0.010)

def transfer_latch(self):
    # Trigger the latch pin from 0->1. This causes the value that we've
    # been shifting into the register to be copied to the output.
    GPIO.output(self.pin_latch, True)
    self.delay()
    GPIO.output(self.pin_latch, False)
    self.delay()

def tick_clock(self):
    # Tick the clock pin. This will cause the register to shift its
    # internal value left one position and the copy the state of the DATA
    # pin into the lowest bit.
    GPIO.output(self.pin_clk, True)
    self.delay()
    GPIO.output(self.pin_clk, False)
    self.delay()

def shift_bit(self, value):
    # Shift one bit into the register.
    GPIO.output(self.pin_data, value)
    self.tick_clock()

def shift_digit(self, value):
    # Shift a 4-bit BCD-encoded value into the register, MSB-first.
    self.shift_bit(value&0x08)
    value = value << 1
    self.shift_bit(value&0x08)
    value = value << 1
    self.shift_bit(value&0x08)
    value = value << 1
    self.shift_bit(value&0x08)

def set_value(self, value):
    # Shift a decimal value into the register

    str = "%0*d" % (self.digits, value)

    for digit in str:
        self.shift_digit(int(digit))
        value = value * 10

    self.transfer_latch()

def main(): try: nixie = Nixie(PIN_DATA, PIN_LATCH, PIN_CLK, 4)

    # Uncomment for a simple test pattern
    #nixie.set_value(1234)

    # Repeatedly get the current time of day and display it on the tubes.
    # (the time retrieved will be in UTC; you'll want to adjust for your
    # time zone)
    while True:
        dt = datetime.datetime.now()
        nixie.set_value(dt.hour*100 + dt.minute)

finally:
    # Cleanup GPIO on exit. Otherwise, you'll get a warning next time toy
    # configure the pins.
    GPIO.cleanup()

如果名称== “”:主()

4

0 回答 0