shiftIn usage

I'm working with a system that has 6 cascaded 74HC595 shift registers and I'm trying to get the Arduino to monitor the data stream that runs them and display the contents of the shift registers on a serial monitor window. The host system repeatedly sends the entire data stream out about 10 times a second. The latch clock is normally high, and it goes low just before each data transfer begins and returns high just after it's complete.

The code I wrote doesn't read the stream properly. When the data being sent is unchanging (repeating the same pattern over and over) I'm seeing all kinds of changes in the results in the serial window. I wonder if my usage of shiftIn is not correct? I'm a bit confused as to the timing of the command. Does shiftIn cause the processor to remain at this command until the clock pin has received 8 positive-going clock pulses? Or does it time-out after a certain period and continue running code, or what?

Here's my sketch:

const int dataPin = 12;
const int clockPin = 11;
const int latchPin = 10;
byte value[6];

void setup()
{
  pinMode(dataPin, INPUT);
  pinMode(clockPin, INPUT);
  pinMode(latchPin, INPUT);
  Serial.begin(9600);
}



void loop()
{
  while (!digitalRead(latchPin))  {}      // Wait for current data transfer cycle to end, if it's in progress
  while (digitalRead(latchPin))  {}       // Wait for the next data transfer cycle to begin
  
// Read the 6 bytes from data stream and write them into value array
  for(int dataByte=0; dataByte<6; dataByte++)  {
    value[dataByte] = shiftIn(dataPin, clockPin, LSBFIRST);
  }
  
// Read the 6 bytes from value array and print their binary equivalents over serial
  for(int dataByte=0; dataByte<6; dataByte++)  {
    for (int dataBit=0; dataBit<8; dataBit++)  {
      if ((value[dataByte] >> dataBit) & 1)  Serial.print("1");
      else  Serial.print("0");
    }
    Serial.print(" ");
  }
  Serial.println();
}

In your sketch, i am not seeing the working code of SHIFTIN function?

It's in there; it's just hiding. It's in the section that reads the data stream:

// Read the 6 bytes from data stream and write them into value array
for(int dataByte=0; dataByte<6; dataByte++) {
value[dataByte] = shiftIn(dataPin, clockPin, LSBFIRST);
}

What are pins 10, 11, and 12 connected to?

jraskell:
What are pins 10, 11, and 12 connected to?

These 3 pins are connected to the lines used by the host system to drive the 6 cascaded 74HC595's. Pin 10 is connected to the latch (strobe) pin, 11 to the clock pin, and 12 to the data pin.

I can see the signals on an oscilloscope well enough to determine the states of the outputs on each shift register, but for some reason the Arduino is having trouble with the task. The shift registers themselves are working fine, whether the Arduino is connected to the circuit or not.

How fast it the data being streamed?

There's quite a delay between detecting the latch going low and starting to read data, if the data is running at high speed you would start reading half way through a byte.


Rob

shiftin uses a clock OUTPUT to shift data from a parallel-in/serial-out shift register (to add input pins).

To capture the data being sent to a shift register by another processor: Connect the shift clock to an interrupt pin. In the interrupt service routine grab the value of the data pin and put it in a circular buffer. Use the latch pin to signal when the buffer is ready to read (and to set the buffer pointer back to the start).

shiftin uses a clock OUTPUT

Yes of course, who knows what shiftIn() was doing with the latch pin, probably trying to drive it against the other source.


Rob

Aha!! No wonder it isn't working for me!

The description of shiftIn found in the online language reference wasn't thorough enough for me to understand. Not to mention that it's mysteriously missing altogether from the reference pages included with the IDE.

Because I originally set the pinMode of the clock pin as an input, I would guess that the function was just turning the pull-up resistor on and off, unless the function itself changes the data direction of the clock pin to an output.

In the meantime I've had better success receiving the data by directly reading the port register bits that correspond to the pins that the signals are connected to. I'll also have to give the interrupt technique a try. Thank you for the suggestion, and for explaining how shiftIn really works!