Modifications

Sauter à la navigation Sauter à la recherche
aucun résumé de modification
Ligne 1 : Ligne 1 :  
{{Pi-WiringPi-NAV}}
 
{{Pi-WiringPi-NAV}}
   −
== Intro ==
+
== Module extension GPIO à 16 broches ==
   −
{{bloc-etroit|text=xxx.  }}  
+
{{bloc-etroit|text=Vous pouvez ignorer cette section si vous ne disposez pas encore de module Quick2Wire.  }}  
   −
<nowiki></nowiki>
+
[[Fichier:Pi-WiringPi-Exemple-Quick2Wire-21.jpg|480px]]<br /><small>Source: [http://wirinPi.com wiringPi.com]<br />
 +
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Ω.</small>
   −
<nowiki></nowiki>
+
The GPIO expansion board uses the MCP23017 GPIO expansion chip. This provides 16 additional IO pins which you can use in your own programs in exactly the same way you might use the Pi’s on-board GPIO pins.
   −
<nowiki></nowiki>
+
=== Note ===
 +
Unlike the 8 IO pins on the Quick2Wire main board, these IO pins do not have any sort of series resistors or over voltage protection, so take care to only connect them to 3.3v devices.
   −
<nowiki></nowiki>
+
=== Raccordements ===
 +
To do some tests, we’ll start with a single LED connected to Port A, bit 0. The circuit above uses a 220Ω resistor then the LED then a connection to the GND/0v pin on the main board.
   −
<nowiki></nowiki>
+
== Les commandes ==
   −
  <nowiki></nowiki>
+
  <nowiki>gpio -x mcp23017:100:0x20 mode 100 out
 +
gpio -x mcp23017:100:0x20 write 100 1
 +
gpio -x mcp23017:100:0x20 write 100 0</nowiki>
   −
<nowiki></nowiki>
+
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?
   −
[[Fichier:Pi-WiringPi-Exemple-Quick2Wire-01.jpg|480px]]<br /><small>Source: [http://wirinPi.com wiringPi.com]</small>
+
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:
 +
 +
<nowiki>gpio mode 0 in
 +
while true; do gpio -x mcp23017:100:0x20 write 100 `gpio read 0`; done</nowiki>
 +
 +
== 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:
 +
 +
<nowiki>gcc -Wall -oblink-io blink-io.c -lwiringPi
 +
sudo ./blink-io</nowiki>
 +
 +
== Test Avancé ==
 +
For more advanced testing, I have a small board with a block of 10 LEDs on and a little push-button switch:
 +
 +
[[Fichier:Pi-WiringPi-Exemple-Quick2Wire-21.jpg|480px]]<br /><small>Source: [http://wirinPi.com wiringPi.com]</small>
 +
 +
Here is a variant of the program I used when testing wiringPi v2′s ability to use the mcp23017 GPIO expansion chip:
 +
 +
<nowiki>/*
 +
* 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 ;
 +
}</nowiki>
 +
 +
This program is called binary.c in the ''q2w'' directory in the wiringPi ''examples'' (exemples) directory. Compile and run with:
 +
 +
<nowiki>gcc -Wall -o binary binary.c -lwiringPi
 +
sudo ./binary</nowiki>
 +
 +
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.
    
{{Pi-WiringPi-TRAILER}}
 
{{Pi-WiringPi-TRAILER}}
29 917

modifications

Menu de navigation