-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathX9C10X.cpp
258 lines (201 loc) · 4.3 KB
/
X9C10X.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
254
255
256
//
// FILE: X9C10X.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.3
// PURPOSE: Arduino Library for X9C10X series digital potentiometer.
// URL: https://github.com/RobTillaart/X9C10X
#include "X9C10X.h"
// minimum pulse width CLOCK = ? us (datasheet);
// digitalWrite takes enough time on UNO / AVR so clock_delay == 0
// Note that if clock pulses are long enough the data pulses are too.
#ifdef __AVR__
#define X9C10X_DELAY_MICROS 0
#else
#define X9C10X_DELAY_MICROS 1
#endif
#define X9C10X_UP HIGH
#define X9C10X_DOWN LOW
#define X9C10X_MAXPOT 99
/////////////////////////////////////////////////////////
//
// MINIMALISTIC BASE CLASS
//
X9C::X9C()
{
}
void X9C::begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin)
{
_pulsePin = pulsePin;
_directionPin = directionPin;
_selectPin = selectPin;
// #7 order of the initialization does matter
// as it might introduce an unwanted STORE pulse.
// use of pull ups might be wise.
digitalWrite(_selectPin, HIGH);
digitalWrite(_pulsePin, HIGH);
digitalWrite(_directionPin, HIGH);
pinMode(_selectPin, OUTPUT);
pinMode(_pulsePin, OUTPUT);
pinMode(_directionPin, OUTPUT);
// wiper power up time. Page 5.
delayMicroseconds(500);
}
bool X9C::incr()
{
_move(X9C10X_UP);
return true;
}
bool X9C::decr()
{
_move(X9C10X_DOWN);
return true;
}
void X9C::store()
{
// _pulsePin starts default HIGH
digitalWrite(_selectPin, LOW);
#if X9C10X_DELAY_MICROS > 0
delayMicroseconds(X9C10X_DELAY_MICROS);
#endif
digitalWrite(_selectPin, HIGH);
delay(20); // Tcph page 5
}
/////////////////////////////////////////////////////////
//
// PROTECTED
//
void X9C::_move(uint8_t direction, uint8_t steps)
{
digitalWrite(_directionPin, direction);
delayMicroseconds(3); // Tdi (page 5)
// _pulsePin starts default HIGH
digitalWrite(_selectPin, LOW);
while (steps--)
{
digitalWrite(_pulsePin, HIGH);
#if X9C10X_DELAY_MICROS > 0
delayMicroseconds(X9C10X_DELAY_MICROS);
#endif
digitalWrite(_pulsePin, LOW);
#if X9C10X_DELAY_MICROS > 0
delayMicroseconds(X9C10X_DELAY_MICROS);
#endif
}
// _pulsePin == LOW, (No Store, page 7)
digitalWrite(_selectPin, HIGH);
// reset _pulsePin to default.
digitalWrite(_pulsePin, HIGH);
}
/////////////////////////////////////////////////////////
//
// X9C10X BASE CLASS
//
X9C10X::X9C10X(uint32_t maxOhm) : X9C()
{
_maxOhm = maxOhm;
}
uint8_t X9C10X::setPosition(uint8_t position, bool forced)
{
if (position > 99)
{
position = 99;
}
// force to nearest end position first to minimize number of steps.
if (forced)
{
if (position < 50)
{
_move(X9C10X_DOWN, 99);
_position = 0;
}
else
{
_move(X9C10X_UP, 99);
_position = 99;
}
}
if (position > _position)
{
_move(X9C10X_UP, position - _position);
}
if (position < _position)
{
_move(X9C10X_DOWN, _position - position);
}
_position = position;
return _position;
}
uint8_t X9C10X::getPosition()
{
return _position;
}
bool X9C10X::incr()
{
if (_position >= 99) return false;
_position++;
_move(X9C10X_UP);
return true;
}
bool X9C10X::decr()
{
if (_position == 0) return false;
_position--;
_move(X9C10X_DOWN);
return true;
}
uint8_t X9C10X::store()
{
X9C::store();
return _position;
}
uint8_t X9C10X::restoreInternalPosition(uint8_t position)
{
if (position > 99)
{
position = 99;
}
_position = position;
return _position;
}
// rounding needed!
uint32_t X9C10X::getOhm()
{
return (_maxOhm * _position + 49) / 99;
};
uint32_t X9C10X::getMaxOhm()
{
return _maxOhm;
};
// rounding needed!
uint8_t X9C10X::Ohm2Position(uint32_t value, bool invert)
{
if (value > _maxOhm) return 99;
uint8_t val = (99 * value + _maxOhm/2) / _maxOhm;
if (invert) return 99 - val;
return val;
}
uint16_t X9C10X::getType()
{
return _type;
};
/////////////////////////////////////////////////////////
//
// SPECIFIC DERIVED DEVICE CLASSES
//
X9C102::X9C102(uint32_t ohm) : X9C10X(ohm)
{
_type = 102;
}
X9C103::X9C103(uint32_t ohm) : X9C10X(ohm)
{
_type = 103;
}
X9C104::X9C104(uint32_t ohm) : X9C10X(ohm)
{
_type = 104;
}
X9C503::X9C503(uint32_t ohm) : X9C10X(ohm)
{
_type = 503;
}
// -- END OF FILE --