Pi-WiringPi-Reference-I2CBus

De MCHobby - Wiki
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.

La librairie I2C

WiringPi includes a library which can make it easier to use the Raspberry Pi’s on-board I2C interface.

Before you can use the I2C interface, you may need to use the gpio utility to load the I2C drivers into the kernel:

gpio load i2c

If you need a baud rate other than the default 100Kbps, then you can supply this on the command-line:

gpio load i2c 1000

will set the baud rate to 1000Kbps – ie. 1,000,000 bps. (K here is times 1000)

To use the I2C library, you need to:

#include <wiringPiI2C.h>

in your program. Programs need to be linked with -lwiringPi as usual.

You can still use the standard system commands to check the I2C devices, and I recommend you do so – e.g. the i2cdetect program. Just remember that on a Rev 1 Raspberry pi it’s device 0, and on a Rev. 2 it’s device 1. e.g.

i2cdetect -y 0 # Rev 1
i2cdetect -y 1 # Rev 2

Note that you can use the gpio command to run the i2cdetect command for you with the correct parameters for your board revision:

 gpio i2cdetect

is all that’s needed.

Fonctions

Voici les fonctions disponibles dans la librairie I2C.

wiringPiI2CSetup

int wiringPiI2CSetup(int devId);

This initialises the I2C system with your given device identifier. The ID is the I2C number of the device and you can use the i2cdetect program to find this out. wiringPiI2CSetup() will work out which revision Raspberry Pi you have and open the appropriate device in /dev.

The return value is the standard Linux filehandle, or -1 if any error – in which case, you can consult errno as usual.

E.g. the popular MCP23017 GPIO expander is usually device Id 0×20, so this is the number you would pass into wiringPiI2CSetup().


wiringPiI2CRead

int wiringPiI2CRead(int fd);

Simple device read. Some devices present data when you read them without having to do any register transactions.

wiringPiI2CWrite

int wiringPiI2CWrite(int fd, int data);

Simple device write. Some devices accept data this way without needing to access any internal registers.

wiringPiI2CWriteReg8 et wiringPiI2CWriteReg16

int wiringPiI2CWriteReg8 (int fd, int reg, int data);
int wiringPiI2CWriteReg16(int fd, int reg, int data);

These write an 8 or 16-bit data value into the device register indicated.

wiringPiI2CReadReg8 et wiringPiI2CReadReg16

int wiringPiI2CReadReg8 (int fd, int reg);
int wiringPiI2CReadReg16(int fd, int reg);

These read an 8 or 16-bit value from the device register indicated.


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.