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:
//shiftOutX(byte _latchPin, byte _dataPin, byte _clockPin, byte _bitOrder, byte _NofRegisters);
shiftOutX regGroupOne(8, 11, 12, MSBFIRST, 4);
//yes you can attach more daisy chained registers just use
//a diferent latchPin number so you can actually add another
//four registers and only take up another pin from the
//Arduino just declare another instance for example
//regGroupTwo and use the same dataPin and clockPin.
//you can use two groups of eight for a total of 128 outputs
Constants
//in shiftOutX.h
#define ON 1 //used to check if bit is set or not
#define OFF 0 //used to check if bit is set or not
#define ALL_BITS_ON 0XFFFFFFFFFFFFFFFFLL
//in shiftPinNo.h
#define shPin1 1
#define shPin2 2
#define shPin3 4
#define shPin4 8
#define shPin5 16
#define shPin6 32
#define shPin7 64
#define shPin8 128
#define shPin9 256
#define shPin10 512
.... 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
//turn on and off specific pins
//self explanatory these functions turn on and off
//individual shift register pins by number
//the numbers are defined in shiftPinNo.h and are
//named shPin1 through shPin64
void pinOn(uint64_t shPin);
void pinOff(uint64_t shPin);
void allOn(void);
void allOff(void);
//this following function returns true if the pin
//is on or false if not, it does not check the
//register state
//though it only checks if the bit is set or not in
//the _bitString
bool pinState(uint64_t shPin););
this are the other functions
//if you prefer to shiftOut manually then this are the
//functions to do it
void shiftOut_16(byte dataPin, byte clockPin, byte bitOrder, uint16_t val); //shifts out 16 bits or to 2
//registers
void shiftOut_24(byte dataPin, byte clockPin, byte bitOrder, uint32_t val); //shifts out 24 bits or to 3
//registers
void shiftOut_32(byte dataPin, byte clockPin, byte bitOrder, uint32_t val); //i think you get the picture
void shiftOut_X(byte dataPin, byte clockPin, byte bitOrder, byte NofRegisters, uint64_t val); //this one can shiftout
//to 1 or all the way to 8 registers
// by entering the number of registers to shiftout to you
//control how many registers you want to shift to.
Example
//**************************************************************//
// Name : shiftFourRegisters
// Author : Juan Hernandez
// Date : 02 Jan 2011
// Modified:
// Version : 1.0
// Notes : Code for using four 74HC595 Shift Registers //
// : with shiftOutX class
//****************************************************************
#include <ShiftOutX.h>
#include <ShiftPinNo.h>
//this are the input parameters to the class constructor
//shiftOutX(_latchPin, _dataPin, _clockPin, _bitOrder, _NofRegisters);
shiftOutX regOne(8, 11, 12, MSBFIRST, 4);
void setup() {
}
void loop() {
regOne.pinOff(shPin28); //turn on pin Q3 of shift
//register number 4 since they are
//daisy chained it is pin number 28 in this case it is
//named shPin28 "shift registers pin 28"
delay(200);
regOne.pinOn(shPin28);
delay(200);
regOne.pinOff(shPin7);
delay(200);
regOne.pinOn(shPin7); //turn on pin Q6 of shift register
//number 1
delay(200);
regOne.pinOff(shPin13); //turn off pin Q4 of shift
//register number 2
delay(200);
regOne.pinOn(shPin13);
delay(200);
regOne.pinOff(shPin20);
delay(200);
regOne.pinOn(shPin20);
delay(200);
if (regOne.pinState(shPin20) == true) //check if shPin20
//is on, you can use ON, OFF, or true or false
{
regOne.pinOn(shPin32); //turn on pin Q7 of shift
//register number 4
delay(1000);
regOne.pinOff(shPin32);
}
delay(1000);
regOne.allOn(); //turn all the pins on
delay(1000);
regOne.allOff(); //turn all the pins off
delay(1000);
}
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
|