Timing issue?: Problem with "sniffing" on Shift Register's SerIN & Clock

I am currently working on hacking an old snack vending machine.
The motor matrix for the dispense is controlled by a 5890 and a 5842, both regular shift registers.
(the 2 form a cascade)

I wanna keep using the existing vending machine’s controller board with the motor drivers.
Now the first step is intercepting the signals sent to the shift registers by the original microcontroller in order to later replace it by an arduino and do a kind of “capture and replay”.

When I connect a LogicAnalyzer(OLS/Arduino), i get nice results(see picture), but somehow my Arduino sketch, which should just
decode the sent signal.

The arduino console output for the same signal as on the picture is

Sniffing...
16
1000000000000000
Sniffing...

…which just can’t be right…

Here’s my code:

#define CLCK 2
#define DATA 3
#define OE   5


volatile int master_count = 0; // universal count

volatile uint16_t in_buffer =0;

void setup() { 
  pinMode(DATA, INPUT);
  pinMode(OE, INPUT);
  pinMode(CLCK,INPUT);
  
  Serial.begin(9600);
}

void loop() {
  
  Serial.println("Sniffing...");
  
  delay(10);
  
  while(1)
    if(digitalRead(OE)==LOW)break;
   
       master_count = 0;
       in_buffer = 0;

  attachInterrupt(0, interrupt, RISING);  
  // interrupt 0 digital pin 2 positive edge trigger
      
    while(1)
    {
  
      if(master_count>15)
      {
        detachInterrupt(0); 
         
        Serial.println(master_count,DEC);
        printBIN(&in_buffer);
       
        delay(6000);
        *in_buffer =0;
        master_count=0;
       break;
      }
    }
}


void interrupt() {
        boolean read = digitalRead(DATA);

        noInterrupts();
        if(read==true)bitSet(in_buffer,(15-master_count));
        master_count++;
        interrupts();
}


//function that also prints leading zeros
void printBIN(volatile uint16_t* binary)
{
  for(int i=15;i!=-1;i--)
  {
    if(bitRead(*binary,i))Serial.print('1');
    else Serial.print('0');
  }
}

I was thinking that it might be a timing issue, but the shift register is being sent to with about 100khz, and the arduino is running at 16mhz, so between to edges on the clock signal there is space for about 160 instructions on the Arduino, which should be fine, rihgt?

Thanks, Robert

You seem to be doing some very weird things with pointers.
In the loop you do this:

*in_buffer =0;

But in_buffer isn't a pointer, why are you dereferencing it?

void printBIN(volatile uint16_t* binary)

Why are you using a pointer here when you don't change binary and it isn't an array. Why not just do:

void printBIN(uint16_t binary)

You don't need the volatile either as it's not going to change.

You also don't need to (nor should) call noInterrupts() and interrupts() inside your interrupt routine - interrupts are disabled during an ISR by the CPU.

The final thing I suggest is that you look up how to use the SPI hardware of the ATMega328 as a Slave. That way you don't have to do the weird things you are doing with interrupts and you are more likely to catch the data correctly.

You would essentially have to wire:
MOSI = DATA
SCLK = CLOCK
SS = OE

hey…
can any1 of u help me ???
i m interfacing cc1101 and aurdino… atmega328p…

i m configuring the cc1101 ic with aurdino… we need to configure the cc1101 registers and set frequency and power by configuring its register… i m using elechouse library …

here is my code

#include <ELECHOUSE_CC1101.h>
byte s;
void setup ()
{
Serial.begin (9600);
ELECHOUSE_cc1101.Init();
s= ELECHOUSE_cc1101.SpiReadReg(CC1101_FREQ0);
Serial.println(s);
}
void loop ()
{}

can sm1 help me ??? its urgent…

Thanks a lot Tom!

Actually I've thought of using the hardware SPI in slave mode at first as well, but then I decided that for such a simple task I could just write the capture-routine myself...

But, I think you are right and in the end, it'll be faster done with SPI hardware than by debugging my script.

P.S.: You're right with the in_buffer and binary stuff, but it's not the core of the problem, because initally i've treated in_buffer as a normal variable and binary in printBIN was given by call-by-value as you proposed. I've first changed these things when trying to find a workaround...