-
Notifications
You must be signed in to change notification settings - Fork 30
/
SR74HC595.cpp
44 lines (33 loc) · 1.11 KB
/
SR74HC595.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef SR74HC595_h
#define SR74HC595_h
#include "Arduino.h"
class SR74HC595 {
public:
/**
* Setup the shift register interface. The "clock pin" is for the shift register
* clock (SCK), the "latch pin" is for the storage register clock (RCK).
* https://www.arduino.cc/en/Tutorial/ShiftOut
*/
void init(byte dataPin, byte clockPin, byte latchPin) {
this->dataPin = dataPin;
this->clockPin = clockPin;
this->latchPin = latchPin;
pinMode(this->dataPin, OUTPUT);
pinMode(this->clockPin, OUTPUT);
pinMode(this->latchPin, OUTPUT);
}
/**
* Writes 8 bits to the shift register, and enables the storage register when finished (latch).
* The default order is MSBFIRST (most significant bit first), but it can be changed to LSBFIRST.
*/
void write(byte value, uint8_t order = MSBFIRST) {
digitalWrite(latchPin, LOW); // So the outputs don't change while sending in bits
shiftOut(this->dataPin, this->clockPin, order, value);
digitalWrite(latchPin, HIGH); // The outputs update at once
}
private:
byte dataPin;
byte clockPin;
byte latchPin;
};
#endif