-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCP40xx.cpp
253 lines (176 loc) · 5.96 KB
/
MCP40xx.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Microchip 63 taps Single Digital Potentiometer
Simple two-wire UP/DOWN interface
Author: dzalf - Daniel Melendrez
Date: June 2020 (COVID-19 Vibes)
Version: 1.1.1 - Initial deployment
1.1.2 - General cleanup. Implemented new and overloaded methods that allow
to select the desired tap. It is now possible to select the nominal
resistance value or override it by setting the measured value
*/
#include "MCP40xx.h"
// Constructor
MCP40xx::MCP40xx(uint8_t CS, uint8_t UD) {
_CSPin = CS;
_UDPin = UD;
_debug = false;
}
MCP40xx::MCP40xx(uint8_t CS, uint8_t UD, bool DEBUG) {
_CSPin = CS;
_UDPin = UD;
_debug = DEBUG;
}
// Methods
void MCP40xx::setup() {
pinMode(_UDPin, OUTPUT);
pinMode(_CSPin, OUTPUT);
digitalWrite(_CSPin, HIGH); // _CSPin is active low, keep it HIGH at the beginning
digitalWrite(_UDPin, HIGH); // _UDPin -> U is active HIGH, keep it LOW at the beginning
}
void MCP40xx::begin() {
_tapPointer = MCP40xx_DEFAULT_TAP_COUNT;
_nominalResistance = MCP40xx_NOMINAL_RESISTANCE;
if(_debug){
Serial.println(F("Initializing MCP40xx..."));
}
}
void MCP40xx::begin(float nomRes) {
_tapPointer = MCP40xx_DEFAULT_TAP_COUNT;
_nominalResistance = nomRes;
if(_debug){
Serial.println(F("Initializing MCP40xx..."));
}
}
float MCP40xx::wiper() {
return (_tapPointer / MCP40xx_TAP_NUMBER);
}
void MCP40xx::inc() { // return wiper count!
if ((_tapPointer < MCP40xx_TAP_NUMBER)) {
// Note: The digitalWrite command is slow enough for the device. No additional delays are needed
unsigned long _startIncTime = micros();
digitalWrite(_UDPin, HIGH); // We want the wiper to go UP
//After at least 500 ns bring _CSPin low
digitalWrite(_CSPin, LOW); // Start the command
// /* Increment command*/
//*Subsequent rising edges of _UDPin move the wiper */
// One tap
digitalWrite(_UDPin, LOW);
digitalWrite(_UDPin, HIGH); // Last _UDPin state is HIGH
// Here the wiper should have increased already
// Leave the _CSPin pin ready for next instruction
digitalWrite(_CSPin, HIGH); // Release _CSPin to avoid changing the Pot
_tapPointer++;
if (_tapPointer >= MCP40xx_TAP_NUMBER)
_tapPointer = MCP40xx_TAP_NUMBER;
_incDelay = micros() - _startIncTime;
}
}
void MCP40xx::dec() {
if ((_tapPointer > 0)) {
unsigned long _startDecTime = micros();
digitalWrite(_UDPin, LOW); // We want the wiper to go DOWN
//After at least 500 ns bring _CSPin low
digitalWrite(_CSPin, LOW); // Start the "move wiper" command
// /* Decrement command*/
// One tap
digitalWrite(_UDPin, HIGH);
digitalWrite(_UDPin, LOW);
// Here the wiper should have decreased already
//*Subsequent falling edges of _UDPin move the wiper */
// Leave the _CSPin pin ready for next instruction
digitalWrite(_CSPin, HIGH);
_tapPointer--;
if (_tapPointer <= 0)
_tapPointer = 0;
_decDelay = micros() - _startDecTime;
}
}
unsigned long MCP40xx::incMicros() {
return _incDelay;
}
unsigned long MCP40xx::decMicros() {
return _decDelay;
}
unsigned long MCP40xx::setMicros() {
return _setDelay;
}
int MCP40xx::taps() {
return _tapPointer; // value within [1-64] that points to the taps between resistors [0,63]
}
uint8_t MCP40xx::setValue(float desiredR) {
float _currentValue;
float _distance;
int _tapTarget;
unsigned long _startSetTime = micros();
_tapTarget = round((desiredR * MCP40xx_TAP_NUMBER) / (_nominalResistance));
_distance = abs(_tapPointer - _tapTarget);
if (_debug) {
Serial.print("Distance to target: ");
Serial.println(_distance);
Serial.print("Target tap: ");
Serial.println(_tapTarget);
}
if (_tapTarget < _tapPointer) {
for (int i = _tapPointer; i > _tapTarget; i--) {
dec();
}
} else if (_tapTarget > _tapPointer) {
for (int i = _tapPointer; i < _tapTarget; i++) {
inc();
}
} else {
// Leave everything where it is
}
_setDelay = micros() - _startSetTime;
return _tapTarget;
}
uint8_t MCP40xx::setTap(uint8_t desiredTap) {
float _currentValue;
float _distance;
int _tapTarget;
unsigned long _startSetTime = micros();
_tapTarget = desiredTap;
_distance = abs(_tapPointer - _tapTarget);
if (_debug) {
Serial.print("Distance to target: ");
Serial.println(_distance);
Serial.print("Target tap: ");
Serial.println(_tapTarget);
}
if (_tapTarget < _tapPointer) {
for (int i = _tapPointer; i > _tapTarget; i--) {
dec();
}
} else if (_tapTarget > _tapPointer) {
for (int i = _tapPointer; i < _tapTarget; i++) {
inc();
}
} else {
// No touchy...Leave everything where it is...drink beer or coffee
}
_setDelay = micros() - _startSetTime;
return _tapTarget;
}
void MCP40xx::zeroWiper() {
// It is possible to do this with the latest setTap() method
/*
for (int i = MCP40xx_DEFAULT_TAP_COUNT ; i >= 0; i--) {
dec();
}
*/
setTap(0);
_tapPointer = 0;
}
void MCP40xx::maxWiper() {
// It is possible to do this with the latest setTap() method
/*
for (int i = _tapPointer; i <= MCP40xx_TAP_NUMBER; i++) {
inc();
}
*/
setTap(MCP40xx_TAP_NUMBER);
_tapPointer = MCP40xx_TAP_NUMBER;
}
float MCP40xx::readValue() {
return (_tapPointer / MCP40xx_TAP_NUMBER) * (_nominalResistance);
}