11//
22// FILE: DS2438.cpp
33// AUTHOR: Rob Tillaart
4- // VERSION: 0.2.0
4+ // VERSION: 0.2.1
55// DATE: 2023-07-28
66// PURPOSE: Arduino Library for DS2438 battery monitor
77// URL: https://github.com/RobTillaart/DS2438
@@ -68,6 +68,14 @@ bool DS2438::isConnected(uint8_t retries)
6868 return _addressFound;
6969}
7070
71+ bool DS2438::isDS2438 ()
72+ {
73+ if (_addressFound || isConnected (3 ))
74+ {
75+ return (_address[0 ] == 0x26 );
76+ }
77+ return false ;
78+ }
7179
7280// /////////////////////////////////////////////////////////
7381//
@@ -86,7 +94,7 @@ float DS2438::readTemperature()
8694 readScratchPad (0 );
8795
8896 // sign MSB LSB step >> 3
89- _temperature = (int (_scratchPad[2 ]) * 256 + _scratchPad[1 ]) * 0.03125 * 0.125 ;
97+ _temperature = (int (_scratchPad[2 ]) * 256 + _scratchPad[1 ]) * ( 0 . 03125f * 0 .125f ) ;
9098
9199 return _temperature;
92100}
@@ -127,7 +135,7 @@ float DS2438::readVDD()
127135 readScratchPad (DS2438_PAGE_CORE );
128136
129137 // 10 mV resolution
130- _vdd = ((_scratchPad[4 ] & 0x03 ) * 256 + _scratchPad[3 ]) * 0.01 ;
138+ _vdd = ((_scratchPad[4 ] & 0x03 ) * 256 + _scratchPad[3 ]) * 0 .01f ;
131139
132140 // convert current in same call?!
133141
@@ -152,7 +160,7 @@ float DS2438::readVAD()
152160 readScratchPad (DS2438_PAGE_CORE );
153161
154162 // 10 mV resolution
155- _vad = ((_scratchPad[4 ] & 0x03 ) * 256 + _scratchPad[3 ]) * 0.01 ;
163+ _vad = ((_scratchPad[4 ] & 0x03 ) * 256 + _scratchPad[3 ]) * 0 .01f ;
156164
157165 // convert current in same call?!
158166
@@ -172,8 +180,8 @@ float DS2438::getVAD()
172180//
173181void DS2438::setResistor (float resistor)
174182{
175- _inverseR = 1.0 / (4096.0 * resistor);
176- _RICA = 1.0 / (2048.0 * resistor);
183+ _inverseR = 1 .0f / (4096 .0f * resistor);
184+ _RICA = 1 .0f / (2048 .0f * resistor);
177185}
178186
179187
@@ -245,9 +253,9 @@ float DS2438::readRemaining()
245253{
246254 // datasheet p.7
247255 readScratchPad (DS2438_PAGE_ETM_ICA_OFFSET );
248- // factor 2.0 from optimization (need to explain this factor)
249256 // Remaining Capacity = ICA / (2048 * RSENS)
250- float remaining = _scratchPad[4 ] / _RICA; // mAhr
257+ // setResistor() : _RICA = 1.0/(2048 * Rsens);
258+ float remaining = _scratchPad[4 ] * _RICA; // mAhr
251259 return remaining;
252260}
253261
@@ -391,15 +399,15 @@ float DS2438::readCCA()
391399 // datasheet p.8 + 16
392400 readScratchPad (DS2438_PAGE_CCA_DCA );
393401 uint16_t raw = (_scratchPad[5 ] * 256 + _scratchPad[4 ]);
394- return raw * 15.625 ;
402+ return raw * 15 .625f ;
395403}
396404
397405float DS2438::readDCA ()
398406{
399407 // datasheet p.8 + 16
400408 readScratchPad (DS2438_PAGE_CCA_DCA );
401409 uint16_t raw = (_scratchPad[7 ] * 256 + _scratchPad[6 ]);
402- return raw * 15.625 ;
410+ return raw * 15 .625f ;
403411}
404412
405413bool DS2438::setCCA (float CCA )
@@ -408,7 +416,7 @@ bool DS2438::setCCA(float CCA)
408416 if (CCA < 0 ) return false ;
409417 clearConfigBit (DS2438_CONFIG_IAD );
410418 readScratchPad (DS2438_PAGE_CCA_DCA );
411- uint16_t tmp = round (CCA / 15.625 );
419+ uint16_t tmp = round (CCA * ( 1 . 0f / 15 .625f ) );
412420 _scratchPad[4 ] = tmp & 0xFF ;
413421 _scratchPad[5 ] = tmp >> 8 ;
414422 writeScratchPad (DS2438_PAGE_CCA_DCA );
@@ -423,7 +431,7 @@ bool DS2438::setDCA(float DCA)
423431 if (DCA < 0 ) return false ;
424432 clearConfigBit (DS2438_CONFIG_IAD );
425433 readScratchPad (DS2438_PAGE_CCA_DCA );
426- uint16_t tmp = round (DCA / 15.625 );
434+ uint16_t tmp = round (DCA * ( 1 . 0f / 15 .625f ) );
427435 _scratchPad[6 ] = tmp & 0xFF ;
428436 _scratchPad[7 ] = tmp >> 8 ;
429437 writeScratchPad (DS2438_PAGE_CCA_DCA );
@@ -453,7 +461,7 @@ void DS2438::resetAccumulators()
453461//
454462void DS2438::setConfigBit (uint8_t bit)
455463{
456- if (bit > 3 ) return ;
464+ if (bit > DS2438_CONFIG_AD ) return ;
457465 uint8_t mask = (0x01 << bit);
458466 readScratchPad (DS2438_PAGE_CORE );
459467 if ((_scratchPad[0 ] & mask) == mask) return ; // already 1
@@ -463,7 +471,7 @@ void DS2438::setConfigBit(uint8_t bit)
463471
464472void DS2438::clearConfigBit (uint8_t bit)
465473{
466- if (bit > 3 ) return ;
474+ if (bit > DS2438_CONFIG_AD ) return ;
467475 uint8_t mask = (0x01 << bit);
468476 readScratchPad (DS2438_PAGE_CORE );
469477 if ((_scratchPad[0 ] & mask) == 0x00 ) return ; // already 0
@@ -515,7 +523,12 @@ void DS2438::readScratchPad(uint8_t page)
515523 _oneWire->write (DS2438_READ_SCRATCH , 0 );
516524 _oneWire->write (page, 0 );
517525 for (uint8_t i = 0 ; i < 8 ; i++) _scratchPad[i] = _oneWire->read ();
518- // skip crc for now.
526+ // skip CRC for now
527+ // - update loop to 9
528+ // - change return type
529+ // - set error flag.
530+ // return _oneWire->crc8(_scratchPad, 8) != _scratchPad[8]);
531+ return ;
519532}
520533
521534
@@ -527,6 +540,7 @@ void DS2438::writeScratchPad(uint8_t page)
527540 _oneWire->write (DS2438_WRITE_SCRATCH , 0 );
528541 _oneWire->write (page, 0 );
529542 for (uint8_t i = 0 ; i < 8 ; i++) _oneWire->write (_scratchPad[i], 0 );
543+ // no CRC when writing.
530544 _oneWire->reset ();
531545 _oneWire->select (_address);
532546 _oneWire->write (DS2438_COPY_SCRATCH , 0 );
0 commit comments