需要忽略输入的非锁存 GPIO 输出

发布于 2025-01-10 15:36:37 字数 663 浏览 0 评论 0原文

编程新手,所以我会尽力解释一下:

我正在尝试在太阳能设置中使用 Pi Zero 来切换 x86 主板上的开机输入(基本上作为电源按钮操作)。这个想法是,当电池电压足够高时,电池电压监视器将 Pi 上的引脚 14 设置为高电平。该脚本读取此输入,然后切换 Pi 上的输出 (26),进而关闭继电器一秒钟,然后激活主板上的开机输入。 问题是我无法在一秒后关闭输出(26)。该脚本不会忽略引脚 14,而只是将输出 (26) 保持为高电平。 (引脚 14 自然会被电池监视器保持为高电平,直到电池电量降至某个阈值以下)

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)

GPIO.output(26, 1)

while True:
        if GPIO.input(14)==1:
                GPIO.output(26, 0)
                sleep(1)
                GPIO.output(26, 1)
        else:
                GPIO.output(26, 1)

GPIO.cleanup()

New to programming, so I'll try my best to explain:

I'm trying to use a Pi Zero in my solar setup to switch the power-on input on an x86 motherboard (basically operating as the power button). The idea is that a battery voltage monitor sets pin 14 on the Pi high when the battery is at a high enough voltage. The script reads this input then switches an output (26) on the Pi, which in turn closes a relay for one second, which will then activate the power-on input on the motherboard.
The problem is that I cannot get the output (26) to turn off after one second. The script won't ignore pin 14, and just keeps the output (26) high. (Pin 14 is naturally held high by the battery monitor until the battery level falls down below a certain threshold)

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)

GPIO.output(26, 1)

while True:
        if GPIO.input(14)==1:
                GPIO.output(26, 0)
                sleep(1)
                GPIO.output(26, 1)
        else:
                GPIO.output(26, 1)

GPIO.cleanup()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的笑 2025-01-17 15:36:37

这是一种可能的解决方案,轮询 GPIO14 1/秒的值
并使用变量 power_on 来确定我们如何响应
value:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)

GPIO.output(26, 1)

power_on = False
while True:
    if not power_on:
        if GPIO.input(14) == 1:
                GPIO.output(26, 0)
                sleep(1)
                GPIO.output(26, 1)
                power_on = True
    else:
        if GPIO.input(14) == 0:
            power_off = True

    time.sleep(1)

# Note that you're never going to reach this line.
GPIO.cleanup()

或者,您可以让代码块直到引脚 14 发生变化,从而导致如下情况:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)

GPIO.output(26, 1)

while True:
    GPIO.wait_for_edge(14, GPIO.RISING)
    GPIO.output(26, 0)
    sleep(1)
    GPIO.output(26, 1)

# Note that you're never going to reach this line.
GPIO.cleanup()

这使用 GPIO.wait_for_edge() 等待 GPIO14 上的上升沿(低 ➡️ 高转换) ,然后触发你的继电器。

Here's one possible solution that polls the value of GPIO14 1/second
and uses the variable power_on to figure out how we respond to the
value:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)

GPIO.output(26, 1)

power_on = False
while True:
    if not power_on:
        if GPIO.input(14) == 1:
                GPIO.output(26, 0)
                sleep(1)
                GPIO.output(26, 1)
                power_on = True
    else:
        if GPIO.input(14) == 0:
            power_off = True

    time.sleep(1)

# Note that you're never going to reach this line.
GPIO.cleanup()

Alternatively, you can have your code block until pin 14 changes, leading to something like this:

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(26,GPIO.OUT)

GPIO.output(26, 1)

while True:
    GPIO.wait_for_edge(14, GPIO.RISING)
    GPIO.output(26, 0)
    sleep(1)
    GPIO.output(26, 1)

# Note that you're never going to reach this line.
GPIO.cleanup()

This uses GPIO.wait_for_edge() to wait for a rising edge on GPIO14 (a low ➡️ high transition), and then triggers your relay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文