Processor

【라즈베리파이】 wiringPi Python : 인터럽트

작성자 임베디드코리아 작성일26-02-20 00:15 조회125회 댓글0건
【라즈베리파이】 wiringPi Python : 인터럽트

--->>> Inerrupt.py <<<-------------------
num = 1
# try~ except 특정 예외
try:
  while True:
      print(num)
      num += 1

# Ctrl + C를 입력할 경우
except KeyboardInterrupt:
  print('Ctrl + C 중지 메시지 출력')

print('codeomni.tistory.com')

-------------------------------------------------------------------
--->>> GPIO_key_Interrupt.py  <<<-----------------------
import RPi.GPIO as GPIO
#GPIO 라이브러리 버젼을 출력한다
print GPIO.VERSION
#핀 넘버링을 BCM 방식을 사용한다.
GPIO.setmode(GPIO.BCM)
 
#4번 핀을 입력모드로 설정
GPIO.setup(4, GPIO.IN)
globalCounter = 0
 
#인터럽트 함수가 호출되면 글로벌 변수 globalCounter 값을 1 증가시킨다.
def myInterrupt(channel):
    global globalCounter
    globalCounter += 1
    print " Done. counter:" , globalCounter
 
 
#4번 핀이 OFF될 때 myInterrupt 함수를  통해 인터럽트를 받겠다는 요청
#GPIO.FALLING은 ON 상태에서 OFF로 변경될 때 시그널을 받겠다는 의미
GPIO.add_event_detect(4, GPIO.FALLING, callback=myInterrupt)
 
try:
    raw_input("Press Enter to Exit\n>")
except KeyboardInterrupt:
    GPIO.cleanup() # clean up GPIO on CTRL+C exit
    GPIO.remove_event_detect(4)
 
GPIO.remove_event_detect(4)
GPIO.cleanup() # clean up GPIO on normal exit
---------------------------------------------------------------------------------

# gpio-interrupt-test.py
# GPIO12에 입력이 들어오면 문장을 출력한다.

# 라이브러리 불러오기
import RPi.GPIO as GPIO
import time

# 스위치 눌렸을 때 콜백함수
def switchPressed(channel):
    print('channel %s pressed!!'%channel)

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# interrupt 선언
GPIO.add_event_detect(12, GPIO.RISING, callback=switchPressed)
# 메인 쓰레드
try:
    while 1:
        print(".")
        time.sleep(0.1)
finally:
    GPIO.cleanup()