首页 > 解决方案 > rpi4 add_event_detect 事件触发器

问题描述

我试图用add_event_detect()函数在 RPi4 上实现中断。我的结构如下

输入:gpio20,源信号
输出:gpio21,光触发信号

entertime_stamp = time.time() 

''' config '''
INPUT_PIN = 20
OUTPUT_PIN = 21

''' gpio '''
GPIO.setmode(GPIO.BCM) 
GPIO.setup(INPUT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # pull_up_down=GPIO.PUD_DOWN
GPIO.setup(OUTPUT_PIN, GPIO.OUT, initial=GPIO.LOW) 
        
''' function for event trigger '''
def Interrupt(channel):
    global OUTPUT_PIN
    global GPIO
    global time_stamp       # put in to debounce  
    
    time_now = time.time()  
    if ((time_now - time_stamp) >= 0.3 and GPIO.input(INPUT_PIN)==1):  
    
        GPIO.output(OUTPUT_PIN, True)    
        
        time.sleep(0.2)
        GPIO.output(OUTPUT_PIN, False)
        
        print('Action trigger');
    
    time_stamp = time_now  

''' interrupt '''
GPIO.add_event_detect(INPUT_PIN, GPIO.RISING, callback = Interrupt, bouncetime = 200)


''' main '''
try:
    
    while True:
        print("input: {0}".format(GPIO.input(INPUT_PIN)))
            
        time.sleep(0.5)

我的规则是在接收到gpio20信号上升时触发gpio21(0到1),所以我将add_event_detect()规则设置为“GPIO.RISING”。

GPIO.add_event_detect(INPUT_PIN, GPIO.RISING, callback = Interrupt, bouncetime = 200) 

但是当接收到gpio20信号下降时它也会触发gpio21(0到1)。如何解决?

标签: eventsinterruptgpioraspberry-pi4

解决方案


推荐阅读