Skip to content

Commit eca7633

Browse files
committed
fix #7 initialization bug
1 parent cab2e26 commit eca7633

File tree

6 files changed

+67
-21
lines changed

6 files changed

+67
-21
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ 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 X9C10X base class and
17+
four derived classes for specific digital potentiometer.
1718

1819
| type | resistance | tested | notes |
1920
|:------:|:----------:|:-------:|:-------------|
@@ -68,8 +69,12 @@ Note: **begin()** has a hard coded 500uS delay so the device can wake up.
6869
Note: multiple devices can be controlled, just by giving them an unique selectPin.
6970
This behaviour is similar to the SPI select pin.
7071

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.
72+
- **void setPosition(uint8_t position, bool forced = false)** sets the wiper
73+
to a position between 0 and 99.
74+
The movement is relative to the current (cached) position.
75+
If forced is set to true, the wiper will be moved to the closest end position and
76+
from there moved to the requested position.
77+
The cached position is ignored and the new position will be cached.
7378
- **uint8_t getPosition()** returns the current position.
7479
- **bool incr()** moves one position up (if possible).
7580
Returns true if moved and false if already at end position.
@@ -78,6 +83,9 @@ Returns true if moved and false if already at end position.
7883
- **uint32_t getOhm()** returns the position expressed in Ohm.
7984
The returned value does depend on the value passed in the constructor.
8085
- **uint32_t getMaxOhm()** returns the maximum value ( = parameter from constructor).
86+
- **uint32_t Ohm2Position(uint32_t value, bool invert = false)**
87+
Calculates (with rounding) the position nearest to the requested value.
88+
If **invert == true** it uses the other wiper end as reference.
8189

8290

8391
#### Store
@@ -87,6 +95,8 @@ Warning: use with care.
8795
- **uint8_t store()** stores the current position in the NVRAM of the device,
8896
and returns the current position so it can later be used as position parameter for **begin()**.
8997

98+
Note: this function blocks for 20 milliseconds.
99+
90100
If one uses an incorrect parameter position in **begin()** the internal state and the device
91101
will probably be out of sync. One way to sync is call **begin()** with the right parameters.
92102
The other way is to call **setPosition(0)** followed by **setPosition(99)** (or vice versa)
@@ -156,10 +166,13 @@ Note: check datasheet for the range of the max voltage allowed.
156166

157167
## Future
158168

169+
- update documentation
159170
- test different platforms
160171
- improve the hardcoded 500us delay in **begin()**
161172
- add error codes ?
162-
- test **store()**
173+
- add examples
174+
- voltage divider
175+
- investigate and test **store()**
163176
- in the constructor rename **Ohm** parameter to value?
164177
- The potentiometer can be used as a voltage divider (see above)
165178
so a better parameter name could be the anonymous **value**.

X9C10X.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: X9C10X.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.3
4+
// VERSION: 0.2.0
55
// PURPOSE: Arduino Library for X9C10X series digital potentiometer.
66
// URL: https://github.com/RobTillaart/X9C10X
77
//
@@ -12,7 +12,9 @@
1212
// rounding in getOhm(), documentation
1313
// 0.1.3 2022-02-22 add forced parameter to setPosition()
1414
// incr() and decr() return bool (made a step)
15-
//
15+
// 0.2.0 2022-07-09 fix #7 incorrect signal during initialize
16+
// update readme
17+
// add uint8_t Ohm2Position()
1618

1719

1820
#include "X9C10X.h"
@@ -30,6 +32,10 @@
3032
#define X9C10X_DOWN LOW
3133

3234

35+
/////////////////////////////////////////////////////////
36+
//
37+
// PUBLIC
38+
//
3339
X9C10X::X9C10X(uint32_t maxOhm)
3440
{
3541
_maxOhm = maxOhm;
@@ -42,16 +48,19 @@ void X9C10X::begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin, ui
4248
_directionPin = directionPin;
4349
_selectPin = selectPin;
4450

51+
// #7 order of the initialization does matter
52+
// as it might introduce an unwanted STORE pulse.
53+
digitalWrite(_selectPin, HIGH);
54+
digitalWrite(_pulsePin, HIGH);
55+
digitalWrite(_directionPin, HIGH);
56+
4557
pinMode(_pulsePin, OUTPUT);
4658
pinMode(_directionPin, OUTPUT);
4759
pinMode(_selectPin, OUTPUT);
4860

49-
digitalWrite(_pulsePin, HIGH);
50-
digitalWrite(_directionPin, HIGH);
51-
digitalWrite(_selectPin, HIGH);
52-
5361
// wiper power up time. Page 5.
54-
delayMicroseconds(500);
62+
// slightly more efficient than delayMicros()
63+
while (micros() < 500);
5564

5665
// reset defined position.
5766
_position = position;
@@ -123,9 +132,32 @@ uint8_t X9C10X::store()
123132
}
124133

125134

126-
////////////////////////////////////////////////////////////////////
135+
// rounding needed!
136+
uint32_t X9C10X::getOhm()
137+
{
138+
return (_maxOhm * _position + 49) / 99;
139+
};
140+
141+
142+
uint32_t X9C10X::getMaxOhm()
143+
{
144+
return _maxOhm;
145+
};
146+
147+
148+
// rounding needed!
149+
uint8_t X9C10X::Ohm2Position(uint32_t value, bool invert)
150+
{
151+
if (value > _maxOhm) return 99;
152+
uint8_t val = (99 * value + _maxOhm/2) / _maxOhm;
153+
if (invert) return 99 - val;
154+
return val;
155+
}
156+
157+
158+
/////////////////////////////////////////////////////////
127159
//
128-
// PRIVATE
160+
// PROTECTED
129161
//
130162
void X9C10X::_move(uint8_t direction, uint8_t steps)
131163
{

X9C10X.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
//
33
// FILE: X9C10X.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.3
5+
// VERSION: 0.2.0
66
// PURPOSE: Arduino Library for X9C10X series digital potentiometer.
77
// URL: https://github.com/RobTillaart/X9C10X
88

99

1010
#include "Arduino.h"
1111

12-
#define X9C10X_LIB_VERSION (F("0.1.3"))
12+
#define X9C10X_LIB_VERSION (F("0.2.0"))
1313

1414

1515
class X9C10X
@@ -35,10 +35,10 @@ class X9C10X
3535
uint8_t store();
3636

3737
// current resistance in ohm.
38-
// Q: rounding needed?
39-
uint32_t getOhm() { return (_maxOhm * _position + 49) / 99; };
40-
// misc
41-
uint32_t getMaxOhm() { return _maxOhm; };
38+
uint32_t getOhm();
39+
uint32_t getMaxOhm();
40+
uint8_t Ohm2Position(uint32_t value, bool invert = false);
41+
4242

4343
// Q: needed?
4444
uint16_t getType() { return _type; };

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ decr KEYWORD2
1717

1818
getOhm KEYWORD2
1919
getMaxOhm KEYWORD2
20+
Ohm2Position KEYWORD2
2021

2122
store KEYWORD2
2223
getType KEYWORD2

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/X9C10X.git"
1717
},
18-
"version": "0.1.3",
18+
"version": "0.2.0",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=X9C10X
2-
version=0.1.3
2+
version=0.2.0
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino Library for X9C10X series digital potentiometer.

0 commit comments

Comments
 (0)