【问题标题】:How to detect change in GPIO input raspberry pi如何检测 GPIO 输入树莓派的变化
【发布时间】:2015-07-05 09:09:27
【问题描述】:

有没有办法在不使用无限循环的情况下检测树莓派 GPIO 的变化?

您可以使用以下方法检测上升或下降:

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)

但您只能将事件检测器设置为一次下降或上升。有没有办法在不检查无限循环中的输入的情况下做到这一点?

【问题讨论】:

    标签: python raspberry-pi gpio


    【解决方案1】:

    您可以在event_detect 上使用线程回调。根据raspberry-gpio-python,您可以使用类似的东西。

    GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)

    其中 event 可以是 GPIO.RISINGGPIO.FALLINGGPIO.BOTHmy_callback 是一个普通的 Python 函数,其行为类似于在不同线程中运行的 ISR。

    希望对您有所帮助。

    【讨论】:

    • 糟糕,我错过了GPIO.BOTH 位。
    【解决方案2】:

    此链接可能会有所帮助raspberry-gpio-python 基本上只是在上升沿或下降沿使用回调来做任何你想做的事情,而不是轮询(你描述的)

    【讨论】:

      【解决方案3】:

      如果您有一个简单的 MCP3004 或 MCP3008 IC,它是一个模数转换器,您可以在输入方面做更多的事情。这是一些示例代码,可帮助您入门。 More info on ADC's here 以及如何将它们连接到您的 pi 上

      import spidev
      
      #this fucntion can be used to find out the ADC value on ADC 0
      def readadc_0(adcnum_0):
          if adcnum_0 > 7 or adcnum_0 < 0:
              return -1
          r_0 = spi_0.xfer2([1, 8 + adcnum_0 << 4, 0])
          adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]
          return adcout_0
      
      reading= readadc_0(0))
      

      根据您的 ADC 的分辨率,您必须进行一些计算才能将读数转换为电压

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-01
        • 1970-01-01
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多