-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShiftOut595.h
206 lines (194 loc) · 5.03 KB
/
ShiftOut595.h
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* ShiftOut595.h
*
* Created: 5/28/2014 7:15:56 AM
* Author: Ketil Wright
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* 595 pinout
*
* ------\/------
* Qb | 1 16| Vcc
* Qc | 2 15| Qa
* Qd | 3 14| data
* Qe | 4 13| output enable (OE active low)
* Qf | 5 12| latch (RCLK)
* Qg | 6 11| clock
* Qh | 7 10| reset active low (empties Shift register if pulled low)
* GND | 8 9| overflow (serial out)
* --------------
*
* Minimal connections:
* 1) data from a MCU digital output
* 2) latch from a MCU digital output
* 3) clock from a MCU digital output
* 4) output enable to GND (active low): eg output is always available on Qa -> Qh
* 5) reset to Vcc: eg never in reset
* 6) Qa -> Qh -> parallel outputs
*
* Further control is available if output and reset are connected.
*/
#pragma once
#include <stdint.h>
#include <Arduino.h>
#define noConnect -1
template<uint8_t numChips>
class ShiftOutMulti595
{
const int m_dataPin;
const int m_latchPin;
const int m_clockPin;
const int m_outputEnablePin;
const int m_resetPin;
uint8_t m_current[numChips];
// support read/modify/write in writePin
typedef uint8_t currentState[numChips];
public:
ShiftOutMulti595(int dataPin,
int latchPin,
int clockPin,
int outputEnablePin = noConnect,
int resetPin = noConnect)
:
m_dataPin(dataPin),
m_latchPin(latchPin),
m_clockPin(clockPin),
m_outputEnablePin(outputEnablePin),
m_resetPin(resetPin)
{
for(int i = 0; i < numChips; i++) m_current[i] = 0;
}
void init();
void write(uint8_t values[numChips], uint8_t bitOrder);
void writePin(uint32_t pin, bool on);
currentState const& allOff(bool latchIt);
currentState & state() const { return m_current;}
};
template<uint8_t numChips>
inline void ShiftOutMulti595<numChips>::init()
{
pinMode(m_dataPin, OUTPUT);
digitalWrite(m_dataPin, LOW);
pinMode(m_latchPin, OUTPUT);
digitalWrite(m_latchPin, LOW);
pinMode(m_clockPin, OUTPUT);
digitalWrite(m_clockPin, LOW);
if(noConnect != m_outputEnablePin)
{
pinMode(m_outputEnablePin, OUTPUT);
digitalWrite(m_outputEnablePin, HIGH); // turn off output (active low)
}
if(noConnect != m_resetPin)
{
pinMode(m_resetPin, OUTPUT);
// reset everything
digitalWrite(m_resetPin, HIGH);
digitalWrite(m_resetPin, LOW);
digitalWrite(m_resetPin, HIGH);
}
// clear everything out
allOff(true);
if(noConnect != m_outputEnablePin)
{
pinMode(m_outputEnablePin, OUTPUT);
digitalWrite(m_outputEnablePin, LOW); // turn on output (active low)
}
}
template<uint8_t numChips>
inline void ShiftOutMulti595<numChips>::write(uint8_t values[numChips], uint8_t bitOrder)
{
for(int i = 0; i < numChips; i++) m_current[i] = 0;
digitalWrite(m_latchPin, LOW);
for(int part = 0; part < numChips; part++)
{
uint8_t & val = values[part];
for (uint8_t i = 0; i < 8; i++)
{
uint16_t bitVal = (LSBFIRST == bitOrder)
?
val & (1 << i)
:
val & (1 << (15 - i));
digitalWrite(m_dataPin, bitVal ? HIGH : LOW);
m_current[part] |= bitVal;
digitalWrite(m_clockPin, HIGH);
delay(1);
digitalWrite(m_clockPin, LOW);
}
}
digitalWrite(m_latchPin, HIGH);
}
template<uint8_t numChips>
inline void ShiftOutMulti595<numChips>::writePin(uint32_t pin, bool on)
{
if(pin > (numChips * 8)) return;
digitalWrite(m_latchPin, LOW);
for(int part = 0; part < numChips; part++)
{
for (uint8_t i = 0; i < 8; i++)
{
if(((8 * part) + i) == pin)
{
if(on)
{
digitalWrite(m_dataPin, HIGH);
m_current[part] |= (1 << (pin - part * 8));
}
else
{
digitalWrite(m_dataPin, LOW);
m_current[part] &= ~(1 << (pin - part * 8));
}
}
else
{
if(m_current[part] & (1 << i))
{
digitalWrite(m_dataPin, HIGH);
}
else
{
digitalWrite(m_dataPin, LOW);
}
}
digitalWrite(m_clockPin, HIGH);
digitalWrite(m_clockPin, LOW);
}
}
digitalWrite(m_latchPin, HIGH);
}
template<uint8_t numChips>
inline typename ShiftOutMulti595<numChips>::currentState const& ShiftOutMulti595<numChips>::allOff(bool latchIt)
{
// clear everything out
if(latchIt) digitalWrite(m_latchPin, LOW);
for(int part = 0; part < numChips; part++)
{
for(int i = 0; i < 8; i++)
{
digitalWrite(m_dataPin, LOW);
digitalWrite(m_clockPin, HIGH);
delay(1);
digitalWrite(m_clockPin, LOW);
}
}
if(latchIt)
{
digitalWrite(m_latchPin, HIGH);
memset(m_current, 0, sizeof(m_current));
}
return m_current;
}