Dans mon projet, j'utilise un MCP23017 sur mon RPI3 (distribution Raspbian).
Mon but est de déclencher une action lorsqu'on appuie sur un bouton poussoir sur l'entrée GPA0 ou GPA3 du MCP23017.
J'ai écrit le programme python suivant :
Code : Tout sélectionner
#-------------------------------------------------
# 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()
Si je ne le fais pas, rien ne se passe !!!
Si je le fais, tout se passe comme prévu...
Pourquoi ? Ça ne me paraît pas très logique...ou j'ai raté quelque chose.
J'ai lu la doc du MCP23017 (http://ww1.microchip.com/downloads/en/D ... 21952b.pdf) et rien n'est dit à ce sujet. Il est juste dit que les interruptions sont effacées à la lecture du INITCAPA ou GPIOA, ce que je fais dans le 'trigger_callback'.
Ci-joint mon schéma de connection. Merci d'avance pour vos réponses ou débuts de réponses...