|
1 | 1 | # ArduinoShiftOut |
2 | | -Small library for writing out values to a 74HC595 8 bit shift register |
| 2 | +This is a small library for writing out values to a 74HC595 8 bit shift register. |
| 3 | + |
| 4 | +The Arduino has only a limited amount of pins, therefore it might happen that you run out of pins. |
| 5 | +For this case, the 74HC595 comes to the rescue. It is an 8 bit shift register. It allows you to write out 8 values by only using 3 pins on your Arduino. Furthermore you can daisy-chain several shift registers by still only using 3 Arduino Pins. |
| 6 | + |
| 7 | +## Easy install (zip import) |
| 8 | +The easiest way to install this library is by downloading the newest release and then importing it. |
| 9 | +You don't have to unzip it. Just open your Arduino IDE and navigate to *Sketch* > *Include Library* > *Add .ZIP* and then select the zip file. |
| 10 | + |
| 11 | +## Manual install |
| 12 | +Of course you can also install this library manually. To do so, download the newest release and unzip it. Then you have to copy the `ShiftOut` folder (NOT the `ShiftOut-x.y.z` folder) and copy it to your Arduino library folder: |
| 13 | +* Windows: `My Documents\Arduino\libraries\` |
| 14 | +* Mac and Linux: `Documents/Arduino/libraries/` |
| 15 | + |
| 16 | +After that you just have to restart your Arduino IDE. |
| 17 | + |
| 18 | +## Usage |
| 19 | +If you have installed this library, you can include it by navigating to *Sketch* > *Include Library* > *ShiftOut*. This will add the line `#include <ShiftOut.h>` to your sketch (of course you could also write this line manually). |
| 20 | + |
| 21 | +Now you can actually use this library: |
| 22 | +``` c++ |
| 23 | +#include <ShiftOut.h> |
| 24 | + |
| 25 | +// Init ShiftOut instance with a single chip |
| 26 | +// If you want to use more shift registers, just modify this number |
| 27 | +ShiftOut<1> shift; |
| 28 | + |
| 29 | +int led = 0; |
| 30 | + |
| 31 | +void setup() { |
| 32 | + Serial.begin(9600); |
| 33 | + // declare pins: data, clock, latch |
| 34 | + shift.begin(4, 6, 5); |
| 35 | +} |
| 36 | + |
| 37 | +void loop() { |
| 38 | + shift.setAllLow(); // set all to zero |
| 39 | + shift.setHigh(led); // set led to high |
| 40 | + shift.write(); // write out to 74HC595 |
| 41 | + led = (led + 1) % shift.getDataWidth(); // increment led |
| 42 | + delay(250); // wait a bit |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +If you want to use two shift registers, you only have to change the declaration from `ShiftOut<1> shift;` to `ShiftOut<2> shift;` and then you just daisy chain your 74HC595. |
| 48 | + |
| 49 | +# API |
| 50 | +Depending on the number of chips, this library will use different data types. |
| 51 | +If you are only using one chip, the type `ShiftType` will be an `unsigned byte` (`uint8_t`). For two chips it will be an `unsigned int` (`uint16_t`). For three and four chips it will be an `unsigned long` (`uint32_t`) and for 5 to 8 chips it will be an `unsigned long long` (`uint64_t`). More than eight chips are not supported yet. |
| 52 | + |
| 53 | +This function must be called in the `setup` function. It is used to tell the library the pins it should use: |
| 54 | +``` c++ |
| 55 | +void begin(int data, int clock, int latch) |
| 56 | +``` |
| 57 | +
|
| 58 | +Returns the number of outputs (bits in the state): |
| 59 | +``` c++ |
| 60 | +uint16_t getDataWidth() |
| 61 | +``` |
| 62 | + |
| 63 | +Gets/sets the value of the output with the given `id`. The value should be either `0` or `1`: |
| 64 | +``` c++ |
| 65 | +boolean get(int id) |
| 66 | +void set(int id, int value) |
| 67 | +``` |
| 68 | +
|
| 69 | +Sets/gets or inverts the output with the given `id`: |
| 70 | +``` c++ |
| 71 | +void setHigh(int id) |
| 72 | +void setLow(int id) |
| 73 | +void invert(int id) |
| 74 | +``` |
| 75 | + |
| 76 | +Sets all outputs to high/low or inverts all: |
| 77 | +``` |
| 78 | +void setAllHigh() |
| 79 | +void setAllLow() |
| 80 | +void invert() |
| 81 | +``` |
| 82 | + |
| 83 | +Finally writes out all data to the chips. *Must* be called to update the actual output. The state that was written out is returned: |
| 84 | +``` c++ |
| 85 | +ShiftType write() |
| 86 | +``` |
| 87 | + |
| 88 | +Sets the state to the given parameter and calls `write()`: |
| 89 | +``` |
| 90 | +ShiftType write(ShiftType state) |
| 91 | +``` |
0 commit comments