forked from bracci/Qlockthree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShiftRegister.cpp
185 lines (172 loc) · 4.02 KB
/
ShiftRegister.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* ShiftRegister
* Mit der ShiftRegister-Klasse wird das Ausgeben der Bitmuster auf die
* 74HC595-Shift-Register gekapselt.
*
* @mc Arduino/RBBB
* @autor Christian Aschoff / caschoff _AT_ mac _DOT_ com
* @version 1.7
* @created 24.2.2011
*/
#include "ShiftRegister.h"
#include "Configuration.h"
#include "Debug.h"
/**
* Initialisierung mit den Pins fuer Serial-Data, Serial-Clock und Store-Clock (Latch)
*/
ShiftRegister::ShiftRegister(byte dataPin, byte clockPin, byte latchPin) {
#ifdef SHIFTREGISTER_TURBO
DEBUG_PRINTLN(F("ShiftRegister is in TURBO-MODE."));
DEBUG_FLUSH();
#else
DEBUG_PRINTLN(F("ShiftRegister is in SLOW-MODE."));
DEBUG_FLUSH();
#endif
// slow version
_dataPin = dataPin;
_clockPin = clockPin;
_latchPin = latchPin;
pinMode(_latchPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
pinMode(_dataPin, OUTPUT);
// fast version
_dataBit = digitalPinToBitMask(dataPin);
_dataPort = digitalPinToPort(dataPin);
_dataOut = portOutputRegister(_dataPort);
_clockBit = digitalPinToBitMask(clockPin);
_clockPort = digitalPinToPort(clockPin);
_clockOut = portOutputRegister(_clockPort);
_latchBit = digitalPinToBitMask(latchPin);
_latchPort = digitalPinToPort(latchPin);
_latchOut = portOutputRegister(_latchPort);
}
/**
* Ein WORD (16 Bit) ausgeben
*/
void ShiftRegister::shiftOut(word data) {
#ifdef OPTIMIZED_FOR_DARKNESS
if (data == (word) 65535) {
fastDigitalWriteToData(HIGH);
for (byte i = 0; i < 16; i++) {
fastDigitalWriteToClock(LOW);
fastDigitalWriteToClock(HIGH);
}
return;
}
if (data == (word) 0) {
fastDigitalWriteToData(LOW);
for (byte i = 0; i < 16; i++) {
fastDigitalWriteToClock(LOW);
fastDigitalWriteToClock(HIGH);
}
return;
}
#endif
#ifdef SHIFTREGISTER_TURBO
for (byte b = 0; b < 16; b++) {
fastDigitalWriteToClock(LOW);
if (data & (1 << b)) {
fastDigitalWriteToData(HIGH);
} else {
fastDigitalWriteToData(LOW);
}
fastDigitalWriteToClock(HIGH);
}
#else
for (byte b = 0; b < 16; b++) {
digitalWrite(_clockPin, LOW);
if (data & (1 << b)) {
digitalWrite(_dataPin, HIGH);
} else {
digitalWrite(_dataPin, LOW);
}
digitalWrite(_clockPin, HIGH);
}
#endif
}
/**
* Ein einzelnes Bit rausshiften...
*/
void ShiftRegister::shiftOutABit(boolean bitIsSet) {
#ifdef SHIFTREGISTER_TURBO
fastDigitalWriteToClock(LOW);
if (bitIsSet) {
fastDigitalWriteToData(HIGH);
} else {
fastDigitalWriteToData(LOW);
}
fastDigitalWriteToClock(HIGH);
#else
digitalWrite(_clockPin, LOW);
if (bitIsSet) {
digitalWrite(_dataPin, HIGH);
} else {
digitalWrite(_dataPin, LOW);
}
digitalWrite(_clockPin, HIGH);
#endif
}
/**
* Vorbereitung fuer die Ausgabe
*/
void ShiftRegister::prepareShiftregisterWrite() {
#ifdef SHIFTREGISTER_TURBO
// fastDigitalWriteToData(LOW);
// fastDigitalWriteToClock(LOW);
fastDigitalWriteToLatch(LOW);
#else
// digitalWrite(_dataPin, LOW);
// digitalWrite(_clockPin, LOW);
digitalWrite(_latchPin, LOW);
#endif
}
/**
* Abschliessen der Ausgabe
*/
void ShiftRegister::finishShiftregisterWrite() {
#ifdef SHIFTREGISTER_TURBO
// fastDigitalWriteToData(LOW);
// fastDigitalWriteToClock(LOW);
fastDigitalWriteToLatch(HIGH);
#else
// digitalWrite(_dataPin, LOW);
// digitalWrite(_clockPin, LOW);
digitalWrite(_latchPin, HIGH);
#endif
}
/**
* Digitale Ausgaenge im Turbo-Mode!
*/
void ShiftRegister::fastDigitalWriteToData(uint8_t val) {
uint8_t oldSREG = SREG;
cli();
if (val == LOW) {
*_dataOut &= ~_dataBit;
} else {
*_dataOut |= _dataBit;
}
sei();
SREG = oldSREG;
}
void ShiftRegister::fastDigitalWriteToLatch(uint8_t val) {
uint8_t oldSREG = SREG;
cli();
if (val == LOW) {
*_latchOut &= ~_latchBit;
} else {
*_latchOut |= _latchBit;
}
sei();
SREG = oldSREG;
}
void ShiftRegister::fastDigitalWriteToClock(uint8_t val) {
uint8_t oldSREG = SREG;
cli();
if (val == LOW) {
*_clockOut &= ~_clockBit;
} else {
*_clockOut |= _clockBit;
}
sei();
SREG = oldSREG;
}