M4 : trame UART 8N1, câblage série croisé. M5 : bus I2C, trame I2C, bus SPI. M6 : tâches millis(), déroulé d'une ISR, modes de déclenchement, encodeur rotatif. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
3.4 KiB
Python
64 lines
3.4 KiB
Python
"""Module 4 — Communication série & débogage."""
|
|
from outils import boite, chronogramme, fleche, toile
|
|
from style import BLEU, ENCRE, GRIS, ORANGE, VERT, Figure
|
|
|
|
|
|
def trame_uart():
|
|
# 'A' = 0x41 = 0b01000001, envoyé bit de poids faible en premier
|
|
donnees = [(0x41 >> k) & 1 for k in range(8)]
|
|
bits = [1] + [0] + donnees + [1] + [1] # repos, start, D0..D7, stop, repos
|
|
points = [(i, b) for i, b in enumerate(bits)]
|
|
fig, ax, y = chronogramme([("TX", points)], 12, largeur_cm=16, hauteur_voie_cm=2.2)
|
|
ax.set_xticks([])
|
|
ax.spines["bottom"].set_visible(False)
|
|
noms = ["repos", "start"] + [f"D{k}" for k in range(8)] + ["stop", "repos"]
|
|
couleurs = [GRIS, ORANGE] + [BLEU] * 8 + [VERT, GRIS]
|
|
for i, (n, c) in enumerate(zip(noms, couleurs)):
|
|
ax.text(i + 0.5, 1.35, n, ha="center", fontsize=8, color=c, fontweight="bold")
|
|
if 2 <= i <= 9:
|
|
ax.text(i + 0.5, -0.35, str(bits[i]), ha="center", fontsize=9, color=ENCRE)
|
|
ax.axvline(i, ymin=0.12, ymax=0.83, color="#e5e7eb", lw=0.7, zorder=0)
|
|
ax.annotate("", xy=(2, 1.85), xytext=(10, 1.85), arrowprops=dict(arrowstyle="<->", lw=0.9, color=BLEU))
|
|
ax.text(6, 1.95, "8 bits de données : 'A' = 0x41, bit de poids faible (D0) en premier",
|
|
ha="center", va="bottom", fontsize=8, color=BLEU)
|
|
ax.annotate("", xy=(1, -0.75), xytext=(2, -0.75), arrowprops=dict(arrowstyle="<->", lw=0.9))
|
|
ax.text(2.15, -0.75, "1 bit = 1 / 115200 s ≈ 8,7 µs", va="center", fontsize=8, color=ENCRE)
|
|
ax.text(12, -0.75, "8N1 : 8 bits, pas de parité, 1 bit de stop", ha="right", va="center",
|
|
fontsize=8, color=GRIS)
|
|
ax.set_ylim(-1.1, 2.5)
|
|
fig.tight_layout()
|
|
return fig
|
|
|
|
|
|
def cablage_serie():
|
|
fig, ax = toile(16, 6.8)
|
|
pc = boite(ax, 1.8, 3.1, 3.0, 2.2, "Ordinateur\n\nMoniteur série", couleur=GRIS, taille=8.5)
|
|
esp = boite(ax, 8.0, 3.2, 4.0, 4.6, couleur=BLEU, fond="#f5f8fe")
|
|
ax.text(8.0, 5.1, "ESP32-S3", ha="center", fontsize=10, color=BLEU, fontweight="bold")
|
|
mod = boite(ax, 14.0, 3.0, 3.0, 3.8, couleur=ORANGE)
|
|
ax.text(14.0, 4.2, "Module externe\n(GPS, RFID…)", ha="center", va="center", fontsize=8.5, color=ENCRE)
|
|
# USB natif
|
|
boite(ax, 7.0, 4.2, 1.6, 0.6, "USB", couleur=GRIS, taille=8)
|
|
fleche(ax, (3.3, 4.2), (6.2, 4.2), "Serial\n(USB CDC)", double=True, couleur=VERT, taille=8)
|
|
# Broches Serial2
|
|
for yy, nom in ((3.0, "GPIO18 (TX)"), (2.0, "GPIO17 (RX)")):
|
|
ax.text(9.9, yy, nom, ha="right", va="center", fontsize=8.5, color=ENCRE)
|
|
for yy, nom in ((3.0, "RX"), (2.0, "TX")):
|
|
ax.text(12.7, yy, nom, ha="left", va="center", fontsize=8.5, color=ENCRE)
|
|
ax.text(9.9, 1.0, "GND", ha="right", va="center", fontsize=8.5, color=ENCRE)
|
|
ax.text(12.7, 1.0, "GND", ha="left", va="center", fontsize=8.5, color=ENCRE)
|
|
fleche(ax, (10.0, 3.0), (12.5, 2.0), couleur=BLEU)
|
|
fleche(ax, (12.5, 3.0), (10.0, 2.0), couleur=ORANGE)
|
|
ax.plot([10.0, 12.5], [1.0, 1.0], color=ENCRE, lw=1.3)
|
|
ax.text(11.25, 3.35, "croisé !", ha="center", fontsize=8.5, color=ORANGE, fontweight="bold")
|
|
ax.text(11.25, 0.55, "masse commune", ha="center", fontsize=7.5, color=GRIS)
|
|
ax.text(8.0, 0.15, "Serial2.begin(9600, SERIAL_8N1, 17, 18);", ha="center", fontsize=7.5,
|
|
color=GRIS, family="monospace")
|
|
return fig
|
|
|
|
|
|
FIGURES = [
|
|
Figure("m04-trame-uart", trame_uart),
|
|
Figure("m04-cablage-serie", cablage_serie),
|
|
]
|