Modifications

Sauter à la navigation Sauter à la recherche
5 526 octets ajoutés ,  26 septembre 2015 à 10:48
aucun résumé de modification
Ligne 57 : Ligne 57 :     
Par conséquent, le bus SPI du skin X n'est pas tolérant à 5 Volts.
 
Par conséquent, le bus SPI du skin X n'est pas tolérant à 5 Volts.
 +
 +
== La classe I2C ==
 +
 +
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.
 +
 +
I2C objects are created attached to a specific bus. They can be initialised when created, or initialised later on:
 +
 +
<nowiki>from pyb import I2C
 +
 +
i2c = I2C(1)                        # create on bus 1
 +
i2c = I2C(1, I2C.MASTER)            # create and init as a master
 +
i2c.init(I2C.MASTER, baudrate=20000) # init as a master
 +
i2c.init(I2C.SLAVE, addr=0x42)      # init as a slave with given address
 +
i2c.deinit()                        # turn off the peripheral</nowiki>
 +
 +
Printing the i2c object gives you information about its configuration.
 +
 +
Basic methods for slave are send and recv:
 +
 +
<nowiki>i2c.send('abc')      # send 3 bytes
 +
i2c.send(0x42)      # send a single byte, given by the number
 +
data = i2c.recv(3)  # receive 3 bytes</nowiki>
 +
 +
To receive inplace, first create a bytearray:
 +
 +
<nowiki>data = bytearray(3)  # create a buffer
 +
i2c.recv(data)      # receive 3 bytes, writing them into data</nowiki>
 +
 +
You can specify a timeout (in ms):
 +
 +
<nowiki>i2c.send(b'123', timeout=2000)  # timout after 2 seconds</nowiki>
 +
 +
A master must specify the recipient's address:
 +
 +
<nowiki>i2c.init(I2C.MASTER)
 +
i2c.send('123', 0x42)        # send 3 bytes to slave with address 0x42
 +
i2c.send(b'456', addr=0x42)  # keyword for address</nowiki>
 +
 +
Master also has other methods:
 +
 +
<nowiki>i2c.is_ready(0x42)          # check if slave 0x42 is ready
 +
i2c.scan()                  # scan for slaves on the bus, returning
 +
                            #  a list of valid addresses
 +
i2c.mem_read(3, 0x42, 2)    # read 3 bytes from memory of slave 0x42,
 +
                            #  starting at address 2 in the slave
 +
i2c.mem_write('abc', 0x42, 2, timeout=1000)</nowiki>
 +
 +
=== Constructeur ===
 +
{fname|I2C(bus, ...)}}
 +
 +
Construct an I2C object on the given bus. bus can be 1 or 2. With no additional parameters, the I2C object is created but not initialised (it has the settings from the last initialisation of the bus, if any). If extra arguments are given, the bus is initialised. See init for parameters of initialisation.
 +
 +
The physical pins of the I2C busses are:
 +
* I2C(1) is on the X position: (SCL, SDA) = (X9, X10) = (PB6, PB7)
 +
* I2C(2) is on the Y position: (SCL, SDA) = (Y9, Y10) = (PB10, PB11)
 +
 +
=== Méthodes ===
 +
==== i2c.deinit() ====
 +
Turn off the I2C bus.
 +
 +
==== i2c.init(...) ====
 +
{{fname|i2c.init(mode, *, addr=0x12, baudrate=400000, gencall=False)}}
 +
 +
Initialise the I2C bus with the given parameters:
 +
* '''mode''' must be either I2C.MASTER or I2C.SLAVE
 +
* '''addr''' is the 7-bit address (only sensible for a slave)
 +
* '''baudrate''' is the SCL clock rate (only sensible for a master)
 +
* '''gencall''' is whether to support general call mode
 +
 +
==== i2c.is_ready(...) ====
 +
{{fname|i2c.is_ready(addr)}}
 +
 +
Check if an I2C device responds to the given address. Only valid when in master mode.
 +
 +
==== i2c.mem_read(....) ====
 +
{{fname|i2c.mem_read(data, addr, memaddr, timeout=5000, addr_size=8)}}
 +
 +
Read from the memory of an I2C device:
 +
* '''data''' can be an integer or a buffer to read into
 +
* '''addr''' is the I2C device address
 +
* '''memaddr''' is the memory location within the I2C device
 +
* '''timeout''' is the timeout in milliseconds to wait for the read
 +
* '''addr_size''' selects width of memaddr: 8 or 16 bits
 +
 +
Returns the read data. This is only valid in master mode.
 +
 +
==== i2c.mem_write(...) ====
 +
{{fname|i2c.mem_write(data, addr, memaddr, timeout=5000, addr_size=8)}}
 +
 +
Ecrit dans la mémoire d'un périphérique I2C.
 +
 +
* '''data''' est entier ou un ''buffer'' contenant les données à envoyer au périphérique I2C.
 +
* '''addr''' est l'adresse du périphérique I2C.
 +
* '''memaddr''' est la position mémoire (adresse mémoire) dans le périphérique I2C.
 +
* '''timeout''' est le temps d'attente max (millisecondes) pour démarrer l'opération d'écriture.
 +
* '''addr_size''' permet de sélectionner la taille de {{fname|memaddr}}: 8 ou 16 bits
 +
 +
Retourne {{fname|None}}.
 +
 +
Uniquement valable en mode ''master''.
 +
 +
==== i2c.recv(...) ====
 +
{{fname|i2c.recv(recv, addr=0x00, timeout=5000)}}
 +
 +
Recevoir des données sur le bus:
 +
* '''recv''' peut être un entier représentant le nombre d'octets/bytes à recevoir --OU-- un ''buffer'' mutable (''modifiable'') qui sera remplis avec les octets reçus.
 +
* '''addr''' l'adresse depuis laquelle les données seront reçues (uniquement valable en mode ''master'')
 +
* '''timeout''' le temps d'attente max pour recevoir les données. ''TimeOut'' s'exprimant en millisecondes.
 +
 +
Return value: if recv is an integer then a new buffer of the bytes received, otherwise the same buffer that was passed in to recv.
 +
i2c.scan()
 +
 +
==== i2c.scan() ====
 +
Scanne toutes les adresses du Bus I2C de 0x01 à 0x7f et retourne la liste des adresses offrant une réponse.
 +
 +
Cette instruction est uniquement utilisable en mode ''master''.
 +
 +
==== i2c.send(...) ====
 +
{{fname|i2c.send(send, addr=0x00, timeout=5000)}}
 +
 +
Envoi des données sur le bus:
 +
* '''send''' est la donnée à envoyer (un entier/integer ou un objet ''buffer'')
 +
* '''addr''' est l'adresse du destinataire (uniquement nécessaire en mode ''master'')
 +
* '''timeout''' est le temps d'attente max (''timeout'') pour l'envoi des données. Temps en millisecondes
 +
 +
Return value: None.
 +
 +
=== Constantes ===
 +
* {{fname|I2C.MASTER}} - pour l'initialisation du bus en mode {{fname|master}} (maître).
 +
* {{fname|I2C.SLAVE}} - pour l'initialisation du bus en mode {{fname|slave}} (esclave).
    
{{MicroPython-I2C-TRAILER}}
 
{{MicroPython-I2C-TRAILER}}
29 917

modifications

Menu de navigation