Skip to content

Commit 2cd4241

Browse files
authored
fix #7 initialization bug (#8)
* fix #7 initialization bug * remove position from begin() * extract X9C base class * update documentation
1 parent cab2e26 commit 2cd4241

File tree

12 files changed

+414
-128
lines changed

12 files changed

+414
-128
lines changed

README.md

Lines changed: 101 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ Arduino Library for X9C10X series digital potentiometer.
1313

1414
## Description
1515

16-
This experimental library provides a X9C10X base class and four derived classes for specific digital potentiometer.
16+
This **experimental** library provides a X9C base class, a X9C10X class and
17+
four derived classes for specific digital potentiometer.
1718

18-
| type | resistance | tested | notes |
19+
| class | resistance | tested | notes |
1920
|:------:|:----------:|:-------:|:-------------|
21+
| X9C | no support | Y | minimalistic base class |
2022
| X9C10X | generic | Y | base class |
2123
| X9C102 | 1 KΩ | N | 10 \* 10^2 |
2224
| X9C103 | 10 KΩ | Y | 10 \* 10^3 |
@@ -26,7 +28,23 @@ This experimental library provides a X9C10X base class and four derived classes
2628

2729
_Note: Ω Ohm sign = ALT-234_
2830

29-
The library keeps cache of the position.
31+
The X9C10X object keeps track of the position of the potentiometer,
32+
but the user should set it with **setPosition(pos, true);**
33+
Otherwise the library and device will probably not be in sync.
34+
35+
Since 0.2.0 the library has a minimal X9C class. See below.
36+
37+
38+
### Multiple devices
39+
40+
Multiple devices can be controlled by assigning them an unique selectPin (CS).
41+
This behaviour is similar to the SPI select pin.
42+
43+
It should be possible to share the U/D and INC lines (not tested) when controlling multiple X9C devices.
44+
45+
Note: one should select one device at a time.
46+
Sharing a CS pin or sending pulses to multiple devices at the same time will
47+
cause the library and devices get oout of sync.
3048

3149

3250
### PINOUT
@@ -42,58 +60,92 @@ The library keeps cache of the position.
4260
// GND | o o | Rwiper
4361
// +--------+
4462
//
45-
// INC pulses
46-
// U/D UP = 1 DOWN = 0
47-
// VCC +5V
48-
//
63+
// INC pulses
64+
// U/D UP = 1 DOWN = 0
65+
// VCC +5V
66+
// GND ground
67+
// RH resistor high end
68+
// RL resistor low end
69+
// Rwiper resistor wiper
70+
// CS chip select
4971
```
5072

73+
It is advised to use pull up resistors - e.g. 10 KΩ - on the CS, UD and INC line.
74+
This will help the lines to start in a defined state and will
75+
improve the signal quality.
76+
The pulses esp. INC can be quite short, so especially with longer lines the
77+
quality can become an issue. (not investigated further)
78+
5179

5280
## Interface
5381

5482

55-
## X9C10X base class
83+
## X9C base class
5684

57-
Use **\#include "X9C10X.h"**
85+
This is the most minimalistic base class.
86+
It does not provide position information but that is sometimes enough.
5887

59-
- **X9C10X(uint32_t Ohm = 10000)** Constructor, default initializes the resistance to 10000 Ω.
60-
To calibrate one can fill in any other value e.g. 9950 Ω.
61-
- **void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin, uint8_t position = 0)**
62-
sets the pins used by the device, and resets the position (default to 0).
63-
The position parameter allows to start the device with a previous stored position.
64-
Use this position with care.
88+
Use **\#include "X9C10X.h"**
6589

90+
- **X9C()** Constructor.
91+
- **void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin)**
92+
sets the INC, UD and CS pins used by the device.
6693
Note: **begin()** has a hard coded 500uS delay so the device can wake up.
94+
- **void incr()** moves one position up (if possible).
95+
- **void decr()** moves one position down (if possible).
96+
- **void store()** stores the current position in NV-RAM to be used at the next restart.
97+
Does not return a value as the position cannot be read from the device.
98+
So the user should keep track of the position if needed.
6799

68-
Note: multiple devices can be controlled, just by giving them an unique selectPin.
69-
This behaviour is similar to the SPI select pin.
70-
71-
- **void setPosition(uint8_t position, bool forced = false)** sets the wiper to a position between 0 and 99. The movement is relative to the current (cached) position.
72-
If forced is set to true, the cached position is ignored and the new position will be cached.
73-
- **uint8_t getPosition()** returns the current position.
74-
- **bool incr()** moves one position up (if possible).
75-
Returns true if moved and false if already at end position.
76-
- **bool decr()** moves one position down (if possible).
77-
Returns true if moved and false if already at end position.
78-
- **uint32_t getOhm()** returns the position expressed in Ohm.
79-
The returned value does depend on the value passed in the constructor.
80-
- **uint32_t getMaxOhm()** returns the maximum value ( = parameter from constructor).
81100

101+
## X9C10X base class
82102

83-
#### Store
103+
This class is derived from the X9C class but adds position, Ohm and type information.
84104

85-
Warning: use with care.
105+
Use **\#include "X9C10X.h"**
86106

107+
- **X9C10X(uint32_t Ohm = 10000)** Constructor, default initializes the resistance to 10000 Ω.
108+
To calibrate one can fill in any other (measured) value e.g. 9950 Ω.
109+
This can be useful e.g. if one sets a fixed resistor parallel over the X9C one.
110+
- **void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin)**
111+
sets the INC, UD and CS pins used by the device.
112+
Note: **begin()** has a hard coded 500uS delay so the device can wake up.
113+
- **void setPosition(uint8_t position, bool forced = false)** sets the wiper
114+
to a position between 0 and 99.
115+
The movement is relative to the current (internal) position.
116+
If forced is set to true, the wiper will be moved to the closest "end" position
117+
and from there moved to the requested position.
118+
The internal position is replaced by the new position.
119+
- **uint8_t getPosition()** returns the current (internal) position. 0..99
120+
- **bool incr()** moves one position up (if possible).
121+
Returns true if moved and false if already at end position
122+
according to internal position math.
123+
- **bool decr()** moves one position down (if possible).
124+
Returns true if moved and false if already at begin position
125+
according to internal position math.
87126
- **uint8_t store()** stores the current position in the NVRAM of the device,
88-
and returns the current position so it can later be used as position parameter for **begin()**.
127+
and returns the current position so it can later be used as position parameter for **setPosition()**.
128+
Warning: use with care (not tested).
129+
Note: **store()** blocks for 20 milliseconds.
130+
131+
Note: **begin()** changed in 0.2.0 as the implicit parameter position
132+
was removed for the explicit function call to **setPosition()**.
133+
If **setPosition()** is not called, the device uses the last stored
134+
value as position. Unfortunately the position cannot be read from the device.
135+
This will result in a mismatch between the internal position and the
136+
external one.
89137

90-
If one uses an incorrect parameter position in **begin()** the internal state and the device
91-
will probably be out of sync. One way to sync is call **begin()** with the right parameters.
92-
The other way is to call **setPosition(0)** followed by **setPosition(99)** (or vice versa)
93-
to get a defined internal state.
94138

139+
#### Ohm
95140

96-
#### Calibration
141+
- **uint32_t getOhm()** returns the position expressed in Ohm.
142+
The returned value does depend on the value passed in the constructor
143+
and the current position.
144+
Note: value returned might differ a bit from the actual value, see below.
145+
- **uint32_t getMaxOhm()** returns the maximum value ( = parameter from constructor). Convenience function.
146+
- **uint32_t Ohm2Position(uint32_t value, bool invert = false)**
147+
Calculates (with rounding) the position nearest to the requested value.
148+
If **invert == true** it uses the other wiper end as reference.
97149

98150
One can measure the resistance between RH and RL and use this value to set
99151
in the constructor. Although the value will not differ much from the datasheet
@@ -109,8 +161,8 @@ There are 4 derived classes, each with a other (appropriate) default value for t
109161
- **X9C104(uint32_t Ohm = 100000)** idem.
110162
- **X9C503(uint32_t Ohm = 50000)** idem.
111163

112-
These classes have the same interface as the base class.
113-
The only difference is that the type is set.
164+
These classes have the same interface as the X9C10X base class.
165+
The only difference is that the type is set to a non zero value.
114166

115167

116168
#### Performance
@@ -151,21 +203,27 @@ The digital potentiometer (esp 10 KΩ and up) can be used as a voltage divider.
151203
Connect RL to GND and RH to +5V and you can do 5V in 100 steps of ~0.05V
152204
A voltage of **3V3** would be **setPosition(66)**.
153205

154-
Note: check datasheet for the range of the max voltage allowed.
206+
Note: check datasheet for the range of the max voltage and current allowed.
155207

156208

157209
## Future
158210

211+
- update documentation
159212
- test different platforms
160-
- improve the hardcoded 500us delay in **begin()**
161213
- add error codes ?
162-
- test **store()**
214+
- add examples
215+
- investigate and test **store()**
216+
- test multiple devices configuration
217+
218+
219+
#### won't
220+
221+
- voltage divider example
163222
- in the constructor rename **Ohm** parameter to value?
164223
- The potentiometer can be used as a voltage divider (see above)
165224
so a better parameter name could be the anonymous **value**.
166225
- **getOhm()** ==> **getValue()**
167226
- **getMaxOhm()** ==> **getMaxValue()**
168227
- think milliVolt, ohm, lux, speed, etc.
169-
User can do this too with **getPosition()**
170-
228+
User can do this too with **getPosition() * factor**
171229

0 commit comments

Comments
 (0)