Wikipédia de RayMind

Arduino exemple DCC : Différence entre versions

De RayMind wiki
Aller à : navigation, rechercher
(Page créée avec « {| align="right" | __TOC__ |} ==DCC== ===DCC_Reception.ino=== <div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;"> <span style="color:... »)
 
 
Ligne 3 : Ligne 3 :
 
|}
 
|}
 
==DCC==
 
==DCC==
 +
===DCC_Emission.ino===
 +
<div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;">
 +
<span style="color: #7E7E7E;">/*</span><br>
 +
<span style="color: #7E7E7E;">&nbsp;&nbsp;DCC_Emission</span><br>
 +
<span style="color: #7E7E7E;">&nbsp;&nbsp;Date&nbsp;de&nbsp;cr&#233;ation&nbsp;le&nbsp;12/02/2012</span><br>
 +
<span style="color: #7E7E7E;">&nbsp;&nbsp;Date&nbsp;de&nbsp;modification&nbsp;le&nbsp;24/02/2012</span><br>
 +
<span style="color: #7E7E7E;">&nbsp;&nbsp;Programme&nbsp;&#233;crit&nbsp;par&nbsp;Raymond&nbsp;M&#233;chiche</span><br>
 +
<span style="color: #7E7E7E;">&nbsp;&nbsp;Copyright&nbsp;(c)&nbsp;2012&nbsp;-&nbsp;RayMind</span><br>
 +
<span style="color: #7E7E7E;">&nbsp;&nbsp;</span><br>
 +
<span style="color: #7E7E7E;">&nbsp;&nbsp;Documentation&nbsp;NMRA&nbsp;DCC&nbsp;:&nbsp;http://www.scribd.com/doc/10941975/64/La-trame-DCC-NMRA</span><br>
 +
<span style="color: #7E7E7E;">*/</span><br>
 +
<br>
 +
<span style="color: #7E7E7E;">//&nbsp;D&#233;claration&nbsp;des&nbsp;constantes</span><br>
 +
<nowiki>#</nowiki>define&nbsp;PIN_ECRITURE&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Pin sur laquelle seront envoy&#233;s les paquets</span><br>
 +
<nowiki>#</nowiki>define&nbsp;PREAMBULE&nbsp;18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Taille par d&#233;faut du pr&#233;ambule &#224; envoyer avec un paquet</span><br>
 +
<nowiki>#</nowiki>define&nbsp;BIT_1_DELAI&nbsp;58&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Dur&#233;e d'un demi-cycle d'un bit &#224; "1"</span><br>
 +
<nowiki>#</nowiki>define&nbsp;BIT_0_DELAI&nbsp;100&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Dur&#233;e d'un demi-cycle d'un bit &#224; "0"</span><br>
 +
<nowiki>#</nowiki>define&nbsp;PAQUET_DELAI&nbsp;26&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Dur&#233;e minimum &#224; mettre entre chaque paquet</span><br>
 +
<br>
 +
<span style="color: #7E7E7E;">//&nbsp;D&#233;claration&nbsp;du&nbsp;tableau&nbsp;du&nbsp;paquet&nbsp;&#224;&nbsp;envoyer</span><br>
 +
<span style="color: #CC6600;">byte</span> gaPaquet[9];<br>
 +
<br>
 +
<span style="color: #7E7E7E;">//&nbsp;D&#233;claration&nbsp;des&nbsp;constantes&nbsp;d'index&nbsp;du&nbsp;tableau&nbsp;du&nbsp;paquet&nbsp;&#224;&nbsp;envoyer</span><br>
 +
<nowiki>#</nowiki>define&nbsp;PAQUET_ADRESSE&nbsp;0<br>
 +
<nowiki>#</nowiki>define&nbsp;PAQUET_VALEUR_NB&nbsp;1<br>
 +
<nowiki>#</nowiki>define&nbsp;PAQUET_VALEUR1&nbsp;2<br>
 +
<nowiki>#</nowiki>define&nbsp;PAQUET_VALEUR6&nbsp;7<br>
 +
<nowiki>#</nowiki>define&nbsp;PAQUET_ERREUR&nbsp;8<br>
 +
<br>
 +
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>()<br>
 +
{&nbsp;&nbsp;<span style="color: #7E7E7E;">// D&#233;marrage de l'Arduino</span><br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Initialisation du port s&#233;rie pour l'affichage du tableau de lecture des bits</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(115200);<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>();<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(<span style="color: #006699;">"Ecriture DCC"</span>);<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">delay</span>(1000);<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Pr&#233;paration de la sortie digitale n&#176;8 pour &#233;crire l'information digitale sur le rail</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(PIN_ECRITURE, <span style="color: #006699;">OUTPUT</span>);<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(PIN_ECRITURE, <span style="color: #006699;">LOW</span>);<br>
 +
<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Initialisation des valeurs du paquet</span><br>
 +
&nbsp;&nbsp;initialisationPaquet();<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Constitution du paquet &#224; envoyer</span><br>
 +
&nbsp;&nbsp;gaPaquet[PAQUET_ADRESSE]&nbsp;=&nbsp;3;<br>
 +
&nbsp;&nbsp;gaPaquet[PAQUET_VALEUR_NB]&nbsp;=&nbsp;0;<br>
 +
&nbsp;&nbsp;gaPaquet[PAQUET_ERREUR]&nbsp;=&nbsp;255;<br>
 +
}<br>
 +
<br>
 +
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>()<br>
 +
{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Boucle d'ex&#233;cution de l'Arduino</span><br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert du paquet</span><br>
 +
&nbsp;&nbsp;transfertPaquet();<br>
 +
}<br>
 +
<br>
 +
<span style="color: #CC6600;">void</span> initialisationPaquet()<br>
 +
{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Initialisation du tableau du paquet &#224; envoyer</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (<span style="color: #CC6600;">int</span> lnCptDonnee = 0; lnCptDonnee &lt; 9; lnCptDonnee++)<br>
 +
&nbsp;&nbsp;{<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;gaPaquet[lnCptDonnee]&nbsp;=&nbsp;0;<br>
 +
&nbsp;&nbsp;}<br>
 +
}<br>
 +
<br>
 +
<span style="color: #CC6600;">void</span> transfertPaquet()<br>
 +
{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert du paquet en cours</span><br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert du pr&#233;ambule</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (<span style="color: #CC6600;">byte</span> lnCpt = 0; lnCpt &lt; PREAMBULE; lnCpt++)<br>
 +
&nbsp;&nbsp;{<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;ecritureBit(1);<br>
 +
&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert du bit de s&#233;paration</span><br>
 +
&nbsp;&nbsp;ecritureBit(0);<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert de l'adresse</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (<span style="color: #CC6600;">byte</span> lnCptBit = 0; lnCptBit &lt; 8; lnCptBit++)<br>
 +
&nbsp;&nbsp;{<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;ecritureBit(<span style="color: #CC6600;">bitRead</span>(gaPaquet[PAQUET_ADRESSE], 7 - lnCptBit));<br>
 +
&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert des valeurs</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">byte</span> lnValeurIndex = 0;<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">while</span> (lnValeurIndex &lt; gaPaquet[PAQUET_VALEUR_NB])<br>
 +
&nbsp;&nbsp;{<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Tranfert du bit de s&#233;paration</span><br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;ecritureBit(0);<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert de la valeur</span><br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (<span style="color: #CC6600;">byte</span> lnCptBit = 0; lnCptBit &lt; 8; lnCptBit++)<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ecritureBit(<span style="color: #CC6600;">bitRead</span>(gaPaquet[PAQUET_VALEUR1 + lnValeurIndex], 7 - lnCptBit));<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Passage &#224; la valeur suivante</span><br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;lnValeurIndex++;<br>
 +
&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert de l'erreur</span><br>
 +
&nbsp;&nbsp;ecritureBit(0);<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (<span style="color: #CC6600;">byte</span> lnCptBit = 0; lnCptBit &lt; 8; lnCptBit++)<br>
 +
&nbsp;&nbsp;{<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;ecritureBit(<span style="color: #CC6600;">bitRead</span>(gaPaquet[PAQUET_ERREUR], 7 - lnCptBit));<br>
 +
&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Transfert du bit marquant la fin du paquet</span><br>
 +
&nbsp;&nbsp;ecritureBit(1);<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// Attente minimum avant l'envoi d'un nouveau paquet</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">delayMicroseconds</span>(PAQUET_DELAI);<br>
 +
}<br>
 +
<br>
 +
<span style="color: #CC6600;">void</span> ecritureBit(<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">byte</span> pnBit)<br>
 +
{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Ecriture d'un bit &#224; "1" sur la sortie digitale</span><br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// D&#233;but du front montant</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(PIN_ECRITURE, <span style="color: #006699;">HIGH</span>);<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (pnBit)<br>
 +
&nbsp;&nbsp;{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Bit &#224; "1"</span><br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delayMicroseconds</span>(BIT_1_DELAI);<br>
 +
&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">else</span><br>
 +
&nbsp;&nbsp;{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Bit &#224; "0"</span><br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delayMicroseconds</span>(BIT_0_DELAI);<br>
 +
&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;<br>
 +
&nbsp;&nbsp;<span style="color: #7E7E7E;">// D&#233;but du front descendant</span><br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(PIN_ECRITURE, <span style="color: #006699;">LOW</span>);<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (pnBit)<br>
 +
&nbsp;&nbsp;{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Bit &#224; "1"</span><br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delayMicroseconds</span>(BIT_1_DELAI);<br>
 +
&nbsp;&nbsp;}<br>
 +
&nbsp;&nbsp;<span style="color: #CC6600;">else</span><br>
 +
&nbsp;&nbsp;{&nbsp;&nbsp;<span style="color: #7E7E7E;">// Bit &#224; "0"</span><br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delayMicroseconds</span>(BIT_0_DELAI);<br>
 +
&nbsp;&nbsp;}<br>
 +
}<br>
 +
<br>
 +
</tt></div>
 
===DCC_Reception.ino===
 
===DCC_Reception.ino===
 
<div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;">
 
<div style="border-left:6px solid lightgray;padding-left:10px;"><tt style="font-size:14px;">

Version actuelle datée du 24 février 2012 à 10:06

DCC

DCC_Emission.ino

/*
  DCC_Emission
  Date de création le 12/02/2012
  Date de modification le 24/02/2012
  Programme écrit par Raymond Méchiche
  Copyright (c) 2012 - RayMind
  
  Documentation NMRA DCC : http://www.scribd.com/doc/10941975/64/La-trame-DCC-NMRA
*/

// Déclaration des constantes
#define PIN_ECRITURE 8      // Pin sur laquelle seront envoyés les paquets
#define PREAMBULE 18        // Taille par défaut du préambule à envoyer avec un paquet
#define BIT_1_DELAI 58      // Durée d'un demi-cycle d'un bit à "1"
#define BIT_0_DELAI 100    // Durée d'un demi-cycle d'un bit à "0"
#define PAQUET_DELAI 26    // Durée minimum à mettre entre chaque paquet

// Déclaration du tableau du paquet à envoyer
byte gaPaquet[9];

// Déclaration des constantes d'index du tableau du paquet à envoyer
#define PAQUET_ADRESSE 0
#define PAQUET_VALEUR_NB 1
#define PAQUET_VALEUR1 2
#define PAQUET_VALEUR6 7
#define PAQUET_ERREUR 8

void setup()
{  // Démarrage de l'Arduino
  // Initialisation du port série pour l'affichage du tableau de lecture des bits
  Serial.begin(115200);
  Serial.println();
  Serial.println("Ecriture DCC");
  delay(1000);
  
  // Préparation de la sortie digitale n°8 pour écrire l'information digitale sur le rail
  pinMode(PIN_ECRITURE, OUTPUT);
  digitalWrite(PIN_ECRITURE, LOW);

  // Initialisation des valeurs du paquet
  initialisationPaquet();
  
  // Constitution du paquet à envoyer
  gaPaquet[PAQUET_ADRESSE] = 3;
  gaPaquet[PAQUET_VALEUR_NB] = 0;
  gaPaquet[PAQUET_ERREUR] = 255;
}

void loop()
{  // Boucle d'exécution de l'Arduino
  // Transfert du paquet
  transfertPaquet();
}

void initialisationPaquet()
{  // Initialisation du tableau du paquet à envoyer
  for (int lnCptDonnee = 0; lnCptDonnee < 9; lnCptDonnee++)
  {
    gaPaquet[lnCptDonnee] = 0;
  }
}

void transfertPaquet()
{  // Transfert du paquet en cours
  // Transfert du préambule
  for (byte lnCpt = 0; lnCpt < PREAMBULE; lnCpt++)
  {
    ecritureBit(1);
  }
  
  // Transfert du bit de séparation
  ecritureBit(0);
  
  // Transfert de l'adresse
  for (byte lnCptBit = 0; lnCptBit < 8; lnCptBit++)
  {
    ecritureBit(bitRead(gaPaquet[PAQUET_ADRESSE], 7 - lnCptBit));
  }
  
  // Transfert des valeurs
  byte lnValeurIndex = 0;
  while (lnValeurIndex < gaPaquet[PAQUET_VALEUR_NB])
  {
    // Tranfert du bit de séparation
    ecritureBit(0);
    
    // Transfert de la valeur
    for (byte lnCptBit = 0; lnCptBit < 8; lnCptBit++)
    {
      ecritureBit(bitRead(gaPaquet[PAQUET_VALEUR1 + lnValeurIndex], 7 - lnCptBit));
    }
    
    // Passage à la valeur suivante
    lnValeurIndex++;
  }
  
  // Transfert de l'erreur
  ecritureBit(0);
  for (byte lnCptBit = 0; lnCptBit < 8; lnCptBit++)
  {
    ecritureBit(bitRead(gaPaquet[PAQUET_ERREUR], 7 - lnCptBit));
  }
  
  // Transfert du bit marquant la fin du paquet
  ecritureBit(1);
  
  // Attente minimum avant l'envoi d'un nouveau paquet
  delayMicroseconds(PAQUET_DELAI);
}

void ecritureBit(
  byte pnBit)
{  // Ecriture d'un bit à "1" sur la sortie digitale
  // Début du front montant
  digitalWrite(PIN_ECRITURE, HIGH);
  if (pnBit)
  {  // Bit à "1"
    delayMicroseconds(BIT_1_DELAI);
  }
  else
  {  // Bit à "0"
    delayMicroseconds(BIT_0_DELAI);
  }
  
  // Début du front descendant
  digitalWrite(PIN_ECRITURE, LOW);
  if (pnBit)
  {  // Bit à "1"
    delayMicroseconds(BIT_1_DELAI);
  }
  else
  {  // Bit à "0"
    delayMicroseconds(BIT_0_DELAI);
  }
}

DCC_Reception.ino

/*
  DCC_Reception
  Date de création le 12/02/2012
  Date de modification le 24/02/2012
  Programme écrit par Raymond Méchiche
  Copyright (c) 2012 - RayMind
  
  Documentation NMRA DCC : http://www.scribd.com/doc/10941975/64/La-trame-DCC-NMRA
*/

// Déclaration des constantes d'affichage sur le port série pour la trace du traitement
#define DUREE_AFFICHAGE true // Indicateur d'affichage des durées de chaque impulsion
#define ERREUR_AFFICHAGE true // Indicateur d'affichage des erreurs de lectures digitales
#define PAQUET_AFFICHAGE true // Indicateur d'affichage des paquets
#define CSV_AFFICHAGE true // Indicateur d'affichage du résultat au format CSV pour l'incorporer dans Excel

// Déclaration des constantes
#define BIT_1_MIN 104UL          // Durée minimum entre 2 impulsions pour déterminer un bit à "1"
#define BIT_1_MAX 128UL        // Durée maximum entre 2 impulsions pour déterminer un bit à "1"
#define BIT_0_MIN 180UL        // Durée minimum entre 2 impulsions pour déterminer un bit à "0"
#define BIT_0_MAX 20000UL      // Durée maximum entre 2 impulsions pour déterminer un bit à "0"
#if defined(__AVR_ATmega328P__)
  #define OCTET_NB 40           // Nombre d'octets à enregistrer dans le tableau de lecture
#else
  #if defined(__AVR_ATmega2560__)
    #define OCTET_NB 200
  #endif
#endif
#define PAQUET_NB OCTET_NB * 8 / 38  // Nombre de paquets à enregistrer dans le tableau des paquets
#define PREAMBULE_TAILLE_MIN 10  // Nombre de bits à "1" minimum nécessaire pour définir le préambule d'un paquet
#if defined(PAQUET_AFFICHAGE) && PAQUET_AFFICHAGE
  #define ETAPE_PREAMBULE 0    // Définition d'étape de composition d'un paquet au préambule
  #define ETAPE_VALEUR 1        // Définition d'étape de composition d'un paquet à une valeur
#endif

// Déclaration des variables
volatile boolean glFrontDescendant = true; // Indicateur de front descendant
volatile unsigned long gdImpulsionDebut = micros(); // Horodatage en µs d'une impulsion
volatile unsigned long gdImpulsionDuree; // Durée entre 2 impulsions
volatile int gnOctetIndex; // Index de lecture des bits enregistrés dans un tableau d'octets
volatile byte gnBitIndex; // Index de lecture des bits enregistrés dans un octet
volatile boolean glAffichage = false; // Indicateur d'affichage sur le port série du tableau d'octets

// Déclaration des tableaux de bits
volatile byte gaBits[OCTET_NB]; // Tableau de lecture des bits sous forme d'octets
#if defined(DUREE_AFFICHAGE) && DUREE_AFFICHAGE
  volatile unsigned long gaDurees[OCTET_NB][8]; // Tableau des durées entre chaque impulsion du tableau de lecture des bits
#endif
#if defined(ERREUR_AFFICHAGE) && ERREUR_AFFICHAGE
  volatile byte gaErreurs[OCTET_NB]; // Tableau des erreurs des impulsions pour les bits non définis
#endif

// Déclaration du tableau des paquets
byte gaPaquets[PAQUET_NB][10];

// Déclaration des constantes d'index du tableau des paquets
#define PAQUET_PREAMBULE 0
#define PAQUET_ADRESSE 1
#define PAQUET_VALEUR_NB 2
#define PAQUET_VALEUR1 3
#define PAQUET_VALEUR6 8
#define PAQUET_ERREUR 9

void setup()
{  // Démarrage de l'Arduino
  // Initialisation du port série pour l'affichage du tableau de lecture des bits
  Serial.begin(115200);
  Serial.println();
  Serial.println("Lecture DCC");
  delay(1000);
  
  //Initialisation du tableau de lecture des bits
  initialisationBits();
  
  // Préparation de l'entrée digitale n°2 pour lire l'information digitale sur le rail
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);

  // Activation de l'interruption sur l'entrée digitale n°2 (interruption #0 <INT0> correspondant à l'entrée n°2) pour lire les impulsions
  // digitales à chaque front descendant du signal (passage par le 0 volt)
  attachInterrupt(0, impulsion, FALLING);
}

void loop()
{  // Boucle d'exécution de l'Arduino
  if (glAffichage)
  {
    // Désactivation de l'interruption le temps de l'affichage sur le port série
    detachInterrupt(0);
    
    glAffichage = false;

    #if defined(PAQUET_AFFICHAGE) && PAQUET_AFFICHAGE    
      // Initialisation du tableau des paquets
      initialisationPaquets();
  
      // Composition des paquets
      compositionPaquets();
    #endif
    
    // Affichage des bits lus
    Serial.println();
    Serial.println("[bits]");
    #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
      Serial.println("#;Bits;Duree_1;Duree_2;Duree_3;Duree_4;Duree_5;Duree_6;Duree_7;Duree_8;Erreur");
    #endif

    for (int lnCptOctet = 0; lnCptOctet < OCTET_NB; lnCptOctet++)
    {
      Serial.print(lnCptOctet);
      
      #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
        Serial.print(";");
      #else
        Serial.print("<");
      #endif
      
      Serial.print(gaBits[lnCptOctet], BIN);

      #if defined(DUREE_AFFICHAGE) && DUREE_AFFICHAGE
        #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
          Serial.print(";");
        #else
          Serial.print("/");
        #endif
        
        for (int lnCptBit = 0; lnCptBit < 8; lnCptBit++)
        {
          if (lnCptBit > 0)
          {
            #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
              Serial.print(";");
            #else
              Serial.print("-");
            #endif
          }
          Serial.print(gaDurees[lnCptOctet][lnCptBit]);
        }
      #endif

      #if defined(ERREUR_AFFICHAGE) && ERREUR_AFFICHAGE
        #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
          Serial.print(";");
        #else
          Serial.print("#");
        #endif
        
        Serial.print(gaErreurs[lnCptOctet], BIN);
      #endif

      #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
        Serial.println();
      #else
        Serial.println(">");
      #endif
    }
    
    #if defined(PAQUET_AFFICHAGE) && PAQUET_AFFICHAGE
      // Affichage des paquets
      Serial.println();
      Serial.println("[paquets]");
      #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
        Serial.println("#;Preambule;Adresse;Nb_valeurs;Valeur_1;Valeur_2;Valeur_3;Valeur_4;Valeur_5;Valeur_6;Erreur");
      #endif
  
      for (int lnCptPaquet = 0; lnCptPaquet < PAQUET_NB; lnCptPaquet++)
      {
        Serial.print(lnCptPaquet);
  
        #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
          Serial.print(";");
        #else
          Serial.print("<");
        #endif
        
        Serial.print(gaPaquets[lnCptPaquet][PAQUET_PREAMBULE]);
  
        #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
          Serial.print(";");
        #else
          Serial.print("/");
        #endif
        
        Serial.print(gaPaquets[lnCptPaquet][PAQUET_ADRESSE]);
  
        #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
          Serial.print(";");
        #else
          Serial.print("~");
        #endif
        
        Serial.print(gaPaquets[lnCptPaquet][PAQUET_VALEUR_NB]);
        
        for (byte lnCptValeur = PAQUET_VALEUR1; lnCptValeur < PAQUET_ERREUR; lnCptValeur++)
        {
          #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
            Serial.print(";");
          #else
            Serial.print("-");
          #endif
          
          Serial.print(gaPaquets[lnCptPaquet][lnCptValeur]);
        }
        
        #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
          Serial.print(";");
        #else
          Serial.print("#");
        #endif
        
        Serial.print(gaPaquets[lnCptPaquet][PAQUET_ERREUR]);
  
        #if defined(CSV_AFFICHAGE) && CSV_AFFICHAGE
          Serial.println();
        #else
          Serial.println(">");
        #endif
      }
    #endif
    
    // Ré-activation de l'interruption pour lire les impulsions sur l'entrée digitale
    attachInterrupt(0, impulsion, FALLING);
  }
}

void impulsion()
{  // Interruption déclenchée à chaque impulsion sur le port digital
  // On est sur le front descendant du signal une fois sur 2
//  glFrontDescendant = !glFrontDescendant;
  
  if (glFrontDescendant)
  {
    // Horodatage de l'impulsion
    volatile unsigned long ldImpulsionFin = micros();
    volatile byte lnBit;
    
    // Calcul de la durée d'une impulsion
    gdImpulsionDuree = ldImpulsionFin - gdImpulsionDebut;
    gdImpulsionDebut = ldImpulsionFin;
    
    // Détermination du bit lu sur l'entrée digitale
    if (gdImpulsionDuree >= BIT_1_MIN && gdImpulsionDuree <= BIT_1_MAX)
    {  // Bit à "1"
      gaBits[gnOctetIndex] |= 1 << (7 - gnBitIndex);
      #if defined(ERREUR_AFFICHAGE) && ERREUR_AFFICHAGE
        gaErreurs[gnOctetIndex] &= ~(1 << (7 - gnBitIndex));
      #endif
    }
    else
    {
      if (gdImpulsionDuree >= BIT_0_MIN && gdImpulsionDuree <= BIT_0_MAX)
      {  // Bit à "0"
        gaBits[gnOctetIndex] &= ~(1 << (7 - gnBitIndex));
        #if defined(ERREUR_AFFICHAGE) && ERREUR_AFFICHAGE
          gaErreurs[gnOctetIndex] &= ~(1 << (7 - gnBitIndex));
        #endif
      }
      else
      {  // Erreur de lecture, la durée de l'impulsion est incorrecte
        #if defined(ERREUR_AFFICHAGE) && ERREUR_AFFICHAGE
          gaBits[gnOctetIndex] &= ~(1 << (7 - gnBitIndex));
          gaErreurs[gnOctetIndex] |= 1 << (7 - gnBitIndex);
        #else
          return;
        #endif
      }
    }
  
    #if defined(DUREE_AFFICHAGE) && DUREE_AFFICHAGE
      gaDurees[gnOctetIndex][gnBitIndex] = gdImpulsionDuree;
    #endif
    
    gnBitIndex++;
    if (gnBitIndex > 7)
    {  // Passage à l'octet suivant
      gnOctetIndex++;
      gnBitIndex = 0;
      if (gnOctetIndex >= OCTET_NB)
      {  // Retour au premier octet pour lire une nouvelle série de bits
        gnOctetIndex = 0;
        glAffichage = true;
      }
    }
  }
}

void initialisationBits()
{  // Initialisation du tableau de lecture des bits et des durées d'impulsion
  gnOctetIndex = 0;
  gnBitIndex = 0;
  for (int lnCptOctet = 0; lnCptOctet < OCTET_NB; lnCptOctet++)
  {
    gaBits[lnCptOctet] = 0;

    #if defined(ERREUR_AFFICHAGE) && ERREUR_AFFICHAGE
      gaErreurs[lnCptOctet] = 0;
    #endif

    #if defined(DUREE_AFFICHAGE) && DUREE_AFFICHAGE
      for (int lnCptBit = 0; lnCptBit < 8; lnCptBit++)
      {
        gaDurees[lnCptOctet][lnCptBit] = 0;
      }
    #endif
  }
}

#if defined(PAQUET_AFFICHAGE) && PAQUET_AFFICHAGE
  void initialisationPaquets()
  {  // Initialisation du tableau des paquets
    for (int lnCptPaquet = 0; lnCptPaquet < PAQUET_NB; lnCptPaquet++)
    {
      for (int lnCptDonnee = 0; lnCptDonnee < 10; lnCptDonnee++)
      {
        gaPaquets[lnCptPaquet][lnCptDonnee] = 0;
      }
    }
  }

  void compositionPaquets()
  {  // Composition des paquets à partir des bits lus
    int lnPaquetIndex = 0;
    byte lnEtape = ETAPE_PREAMBULE;
    byte lnBitIndex;
    byte lnValeurIndex;
    
    for (int lnCptOctet = 0; lnCptOctet < OCTET_NB; lnCptOctet++)
    {
      for (byte lnCptBit = 0; lnCptBit < 8; lnCptBit++)
      {
        if (lnPaquetIndex < PAQUET_NB)
        {  // On ne compose un paquet que tant qu'on ne dépasse pas le nombre de paquets total admis
          if (bitRead(gaErreurs[lnCptOctet], 7 - lnCptBit) == 0)
          {  // On ne tient compte que des bits lus qui ne sont pas en erreur
            switch(lnEtape)
            {
              case ETAPE_PREAMBULE:
                if (bitRead(gaBits[lnCptOctet], 7 - lnCptBit))
                {  // Bit à "1"
                  gaPaquets[lnPaquetIndex][PAQUET_PREAMBULE]++;
                }
                else
                {  // Bit à "0"
                  if (gaPaquets[lnPaquetIndex][PAQUET_PREAMBULE] >= PREAMBULE_TAILLE_MIN)
                  {
                    lnEtape = ETAPE_VALEUR;
                    lnBitIndex = 0;
                    lnValeurIndex = PAQUET_ADRESSE;
  
                  }
                  else
                  {
                    gaPaquets[lnPaquetIndex][PAQUET_PREAMBULE] = 0;
                  }
                }
                break;
                
              case ETAPE_VALEUR:
                if (lnBitIndex < 8)
                {
                  bitWrite(gaPaquets[lnPaquetIndex][lnValeurIndex], lnBitIndex, bitRead(gaBits[lnCptOctet], 7 - lnCptBit));
                  lnBitIndex++;
                }
                else
                {
                  if (bitRead(gaBits[lnCptOctet], 7 - lnCptBit))
                  {  // Bit à "1", c'est la fin du paquet
                    if (lnValeurIndex >= PAQUET_VALEUR1)
                    {
                      gaPaquets[lnPaquetIndex][PAQUET_VALEUR_NB] = gaPaquets[lnPaquetIndex][PAQUET_VALEUR_NB] - 1;
                      gaPaquets[lnPaquetIndex][PAQUET_ERREUR] = gaPaquets[lnPaquetIndex][lnValeurIndex];
                      gaPaquets[lnPaquetIndex][lnValeurIndex] = 0;
                    }
                    lnEtape = ETAPE_PREAMBULE;
                    lnPaquetIndex++;
                  }
                  else
                  {  // Bit à "0", lecture de la valeur suivante
                    if (lnValeurIndex == PAQUET_ADRESSE)
                    {  // La valeur précédemment lue était l'adresse, on se positionne sur la première valeur
                      lnValeurIndex = PAQUET_VALEUR1;
                      gaPaquets[lnPaquetIndex][PAQUET_VALEUR_NB] = 1;
                    }
                    else
                    {
                      lnValeurIndex++;
                      lnBitIndex = 0;
                      if (lnValeurIndex >= PAQUET_ERREUR)
                      {  // Fin prématurée du paquet car il y a trop de valeurs
                        lnEtape = ETAPE_PREAMBULE;
                        lnPaquetIndex++;
                      }
                      else
                      {  // On incrémente de nombre de valeurs contenues dans le paquet
                        gaPaquets[lnPaquetIndex][PAQUET_VALEUR_NB]++;
                      }
                    }
                  }
                }
                break;
            }
          }
        }
      }
    }
  }
#endif