TPs Protocoles Communication - 3 Projets

TPs Protocoles Communication

3 Projets : UART, I2C, SPI

📡 UART 🔌 I2C ⚡ SPI
📡

TP1 : Communication UART

Liaison serie asynchrone entre deux cartes

🎯 Objectifs

  • Comprendre le protocole UART (Start, Data, Parity, Stop)
  • Configurer la communication serie (baud rate, format)
  • Établir une communication bidirectionnelle
  • Analyser les trames avec oscilloscope/analyseur logique

Schema de câblage

    CARTE 1 (Master)              CARTE 2 (Slave)
    ┌─────────────┐               ┌─────────────┐
    │             │               │             │
    │         TX ─┼───────────────┼─ RX         │
    │             │               │             │
    │         RX ─┼───────────────┼─ TX         │
    │             │               │             │
    │        GND ─┼───────────────┼─ GND        │
    └─────────────┘               └─────────────┘

    Configuration: 115200 baud, 8N1 (8 bits, No parity, 1 stop)
                    

Code Arduino : Émetteur

// TP1 : UART Émetteur

void setup() {
    Serial.begin(115200);
}

void loop() {
    // Envoyer donnees capteur
    int sensorValue = analogRead(A0);
    
    // Format: "DATA:1234\n"
    Serial.print("DATA:");
    Serial.println(sensorValue);
    
    delay(500);
}

Recepteur

// TP1 : UART Recepteur

String inputString = "";

void setup() {
    Serial.begin(115200);
}

void loop() {
    while (Serial.available()) {
        char c = Serial.read();
        
        if (c == '\n') {
            // Traiter la ligne recue
            if (inputString.startsWith("DATA:")) {
                int value = inputString.substring(5).toInt();
                Serial.print("Recu: ");
                Serial.println(value);
            }
            inputString = "";
        } else {
            inputString += c;
        }
    }
}
🔌

TP2 : Communication I2C

Bus serie synchrone multi-peripheriques

Schema I2C

                VCC
                 │
               ┌─┴─┐ ┌─┴─┐
               │4K7│ │4K7│  Resistances pull-up
               └─┬─┘ └─┬─┘
                 │     │
    ┌──────┬─────┼─────┼─────┬──────┬──────┐
    │      │     │     │     │      │      │
    │   ┌──┴──┐  │  ┌──┴──┐  │   ┌──┴──┐   │
    │   │Master│  │  │Slave│  │   │Slave│   │
    │   │     │  │  │0x27 │  │   │0x3C │   │
    │   │ SDA─┼──┴──┼─SDA │  │   │ SDA─┼───┘
    │   │ SCL─┼─────┼─SCL │  │   │ SCL─┼───┐
    │   └─────┘     └─────┘  │   └─────┘   │
    │                        │             │
    GND──────────────────────┴─────────────┘

    SDA = Data, SCL = Clock
    Adresses 7 bits: 0x27 (LCD), 0x3C (OLED), etc.
                    

Code : Scanner I2C

// Scanner d'adresses I2C
#include <Wire.h>

void setup() {
    Wire.begin();
    Serial.begin(115200);
    Serial.println("Scanner I2C...");
    
    for (byte addr = 1; addr < 127; addr++) {
        Wire.beginTransmission(addr);
        if (Wire.endTransmission() == 0) {
            Serial.print("Device at 0x");
            Serial.println(addr, HEX);
        }
    }
    Serial.println("Scan termine.");
}

void loop() {}

Code : Master/Slave I2C

// MASTER - Envoie temperature au slave
#include <Wire.h>

void setup() {
    Wire.begin();  // Master
}

void loop() {
    int temp = analogRead(A0) / 10;  // Simuler temperature
    
    Wire.beginTransmission(0x08);  // Adresse slave
    Wire.write(temp);
    Wire.endTransmission();
    
    delay(1000);
}

// SLAVE - Recoit et affiche
#include <Wire.h>

void receiveEvent(int bytes) {
    while (Wire.available()) {
        int temp = Wire.read();
        Serial.print("Temp: ");
        Serial.println(temp);
    }
}

void setup() {
    Wire.begin(0x08);  // Slave adresse 0x08
    Wire.onReceive(receiveEvent);
    Serial.begin(115200);
}

void loop() {}

TP3 : Communication SPI

Bus serie synchrone haute vitesse

Schema SPI

    MASTER                        SLAVE
    ┌─────────────┐              ┌─────────────┐
    │             │              │             │
    │       MOSI ─┼──────────────┼─ MOSI       │
    │             │              │             │
    │       MISO ─┼──────────────┼─ MISO       │
    │             │              │             │
    │        SCK ─┼──────────────┼─ SCK        │
    │             │              │             │
    │         SS ─┼──────────────┼─ SS/CS      │
    │             │              │             │
    └─────────────┘              └─────────────┘

    MOSI = Master Out Slave In
    MISO = Master In Slave Out
    SCK  = Serial Clock
    SS   = Slave Select (actif LOW)
                    

Code : Communication SPI

// TP3 : SPI Master
#include <SPI.h>

const int CS_PIN = 10;

void setup() {
    pinMode(CS_PIN, OUTPUT);
    digitalWrite(CS_PIN, HIGH);
    
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV16);  // 1MHz
    
    Serial.begin(115200);
}

byte spiTransfer(byte data) {
    digitalWrite(CS_PIN, LOW);
    byte result = SPI.transfer(data);
    digitalWrite(CS_PIN, HIGH);
    return result;
}

void loop() {
    // Envoyer commande, recevoir reponse
    byte response = spiTransfer(0xAA);
    
    Serial.print("Response: 0x");
    Serial.println(response, HEX);
    
    delay(500);
}

Tableau comparatif

CritereUARTI2CSPI
TypeAsynchroneSynchroneSynchrone
Fils2 (TX/RX)2 (SDA/SCL)4 (MOSI/MISO/SCK/SS)
Vitesse max~1 Mbps~3.4 Mbps~50 Mbps
Multi-slaveNonOui (adresses)Oui (CS multiples)
Full duplexOuiNonOui

TPs Protocoles Communication - UART, I2C, SPI