Hi everyone,
I'm building an S88 decoder for my model railroad. The S88 protocol is based on the 4014 shift registers.
My code works almost... The first 16 bits are OK but the last 16 not, I get on my command station all the bits moved one place to the right on my command station screen.
So the zero on S88data[16] disappeared... Anyone who can find my mistake?
// S88 pins setup
#define S88DATAIN 5
#define S88DATAOUT 4
#define S88CLOCK 2
#define S88LOAD 3
volatile uint16_t S88DataOutShiftRegister1; // Shift register 1
volatile uint16_t S88DataOutShiftRegister2; // Shift register 2
volatile uint32_t S88data[33] = {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1}; // S88 data, when this works completely I want to add the wireless stuff to manipulate this data array.
void setup() {
attachInterrupt(0, CLOCK, CHANGE); // Interrupt stuff for the CLOCK and LOAD
attachInterrupt(1, LOAD, RISING);
pinMode(S88DATAOUT, OUTPUT);
}
void loop() { // No loop stuff because it's all interrupt driven.
}
void CLOCK()
{
detachInterrupt(0); // Disable interrupt while doing some stuff with it.
if (((PIND & (1 << S88CLOCK)) == (1 << S88CLOCK))) { // If rising edge, write S88DATAOUT
if ((S88DataOutShiftRegister1 & 1) == 0)
PORTD &= ~(1 << S88DATAOUT);
else {
PORTD |= (1 << S88DATAOUT);
}
// Shift register and add value (Make place & move on)
S88DataOutShiftRegister1 = (S88DataOutShiftRegister1 >> 1);
S88DataOutShiftRegister2 = (S88DataOutShiftRegister2 >> 1);
} else { // If falling edge, read S88DATAIN -> Put the data from the 2nd shift register into the 1st shift register.
S88DataOutShiftRegister1 |= (S88DataOutShiftRegister2 << 15); // Add it at the last place in the S88DataOut.
};
attachInterrupt(0, CLOCK, CHANGE);
}
void LOAD() // Setup the S88data arrays / shift registers... Now it's ready to be send!
{
detachInterrupt(1); // Disable interrupt while doing some stuff with it.
S88DataOutShiftRegister1 = S88data[0] | (S88data[1]<<1) | (S88data[2]<<2) | (S88data[3]<<3) | (S88data[4]<<4) | (S88data[5]<<5) | (S88data[6]<<6) | (S88data[7]<<7) | (S88data[8]<<8) | (S88data[9]<<9) | (S88data[10]<<10) | (S88data[11]<<11) | (S88data[12]<<12) | (S88data[13]<<13) | (S88data[14]<<14) | (S88data[15]<<15);
S88DataOutShiftRegister2 = S88data[16] | (S88data[17]<<1) | (S88data[18]<<2) | (S88data[19]<<3) | (S88data[20]<<4) | (S88data[21]<<5) | (S88data[22]<<6) | (S88data[23]<<7) | (S88data[24]<<8) | (S88data[25]<<9) | (S88data[26]<<10) | (S88data[27]<<11) | (S88data[28]<<12) | (S88data[29]<<13) | (S88data[30]<<14) | (S88data[31]<<15);
attachInterrupt(1, LOAD, RISING);
}
Thanks!
Cheers,
Dylan