16
Vos projets / Re : Récupération données DCC: CircularBuffer et ShiftOut
« le: janvier 24, 2018, 10:40:58 pm »
Ci-joint mon sketch
Le forum LOCODUINO est consacré aux discussions ayant trait à l'utilisation de l'Arduino dans les automatismes et les animations pour le train miniature. Nous avons eu récemment quelques inscriptions de personnes ayant des projets plus généraux mais surtout inapplicables au train miniature. Si votre projet ou vos questions ne concernent pas le modélisme ferroviaire, ne vous inscrivez pas, vous perdriez votre temps et nous aussi.
Cette section vous permet de consulter les contributions (messages, sujets et fichiers joints) d'un utilisateur. Vous ne pourrez voir que les contributions des zones auxquelles vous avez accès.
//Programme de gestion des aiguillages de la zone Bréauté
#include <DCC_Decoder.h>
#define kDCC_INTERRUPT 0 // pin 2 for UNO
#include <CircularBuffer.h>
CircularBuffer <int, 100> storadresse;
CircularBuffer <byte, 100> storaction;
int add = 0;
int act = 0;
int old_add = 0;
int old_act = 0;
int action = 0;
const int LATCH1 = 6;
const int CLOCK1 = 7;
const int DATA1 = 5;
int OUT1[8];
int OUT2[8];
int OUT3[8];
int CDE1 = 0;
int CDE2 = 0;
int CDE3 = 0;
//Handler DCC
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data){
storadresse.push (address);
storaction.push (data);
Serial.print(address);
Serial.print(" ");
Serial.println(data);
}
void setup() {
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
Serial.begin (115200);
Serial.println(" DCC brut ADRESSE ACTION");
pinMode(LATCH1, OUTPUT);
pinMode(CLOCK1, OUTPUT);
pinMode(DATA1, OUTPUT);
}
void loop() {
DCC.loop();
while(!storadresse.isEmpty()) {
add = storadresse.shift();
act = storaction.shift();
if ((add != old_add) || (act != old_act)){
old_add = add;
old_act = act;
// Conversion de l'adresse NMRA en adresse decodeur d'accessoire
add -= 1;
add *= 4;
add += 1;
add += (act & 0x06) >> 1;
action = (act & 0x01);
Serial.print(" ");
Serial.print(add);
Serial.print(" ");
Serial.println(action);
//Gestion aiguillages
//registre à décalage OUT1
if((add >= 1) && (add <= 8)) {
if (action == 1) {
OUT1[add-1] = bit(add - 1);
}
else {
OUT1[add-1] = 0;
}
} //fin OUT1
//registre à décalage OUT2
if((add >= 9) && (add <= 16)) {
if (action == 1) {
OUT2[add-9] = bit(add - 9);
}
else {
OUT2[add-9] = 0;
}
} //fin OUT2
//registre à décalage OUT3
if((add >= 17) && (add <= 24)) {
if (action == 1) {
OUT3[add-17] = bit(add - 17);
}
else {
OUT3[add-17] = 0;
}
} //fin OUT3
//envoi des commandes
CDE1 = OUT1[0]+OUT1[1]+OUT1[2]+OUT1[3]+OUT1[4]+OUT1[5]+OUT1[6]+OUT1[7];
CDE2 = OUT2[0]+OUT2[1]+OUT2[2]+OUT2[3]+OUT2[4]+OUT2[5]+OUT2[6]+OUT2[7];
CDE3 = OUT3[0]+OUT3[1]+OUT3[2]+OUT3[3]+OUT3[4]+OUT3[5]+OUT3[6]+OUT3[7];
digitalWrite(LATCH1, LOW);
shiftOut(DATA1, CLOCK1, LSBFIRST, CDE3);
shiftOut(DATA1, CLOCK1, LSBFIRST, CDE2);
shiftOut(DATA1, CLOCK1, LSBFIRST, CDE1);
digitalWrite(LATCH1, HIGH);
}//fin boucle si nouveaux data
}//fin while
}//fin programme principal
#include <DCC_Decoder.h>
#define kDCC_INTERRUPT 0 // pin 2 for UNO
#include <CircularBuffer.h>
CircularBuffer <int, 100> storadresse;
CircularBuffer <byte, 100> storaction;
int add = 0;
int act = 0;
int old_add = 0;
int old_act = 0;
int action = 0;
const int CLOCK2 = 8;
const int LATCH2 = 9;
const int DATA2 = 10;
int OUT4[8];
int CDE4 = 0;
//Handler DCC
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data){
//Serial.print(address);
//Serial.print(" ");
//Serial.println(data);
storadresse.push (address);
storaction.push (data);
}
void setup() {
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
Serial.begin (115200);
Serial.println("Debut test");
}
void loop() {
DCC.loop();
while(!storadresse.isEmpty()) {
add = storadresse.shift();
act = storaction.shift();
if ((add != old_add) || (act != old_act)){
old_add = add;
old_act = act;
// Conversion de l'adresse NMRA en adresse decodeur d'accessoire
add -= 1;
add *= 4;
add += 1;
add += (act & 0x06) >> 1;
action =(act & 0x01);
Serial.print(add);
Serial.print(" ");
Serial.println(action);
//Gestion des signaux
//Registre à décalage OUT4
if((add >=127) && (add <= 134)){
if(action = 1){
OUT4[add - 127] = bit(add - 127);
}
else{
OUT4[add - 127] = 0;
}
}//fin boucle OUT4
//Envoi OUTT4
CDE4 = OUT4[0]+OUT4[1]+OUT4[2]+OUT4[3]+OUT4[4]+OUT4[5]+OUT4[6]+OUT4[7];
Serial.println(CDE4);
digitalWrite(LATCH2, LOW);
shiftOut(DATA2, CLOCK2, LSBFIRST, CDE4);
digitalWrite(LATCH2, HIGH);
} //fin nouvelle donnée
} //fin while
} //fin loop
--------------------------------------------------------------------------------------------------------------------