如何在时钟程序中使用 Blink_1Hz 程序?

问题描述 投票:0回答:2

我想用树莓派 Pico 做一个时钟。 在我找到“blink_1hz.py”的文档中,我想使用 1 秒中断作为我的时钟的计数器。

原程序每秒正确打印系统时间。 我只是用自己的秒计数器函数替换了 irq 中对 lambda 函数的调用。

我的问题是: 我的程序版本只打印一次而不是十次,并且不会增加

t
.

我在网上找遍了,但几乎没有关于状态机使用的中断的具体信息。 欢迎所有建议

这是我的代码:

# Example using PIO to blink an LED and raise an IRQ at 1Hz.
import time
from machine import Pin
import rp2

   
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink_1hz():
    # Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000
    irq(rel(0))
    set(pins, 1)
    set(x, 31) [5]
    label("delay_high")
    nop() [29]
    jmp(x_dec, "delay_high")

# Cycles: 1 + 7 + 32 * (30 + 1) = 1000
    set(pins, 0)
    set(x, 31) [6]
    label("delay_low")
    nop() [29]
    jmp(x_dec, "delay_low")

def secs():
    global t
    t = t+1
    print("secs",t)

t = 0

# Create the StateMachine with the blink_1hz program, outputting on Pin(25).
sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25))


# Set the IRQ handler to print the millisecond timestamp.

sm.irq(handler = secs()) # prints secs only once
#sm.irq(lambda p: print( time.ticks_ms())) # original, prints ticks every second

# Start the StateMachine.
sm.active(1)
time.sleep(10)
# Stop the StateMachine
sm.active(0)
print("main",t)
interrupt
2个回答
0
投票

这是我的猜测:

处理程序需要传递一个可调用对象(例如函数),但是通过传递

secs()
而不是
secs
你传递的是被调用函数的单个实例(我对这部分一头雾水)而不是可调用对象。我知道基于 pin 的 IRQ 处理程序也会传递 pin 号的参数,因此状态机 IRQ 可能会将引用传递给引发中断的状态机实例,因此您的函数定义可能需要读取
def secs(sm):
以捕获该参数即使你不使用它?


-2
投票

我希望我们现在能得到一些反应。 否则我认为这个问题不够有趣,或者现在没有人在为任何 Pico 编程

© www.soinside.com 2019 - 2024. All rights reserved.