Outils pour utilisateurs

Outils du site


communication_protocole_i2c

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
communication_protocole_i2c [2022/09/07 08:45] – [Exemple entre Raspberry Pi et Arduino Leonardo] sergecommunication_protocole_i2c [2022/09/07 09:33] (Version actuelle) – [Communication avec le protocole I2C] serge
Ligne 1: Ligne 1:
 ====== Communication avec le protocole I2C ====== ====== Communication avec le protocole I2C ======
 +{{ :media_16:2_codeurs_ok.png?600 |}}
  
 =====Ressources===== =====Ressources=====
Ligne 7: Ligne 7:
   * **[[https://www.gammon.com.au/i2c|I2C par Nick Gammon]]** This post describes how the I2C (Inter-Integrated Circuit, or "Two-Wire") interface works, with particular reference to the Arduino Uno which is based on the ATmega328P microprocessor chip. A lot of the details however will be of more general interest.   * **[[https://www.gammon.com.au/i2c|I2C par Nick Gammon]]** This post describes how the I2C (Inter-Integrated Circuit, or "Two-Wire") interface works, with particular reference to the Arduino Uno which is based on the ATmega328P microprocessor chip. A lot of the details however will be of more general interest.
   * https://github.com/nickgammon/I2C_Anything   * https://github.com/nickgammon/I2C_Anything
 +
 +
 +=====Installation sur une Pi=====
 +
 +From https://gist.github.com/ribasco/c22ab6b791e681800df47dd0a46c7c3a
 +
 +<code bash>
 +sudo nano /boot/config.txt
 +
 +# Find the line containing dtparam=i2c_arm=on
 +# Add i2c_arm_baudrate=<new speed> (Separate with a Comma)
 +dtparam=i2c_arm=on,i2c_arm_baudrate=400000
 +</code>
 +
 +Reboot Raspberry Pi\\
 +Create a simple test script to verify the speed
 +
 +Switch to home directory 
 +  cd ~
 +Create shell script 
 +  nano i2cspeed.sh
 +Copy and paste the following lines of code
 +<code bash>
 +#!/bin/bash
 +var="$(xxd /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency | awk -F': ' '{print $2}')"
 +var=${var//[[:blank:].\}]/}
 +printf "%d\n" 0x$var
 +</code>
 +Change file permissions
 +  chmod +x i2cspeed.sh
 +
 +Execute test script
 +  ./i2cspeed.sh
 +
 +=====Installation sur Arduino=====
 +  * https://docs.arduino.cc/learn/communication/wire
 +  * https://radiostud.io/howto-i2c-communication-rpi/
 +  * [[https://www.arduino.cc/reference/en/language/functions/communication/wire/|la doc arduino pour la lib Wire]]
 +
 +=====smbus2=====
 +La lib I2C python
 +  * https://github.com/kplindegaard/smbus2
 +
  
 =====Exemple à revérifier===== =====Exemple à revérifier=====
   * https://github.com/sergeLabo/furuta/tree/main/doc/NickGammon_I2C l'exemple de Nick simplifié   * https://github.com/sergeLabo/furuta/tree/main/doc/NickGammon_I2C l'exemple de Nick simplifié
- 
  
 =====Exemple entre Raspberry Pi et Arduino Leonardo===== =====Exemple entre Raspberry Pi et Arduino Leonardo=====
Ligne 20: Ligne 62:
 from smbus2 import SMBus from smbus2 import SMBus
  
- +addr = 44
-addr = 42+
 bus = SMBus(1) bus = SMBus(1)
-sleep(1)+sleep(0.1)
 E = 0 E = 0
  
Ligne 29: Ligne 70:
     global E     global E
     try:     try:
-        b2 = bus.read_i2c_block_data(addr, 0, 4)+        b2 = bus.read_i2c_block_data(addr, 0, 2)
         b = bytearray(b2)         b = bytearray(b2)
         pos = (b[0] << 8) + b[1]         pos = (b[0] << 8) + b[1]
Ligne 41: Ligne 82:
 e = 0 e = 0
 nbr = 1000 nbr = 1000
-print("Speed Test ...")+tempo = 0.03 
 +pos = 0
 while n < nbr: while n < nbr:
     n += 1     n += 1
     pos1 = request()     pos1 = request()
-    sleep(0.01) +    # #if n % 200 == 0: 
-    pos2 = request(+        # #print(pos1
-    sleep(0.025+    if pos1 !pos: 
-    if pos1 and pos2 and nbr % 2 == 0+        pos = pos1 
-        print(pos2 - pos1+        print(pos
- +    sleep(tempo) 
- +d = ((((time() - t)/nbr) - tempo))*1000 
-d = ((((time() - t)/nbr) - 0.035)*1000) +print("Temps de transmission", round(d, 2), "ms")
-print("Temps de transmission", round(d, 1), "ms")+
 print("Nombre d'erreur:", E) print("Nombre d'erreur:", E)
 bus.close() bus.close()
 +"""
 +freq = 100 000 Hz avec adaptateur
 +Temps de transmission 0.93 ms
 +Nombre d'erreur: 2
 +freq = 100 000 Hz avec adaptateur et pull up
 +Temps de transmission 1.02 ms
 +Nombre d'erreur: 2
 +"""
 </code> </code>
  
Ligne 61: Ligne 110:
 <code C++> <code C++>
 #include <Wire.h> #include <Wire.h>
-#include <AS5048A.h>+#include <Encoder.h>
  
-// 10 SS +// Interrupt 
-// false = no debug +const int ENC_Z 7; 
-AS5048A angleSensor(10false);+// Objet Encoder 
 +Encoder myEnc(01)
 +// I2C 
 +const byte MY_ADDRESS = 44; 
 +// Variable globale 
 +long val = 0; 
 +int hi = 0; 
 +int lo = 0;
  
-const byte MY_ADDRESS = 42; +void index_ISR(){ 
-int val = 0;+    myEnc.write(0); 
 +}
  
-void setup() +void setup() { 
-  +    //Serial.begin(115200); 
-  // Capteurs Panauto +    // I2C 
-  angleSensor.begin();+    Wire.begin (MY_ADDRESS); 
 +    Wire.onRequest(requestEvent);
  
-  Wire.begin (MY_ADDRESS); +    pinMode(ENC_Z, INPUT_PULLUP); 
-  Wire.onRequest(requestEvent); +    attachInterrupt(digitalPinToInterrupt(ENC_Z), index_ISR, RISING); 
-  }+}
  
-void loop() {}+void loop() { 
 +    val = myEnc.read(); 
 +    delayMicroseconds(500); 
 +    //Serial.print("loop"); 
 +    //Serial.println(val); 
 +    //delay(100); 
 +    }
  
 void requestEvent () void requestEvent ()
-  +    
-  int ang angleSensor.getRotationInDegrees()*10; +    //val myEnc.read(); 
- +    byte buf [2]; 
-  val = ang+    buf [0] = val >> 8; 
-  byte buf [2]; +    buf [1] = val & 0xFF; 
-  buf [0] = val >> 8; +    Wire.write (buf, 2); 
-  buf [1] = val & 0xFF; +    }
-  Wire.write (buf, 2); +
-  }+
 </code> </code>
 =====Pull-up Resistors===== =====Pull-up Resistors=====
-https://learn.sparkfun.com/tutorials/pull-up-resistors +  * https://learn.sparkfun.com/tutorials/pull-up-resistors 
 +  * **[[https://www.gammon.com.au/forum/?id=10896&reply=5#reply5|Nick explique tout]]**
  
-{{:media_16:pull_up_resistor.png?400|}}+On tire le signal vers le **+** avec une résistance sur SDA et SDL: 
 +{{ :media_16:pull_up_resistor.png?200 |}}
  
  
communication_protocole_i2c.1662540353.txt.gz · Dernière modification : 2022/09/07 08:45 de serge