python_btn_click_isr.py 580 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import RPi.GPIO as GPIO
  4. import time
  5. #GPIO BOARD mode
  6. BTN_PIN = 36
  7. #GPIO BCM mode
  8. #BTN_PIN = 16
  9. #统计按键次数
  10. number=0
  11. def btnInterrupt(BTN_PIN):
  12. global number
  13. print("Button Click:%s"%number)
  14. number = number + 1
  15. GPIO.setmode(GPIO.BOARD)
  16. #GPIO.setmode(GPIO.BCM)
  17. GPIO.setup(BTN_PIN, GPIO.IN, GPIO.PUD_UP)
  18. GPIO.add_event_detect(BTN_PIN, GPIO.RISING, btnInterrupt, 400)
  19. try:
  20. while True:
  21. time.sleep(3)
  22. except KeyboardInterrupt:
  23. pass
  24. #GPIO资源清理
  25. GPIO.remove_event_detect(BTN_PIN)
  26. GPIO.cleanup()