Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

ShiftOutX Library for Arduino
Author:  Juan Hernandez


Navigation


Current version

1.1.0 2011-03-02: Support IDE 1.0. and tested on 0021 and 0022


History

1.0.0 2012-01-13: First release.
1.1.0 2012-03-02: second release.


Description

shiftOutX is a library to control shift registers. I have used it on four 74HC595 shift registers daisy chained, but it can control up to eight registers. it does not use SPI so it does not work for PWM but if ON OFF switches are all that is needed to drive LEDs and/or Relays(probably transistors to drive the relays that is actually what I was using it for) then this will work. is just like the digital outputs on the arduino. it is made up of modified shiftOut functions from 'wiring_shift.c'. hopefully it will serve someone.

in version 1.1.0 SPI was added and pointers to byte arrays are used instead of long long integers, it is faster and PWM does work(software implemented).


Download, install and import

Download here: shiftOutX-1.0.zip
Download here: shiftOutX-1.1.0.zip

and put the shiftOutX directory in "~/Documents/Arduino/libraries (Mac) or "My documents\Arduino\libraries\ (Windows)" of Arduino IDE.

You can see an example sketch from "File -> Sketchbook -> Example -> shiftOutX -> shiftFourRegisters".

To create a new sketch, select from the menubar "Sketch->Import Library->shiftOutX". Once the library is imported, an '#include <shiftOutX.h>' line will appear at the top of your Sketch. and also, a '#include <shiftPinNo.h>'


Creation

You need to create an instance of the shiftOut class like this:

  1. //shiftOutX(byte _latchPin, byte _dataPin, byte _clockPin, byte _bitOrder, byte _NofRegisters);
  2. shiftOutX regGroupOne(8, 11, 12, MSBFIRST, 4);
  3. //yes you can attach more daisy chained registers just use
  4. //a diferent latchPin number so you can actually add another
  5. //four registers and only take up another pin from the
  6. //Arduino just declare another instance for example
  7. //regGroupTwo and use the same dataPin and clockPin.
  8. //you can use two groups of eight for a total of 128 outputs


Constants

  1. //in shiftOutX.h
  2. #define ON  1   //used to check if bit is set or not
  3. #define OFF 0   //used to check if bit is set or not
  4. #define ALL_BITS_ON 0XFFFFFFFFFFFFFFFFLL
  5.  
  6. //in shiftPinNo.h
  7. #define shPin1 1                               
  8. #define shPin2 2                               
  9. #define shPin3 4                               
  10. #define shPin4 8                               
  11. #define shPin5 16                              
  12. #define shPin6 32                              
  13. #define shPin7 64                              
  14. #define shPin8 128                             
  15. #define shPin9 256                             
  16. #define shPin10 512
  17. .... to shPin64

by ORing or ANDing this constants to an eight bit int then shifting out that integer is how this class is able to turn on or off individual pins on the shift registers.


Functions

  1. //turn on and off specific pins
  2. //self explanatory these functions turn on and off
  3. //individual shift register pins by number
  4. //the numbers are defined in shiftPinNo.h and are
  5. //named shPin1 through shPin64                 
  6. void pinOn(uint64_t shPin);
  7. void pinOff(uint64_t shPin);
  8. void allOn(void);
  9. void allOff(void);
  10. //this following function returns true if the pin
  11. //is on or false if not, it does not check the
  12. //register state
  13. //though it only checks if the bit is set or not in
  14. //the _bitString
  15. bool pinState(uint64_t shPin););

this are the other functions

  1. //if you prefer to shiftOut manually then this are the
  2. //functions to do it
  3. void shiftOut_16(byte dataPin, byte clockPin, byte bitOrder, uint16_t val); //shifts out 16 bits or to 2
  4. //registers
  5. void shiftOut_24(byte dataPin, byte clockPin, byte bitOrder, uint32_t val); //shifts out 24 bits or to 3
  6. //registers
  7. void shiftOut_32(byte dataPin, byte clockPin, byte bitOrder, uint32_t val); //i think you get the picture
  8. void shiftOut_X(byte dataPin, byte clockPin, byte bitOrder, byte NofRegisters, uint64_t val); //this one can shiftout
  9. //to 1 or all the way to 8 registers
  10. // by entering the number of registers to shiftout to you
  11. //control how many registers you want to shift to.


Example

  1. //**************************************************************//
  2. //  Name    : shiftFourRegisters                                
  3. //  Author  : Juan Hernandez
  4. //  Date    : 02 Jan 2011    
  5. //  Modified:                                  
  6. //  Version : 1.0                                            
  7. //  Notes   : Code for using four 74HC595 Shift Registers           //
  8. //          : with shiftOutX class                          
  9. //****************************************************************
  10. #include <ShiftOutX.h>
  11. #include <ShiftPinNo.h>
  12. //this are the input parameters to the class constructor
  13. //shiftOutX(_latchPin, _dataPin, _clockPin, _bitOrder, _NofRegisters);
  14. shiftOutX regOne(8, 11, 12, MSBFIRST, 4);
  15.  
  16. void setup() {
  17.  
  18. }
  19.  
  20. void loop() {  
  21.   regOne.pinOff(shPin28); //turn on pin Q3 of shift
  22.   //register number 4 since they are
  23.   //daisy chained it is pin number 28 in this case it is
  24.   //named shPin28 "shift registers pin 28"
  25.   delay(200);
  26.   regOne.pinOn(shPin28);
  27.   delay(200);  
  28.   regOne.pinOff(shPin7);
  29.   delay(200);
  30.   regOne.pinOn(shPin7);  //turn on pin Q6 of shift register
  31.   //number 1
  32.   delay(200);
  33.   regOne.pinOff(shPin13); //turn off pin Q4 of shift
  34.   //register number 2
  35.   delay(200);
  36.   regOne.pinOn(shPin13);
  37.   delay(200);
  38.   regOne.pinOff(shPin20);
  39.   delay(200);
  40.   regOne.pinOn(shPin20);
  41.   delay(200);
  42.  
  43.   if (regOne.pinState(shPin20) == true) //check if shPin20
  44.   //is on, you can use ON, OFF, or true or false
  45.   {
  46.     regOne.pinOn(shPin32); //turn on pin Q7 of shift
  47.   //register number 4
  48.     delay(1000);
  49.     regOne.pinOff(shPin32);
  50.   }
  51.   delay(1000);
  52.   regOne.allOn(); //turn all the pins on
  53.   delay(1000);
  54.   regOne.allOff(); //turn all the pins off
  55.   delay(1000);
  56.  
  57. }
  58.  


Information about this page

I used the Twitter library page as a template I hope NeoCat don't mind.

Last Modified: May 13, 2012, at 01:59 PM
By: juanh0238