Go Down

Topic: shiftIn function (Read 64 times) previous topic - next topic


Sep 26, 2015, 10:15 am Last Edit: Sep 26, 2015, 10:19 am by spirit
hi all.

im useing arduino and C for allmost 2years now.
one of the first projects, have been to get the 74HC165 working on arduino.

i never got this working nice, also never realy needed it.
until now. so i needed to solve this.

i see many projects on the WEB but they use 4pins. and i get strange results from them.
and i know i should be possible with 3 pins.

but i got it working perfect now, and i have been comparing my program and the one that comes with the IDE "shiftIn"

the problem with the "shiftIn" function is that it allways misses the first bit.

here be differnce between my function and the "shift├Ćn" function from the IDE

IDE "shiftIn" function
Code: [Select]
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
  uint8_t value = 0;
  uint8_t i;

  for (i = 0; i < 8; ++i) {
    digitalWrite(clockPin, HIGH);
    if (bitOrder == LSBFIRST)
      value |= digitalRead(dataPin) << i;
      value |= digitalRead(dataPin) << (7 - i);
    digitalWrite(clockPin, LOW);
  return value;

my fnction
Code: [Select]
byte shiftin(byte dataPin, byte clkPin, byte bitOrder) {
  byte shiftvalue = 0;
  byte x;
  for (x = 0; x < 8; x++) {
    digitalWrite(clkPin, LOW);
    if (bitOrder == LSBFIRST)
      shiftvalue |= digitalRead(dataPin) << x;
      shiftvalue |= digitalRead(dataPin) << (7 - x);
    digitalWrite(clkPin, HIGH);
  return shiftvalue;

the only differnce is that i keep the clkpin:
clk_LOW -> get data -> clk_HIGH

and the IDE function does:
clk_HIGH -> get data - > clk_LOW

this way you miss the first bit.

kind regards, Emile B

Go Up