Pi-WiringPi-Exemple-Quick2Wire-GPIOExtension

De MCHobby - Wiki
Révision datée du 30 septembre 2013 à 12:07 par Admin (discussion | contributions)
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Module extension GPIO à 16 broches

Vous pouvez ignorer cette section si vous ne disposez pas encore de module Quick2Wire.

 
Source: wiringPi.com
La carte d'extension Quick2Wire à 16 broches digitale. Le port Port A, bit 0 connecté sur une LED à l'aide d'une résistance de 220Ω.

La carte d'extension GPIO utilise un circuit intégré MCP23017. Ce fournit 16 broches d'entrée/sortie additionnelles que vous pouvez utiliser dans vos propres programmes de la même façon que si vous utiliser une broche du GPIO de votre Raspberry-Pi.

Résistance de protection

A l'opposé de la carte d'extension 8 entrée/sortie de Quick2Wire, les IO (entrées/sorties) de la carte utilisé dans cet exemple ne disposent pas de résistance de protection ni de protection contre les surtensions.

Vous devez faire attention à n'utiliser que des périphériques 3.3v..

Si vous désirez utiliser des périphériques en logique 5v, vous pouvez ajouter un convertisseur de niveau logique comme celui-ci ou celui-là.

Raccordements

Pour faire quelques tests, nous allons commencer par raccorder une LED connectée que le Port A, bit 0.

Le cirThe circuit above uses a 220Ω resistor then the LED then a connection to the GND/0v pin on the main board.

Les commandes

gpio -x mcp23017:100:0x20 mode 100 out
gpio -x mcp23017:100:0x20 write 100 1
gpio -x mcp23017:100:0x20 write 100 0

should turn the LED on then off again. If not, check that its wired up correctly – that the LED is the right way round and that the MCP23017 appears as address 0×20 in the output of the gpio i2cd command. (If it’s something other than 0×20 then substitute that for the 0×20 in the command above).

Que fait la commande?

What those gpio commands are doing?

The -x flag tells gpio to use an expansion module – the mcp23017 in this instance. The parameters (separated by colons) for the mcp23017 module are the new pin base number (100 in this case), and the I2C address of the chip (0×20). At that point, you can issue most standard gpio commands.

Script de test

We can use a variant of the button push script that we used in the testing on the main board too:

gpio mode 0 in
while true; do gpio -x mcp23017:100:0x20 write 100 `gpio read 0`; done

Programme Blink

In the q2w directory in the wiringPi examples (exemple) directory, you’ll find a program called blink-io.c – this is the blink program adapted to use the 16-bit GPIO expander board. This program will blink the on-board LED at the same time as the one connected to the IO expander board.

Study it to compare with the standard blink.c program. Compile and run with:

gcc -Wall -oblink-io blink-io.c -lwiringPi
sudo ./blink-io

Test Avancé

For more advanced testing, I have a small board with a block of 10 LEDs on and a little push-button switch:

 
Source: wiringPi.com

Here is a variant of the program I used when testing wiringPi v2′s ability to use the mcp23017 GPIO expansion chip:

/*
 * binary.c:
 *      Using the Quick 2 wire 16-bit GPIO expansion board
 *
 * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
 ***********************************************************************
 */

#include <stdio.h>
#include <wiringPi.h>
#include <mcp23017.h>

#define Q2W_BASE        100

int main (void)
{
  int i, bit ;

// Enable the on-goard GPIO

  wiringPiSetup () ;

// Add in the mcp23017 on the q2w board

  mcp23017Setup (Q2W_BASE, 0x20) ;

  printf ("Raspberry Pi - Quick2Wire MCP23017 Test\n") ;

// On-board button Input:

  pinMode (0, INPUT) ;

// First 10 pins on q2w board as outputs:

  for (i = 0 ; i < 10 ; ++i)
    pinMode (Q2W_BASE + i, OUTPUT) ;

// Last pin as an input with the internal pull-up enabled

  pinMode         (Q2W_BASE + 15, INPUT) ;
  pullUpDnControl (Q2W_BASE + 15, PUD_UP) ;

  for (;;)
  {
    for (i = 0 ; i < 1024 ; ++i)
    {
      for (bit = 0 ; bit < 10 ; ++bit)
        digitalWrite (Q2W_BASE + bit, i & (1 << bit)) ;

      while (digitalRead (0) == HIGH)           // While pushed
        delay (1) ;

      if (digitalRead (Q2W_BASE + 15) == HIGH)  // Not Pushed
        delay (100) ;
    }
  }
  return 0 ;
}

This program is called binary.c in the q2w directory in the wiringPi examples (exemples) directory. Compile and run with:

gcc -Wall -o binary binary.c -lwiringPi
sudo ./binary

Push the main button to stop the counter, and push the button on the LED board to make it go faster. The LED board is simply 10 LEDs and series resistors and a single push-button connected to the 0v/GND line.

Note that this program uses both buttons – the one on the main Quick2Wire board and the one on the little test board. The one on the main Q2W board returns 0 (or LOW) when not pushed and 1 (or HIGH) when pushed, but the one on the expansion board works the opposite way, and returns 1 when not pushed and 0 when pushed.

Conclusion

Using the Quick2Wire MCP23017 GPIO expander with wiringPi is as simple as telling wiringPi about the gpio expander, assigning it a new pin number base (for its 16 pins), then using digitalRead(), digitalWrite(), pinMode() as before.


Source: WiringPi.com. WiringPi est une libraire sous licence GPL écrite par Gordon Henderson. Crédit: wiringpi.com

Traduit de l'anglais par Meurisse D. pour MCHobby.be

Traduit avec l'accord de Gordon Henderson, créateur de wiringPi - Translated with authorization of Gordon Henderson, creator of wiringPi

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.