0% found this document useful (1 vote)
267 views

Arduino Playground - ShiftOutX Library

The shiftOutX library allows control of up to eight daisy chained shift registers from an Arduino without using SPI. It provides functions to turn individual pins on the registers on and off, check pin states, and shift out data to the registers. An example sketch shows how to use the shiftOutX class to control four daisy chained 74HC595 shift registers, turning pins on the registers on and off with delays.

Uploaded by

Winex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
267 views

Arduino Playground - ShiftOutX Library

The shiftOutX library allows control of up to eight daisy chained shift registers from an Arduino without using SPI. It provides functions to turn individual pins on the registers on and off, check pin states, and shift out data to the registers. An example sketch shows how to use the shiftOutX class to control four daisy chained 74HC595 shift registers, turning pins on the registers on and off with delays.

Uploaded by

Winex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Arduino Playground ­ shiftOutX Library

ShiftOutX Library for Arduino


Author: Juan Hernandez

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. //in shiftPinNo.h
6. #define shPin1 1                               
7. #define shPin2 2                               
8. #define shPin3 4                               
9. #define shPin4 8                               
10. #define shPin5 16                              
11. #define shPin6 32                              
12. #define shPin7 64                              
13. #define shPin8 128                             
14. #define shPin9 256                             
15. #define shPin10 512
16. .... 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););

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. void setup() {
16. }
17. void loop() {  
18.   regOne.pinOff(shPin28); //turn on pin Q3 of shift
19.   //register number 4 since they are
20.   //daisy chained it is pin number 28 in this case it is
21.   //named shPin28 "shift registers pin 28"
22.   delay(200);
23.   regOne.pinOn(shPin28);
24.   delay(200);  
25.   regOne.pinOff(shPin7);
26.   delay(200);
27.   regOne.pinOn(shPin7);  //turn on pin Q6 of shift register
28.   //number 1
29.   delay(200);
30.   regOne.pinOff(shPin13); //turn off pin Q4 of shift
31.   //register number 2
32.   delay(200);
33.   regOne.pinOn(shPin13);
34.   delay(200);
35.   regOne.pinOff(shPin20);
36.   delay(200);
37.   regOne.pinOn(shPin20);
38.   delay(200);
39.   if (regOne.pinState(shPin20) == true) //check if shPin20
40.   //is on, you can use ON, OFF, or true or false
41.   {
42.     regOne.pinOn(shPin32); //turn on pin Q7 of shift
43.   //register number 4
44.     delay(1000);
45.     regOne.pinOff(shPin32);
46.   }
47.   delay(1000);
48.   regOne.allOn(); //turn all the pins on
49.   delay(1000);
50.   regOne.allOff(); //turn all the pins off
51.   delay(1000);
52. }

You might also like