Skip to content

Commit 5d1c45a

Browse files
committed
update LTC2991
1 parent 28442ea commit 5d1c45a

File tree

6 files changed

+84
-50
lines changed

6 files changed

+84
-50
lines changed

libraries/LTC2991/LTC2991.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//
22
// FILE: LTC2991.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// DATE: 2021-05-10
66
// PURPOSE: Library for LTC2991 temperature and voltage control IC
77
// URL: https://github.com/RobTillaart/LTC2991
88
//
99
// HISTORY:
1010
// 0.1.0 2021-05-10 initial version
11-
//
11+
// 0.1.1 2021-05-16 add trigger_conversion(), set_PWM_fast()
12+
// performance optimizations, some default values, and cleanup.
1213

1314

1415
#include "LTC2991.h"
@@ -74,7 +75,7 @@ LTC2991::LTC2991(const int8_t address, TwoWire *wire)
7475

7576

7677
#if defined (ESP8266) || defined(ESP32)
77-
bool LTC2991::begin(uint8_t sda, uint8_t scl)
78+
bool LTC2991::begin(const uint8_t sda, const uint8_t scl)
7879
{
7980
_wire = &Wire;
8081
_wire->begin(sda, scl);
@@ -134,6 +135,12 @@ bool LTC2991::is_busy()
134135
//
135136
// EXTERNAL CHANNELS (8 voltage or 4 temperature)
136137
//
138+
void LTC2991::trigger_conversion_all()
139+
{
140+
_setRegisterMask(STATUS_HIGH, 0xF0);
141+
}
142+
143+
137144
void LTC2991::enable(uint8_t n, bool enable)
138145
{
139146
if (enable) _setRegisterMask(STATUS_HIGH, (0x08 << n));
@@ -358,6 +365,16 @@ void LTC2991::set_PWM(uint16_t value)
358365
}
359366

360367

368+
void LTC2991::set_PWM_fast(uint16_t value)
369+
{
370+
if (value > 511) value = 511;
371+
_writeRegister(PWM_THRESHOLD_MSB, value >> 1);
372+
// last bit is never set, only when value is zero
373+
// to be sure there is no dangling bit.
374+
if (value == 0) _clrRegisterMask(PWM_THRESHOLD_LSB, 0x80);
375+
}
376+
377+
361378
uint16_t LTC2991::get_PWM()
362379
{
363380
uint16_t pwm = _readRegister(PWM_THRESHOLD_MSB);
@@ -521,6 +538,7 @@ uint8_t LTC2991::_readRegister(const uint8_t reg)
521538
return _wire->read();
522539
}
523540

541+
524542
uint16_t LTC2991::_readRegister16(const uint8_t reg)
525543
{
526544
uint16_t x = _readRegister(reg) << 8;
@@ -530,19 +548,26 @@ uint16_t LTC2991::_readRegister16(const uint8_t reg)
530548
return x;
531549
}
532550

551+
533552
void LTC2991::_setRegisterMask(const uint8_t reg, uint8_t mask)
534553
{
535554
uint8_t x = _readRegister(reg);
536-
x |= mask;
537-
_writeRegister(reg, x);
555+
if ((x & mask) != mask) // if not all bits set, set them
556+
{
557+
x |= mask;
558+
_writeRegister(reg, x);
559+
}
538560
}
539561

540562

541563
void LTC2991::_clrRegisterMask(const uint8_t reg, uint8_t mask)
542564
{
543565
uint8_t x = _readRegister(reg);
544-
x &= ~mask;
545-
_writeRegister(reg, x);
566+
if (x | mask) // if any bit of the mask set clear it
567+
{
568+
x &= ~mask;
569+
_writeRegister(reg, x);
570+
}
546571
}
547572

548573
uint8_t LTC2991::_getRegisterMask(const uint8_t reg, uint8_t mask)

libraries/LTC2991/LTC2991.h

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: LTC2991.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.0
5+
// VERSION: 0.1.1
66
// DATE: 2021-05-10
77
// PURPOSE: Library for LTC2991 temperature and voltage control IC
88
// URL: https://github.com/RobTillaart/LTC2991
@@ -11,7 +11,7 @@
1111
#include "Arduino.h"
1212
#include "Wire.h"
1313

14-
#define LTC2991_LIB_VERSION (F("0.1.0"))
14+
#define LTC2991_LIB_VERSION (F("0.1.1"))
1515

1616

1717
class LTC2991
@@ -20,58 +20,56 @@ class LTC2991
2020
explicit LTC2991(const int8_t address, TwoWire *wire = &Wire);
2121

2222
#if defined (ESP8266) || defined(ESP32)
23-
bool begin(uint8_t sda, uint8_t scl);
23+
bool begin(const uint8_t sda, const uint8_t scl);
2424
#endif
2525

2626
bool begin();
2727
bool isConnected();
2828

2929

30-
// REGISTER 0x00 .. 0x01
3130
// channel = 1..8
3231
bool new_data(uint8_t channel); // external
3332
bool new_temperature(); // internal
3433
bool new_voltage(); // VCC
3534
bool is_busy();
3635

36+
3737
//
38-
// EXTERNAL CHANNELS (8 voltage or 4 temperature)
38+
// EXTERNAL CHANNELS (8 voltage, 4 differentials or 4 temperature)
3939
//
4040
// n = 1 ==> V1 V2 T1
4141
// n = 2 ==> V3 V4 T2
4242
// n = 3 ==> V5 V6 T3
4343
// n = 4 ==> V7 V8 T4
44+
void trigger_conversion(uint8_t n) { enable(n, true); };
45+
void trigger_conversion_all();
4446
void enable(uint8_t n, bool enable);
4547
bool is_enabled(uint8_t n);
4648

47-
// REGISTER 0x06 .. 0x07
49+
4850
// n: 1..4
4951
void enable_filter(uint8_t n, bool enable);
5052
bool is_enabled_filter(uint8_t n);
51-
void set_Kelvin(uint8_t n); // implicit set_mode_temperature
52-
void set_Celsius(uint8_t n); // implicit set_mode_temperature
53-
void set_temp_scale(uint8_t n, bool Kelvin = true); // MIGHT BECOME OBSOLETE ?
54-
// returns 'C' or 'K'
55-
char get_temp_scale(uint8_t n);
53+
void set_Kelvin(uint8_t n); // implicit set_mode_temperature
54+
void set_Celsius(uint8_t n); // implicit set_mode_temperature
55+
void set_temp_scale(uint8_t n, bool Kelvin = true);
56+
char get_temp_scale(uint8_t n); // returns 'C' or 'K'
5657
void set_mode_temperature(uint8_t n);
5758
void set_mode_voltage_differential(uint8_t n);
5859
void set_mode_voltage_normal(uint8_t n);
59-
uint8_t get_operational_mode(uint8_t n);
60+
uint8_t get_operational_mode(uint8_t n); // enumeration?
6061
uint8_t get_differential_mode(uint8_t n);
61-
62-
// REGISTER 0x0A .. 0x19
63-
float get_value(uint8_t channel); // chan = 1..8
64-
62+
float get_value(uint8_t channel); // chan = 1..8
6563

6664

6765
//
6866
// PWM
6967
//
70-
// REGISTER 0x08 .. 0x09
7168
// value = 0..511
72-
void set_PWM(uint16_t value);
69+
void set_PWM(uint16_t value = 0);
70+
void set_PWM_fast(uint16_t value = 0); // less resolution
7371
uint16_t get_PWM();
74-
void invert_PWM(bool invert);
72+
void invert_PWM(bool invert = false);
7573
bool is_inverted_PWM();
7674
void enable_PWM(bool enable);
7775
bool is_enabled_PWM();
@@ -95,11 +93,8 @@ class LTC2991
9593
void set_Kelvin_Tintern() { set_temp_scale_Tintern(true); };
9694
void set_Celsius_Tintern() { set_temp_scale_Tintern(false); };
9795
void set_temp_scale_Tintern(bool Kelvin = true);
98-
// returns 'C' or 'K'
99-
char get_temp_scale_Tintern();
100-
// REGISTER 0x1A .. 0x1B
96+
char get_temp_scale_Tintern(); // returns 'C' or 'K'
10197
float get_Tintern();
102-
// REGISTER 0x1C .. 0x1D
10398
float get_VCC();
10499

105100

libraries/LTC2991/README.md

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Arduino library for an LTC2991 temperature and voltage control IC
1111

1212
## Description
1313

14-
Experimental - not tested yet
14+
Experimental - not tested myself, (no hardware)
1515

16-
LTC2991 is an experimental library for the LTC2991 IC which is typically used to control temperature and voltage lines.
16+
LTC2991 is an experimental library for the LTC2991 IC which is typically used to monitor temperature and voltage. It also has a PWM out to control e.g. a fan.
1717

1818
The IC supports normal voltage measurement (8 lines), differential voltage measurements (4 pairs) and temperature measurements (4 pairs). These can be combined in a mix. As the IC has only 8 inputs available one has to choose what.
1919

@@ -26,12 +26,12 @@ Read the datasheet for the details.
2626
### Constructor and setup
2727

2828
- **LTC2991(const int8_t address, TwoWire \*wire = Wire);**
29-
- **bool begin(uint8_t sda, uint8_t scl)** ESP32 ea initializes the class.
29+
- **bool begin(const uint8_t sda, const uint8_t scl)** for ESP32 and ESP8266; initializes the class.
3030
sets I2C pins.
31-
returns true if the LTC2991 is on the I2C bus.
31+
returns true if the LTC2991 address is on the I2C bus.
3232
- **bool begin()** UNO ea. initializes the class.
33-
returns true if the LTC2991 is on the I2C bus.
34-
- **bool isConnected()** returns true if the LTC2991 is on the I2C bus.
33+
returns true if the LTC2991 address is on the I2C bus.
34+
- **bool isConnected()** returns true if the LTC2991 address is on the I2C bus.
3535

3636

3737
### Status functions
@@ -53,15 +53,17 @@ The following functions work on pairs
5353
| 3 | V5 V6 T3 |
5454
| 4 | V7 V8 T4 |
5555

56-
- **void enable(uint8_t n, bool enable)** enable or disable an external line.
56+
- **void trigger_conversion(uint8_t n)** wrapper around enable(n, true), better naming.
57+
- **void trigger_conversion_all()** triggers conversions for all 4 channels/pairs.
58+
- **void enable(uint8_t n, bool enable)** enable or disable an external line. disable can be used to stop the repeat mode.
5759
- **bool is_enabled(uint8_t n)** idem
58-
- **void enable_filter(uint8_t n, bool enable)** not investigated
60+
- **void enable_filter(uint8_t n, bool enable)** enable filter - not investigated.
5961
- **bool is_enabled_filter(uint8_t n)** idem
6062
- **void set_Kelvin(uint8_t n)** sets temperature mode to Kelvin,
6163
implicit set_mode_temperature(),
6264
- **void set_Celsius(uint8_t n)** sets temperature mode to Celsius,
6365
implicit set_mode_temperature
64-
- **void set_temp_scale(uint8_t n, bool Kelvin = true)** obsolete?
66+
- **void set_temp_scale(uint8_t n, bool Kelvin = true)** used to switch between Kelvin and Celsius.
6567
- **char get_temp_scale(uint8_t n)** returns 'K' or 'C'
6668
- **void set_mode_temperature(uint8_t n)** sets operational mode
6769
- **void set_mode_voltage_differential(uint8_t n)** sets operational mode
@@ -75,7 +77,7 @@ depending on the operational mode it returns the temperature or the
7577

7678
### Internal measurements
7779

78-
- **void enable_Tintern_Vcc(bool enable)** enable internal temperature sensor
80+
- **void enable_Tintern_Vcc(bool enable)** enable internal temperature measurements
7981
- **bool is_enabled_Tintern_Vcc()** idem
8082
- **void enable_filter_Tintern(bool enable)** enable filter - not investigated
8183
- **bool is_enabled_filter_Tintern()**
@@ -89,14 +91,15 @@ depending on the operational mode it returns the temperature or the
8991

9092
### Configuration
9193
- **void set_acquisition_repeat()** set continuous measurement mode
92-
- **void set_acquisition_single()** set single shot mode
94+
- **void set_acquisition_single()** set single shot mode. Note that before a measurement one needs to call trigger_conversion();
9395
- **uint8_t get_acquisition_mode()** return mode set (0,1)
9496

9597

9698
### PWM functions
9799

98-
- **void set_PWM(uint16_t value)** value is 0..511
99-
- **uint16_t get_PWM()** idem
100+
- **void set_PWM(uint16_t value = 0)** value is 0..511
101+
- **void set_PWM(uint16_t value = 0)** value is 0..511, less resolution (256 steps)
102+
- **uint16_t get_PWM()** returns the value from the PWM register, when using PWM_fast this can differ 1.
100103
- **void invert_PWM(bool invert)** idem
101104
- **bool is_inverted_PWM()** idem
102105
- **void enable_PWM(bool enable)** idem
@@ -116,14 +119,19 @@ See examples..
116119

117120
### TODO
118121

122+
#### must
119123
- get hardware to
120124
- test test test
121-
- elaborate on the documentation
122-
- more examples
123125

124-
COULD
125-
- cache all registers or
126+
127+
#### could
128+
- cache registers (which) or
126129
- cache a number of flags to speed up retrieving data
127130
- optimize multibyte read / write
131+
- add Fahrenheit
132+
- do low level in Kelvin and convert to KFC as needed.
133+
- would simplify get_value
134+
- need an enum
128135
- look for code optimizations
129-
-
136+
137+

libraries/LTC2991/keywords.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ new_temperature KEYWORD2
1313
new_voltage KEYWORD2
1414
is_busy KEYWORD2
1515

16+
17+
trigger_conversion KEYWORD2
18+
trigger_conversion_all KEYWORD2
1619
enable KEYWORD2
1720
is_enabled KEYWORD2
1821
enable_filter KEYWORD2
1922
is_enabled_filter KEYWORD2
23+
24+
2025
set_Kelvin KEYWORD2
2126
set_Celsius KEYWORD2
2227
get_temp_scale KEYWORD2
@@ -29,6 +34,7 @@ get_value KEYWORD2
2934

3035

3136
set_PWM KEYWORD2
37+
set_PWM_fast KEYWORD2
3238
get_PWM KEYWORD2
3339
invert_PWM KEYWORD2
3440
is_inverted_PWM KEYWORD2

libraries/LTC2991/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/LTC2991.git"
1717
},
18-
"version": "0.1.0",
18+
"version": "0.1.1",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*"

libraries/LTC2991/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=LTC2991
2-
version=0.1.0
2+
version=0.1.1
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for LTC2991

0 commit comments

Comments
 (0)