0

I have problems getting the raspi/wiringPi to work with a HCF4094 shift register.

I am using the following code part:

#include <wiringPi.h>
#include <wiringShift.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

int LEDClockPin  = 5;
int LEDDataPin   = 3;
int LEDStrobePin = 4;

void setup()
{
    wiringPiSetup();
    pinMode(LEDClockPin, OUTPUT);
    pinMode(LEDDataPin, OUTPUT);
    pinMode(LEDStrobePin, OUTPUT);
}

void writeLEDs(uint8_t value) {
    printf("%#8x\n", value);
    std::string s;
    shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, value);
    digitalWrite(LEDStrobePin,HIGH);
    delay(2);
    digitalWrite(LEDStrobePin,LOW);
    std::getline(std::cin, s);
}

int main(int argc, char *argv[]) {
    setup();
    writeLEDs((1<<0));
    writeLEDs((1<<1));
    writeLEDs((1<<2));
    writeLEDs((1<<3));
    writeLEDs((1<<4));
    writeLEDs((1<<5));
    writeLEDs((1<<6));
}

and I have connected VDD + OE (Output Enable) on +5V, VSS on GND, and Strobe, Data and Clock to the according pins on the Pi.

Here are the pins that are high after each press of [enter]:

value - expected pins - real pins
1     - Q1            - Q1
2     - Q2            - Q2
4     - Q3            - Q2, Q6
8     - Q4            - Q4
16    - Q5            - Q6, Q7
32    - Q6            - Q7
64    - Q7            - Q7

And I really don't get what I am doing wrong ... Any ideas?

  • 2
    Try adding another delay(2); after digitalWrite(LEDStrobePin,LOW);. The reason you might need that is you are calling that function consecutively. – Janka Jul 6 '18 at 0:48
  • @Janka: You just made my day :) That really did the trick, but I really don't know why ... If you post your comment as answer, I am happy to accept it ;) – david Jul 7 '18 at 9:17
1

Unroll your calls, then it's

[...]
digitalWrite(LEDStrobePin,HIGH);
delay(2);
digitalWrite(LEDStrobePin,LOW);
[...]
digitalWrite(LEDStrobePin,HIGH);
delay(2);
digitalWrite(LEDStrobePin,LOW);
[...]
digitalWrite(LEDStrobePin,HIGH);
delay(2);
digitalWrite(LEDStrobePin,LOW);

If you don't put a delay(2); after digitalWrite(LEDStrobePin,LOW);, the switch from high to low back to high may be too short for the 4094 to notice. That depends on the stray capacitance of your layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.