1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#-------------------------------------------------
# Test for interrupt detection on GPA0 or GPA3
#-------------------------------------------------
import RPi.GPIO as GPIO
import smbus
import time
DEVICE = 0x20 # Device address (A0-A2)
IODIRA = 0x00 # Pin direction register for GPA
GPINTENA= 0x04 # Enable interrupt for GPA
DEFVAL = 0x06 # Reference values for the interruptions on GPA
INTCONA = 0x08 # Interruption mode -- 1:compare to DEFVAL, 0:compare to its old value
INTCAPA = 0x10 # value stored after an interruption
GPIOA = 0x12 # GPA register for input
GPIOINT = 26 # GPIO pin for the interruption line
GPA0 = 1 # decimal value of GPA0 in the GPA register
GPA3 = 8 # decimal value of GPA3 in the GPA register
bus = smbus.SMBus(1)
# Set GPA1,2,4,5,6,7 as output the GPA0,3 as input
bus.write_byte_data(DEVICE,IODIRA,0x09)
# Define the reference values for the GPAs
bus.write_byte_data(DEVICE,DEFVAL,0x00)
# Define the interruption mode for GPAs
bus.write_byte_data(DEVICE,INTCONA,0x09)
# Enable interrupt on GPA0 and GPA3
bus.write_byte_data(DEVICE,GPINTENA,0x09)
# set up BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# set GPIOINT as input : pin GPIO26 of the RPI3 linked to INTA
GPIO.setup(GPIOINT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def trigger_callback(channel):
reg_interrupt_a = bus.read_byte_data(DEVICE,INTCAPA)
if GPIO.input(channel) == 1:
print time.strftime("%d.%m.%Y %H:%M:%S"), "Channel " + str(channel) + " :"
if reg_interrupt_a == GPA0:
print "-- GPA0 has been triggered"
elif reg_interrupt_a == GPA3:
print "-- GPA3 has been triggered"
# When a rising edge is detected on port GPIOINT,
# the function trigger_callback will run
# Add 75ms to avoid any bounce effect
GPIO.add_event_detect(GPIOINT, GPIO.RISING, callback=trigger_callback, bouncetime=75)
# Main loop
while True:
try:
reg_input = bus.read_byte_data(DEVICE,INTCAPA) # **** Needed to trigger the interruption !!!!
time.sleep(0.1)
except KeyboardInterrupt:
print "Program has ended with a CTRL C"
print "Cleaning all..."
GPIO.remove_event_detect(GPIOINT) # remove any event detection
GPIO.cleanup() # clean up GPIO on CTRL+C
exit()
GPIO.remove_event_detect(GPIOINT)
GPIO.cleanup() |
Partager