WiringPiShift Library and 74HC164N register


5 posts
by Martwana » Thu Dec 27, 2012 10:35 pm
Hello yet again

Time to learn C now. Managing to control LED's via GPIO no problem using a separate pins, but I wanted to learn how to use shift registers.

I found an instructable on using them to drive LED's which I thought was perfect, so I bought a bundle of 74HC164N shift registers so the tutorial uses so there was less things to go wrong.

However, the python code didn't work for some reason, everything was wired exactly as it is online but not working.

I then seen that wiringPi has a Shift library, so I tried to use that to get the LEDs to flash just once, but the code I wrote(which compiles fine) just doesn't work at all. Not a hint of working.

I read that the unused input must be set high, so it is and all the rest is set as outputs.

Can someone please explain how to get these things working, as I'm clearly missing out something. I tried copying the Python script and rewriting it in C myself, and had a few flickers, but nothing like the instructable shows.

http://www.instructables.com/id/Using-a-shift-register-with-Raspberry-Pi/

Here is the crap code.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringShift.h>
#include <stdint.h>


int inputA = 15;
int inputB = 16;
int clock = 9;
int clear = 7;

setupPins(){
        pinMode(inputA, OUTPUT);
        pinMode(inputB, OUTPUT);
        pinMode(clock, OUTPUT);
        pinMode(clear, OUTPUT);
        digitalWrite(inputB, HIGH);
        digitalWrite(clear, HIGH);
        delay(10);
        digitalWrite(clear, LOW);
}


int main (void){

if (wiringPiSetup () == -1){
    exit (1);
}

setupPins();

int running = 1;

int i = 1;

while (running = 1){
   shiftOut(inputA, clock, LSBFIRST, i);
   delay(1500);
   i++;
   }
}
Posts: 30
Joined: Sun Dec 16, 2012 1:12 pm
by Martwana » Fri Dec 28, 2012 12:43 am
Not to worry, SOLVED, I'll post details on how I did it tomorrow.
Posts: 30
Joined: Sun Dec 16, 2012 1:12 pm
by gordon@drogon.net » Fri Dec 28, 2012 4:38 pm
Martwana wrote:Not to worry, SOLVED, I'll post details on how I did it tomorrow.


Anything to do with the while line by any chance?

Always good to compile with -Wall to catch things like that.

Another thing to note is that shiftOut() goes at 'full speed' - which is about 10MHz on a normal Pi. Keep the wires as short as possible to minimise any interference, crosstalk ,etc. It's on my to-do list to introduce some slow-downs into it, if needed.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by Martwana » Fri Dec 28, 2012 4:43 pm
Nothing to do with it, i know it was missing one = but i rewrote the script totally. Im not in the house yet, but ill post the script when i get in.
Posts: 30
Joined: Sun Dec 16, 2012 1:12 pm
by Martwana » Fri Dec 28, 2012 8:47 pm
Okay, here's the script i made, that works fine.
Basically, it initializes the 4 pins for the shift register in setupPins(), and tick() moves the clock HIGH then LOW.

clear(), is clears the shit register by turning the clear pin LOW then HIGH and ticking the clock.

In main(), first, the register is cleared, then wiringPi's shiftOut() to write out to the register.

I don't really think there is anything else for me to tell, except that it works. I used this software to control 8 led's just like the instructable I liked in the first post. I calculated the value to send to the register using a simple binary to decimal calculation.

For anyone who doesn't understand it, 1 = on, 0 = off.

Has a unique bit, so for all 8 LEDs(or each of the shift registers 8 outputs) to power on, 11111111 is sent to the register, which turns then all HIGH. 11111111 in decimal is 255 so, since my LEDs were in 2 rows of 4, I used 11110000 which is 240, and to swap rows with the binary 00001111 which is 15 in decimal.

Surely if anyone is reading this forum, then they know binary to decimal lol, but if anyone doesn't understand anything, ask me ;)

Code: Select all
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <wiringShift.h>

int inputA = 15;
int inputB = 16;
int clockPin = 9;
int clearPin = 7;
int value;

setupPins(){
        int pins[4] = {inputA, inputB, clockPin, clearPin};
        int i = 0;
        for (i=0;i<sizeof(pins);i++){
                pinMode(pins[i], OUTPUT);
        }
        digitalWrite(inputA, HIGH);
        digitalWrite(inputB, HIGH);
        digitalWrite(clockPin, LOW);
        digitalWrite(clearPin, HIGH);
        tick();
}

tick(){
        digitalWrite(clockPin, HIGH);
        delay(10);
        digitalWrite(clockPin, LOW);
}

clear(){
        digitalWrite(clearPin, LOW);
        tick();
        digitalWrite(clearPin, HIGH);
}

int main(void){

        if (wiringPiSetup () == -1){
                exit (1) ;
        }

        setupPins();
        int running = 1;
        for (;;){
                clear();
 shiftOut(inputB, clockPin, LSBFIRST, 240);
                delay(200);
                clear();
                shiftOut(inputB, clockPin, LSBFIRST, 15);
                delay(200);
        }
}
Posts: 30
Joined: Sun Dec 16, 2012 1:12 pm