Shift Register code

Hi,

I was reading the shift register tutorial on sparkfun (74HC165)

sparkfun shift register

? = areas I’m not sure

This is the code

// HARDWARE CONNECTIONS
// Connect the following pins between your Arduino and the 74HC165 Breakout Board
// Connect pins A-H to 5V or GND or switches or whatever
const int data_pin = 11; // Connect Pin 11 to SER_OUT (serial data out)
const int shld_pin = 8; // Connect Pin 8 to SH/!LD (shift or active low load)
const int clk_pin = 12; // Connect Pin 12 to CLK (the clock that times the shifting)
const int ce_pin = 9; // Connect Pin 9 to !CE (clock enable, active low)

byte incoming; // Variable to store the 8 values loaded from the shift register

// The part that runs once
void setup() 
{                
  // Initialize serial to gain the power to obtain relevant information, 9600 baud
  Serial.begin(9600);

  // Initialize each digital pin to either output or input
  // We are commanding the shift register with each pin with the exception of the serial
  // data we get back on the data_pin line.
  pinMode(shld_pin, OUTPUT);
  pinMode(ce_pin, OUTPUT);
  pinMode(clk_pin, OUTPUT);
  pinMode(data_pin, INPUT);

  // Required initial states of these two pins according to the datasheet timing diagram
  digitalWrite(clk_pin, HIGH);
  digitalWrite(shld_pin, HIGH);

}

// The part that runs to infinity and beyond
void loop() {

  incoming = read_shift_regs(); // Read the shift register, it likes that

  // Print out the values being read from the shift register
  Serial.println("\nThe incoming values of the shift register are: ");
  Serial.print("ABCDEFGH : ");
  print_byte(incoming); // Print every 1 and 0 that correlates with A through H
  //Serial.println(incoming,BIN); // This way works too but leaves out the leading zeros

  delay(2000); // Wait for some arbitrary amount of time

}

// This code is intended to trigger the shift register to grab values from it's A-H inputs
byte read_shift_regs()
{
  byte the_shifted = 0;  // An 8 bit number to carry each bit value of A-H

  // Trigger loading the state of the A-H data lines into the shift register
  digitalWrite(shld_pin, LOW);
  delayMicroseconds(5); // Requires a delay here according to the datasheet timing diagram
  digitalWrite(shld_pin, HIGH);
  delayMicroseconds(5);

  // Required initial states of these two pins according to the datasheet timing diagram
  pinMode(clk_pin, OUTPUT);
  pinMode(data_pin, INPUT);
  digitalWrite(clk_pin, HIGH);
  digitalWrite(ce_pin, LOW); // Enable the clock

  // Get the A-H values
  the_shifted = shiftIn(data_pin, clk_pin, MSBFIRST);
  digitalWrite(ce_pin, HIGH); // Disable the clock

  return the_shifted;

}

// A function that prints all the 1's and 0's of a byte, so 8 bits +or- 2
void print_byte(byte val)
{
    byte i;
    for(byte i=0; i<=7; i++)
    {
      Serial.print(val >> i & 1, BIN); // Magic bit shift, if you care look up the <<, >>, and & operators
    }
    Serial.print("\n"); // Go to the next line, do not collect $200
}

Can anyone help me understand it??

I read the brief explanation from the site but I’m confused

When the parallel load input (PL) is LOW the
data from D0 to D7 is loaded into the shift register asynchronously. When PL is HIGH data
enters the register serially at DS. When the clock enable input (CE) is LOW data is shifted
on the LOW-to-HIGH transitions of the CP input. A HIGH on CE will disable the CP input.

From what I understand after googling , that only the input at D7 will be available at DATA.
Then for the each next positive clock pulse these data is shifted to data pin in the order D6, D5, D4, D3, D2, D1 and finally D0. (please correct me if I’m wrong)

When program reaches loop for the first time

void loop() {

  incoming = read_shift_regs(); // Read the shift register, it likes that

  // Print out the values being read from the shift register
  Serial.println("\nThe incoming values of the shift register are: ");
  Serial.print("ABCDEFGH : ");
  print_byte(incoming); // Print every 1 and 0 that correlates with A through H
  //Serial.println(incoming,BIN); // This way works too but leaves out the leading zeros

  delay(2000); // Wait for some arbitrary amount of time

}

It’s gonna call the function read_shift_regs();

byte read_shift_regs()
{
  byte the_shifted = 0;  // An 8 bit number to carry each bit value of A-H

  // Trigger loading the state of the A-H data lines into the shift register
  digitalWrite(shld_pin, LOW);
  delayMicroseconds(5); // Requires a delay here according to the datasheet timing diagram
  digitalWrite(shld_pin, HIGH);
  delayMicroseconds(5);

  // Required initial states of these two pins according to the datasheet timing diagram
  pinMode(clk_pin, OUTPUT);
  pinMode(data_pin, INPUT);
  digitalWrite(clk_pin, HIGH);
  digitalWrite(ce_pin, LOW); // Enable the clock

  // Get the A-H values
  the_shifted = shiftIn(data_pin, clk_pin, MSBFIRST);
  digitalWrite(ce_pin, HIGH); // Disable the clock

  return the_shifted;

}

Let’s say 8 inputs ar
H G F E D C B A or(D7-D0)
1 0 0 0 0 0 0 1

SO when the program runs for the first time it make the PL pin high then after a delay PL made low. SO it load the state of all the pins to its internal register. Then clock is made high and CE made low so whatever the input at H it will available at DATA pin. so DATA =1 (H)
or does all the data available at the DATA pin.(10000001)?

From what I understand after googling , that only the input at H will be available at DATA (since it’s first clock pulse that’s why I said H(1) will be available at DATA) please correct me if I’m wrong.

 the_shifted = shiftIn(data_pin, clk_pin, MSBFIRST);

What does this line do? I checked the reference section of arduino but didn’t understand?

I think whatever the value in the DATA will be assigned to the_shifted

if DATA =1 ,then the_shifted = 1? or is it 10000000 or 00000001 ?

Then it returns the_shifted

Then in the void loop this value will be assigned to incoming

incoming = read_shift_regs();

the call another function with incoming as it’s parameter

print_byte(incoming);

void print_byte(byte val)
{
    byte i;
    for(byte i=0; i<=7; i++)
    {
      Serial.print(val >> i & 1, BIN); // Magic bit shift, if you care look up the <<, >>, and & operators
    }
    Serial.print("\n"); // Go to the next line, do not collect $200
}

What does this function do? i didn’t understand anything
we have val = incoming = 1(since program is running the first loop, so far only one clock pulse.

After this program will display the value H(1) (will it?)?

And loop starts again the_shifted will become G(0), E(0), F(0), D(0), C(0), B(0), A(1) in each loop ?

SO how all the inputs are displayed in the serial monitor at the same time??
Is there 8 serial prints on the serial monitor for each bit?? or one display like THIS

The incoming values of the shift register are:
** ABCDEFGH:10000001**

Can someone explain me with an example 8 bit input
I don’t have much knowledge in arduino?

Hope someone can give me a detailed description of program, what happens in each loop??

Thanks

74HC165_pinouts.png

the 74HC165 is good for gathering the status of a number of inputs, like buttons, connected on A to H pins, and read them in one go --> you send a clock signal to lock in the values and then you read each pin value sequentially. As each pin in either HIGH or LOW, you can represent the pins value with 0 or 1. If you connect only 8 lines then the 1 and 0 can fit in a byte, each bit representing the status of one of the entry.

Does it help?

Too complicated. Try it this way:

digitalWrite (latchPin, LOW); // SH/LD pin
digitalWrite (latchPin,  HIGH); // capture the data at the input pins
byte incoming = shiftIn(dataPin, clockPin, MSBFIRST);

http://www.ti.com/lit/ds/symlink/sn74hc165.pdf

J-M-L:
the 74HC165 is good for gathering the status of a number of inputs, like buttons, connected on A to H pins, and read them in one go → you send a clock signal to lock in the values and then you read each pin value sequentially. As each pin in either HIGH or LOW, you can represent the pins value with 0 or 1. If you connect only 8 lines then the 1 and 0 can fit in a byte, each bit representing the status of one of the entry.

Does it help?

@J-M-L

No, I learned how the shift register works , There’s an excellent video tutorial in Youtube

What I don’t understand is how the program works??

CrossRoads:
Too complicated.
Try it this way:

digitalWrite (latchPin, LOW); // SH/LD pin

digitalWrite (latchPin,  HIGH); // capture the data at the input pins
byte incoming = shiftIn(dataPin, clockPin, MSBFIRST);



http://www.ti.com/lit/ds/symlink/sn74hc165.pdf
byte incoming = shiftIn(dataPin, clockPin, MSBFIRST);

I don’t understand how this function works

If the 8 input lines have value

D7 D6 D5 D4 D3 D2 D1 D0

1 0 0 1 1 0 0 1

When the program runs for the first time

what will be the value of incoming

Is it 1 (D7) ? and acquire each bit as the loop repeats

or 10011001(entire byte)

Can you help me understand how the program works?

shiftIn() reads the 8 bits one after the other and puts them into a byte, so using it once gives you all 8 bits read from the shift register.