Bonjour,
je debug un programme et je ne comprends pas pourquoi j'obtiens 0 quand je souhaite obtenir la valeur binaire de data pour tester la fonction setFunction(1).
DCC_FUNCTION_MAX-4=8 ==> 0000 1000
0x1F ==> 0001 1111
1>>8 ==> 0000 0100
data = (1>>8)&0x1F = 0000 0100
Merci pour vos éclairages
#define DCC_PACKET_TYPE_SPEED 0
#define DCC_PACKET_TYPE_STEP_14 0x00 // 0000 0000 01D0 VVVV 0 (VVVV = vitesse, 0 stop, 1 arret urgence)
#define DCC_PACKET_TYPE_STEP_27 0x10 // 0001 0000
#define DCC_PACKET_TYPE_STEP_28 0x20 // 0010 0000 01DE VVV 0 (VVVE = vitesse, 0 et 1 STOP, 2 et 3 arret uregnce)
#define DCC_PACKET_TYPE_STEP_128 0x30 // 0011 0000
#define DCC_PACKET_TYPE_ADDR_LONG 0x80 // 1000 0000
byte dccSpeed; // valeur de la vitesse (2...15, 4...31)
byte dccDir=1; // sens de la mache (1 forward, 0 reverse)
int ChoixCran=2; // 14, 27/28, 128
int ChoixMode=0; // choix adresse long 1 ou bien adresse courte 0
byte ChoixLoco=3;
//int index = 4;
long dccFctField=0;
#define DCC_PACKET_TYPE_F0_F4 1
#define DCC_PACKET_TYPE_F5_F8 2
#define DCC_PACKET_TYPE_F9_F12 3
#define DCC_FUNCTION_MAX 12 // Nombre de fonctions à commander
byte SpeedAndDir() // vecteur de direction et de vitesse
{
byte type;
switch(ChoixCran) {
case 0:
type=DCC_PACKET_TYPE_SPEED|DCC_PACKET_TYPE_STEP_14;
//Serial.println(type,DEC);
dccSpeed=8;
if (dccSpeed) dccSpeed++;
break;
case 1:
type=DCC_PACKET_TYPE_SPEED|DCC_PACKET_TYPE_STEP_27;
dccSpeed=10;
if (dccSpeed) dccSpeed+=3;
//Serial.println(type,DEC);
break;
case 2:
type=DCC_PACKET_TYPE_SPEED|DCC_PACKET_TYPE_STEP_28;
dccSpeed=10;
// Serial.println(type, DEC);
if (dccSpeed) dccSpeed+=3; // ajoute 3
//Serial.println(type,DEC);
break;
case 3:
type=DCC_PACKET_TYPE_SPEED|DCC_PACKET_TYPE_STEP_128;
dccSpeed=10;
if (dccSpeed) dccSpeed++;
//Serial.println(type,DEC);
break;
}
if(ChoixMode) type|=DCC_PACKET_TYPE_ADDR_LONG;
word data=(dccDir>0)?0x100:0;
if(dccDir) data|=dccSpeed;
//dccPacketFormat(type, ChoixLoco, data);
// return(dccSpeed);
// Serial.println(type, DEC);
// Serial.println(dccSpeed);
// Serial.println(data);
}
void setFunction(byte index)
{
byte type;
word data;
if(index<=4)
{type=DCC_PACKET_TYPE_F0_F4;
data=(1>>(DCC_FUNCTION_MAX-4))&0x1F; //// 0x1F= 0001 1111
Serial.println(data,BIN);}
else if(index<=8)
{type=DCC_PACKET_TYPE_F5_F8;
data=(dccFctField>>(DCC_FUNCTION_MAX-8))&0xF;}
// Serial.println(data, HEX);}
else if(index<=12)
{type=DCC_PACKET_TYPE_F9_F12;
data=(dccFctField>>(DCC_FUNCTION_MAX-12))&0xF;}
if(ChoixMode) type|=DCC_PACKET_TYPE_ADDR_LONG;
// dccPacketFormat(type, ChoixLoco, data);
//Serial.println(DCC_FUNCTION_MAX-4);
// Serial.println(data);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//SpeedAndDir();
setFunction(1);
delay(1000);
}