Outils pour utilisateurs

Outils du site


start:arduino:mcp23017:dfrobot

MCP23017 DFROBOT

Achat

introduction

Il s'agit d'une carte d'extension IO basée sur MCP23017 qui fournit 16 ports IO supplémentaires pour votre microcontrôleur. Il peut être configuré pour 8 adresses différentes, ce qui signifie que 8 modules peuvent être connectés via deux bus IIC pour atteindre jusqu'à 128 extensions IO. Ce module est une excellente solution au problème d'insuffisance d'E/S dans des applications telles que les robots et les médias interactifs.

Cette carte d'extension IO dispose de 2 groupes de broches IO : GPIOA et GPIOB. Chaque groupe dispose de 8 E/S indépendantes et chacune d'entre elles peut être arbitrairement configurée comme entrée ou sortie, et une entrée pull-up (connectée à 100KΩ pull-up), interruption et ainsi de suite. En plus de cela, le module possède 2 broches de signal d'interruption IA et IB qui sont utilisées pour détecter l'interruption dans les ports GPIOA et CPIOB respectivement. Lorsqu'une interruption se produit sur l'une des broches de CPIOA ou GPIOB, la broche IA ou IB produira un signal de niveau haut en conséquence. Applications

  • Robots
  • Média interactif
  • Cube lumineux

spécification

  • Alimentation : 3,3 V-5,5 V
  • Adresse IIC : 0x20~0x27
  • E/S numériques/Entrée/Sortie : PA0-PA7, PB0-PB7
  • Courant de conduite IO : 20 mA
  • Broche d'interruption de signal : IA, IB
  • Dimensions : 44x32mm / 1.73×1.26“

Présentation du conseil

Numéro Nom La description
1 +/CCV Positif
2 -/GND Négatif
3 C Ligne d'horloge IIC
4 D Ligne de données IIC
5 RST Broche de réinitialisation
6 IA Broche de détection d'interruption GPIOA
7 IB Broche de détection d'interruption GPIOB
8 REP Indicateur d'alimentation
9 A0/A1/A2 Commutateur d'adresse IIC

Tableau des ports E/S numériques

CPIOA 0 1 2 3 4 5 6
PA0 PA1 PA2 PA3 PA4 PA5 PA6
OPCIP 8 9 dix 11 12 13 14
PB0 PB1 PB2 PB3 PB4 PB5 PB6

Chaque broche numérique de la table peut être arbitrairement configurée comme entrée ou sortie, et comme entrée pull-up, interruption, etc.

Didacticiel

Conditions

  • Matériel
    • DFRduino UNO R3 (ou similaire) x 1
    • MCP23017 Module d'extension IIC à 16 E/S numériques x1
    • Boutons
    • Modules DEL
  • Logiciel
    • EDI Arduino
    • Téléchargez et installez la bibliothèque MCP23017 ( A propos de comment installer la bibliothèque ? )

Liste des fonctions API

fonctionsAPI.c
 /**
   * @brief Set the pin mode to  input, output or pull-up input (internal 100KΩ pull-up resistor)
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) included in ePin_t.
   * @param mode Mode, it can be set to Input, Output, Pull-up Input (internal 100KΩ pull-up resistor)
   * @return Return 0 if the setting is successful, otherwise return non-zero. 
   */
  int pinMode(ePin_t pin, uint8_t mode);
 
  /**
   * @brief Write digtial pin. The pin needs to be set to output mode before writing.
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) inlcuded in ePin_t.
   * @param level High level 1 or Low level 0
   * @return Return 0 if the writing is successful, otherwise return non-zero. 
   */
  int digitalWrite(ePin_t pin, uint8_t level);
 
  /**
   * @brief Read digital pin. The pin needs to be set to input mode before reading. 
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) included in ePin_t.
   * @return Return High or Low
   */
  int digitalRead(ePin_t pin);
 
  /**
   * @brief Set a pin to interrupt mode
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) included in ePin_t.
   * @param mode Interrupt mode: all enumeration values included in eInterruptMode_t.
   * @param cb Interrupt service function, needs to be defined and transferred parameter by users. Prototype: void func(int)
   */
  void pinModeInterrupt(ePin_t pin, eInterruptMode_t mode,  MCP23017_INT_CB cb);
 
  /**
   * @brief Poll if an interrupt occurs on a port group.
   * @param group Port group, it could be all enumeration values included in eGPIOGroup_t,  GPIO GroupA(eGPIOA), 
   * @n GPIO GroupB(eGPIOB) GroupA+B (eGPIOALL).
   * @n When setting to eGPIOA,poll if an interrupt occurs on the port group A. 
   * @n When setting to eGPIOB, poll if an interrupt occurs on the port group B.
   * @n When setting to eGPIOALL, poll if an interrupt occurs on the port group A+B
   * @n None, poll if an interrupt occurs on the all ports of group A and B by default. 
   */
  void pollInterrupts(eGPIOGroup_t group=eGPIOALL);
 
  /**
   * @brief Convert pin into string description
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) inlcuded in ePin_t.
   * @return Return pin description string
   * @n such as "eGPA0" "eGPA1" "eGPA2" "eGPA3" "eGPA4" "eGPA5" "eGPA6" "eGPA7"
   * @n   "eGPB0" "eGPB1" "eGPB2" "eGPB3" "eGPB4" "eGPB5" "eGPB6" "eGPB7"
   * @n   "eGPA" "eGPB"
   */
  String pinDescription(ePin_t pin);
 
  /**
   * @brief Convert pin into string description 
   * @param pin  Pin number, range 0~15
   * @return Return pin description string
   * @n such as "eGPA0" "eGPA1" "eGPA2" "eGPA3" "eGPA4" "eGPA5" "eGPA6" "eGPA7"
   * @n   "eGPB0" "eGPB1" "eGPB2" "eGPB3" "eGPB4" "eGPB5" "eGPB6" "eGPB7"
   * @n   "eGPA" "eGPB"
   */
  String pinDescription(int pin);

La relation entre l'adresse DIP et IIC

A2 A1 A0 Adresse IIC
0 0 0 0x20
0 0 1 0x21
0 1 0 0x22
0 1 1 0x23
1 0 0 0x24
1 0 1 0x25
1 1 0 0x26
1 1 1 0x27 (par défaut)

Diagramme de connexion

Exemple de code 1 - Entrée du bouton

Réglez la broche PA0 de la carte en mode entrée, connectez-la avec un bouton. Lorsque le bouton est enfoncé, la série imprime la chaîne “Button press!”.

Bouton1.ino
/*!
 * @file buttonInput.ino
 * @brief Connect a button to the IO expansion board, set a pin of the board(eg: eGPA0) to input mode to detect the button status.
 * @n Experiment phenomenon: connect a button on a pin of the IO board(eg:eGPA0), detect the level of the pin and print out
 * @n the button status on serial port.
 *
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [Arya](xue.peng@dfrobot.com)
 * @version  V1.0
 * @date  2019-07-18
 * @get from https://www.dfrobot.com
 * @url https://github.com/DFRobot/DFRobot_MCP23017
 */
#include <DFRobot_MCP23017.h>
/*DFRobot_MCP23017 constructor
 *Parameter&wire Wire
 *Parameter addr  I2C address can be select from 0x20~0x27; the relationship of DIP switch(A2, A1, A0) and I2C address(0x27) is
 *shown below:
  * 0  0  1  0  | 0  A2 A1 A0
    0  0  1  0  | 0  1  1  1    0x27
    0  0  1  0  | 0  1  1  0    0x26
    0  0  1  0  | 0  1  0  1    0x25
    0  0  1  0  | 0  1  0  0    0x24
    0  0  1  0  | 0  0  1  1    0x23
    0  0  1  0  | 0  0  1  0    0x22
    0  0  1  0  | 0  0  0  1    0x21
    0  0  1  0  | 0  0  0  0    0x20
 */
DFRobot_MCP23017 mcp(Wire, /*addr =*/0x27);//constructor, change the Level of A2, A1, A0 via DIP switch to revise the I2C address within 0x20~0x27.
//DFRobot_MCP23017 mcp;//use default parameter, Wire  0x27(default I2C address)
 
//Prepare: connect a button to a digital pin of the IO expansion board(eg: eGPA0)
void setup() {
  Serial.begin(115200);
  /*wait for the chip to be initialized completely, and then exit*/
  while(mcp.begin() != 0){
    Serial.println("Initialization of the chip failed, please confirm that the chip connection is correct!");
    delay(1000);
  }
  /*pinMode function is used to set the pin mode of module
  Parameter pin, the available parameter is shown below:
  eGPA0  eGPA1  eGPA2  eGPA3  eGPA4  eGPA5  eGPA6  eGPA7  eGPA
   0       1      2      3      4      5      6      7
  eGPB0  eGPB1  eGPB2  eGPB3  eGPB4  eGPB5  eGPB6  eGPB7  eGPB
   8       9      10     11     12     13     14     15
  Parameter mode, can be set to: INPUT, OUTPUT, INPUT_PULLUP(internal 100KΩ pull-up resistor)
  */
  mcp.pinMode(/*pin = */mcp.eGPA0, /*mode = */INPUT);
  /*Set all Group GPIOA pins to input*/
  //mcp.pinMode(/*pin = */mcp.eGPA, /*mode = */INPUT);
}
 
void loop() {
  /*digitalRead function is used to read the Level of a digital pin. The pin needs to be set to input mode before using this function. 
  Parameter pin, the available parameter is shown below:
  eGPA0  eGPA1  eGPA2  eGPA3  eGPA4  eGPA5  eGPA6  eGPA7  eGPA
   0       1      2      3      4      5      6      7
  eGPB0  eGPB1  eGPB2  eGPB3  eGPB4  eGPB5  eGPB6  eGPB7  eGPB
   8       9      10     11     12     13     14     15
  */
  uint8_t value = mcp.digitalRead(/*pin = */mcp.eGPA0);
  /*Read  level of Group GPIOA pins*/
  //value = mcp.digitalRead(/*pin = */mcp.eGPA);
  if(value){
      Serial.println("Button press!");
      delay(200);
  }else{
      //Serial.println("Button release!");
  }
}
  • Résultats attendus Lorsque le bouton est enfoncé, imprimez le “Button press” sur le port série.

Exemple de code de sortie à 2 broches

Réglez la broche PA7 de la carte en mode sortie, connectez-la avec une LED et changez l'état de la LED en une minute.

2broches.ino
/*!
 * @file ledOutput.ino
 * @brief Set a pin of IO expansion board(eg:eGPA7) to output mode, and output High/Low.
 * @n Experiment phenomenon: the LED connected to the pin of IO board(eg:eGPA7) repeatedly lights up for 1s and turns off 1s.
 *
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [Arya](xue.peng@dfrobot.com)
 * @version  V1.0
 * @eGPAte  2019-07-18
 * @get from https://www.dfrobot.com
 * @url https://github.com/DFRobot/DFRobot_MCP23017
 */
 
#include <DFRobot_MCP23017.h>
/*DFRobot_MCP23017 Constructor
 *Parameter &wire  Wire
 *Parameter addr  I2C address can be selected from 0x20~0x27, the relationship of DIP switch (A2,A1,A0) and I2C address (0x27)is shown below:
  * 0  0  1  0  | 0  A2 A1 A0
    0  0  1  0  | 0  1  1  1    0x27
    0  0  1  0  | 0  1  1  0    0x26
    0  0  1  0  | 0  1  0  1    0x25
    0  0  1  0  | 0  1  0  0    0x24
    0  0  1  0  | 0  0  1  1    0x23
    0  0  1  0  | 0  0  1  0    0x22
    0  0  1  0  | 0  0  0  1    0x21
    0  0  1  0  | 0  0  0  0    0x20
 */
DFRobot_MCP23017 mcp(Wire, /*addr =*/0x27);//constructor, change the Level of A2, A1, A0 via DIP switch to revise I2C address within 0x20~0x27
//DFRobot_MCP23017 mcp;//use default parameter, Wire  0x27(default I2C address)
 
//Prepare: connect the LED to a digital pin of IO expansion board(eg:eGPA7)
void setup(void)
{
  Serial.begin(115200);
  /*wait for the chip to be initialized completely, and then exit*/
  while(mcp.begin() != 0){
    Serial.println("Initialization of the chip failed, please confirm that the chip connection is correct!");
    delay(1000);
  }
  /*pinMode function is used to set the pin mode of the module
  Parameter pin, the available parameter is shown below:
  eGPA0  eGPA1  eGPA2  eGPA3  eGPA4  eGPA5  eGPA6  eGPA7  eGPA
   0       1      2      3      4      5      6      7
  eGPB0  eGPB1  eGPB2  eGPB3  eGPB4  eGPB5  eGPB6  eGPB7  eGPB
   8       9      10     11     12     13     14     15
  Parameter mode, can be set to: INPUT, OUTPUT, INPUT_PULLUP mode (internal 100KΩ pull-up resistor)
  */
  mcp.pinMode(/*pin = */mcp.eGPA7, /*mode = */OUTPUT);
  /*Set all Group GPIOA pins to output*/
  //mcp.pinMode(/*pin = */mcp.eGPA, /*mode = */OUTPUT);
}
 
void loop(void)
{
  Serial.println("Pin output high level!");
  /*digitalWrite function is used to make the pin output HIGH or LOW. The pin needs to be set to output mode before using this function.
  Designate a pin on the IO expansion board; parameter pin, the available parameter is shown below:
  eGPA0  eGPA1  eGPA2  eGPA3  eGPA4  eGPA5  eGPA6  eGPA7  eGPA
   0       1      2      3      4      5      6      7
  eGPB0  eGPB1  eGPB2  eGPB3  eGPB4  eGPB5  eGPB6  eGPB7  eGPB
   8       9      10     11     12     13     14     15
  */
  mcp.digitalWrite(/*pin = */mcp.eGPA7, /*level = */HIGH);
  /*Set GPIOIA0-GPIOIA3 to low and GPIOIA4-GPIOIA7 to high*/
  //mcp.digitalWrite(/*pin = */mcp.eGPA, /*Port Value = */0xF0);
  delay(1000);
  Serial.println("Pin output low level!");
  mcp.digitalWrite(/*pin = */mcp.eGPA7, /*level = */LOW);
  /*Set GPIOIA0-GPIOIA3 to high and GPIOIA4-GPIOIA7 to low*/
  //mcp.digitalWrite(/*pin = */mcp.eGPA, /*Port Value = */0x0F);
  delay(1000);
}
  • Résultats attendus

La LED connectée à la broche PA7 change d'état en une minute, imprime en série la sortie de niveau de la broche PA7.

Exemple de code - interrogation d'interruption

Réglez les broches IO de la carte en mode interruption, comme eChangeLevel, eFalling, eRising, eHighLevel, eLowLevel. Réglez PA0, PA1, PB6, PB7 sur eChangeLevel, eFalling, eRising, eHighLevel respectivement, interrogez et imprimez en série les broches où l'interruption s'est produite.

interruption1.ino
/*!
 * @file pollInterrupt.ino
 * @brief Set a pin of IO expansion board to interrupt mode, poll if there is an interrupt occurring on the pins of the port.
 * @n Experiment phenomenon: poll if there is an interrupt occurring on the port group(A, B, A+B), if there is, serial print the pin which is interrupted.
 *
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [Arya](xue.peng@dfrobot.com)
 * @version  V1.0
 * @date  2019-07-18
 * @get from https://www.dfrobot.com
 * @url https://github.com/DFRobot/DFRobot_MCP23017
 */
#include <DFRobot_MCP23017.h>
DFRobot_MCP23017 mcp(Wire, 0x27);//constructor, change the Level of A2, A1, A0 via DIP switch to revise I2C address within 0x20~0x27.
//DFRobot_MCP23017 mcp;//use default parameter, Wire  0x27(default I2C address)
 
/*Interrupt service function, prototype void func(int index), index: the number of the pin that is interrupted*/
void func(int index){
  String description = mcp.pinDescription(/*pin = */index);
  Serial.print(description);
  Serial.println(" Interruption occurs!");
}
 
void setup() {
  Serial.begin(115200);
 
  /*wait for the chip to be initialized completely, and then exit*/
  while(mcp.begin() != 0){
    Serial.println("Initialization of the chip failed, please confirm that the chip connection is correct!");
    delay(1000);
  }
 
  /*Parameter mode, the available is shown below:  
  eLowLevel             eHighLevel             eRising                  eFalling                 eChangeLevel
  Low-level interrupt   High-level interrupt   Rising-edge interrupt    Falling-edge interrupt   Double-edge interrupts 
  Parameter cb Interrupt service function(with parameter)
  Prototype void func(int)
  */
  mcp.pinModeInterrupt(/*p = */mcp.eGPA0, /*mode = */mcp.eChangeLevel, /*cb = */func);//digital pin0(eGPA0), double edge interrupt, generate an interrupt when the status of pin0 changes, INTA output High level.
  mcp.pinModeInterrupt(/*p = */mcp.eGPA1, /*mode = */mcp.eFalling, /*cb = */func);//digital pin1(eGPA1), falling edge interrupt, generate an interrupt when the status of pin 1 changes from High to Low, INTA output High level.
  mcp.pinModeInterrupt(/*p = */mcp.eGPB7, /*mode = */mcp.eRising, /*cb = */func);//digital pin15(eGPB7), rising edge interrupt, generate an interrupt when the status of pin15 changes from Low to High, INTB output High level.
  mcp.pinModeInterrupt(/*p = */mcp.eGPB6, /*mode = */mcp.eHighLevel, /*cb = */func);//digital pin14(eGPB6), high level interrupt, generate an interrupt when the pin 14 is in high level, INTB output High level. 
}
 
void loop() {
  /*pollInterrupts function is used to poll if an interrupt occurs on a port group
  Parameter group, the available parameter is shown below: (default: eGPIOALL)
   eGPIOA         eGPIOB             eGPIOALL
   Port Group A   Port Group B       Port Group A+B
  */
  mcp.pollInterrupts();
 // delay(1000);
}
  • Résultats attendus

Appuyez sur le bouton connecté à PA0, PA1, PB6, PB7 pour déclencher l'interruption et imprimez en série les broches où l'interruption s'est produite.

Exemple de code 4- Interruption IO

Combinez les broches INTA et INTB de la carte d'extension et l'interruption externe de UNO pour réaliser l'interruption IO. Avant de télécharger l'exemple de code suivant, connectez l'IA et l'IB de la carte d'extension à la broche numérique UNO 2 (interruption externe 0) et à la broche 3 (interruption externe 1). Réglez la broche PA0 sur l'interruption eHighLevel, PB7 sur l'interruption à double front et connectez les boutons avec eux. Définissez les interruptions externes UNO sur une interruption de front montant. Exécute la fonction de service d'interruption associée lorsqu'une interruption se produit.

Pour les autres microcontrôleurs, vérifiez la broche d'interruption dans le tableau de l'exemple de code ci-dessous.

interruption2.ino
/*!
 * @file ioInterrupt.ino
 * @brief IO interrupt, set a pin of a port group(A or B) IO to interrupt mode. When an interrupt occurs on the related port group, pin INTA(group A) or INTB(group B) will output a High level.
 * INTA and INTB are used to detect if an interrupt occurs on the pin of port eGPA and eGPB respectively; connect pin INTA and INTB to main-controller's external interrupt 0 and 1 respectively.
 * @n Experiment phenomenon: when the signal change of pin INTA or INTB is detected by main-board, the related interrupt service function will be executed to print out which pin was interrupted on serial port.
 *
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [Arya](xue.peng@dfrobot.com)
 * @version  V1.0
 * @eGPAte  2019-07-18
 * @get from https://www.dfrobot.com
 * @url https://github.com/DFRobot/DFRobot_MCP23017
 */
 
#include <DFRobot_MCP23017.h>
DFRobot_MCP23017 mcp(Wire, /*addr =*/0x27);//constructor, change the Level of A2, A1, A0 via DIP switch to revise I2C address within 0x20~0x27.
//DFRobot_MCP23017 mcp;//use default parameter, Wire  0x27(default I2C address)
 
//Connect 2 buttons to IO expansion board, one to a pin of port eGPA(eg: eGPA0), the other to a pin of port eGPB(eg: eGPB0)
//Connect INTA to the external interrupt pin0 of UNO, INTB to external interrupt pin1 of UNO.
 
bool intFlagA = false;//INTA interrupt sign
bool intFlagB = false;//INTB interrupt sign
 
/*Interrupt service function, prototype void func(int index), index represents the pin which is interrupted*/
void gpa0CB(int index){
  String description = mcp.pinDescription(index);
  Serial.print(description);Serial.println(" Interruption occurs!");
}
 
void gpb7CB(int index){
  String description = mcp.pinDescription(index);
  Serial.print(description);Serial.println(" Interruption occurs!");
}
 
void setup() {
  Serial.begin(115200);
  #ifdef ARDUINO_ARCH_MPYTHON 
  pinMode(P0, INPUT);//use mPython external interrupt, connect INTA to pin 0 of mPython.
  pinMode(P1, INPUT);//use mPython external interrupt, connect INTB to pin 1 of mPython.
  #else
  pinMode(2, INPUT);//use UNO external interrupt 0
  pinMode(3, INPUT);//use UNO external interrupt 1
  #endif
 
  /*wait for the chip to be initialized completely, and then exit*/
  while(mcp.begin() != 0){
    Serial.println("Initialization of the chip failed, please confirm that the chip connection is correct!");
    delay(1000);
  }
  /*Parameter mode, the available parameter is shown below:
  eLowLevel              eHighLevel              eRising                eFalling                  eChangeLevel
  Low-level interrupt    High-level interrupt    Rising edge interrupt  Falling edge interrupt    Double edge interrupts 
  Parameter cb interrupt service function(with parameter)
  Prototype void func(int)
  */
  mcp.pinModeInterrupt(/*pin = */mcp.eGPA0, /*mode = */mcp.eHighLevel, /*cb = */gpa0CB);//digital pin 0(eGPA0), interrupt in High level. Generate an interrupt when pin 0 is in High level state.INTA output High level.
  mcp.pinModeInterrupt(/*pin = */mcp.eGPB7, /*mode = */mcp.eChangeLevel, /*cb = */gpb7CB);//digital pin 15(eGPB7), double edge interrupts. Generate an interrupt when the status of Pin 15 changes. INTB output High level.
 
  #ifdef ARDUINO_ARCH_MPYTHON // 
  /* mPython Interrupt Pin vs Interrupt NO
   * -------------------------------------------------------------------------------------
   * |                    |  DigitalPin  |        P0~P20 can be used as external interrupt|
   * |    mPython           |--------------------------------------------------------------|
   * |                    | Interrupt No |  use digitalPinToInterrupt(Pn) to query interrupt number     |
   * |-----------------------------------------------------------------------------------|
   */
  attachInterrupt(digitalPinToInterrupt(P0)/*query Interrupt NO of P0*/,notifyA,RISING);//Enable the external interrupt of mPython P0; rising edge trigger; connect INTA to P0
  attachInterrupt(digitalPinToInterrupt(P1)/*query Interrupt NO of P1*/,notifyB,RISING);//Enable the external interrupt of mPython P1; rising edge trigger; connect INTB to P1
  #else
  /* Main-board of AVR series    Interrupt Pin vs Interrupt NO
   * ---------------------------------------------------------------------------------------
   * |                                        |  DigitalPin  | 2  | 3  |                   |
   * |    Uno, Nano, Mini, other 328-based    |--------------------------------------------|
   * |                                        | Interrupt No | 0  | 1  |                   |
   * |-------------------------------------------------------------------------------------|
   * |                                        |    Pin       | 2  | 3  | 21 | 20 | 19 | 18 |
   * |               Mega2560                 |--------------------------------------------|
   * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  | 5  |
   * |-------------------------------------------------------------------------------------|
   * |                                        |    Pin       | 3  | 2  | 0  | 1  | 7  |    |
   * |    Leonardo, other 32u4-based          |--------------------------------------------|
   * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  |    |
   * |--------------------------------------------------------------------------------------
   */
  /* microbit  Interrupt Pin vs Interrupt NO
   * ---------------------------------------------------------------------------------------------------------------
   * |                                                   |  DigitalPin  |    P0~P20 can be used as external interrupt           |
   * |                  microbit                         |---------------------------------------------------------|
   * |(when used as external interrupt, do not need to set it to input mode via pinMode)  | Interrupt No | Interrupt NO is pin value, for instance, the Interrupt NO of P0 is 0, P1 is 1. |
   * |-------------------------------------------------------------------------------------------------------------|
   */
  attachInterrupt(/*Interrupt NO*/0,notifyA,RISING);//Enable external interrupt 0, connect INTA to the main-controller's digital pin: UNO(2),Mega2560(2),Leonardo(3),microbit(P0)
  attachInterrupt(/*Interrupt NO*/1,notifyB,RISING);//Enable external interrupt 1, connect INTB to the main-controller's digital pin: UNO(3),Mega2560(3),Leonardo(2),microbit(P1)
  #endif
}
/*Interrupt service function*/
void notifyA(){
  intFlagA = true;
}
void notifyB(){
  intFlagB = true;
}
 
void loop() {
  if(intFlagA){
    intFlagA = false;
    /*pollInterrupts function is used to poll if an interrupt occurs on a port group 
    parameter group, the available parameter is shown below: (default value: eGPIOALL):
     eGPIOA        eGPIOB         eGPIOALL
     Port groupA  Port groupB   Port groupA+B
    */
    mcp.pollInterrupts(/*group = */mcp.eGPIOA);
  }
  if(intFlagB){
    intFlagB = false;
    mcp.pollInterrupts(/*group = */mcp.eGPIOB);
  }
}
  • Résultats attendus

Appuyez sur le bouton sur PAO ou PB7, le résultat suivant sera imprimé sur le port série.

Connexion en cascade

Schema du DFT0626 - MCP23017 DFROBOT

Doc technique MCP23017

Programme APC220 (Sans fil) et MCP23017 et ESP32

Emmision_ESP32_LED_BP_APC220_MCP23017.ino
#include <DFRobot_MCP23017.h>
#include <HardwareSerial.h>
 
HardwareSerial Sender(2);   // Define a Serial port instance called 'Sender' using serial port 1
 
#define Sender_Txd_pin 17
#define Sender_Rxd_pin 16
 
int EtatLED = 0;
int  EtatBP = 0;
int  EtatPrecd = 0;
int EtatLEDPrecd =0;
bool Unefois = false;
 
 
DFRobot_MCP23017 mcp027(Wire, /*addr =*/0x27);//constructor, change the Level of A2, A1, A0 via DIP switch to revise I2C address within 0x20~0x27
 
//Prepare: connect the LED to a digital pin of IO expansion board(eg:eGPA7)
void setup(void)
{
 Serial.begin(115200);                                             // Define and start serial monitor
 Sender.begin(9600, SERIAL_8N1, Sender_Txd_pin, Sender_Rxd_pin);
 
  while(mcp027.begin() != 0){
    Serial.println("Initialization of the chip failed, please confirm that the chip connection is correct!");
     delay(1000);
  }
 
 
  mcp027.pinMode(/*pin = */mcp027.eGPB0, /*mode = */OUTPUT);
  mcp027.pinMode(/*pin = */mcp027.eGPA0, /*mode = */INPUT);
 
}
 
void loop(void)
{
      EtatPrecd = EtatBP ;
      EtatBP = mcp027.digitalRead(/*pin = */mcp027.eGPA0);
      delay(100);
      //Serial.print("EtatBP  :");
      //Serial.println(EtatBP);
      /*delay(1000);
      Serial.print("verrou1  :");
      Serial.println(verrou);*/
 
 if  ((EtatPrecd == 1) && (EtatBP == 0 )) { 
      Serial.print("BP pressé  :");
      //Serial.println(verrou);
      EtatLEDPrecd = EtatLED;
      EtatLED = !EtatLED;
      Serial.print("EtatLED  :");
      Serial.println(EtatLED);
      }  
  if(EtatLED == 0 ){
  mcp027.digitalWrite(mcp027.eGPB0, LOW); 
  if ( Unefois == false ) {
      Sender.print("allume"); 
      Unefois = !Unefois ;
  }   
  Serial.print("Valeur transmise avec appui BP  : ");
  Serial.println(EtatLED );
  delay(100);
 
  }
  else if  (( EtatLED ==1 ) && (EtatLEDPrecd == 0)) {
  mcp027.digitalWrite(mcp027.eGPB0, HIGH);
  //Sender.print(EtatLED ); 
  if ( Unefois == true) {
      Sender.print("eteint"); 
      Unefois = !Unefois ;
  }   
  Serial.print("Valeur transmise sans appui BP   : ");
  Serial.println(EtatLED );
  delay(100);
  }
 
}
/home/chanteri/www/fablab37110/data/pages/start/arduino/mcp23017/dfrobot.txt · Dernière modification : 2023/01/27 16:08 de 127.0.0.1