Ligne 59 : |
Ligne 59 : |
| | | |
| == La classe I2C == | | == La classe I2C == |
| + | I2C est un protocole de communication 2-fils permettant à des périphérique de communiquer ensembles. |
| | | |
− | I2C is a two-wire protocol for communicating between devices. At the physical level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.
| + | Sur le plan physique, cela consiste en deux fils: SCL et SDA, la ligne d'horloge (SCL) et ligne de donnée (SDA). |
| | | |
− | I2C objects are created attached to a specific bus. They can be initialised when created, or initialised later on: | + | Les objets I2C son créés en étant rattaché spécifiquement sur un bus. Le bus peut être initialisé à la création de l'objet --ou-- initialisé plus tard: |
| | | |
| + | Voici quelques exemples: |
| <nowiki>from pyb import I2C | | <nowiki>from pyb import I2C |
| | | |
− | i2c = I2C(1) # create on bus 1 | + | i2c = I2C(1) # créé sur le bus 1 |
− | i2c = I2C(1, I2C.MASTER) # create and init as a master | + | i2c = I2C(1, I2C.MASTER) # Créé et initialisé comme maître (master) |
− | i2c.init(I2C.MASTER, baudrate=20000) # init as a master | + | i2c.init(I2C.MASTER, baudrate=20000) # Initialisé comme maître (master) |
− | i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address | + | i2c.init(I2C.SLAVE, addr=0x42) # Initialisé comme esclave (à l'adresse 0x42) |
− | i2c.deinit() # turn off the peripheral</nowiki> | + | i2c.deinit() # Désactive le périphérique (bus I2C)</nowiki> |
| | | |
− | Printing the i2c object gives you information about its configuration.
| + | Afficher l'objet i2c vous permet d'avoir des informations à propos de sa configuration. |
| | | |
− | Basic methods for slave are send and recv:
| + | === En mode esclave === |
| + | Les méthodes de base pour un esclave I2C sont: |
| + | * {{fname|send}} |
| + | * {{fname|recv}} |
| | | |
− | <nowiki>i2c.send('abc') # send 3 bytes | + | <nowiki>i2c.send('abc') # esclave envoyant 3 bytes/octets |
− | i2c.send(0x42) # send a single byte, given by the number | + | i2c.send(0x42) # esclave envoyant un seul byte/octet (fourni en valeur numérique) |
− | data = i2c.recv(3) # receive 3 bytes</nowiki> | + | data = i2c.recv(3) # esclave réceptionnant 3 bytes/octets</nowiki> |
| | | |
− | To receive inplace, first create a bytearray:
| + | Pour faire une réception ''in situ'', il faut premièrement créer un {{fname|bytearray}}: |
| | | |
− | <nowiki>data = bytearray(3) # create a buffer | + | <nowiki>data = bytearray(3) # Création d'un buffer (mémoire tampon) |
− | i2c.recv(data) # receive 3 bytes, writing them into data</nowiki> | + | i2c.recv(data) # Réception de 3 bytes/octets, écriture dans "data"</nowiki> |
| | | |
− | You can specify a timeout (in ms):
| + | Vous pouvez spécifier un temps d'attente maximum avec le paramètre ''timeout'' (en ms): |
| | | |
− | <nowiki>i2c.send(b'123', timeout=2000) # timout after 2 seconds</nowiki> | + | <nowiki>i2c.send(b'123', timeout=2000) # timeout après 2 secondes</nowiki> |
| | | |
− | A master must specify the recipient's address:
| + | === En mode maître === |
| + | Lorsqu'un maître (''master'') envoi des données à un périphérique doit spécifier l'adresse du destinataire: |
| | | |
| <nowiki>i2c.init(I2C.MASTER) | | <nowiki>i2c.init(I2C.MASTER) |
− | i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42 | + | i2c.send('123', 0x42) # envoyer 3 bytes/octets à l'esclave ayant l'adresse 0x42 |
− | i2c.send(b'456', addr=0x42) # keyword for address</nowiki> | + | i2c.send(b'456', addr=0x42) # Utilisation du mot clé "addr" pour indiquer l'adresse</nowiki> |
| | | |
− | Master also has other methods:
| + | Le maître dispose également d'autres méthodes: |
| | | |
− | <nowiki>i2c.is_ready(0x42) # check if slave 0x42 is ready | + | <nowiki>i2c.is_ready(0x42) # vérifié si l'esclave 0x42 est prêt |
− | i2c.scan() # scan for slaves on the bus, returning | + | i2c.scan() # Scanne les esclaves du bus et retourne une |
− | # a list of valid addresses | + | # liste avec les adresses valides |
− | i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42, | + | i2c.mem_read(3, 0x42, 2) # Lecture de 3 bytes/octets depuis la mémoire |
− | # starting at address 2 in the slave | + | # de l'esclave 0x42, en démarrant à l'adresse |
| + | # 2 dans l'esclave |
| i2c.mem_write('abc', 0x42, 2, timeout=1000)</nowiki> | | i2c.mem_write('abc', 0x42, 2, timeout=1000)</nowiki> |
| | | |