Outils pour utilisateurs

Outils du site


start:arduino:mcp23017:dfrobot

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
start:arduino:mcp23017:dfrobot [2022/03/29 10:45] – [Exemple de code de sortie à 2 broches] gerardadminstart:arduino:mcp23017:dfrobot [2023/01/27 16:08] (Version actuelle) – modification externe 127.0.0.1
Ligne 2: Ligne 2:
  
 [[https://wiki.dfrobot.com/Gravity:%20MCP23017%20IIC%20to%2016%20Digital%20IO%20Expansion%20Module%20SKU:%20DFR0626|PCP23017 DFROBOT EN]] [[https://wiki.dfrobot.com/Gravity:%20MCP23017%20IIC%20to%2016%20Digital%20IO%20Expansion%20Module%20SKU:%20DFR0626|PCP23017 DFROBOT EN]]
 +
 +==== Achat ====
 +
 +[[https://www.gotronic.fr/art-module-d-expansion-i2c-gravity-dfr0626-31815.htm| Achat MCP23017 Gotronic  ]]
 +
 +[[https://shop.mchobby.be/fr/ci/218-mcp23017-extension-16-entree-sortie-i2c-3232100002180.html| Achat MCP23017 McHobby]]
 +
 +[[https://www.dfrobot.com/product-2002.html|Achat MCP23017 DFRobot ]]
  
 {{ :start:arduino:mcp23017:capture_du_2022-03-29_10-16-31.jpg?direct&400 |}} {{ :start:arduino:mcp23017:capture_du_2022-03-29_10-16-31.jpg?direct&400 |}}
Ligne 374: Ligne 382:
  
  
 +      * **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.
 +
 +{{ :start:arduino:mcp23017:aa62e2f3d0876cac7a28fc17bf3ef7f2.png?direct&400 |}}
 +
 +====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.
 +
 +<code c 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);
 +  }
 +}
 +</code>
 +
 +
 +
 +    * **Résultats attendus**
 +
 +Appuyez sur le bouton sur PAO ou PB7, le résultat suivant sera imprimé sur le port série.
 +
 +{{ :start:arduino:mcp23017:0df48411de0ff0f98a28de41cb1254eb.png?direct&400 |}}
 +
 +====Connexion en cascade====
 +
 +{{ :start:arduino:mcp23017:d3c707ef60a2006d9fc16a974c2d42e9.jpeg?direct&400 |}}
 +
 +{{ :start:arduino:mcp23017:f3259a825dac9d179941eef88353e3dd.jpeg?direct&400 |}}
 +
 +==== Schema du DFT0626 - MCP23017 DFROBOT ====
 +
 +{{ :start:arduino:mcp23017:aa62e2f3d0876cac7a28fc17bf3ef7f2.jpg?direct&400 |}}
 +
 +
 +==== Doc technique MCP23017 ====
 +
 +[[https://dfimg.dfrobot.com/nobody/wiki/9401e19f637db318a458f6f5f468413b.pdf|Doc technique MCP23017 .pdf EN]]
 +
 +
 +===== Programme APC220 (Sans fil) et MCP23017  et ESP32 =====
 +
 +<code c 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);
 +  }
 +
 +}
 +</code>
  
  
  
/home/chanteri/www/fablab37110/data/attic/start/arduino/mcp23017/dfrobot.1648543506.txt.gz · Dernière modification : 2023/01/27 16:08 (modification externe)