0.2.0 X9C10X

This commit is contained in:
rob tillaart 2022-07-15 12:26:27 +02:00
parent 3e88bac095
commit 5beb765ee7
12 changed files with 415 additions and 129 deletions

View File

@ -13,10 +13,12 @@ Arduino Library for X9C10X series digital potentiometer.
## Description
This experimental library provides a X9C10X base class and four derived classes for specific digital potentiometer.
This **experimental** library provides a X9C base class, a X9C10X class and
four derived classes for specific digital potentiometer.
| type | resistance | tested | notes |
| class | resistance | tested | notes |
|:------:|:----------:|:-------:|:-------------|
| X9C | no support | Y | minimalistic base class |
| X9C10X | generic | Y | base class |
| X9C102 | 1 KΩ | N | 10 \* 10^2 |
| X9C103 | 10 KΩ | Y | 10 \* 10^3 |
@ -26,7 +28,23 @@ This experimental library provides a X9C10X base class and four derived classes
_Note: Ω Ohm sign = ALT-234_
The library keeps cache of the position.
The X9C10X object keeps track of the position of the potentiometer,
but the user should set it with **setPosition(pos, true);**
Otherwise the library and device will probably not be in sync.
Since 0.2.0 the library has a minimal X9C class. See below.
### Multiple devices
Multiple devices can be controlled by assigning them an unique selectPin (CS).
This behaviour is similar to the SPI select pin.
It should be possible to share the U/D and INC lines (not tested) when controlling multiple X9C devices.
Note: one should select one device at a time.
Sharing a CS pin or sending pulses to multiple devices at the same time will
cause the library and devices get oout of sync.
### PINOUT
@ -42,58 +60,92 @@ The library keeps cache of the position.
// GND | o o | Rwiper
// +--------+
//
// INC pulses
// U/D UP = 1 DOWN = 0
// VCC +5V
//
// INC pulses
// U/D UP = 1 DOWN = 0
// VCC +5V
// GND ground
// RH resistor high end
// RL resistor low end
// Rwiper resistor wiper
// CS chip select
```
It is advised to use pull up resistors - e.g. 10 KΩ - on the CS, UD and INC line.
This will help the lines to start in a defined state and will
improve the signal quality.
The pulses esp. INC can be quite short, so especially with longer lines the
quality can become an issue. (not investigated further)
## Interface
## X9C base class
This is the most minimalistic base class.
It does not provide position information but that is sometimes enough.
Use **\#include "X9C10X.h"**
- **X9C()** Constructor.
- **void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin)**
sets the INC, UD and CS pins used by the device.
Note: **begin()** has a hard coded 500uS delay so the device can wake up.
- **void incr()** moves one position up (if possible).
- **void decr()** moves one position down (if possible).
- **void store()** stores the current position in NV-RAM to be used at the next restart.
Does not return a value as the position cannot be read from the device.
So the user should keep track of the position if needed.
## X9C10X base class
This class is derived from the X9C class but adds position, Ohm and type information.
Use **\#include "X9C10X.h"**
- **X9C10X(uint32_t Ohm = 10000)** Constructor, default initializes the resistance to 10000 Ω.
To calibrate one can fill in any other value e.g. 9950 Ω.
- **void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin, uint8_t position = 0)**
sets the pins used by the device, and resets the position (default to 0).
The position parameter allows to start the device with a previous stored position.
Use this position with care.
To calibrate one can fill in any other (measured) value e.g. 9950 Ω.
This can be useful e.g. if one sets a fixed resistor parallel over the X9C one.
- **void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin)**
sets the INC, UD and CS pins used by the device.
Note: **begin()** has a hard coded 500uS delay so the device can wake up.
Note: multiple devices can be controlled, just by giving them an unique selectPin.
This behaviour is similar to the SPI select pin.
- **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.
If forced is set to true, the cached position is ignored and the new position will be cached.
- **uint8_t getPosition()** returns the current position.
- **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 (internal) position.
If forced is set to true, the wiper will be moved to the closest "end" position
and from there moved to the requested position.
The internal position is replaced by the new position.
- **uint8_t getPosition()** returns the current (internal) position. 0..99
- **bool incr()** moves one position up (if possible).
Returns true if moved and false if already at end position.
Returns true if moved and false if already at end position
according to internal position math.
- **bool decr()** moves one position down (if possible).
Returns true if moved and false if already at end position.
- **uint32_t getOhm()** returns the position expressed in Ohm.
The returned value does depend on the value passed in the constructor.
- **uint32_t getMaxOhm()** returns the maximum value ( = parameter from constructor).
#### Store
Warning: use with care.
Returns true if moved and false if already at begin position
according to internal position math.
- **uint8_t store()** stores the current position in the NVRAM of the device,
and returns the current position so it can later be used as position parameter for **begin()**.
and returns the current position so it can later be used as position parameter for **setPosition()**.
Warning: use with care (not tested).
Note: **store()** blocks for 20 milliseconds.
If one uses an incorrect parameter position in **begin()** the internal state and the device
will probably be out of sync. One way to sync is call **begin()** with the right parameters.
The other way is to call **setPosition(0)** followed by **setPosition(99)** (or vice versa)
to get a defined internal state.
Note: **begin()** changed in 0.2.0 as the implicit parameter position
was removed for the explicit function call to **setPosition()**.
If **setPosition()** is not called, the device uses the last stored
value as position. Unfortunately the position cannot be read from the device.
This will result in a mismatch between the internal position and the
external one.
#### Calibration
#### Ohm
- **uint32_t getOhm()** returns the position expressed in Ohm.
The returned value does depend on the value passed in the constructor
and the current position.
Note: value returned might differ a bit from the actual value, see below.
- **uint32_t getMaxOhm()** returns the maximum value ( = parameter from constructor). Convenience function.
- **uint32_t Ohm2Position(uint32_t value, bool invert = false)**
Calculates (with rounding) the position nearest to the requested value.
If **invert == true** it uses the other wiper end as reference.
One can measure the resistance between RH and RL and use this value to set
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
- **X9C104(uint32_t Ohm = 100000)** idem.
- **X9C503(uint32_t Ohm = 50000)** idem.
These classes have the same interface as the base class.
The only difference is that the type is set.
These classes have the same interface as the X9C10X base class.
The only difference is that the type is set to a non zero value.
#### Performance
@ -151,21 +203,27 @@ The digital potentiometer (esp 10 KΩ and up) can be used as a voltage divider.
Connect RL to GND and RH to +5V and you can do 5V in 100 steps of ~0.05V
A voltage of **3V3** would be **setPosition(66)**.
Note: check datasheet for the range of the max voltage allowed.
Note: check datasheet for the range of the max voltage and current allowed.
## Future
- update documentation
- test different platforms
- improve the hardcoded 500us delay in **begin()**
- add error codes ?
- test **store()**
- add examples
- investigate and test **store()**
- test multiple devices configuration
#### won't
- voltage divider example
- in the constructor rename **Ohm** parameter to value?
- The potentiometer can be used as a voltage divider (see above)
so a better parameter name could be the anonymous **value**.
- **getOhm()** ==> **getValue()**
- **getMaxOhm()** ==> **getMaxValue()**
- think milliVolt, ohm, lux, speed, etc.
User can do this too with **getPosition()**
User can do this too with **getPosition() * factor**

View File

@ -1,7 +1,7 @@
//
// FILE: X9C10X.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.2.0
// PURPOSE: Arduino Library for X9C10X series digital potentiometer.
// URL: https://github.com/RobTillaart/X9C10X
//
@ -12,7 +12,11 @@
// rounding in getOhm(), documentation
// 0.1.3 2022-02-22 add forced parameter to setPosition()
// incr() and decr() return bool (made a step)
//
// 0.2.0 2022-07-09 fix #7 incorrect signal during initialize
// remove position parameter from begin()
// to make setting position more explicit.
// update readme
// add uint8_t Ohm2Position()
#include "X9C10X.h"
@ -30,31 +34,103 @@
#define X9C10X_DOWN LOW
X9C10X::X9C10X(uint32_t maxOhm)
/////////////////////////////////////////////////////////
//
// MINIMALISTIC BASE CLASS
//
X9C::X9C()
{
_maxOhm = maxOhm;
}
void X9C10X::begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin, uint8_t position)
void X9C::begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin)
{
_pulsePin = pulsePin;
_directionPin = directionPin;
_selectPin = selectPin;
pinMode(_pulsePin, OUTPUT);
pinMode(_directionPin, OUTPUT);
pinMode(_selectPin, OUTPUT);
// #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);
digitalWrite(_selectPin, HIGH);
pinMode(_selectPin, OUTPUT);
pinMode(_pulsePin, OUTPUT);
pinMode(_directionPin, OUTPUT);
// wiper power up time. Page 5.
delayMicroseconds(500);
}
// reset defined position.
_position = position;
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;
}
@ -112,51 +188,43 @@ bool X9C10X::decr()
uint8_t X9C10X::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
X9C::store();
return _position;
}
////////////////////////////////////////////////////////////////////
//
// PRIVATE
//
void X9C10X::_move(uint8_t direction, uint8_t steps)
// rounding needed!
uint32_t X9C10X::getOhm()
{
digitalWrite(_directionPin, direction);
delayMicroseconds(3); // Tdi (page 5)
return (_maxOhm * _position + 49) / 99;
};
// _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);
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;
};
/////////////////////////////////////////////////////////
//
// DERIVED
// SPECIFIC DERIVED DEVICE CLASSES
//
X9C102::X9C102(uint32_t ohm) : X9C10X(ohm)
{

View File

@ -2,24 +2,54 @@
//
// FILE: X9C10X.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.2.0
// PURPOSE: Arduino Library for X9C10X series digital potentiometer.
// URL: https://github.com/RobTillaart/X9C10X
#include "Arduino.h"
#define X9C10X_LIB_VERSION (F("0.1.3"))
#define X9C10X_LIB_VERSION (F("0.2.0"))
class X9C10X
/////////////////////////////////////////////////////////
//
// X9C MINIMAL BASE CLASS
//
class X9C
{
public:
X9C();
void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin);
// step size 1.
bool incr();
bool decr();
// use with care
void store();
protected:
uint8_t _pulsePin;
uint8_t _directionPin;
uint8_t _selectPin;
void _move(uint8_t direction, uint8_t steps = 1);
};
/////////////////////////////////////////////////////////
//
// X9C10X BASE CLASS
//
class X9C10X : public X9C
{
public:
// ohm can be actual measured value e.g 9950 ohm (calibration)
X9C10X(uint32_t maxOhm = 10000);
void begin(uint8_t pulsePin, uint8_t directionPin, uint8_t selectPin, uint8_t position = 0);
// position = 0..99
// forced = true will ignore the cached position
// takes up to 150 steps as one cannot read the position from device.
@ -35,31 +65,24 @@ public:
uint8_t store();
// current resistance in ohm.
// Q: rounding needed?
uint32_t getOhm() { return (_maxOhm * _position + 49) / 99; };
// misc
uint32_t getMaxOhm() { return _maxOhm; };
uint32_t getOhm();
uint32_t getMaxOhm();
uint8_t Ohm2Position(uint32_t value, bool invert = false);
// Q: needed?
uint16_t getType() { return _type; };
// returns 0 as it is unknown for X9C10X
uint16_t getType();
protected:
uint8_t _pulsePin;
uint8_t _directionPin;
uint8_t _selectPin;
uint32_t _maxOhm;
uint8_t _position;
uint16_t _type = 0; // needed?
void _move(uint8_t direction, uint8_t steps = 1);
uint32_t _maxOhm = 0;
uint8_t _position = 0;
uint16_t _type = 0;
};
/////////////////////////////////////////////////////////
//
// DERIVED
// SPECIFIC DERIVED DEVICE CLASSES
//
class X9C102 : public X9C10X
{
@ -89,7 +112,5 @@ public:
};
// -- END OF FILE --

View File

@ -38,9 +38,8 @@ void setup()
Serial.print("X9C10X_LIB_VERSION: ");
Serial.println(X9C10X_LIB_VERSION);
pot.begin(8, 9, 10, 0); // pulse, direction, select, position
pot.setPosition(0);
pot.begin(8, 9, 10); // pulse, direction, select
pot.setPosition(0); // position
start = micros();
pos = pot.getPosition();

View File

@ -0,0 +1,27 @@
Tested on Arduino UNO
IDE 1.18.19
X9C10X_LIB_VERSION: 0.2.0
getPosition: 4
from 0 to ...
setPosition(33): 304
setPosition(66): 524
setPosition(99): 780
from n to m
setPosition(33): 288
setPosition(66): 276
setPosition(99): 276
no movement
setPosition(33): 12
setPosition(66): 4
setPosition(99): 4
getMaxOhm(): 4
getOhm(): 44
10 x incr(): 288
10 x decr(): 288

View File

@ -36,7 +36,7 @@ void setup()
Serial.print("X9C10X_LIB_VERSION: ");
Serial.println(X9C10X_LIB_VERSION);
pot.begin(8, 9, 10); // pulse, direction, select, position*
pot.begin(8, 9, 10); // pulse, direction, select
pot.setPosition(0);
}

View File

@ -34,8 +34,11 @@ void setup()
Serial.println();
Serial.print("X9C10X_LIB_VERSION: ");
Serial.println(X9C10X_LIB_VERSION);
Serial.println();
Serial.println(pot.getType());
pot.begin(8, 9, 10, 0); // pulse, direction, select, position
pot.begin(8, 9, 10); // pulse, direction, select
pot.setPosition(0); // position
Serial.print("POS:\t0\t");
Serial.println(pot.getPosition());

View File

@ -0,0 +1,66 @@
//
// FILE: X9C_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo minimalistic base class for X9Cxxx devices.
#include "Arduino.h"
#include "X9C10X.h"
// PINOUT X9C10X TOP VIEW (see datasheet)
//
// +--------+
// INC | o o | VCC
// U/D | o o | CS
// RH | o o | RL
// GND | o o | Rwiper
// +--------+
//
// INC pulses
// U/D UP = 1 DOWN = 0
// VCC +5V
//
X9C pot; // unknown Ω (ALT-234)
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println();
Serial.print("X9C10X_LIB_VERSION: ");
Serial.println(X9C10X_LIB_VERSION);
pot.begin(8, 9, 10); // pulse, direction, select
// force to the end of the wiper.
for (uint8_t i = 0; i < 99; i++)
{
pot.incr();
Serial.print('+');
}
Serial.println();
// go to the wanted position. (99 - 24 = 75)
for (uint8_t i = 0; i < 24; i++)
{
pot.decr();
Serial.print('-');
}
Serial.println();
// store the current position in NV-RAM.
pot.store();
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,6 +1,7 @@
# Syntax Colouring Map For X9C10X
# Data types (KEYWORD1)
X9C KEYWORD1
X9C10X KEYWORD1
X9C102 KEYWORD1
X9C103 KEYWORD1
@ -17,6 +18,7 @@ decr KEYWORD2
getOhm KEYWORD2
getMaxOhm KEYWORD2
Ohm2Position KEYWORD2
store KEYWORD2
getType KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/X9C10X.git"
},
"version": "0.1.3",
"version": "0.2.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=X9C10X
version=0.1.3
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino Library for X9C10X series digital potentiometer.

View File

@ -39,7 +39,17 @@ unittest_teardown()
}
unittest(test_constructor)
unittest(test_X9C_all)
{
X9C pot;
pot.begin(4,5,6);
assertTrue(pot.incr());
assertTrue(pot.decr());
// store() cannot be tested as it calls delay().
}
unittest(test_X9C10X_constructor)
{
X9C10X dp0;
X9C10X dp1(1000);
@ -51,19 +61,27 @@ unittest(test_constructor)
assertEqual(900, dp2.getMaxOhm());
assertEqual(1100, dp3.getMaxOhm());
X9C10X x10x;
X9C102 x102;
X9C103 x103;
X9C104 x104;
X9C503 x503;
assertEqual(10000, x10x.getMaxOhm());
assertEqual(1000, x102.getMaxOhm());
assertEqual(10000, x103.getMaxOhm());
assertEqual(100000, x104.getMaxOhm());
assertEqual(50000, x503.getMaxOhm());
assertEqual(000, x10x.getType());
assertEqual(102, x102.getType());
assertEqual(103, x103.getType());
assertEqual(104, x104.getType());
assertEqual(503, x503.getType());
}
unittest(test_position)
unittest(test_X9C10X_position)
{
X9C10X dp0;
@ -76,14 +94,10 @@ unittest(test_position)
dp0.setPosition(pos);
assertEqual(pos, dp0.getPosition());
}
X9C10X dp1;
dp1.begin(7, 8, 9, 50);
assertEqual(50, dp1.getPosition());
}
unittest(test_incr_decr)
unittest(test_X9C10X_incr_decr)
{
X9C10X dp0;
@ -94,18 +108,30 @@ unittest(test_incr_decr)
for (uint8_t pos = 0; pos < 10; pos++)
{
assertEqual(pos, dp0.getPosition());
dp0.incr();
assertTrue(dp0.incr());
}
for (uint8_t pos = 0; pos < 5; pos++)
{
assertEqual(10 - pos, dp0.getPosition());
dp0.decr();
assertTrue(dp0.decr());
}
dp0.setPosition(100);
assertEqual(99, dp0.getPosition());
assertFalse(dp0.incr());
assertTrue(dp0.decr());
assertTrue(dp0.incr());
dp0.setPosition(0);
assertEqual(0, dp0.getPosition());
assertFalse(dp0.decr());
assertTrue(dp0.incr());
assertTrue(dp0.decr());
}
unittest(test_getOhm)
unittest(test_X9C10X_getOhm)
{
X9C10X dp0;
@ -116,12 +142,28 @@ unittest(test_getOhm)
for (uint8_t pos = 0; pos < 100; pos += 9)
{
fprintf(stderr, "VALUE: %d %d Ω\n", dp0.getPosition(), dp0.getOhm());
dp0.incr();
assertTrue(dp0.incr());
}
}
unittest(test_X9C10X_Ohm2Position)
{
X9C10X dp0(1000);
dp0.begin(7, 8, 9);
assertEqual(0, dp0.getPosition());
assertEqual(1000, dp0.getMaxOhm());
for (uint16_t ohm = 0; ohm < 1000; ohm += 50)
{
fprintf(stderr, "OHM: %d Ω \tPOS: %d \tPOS: %d\n", ohm,
dp0.Ohm2Position(ohm), dp0.Ohm2Position(ohm, true));
}
}
unittest_main()
// --------