Example of using the DS271 battery monitor on the ESP motor driver board using PC_12 as the OneWire interface (Any other GPIO can be used)
ds2781.cpp
00001 #include "ds2781.h" 00002 #include "OneWire_Methods.h" 00003 00004 /* -------------------------------------------------------------------------- 00005 This file includes the functions needed to access and modify the registers 00006 in a DS2781 using the 1-Wire protocol. The DS2781 is an IC that measures 00007 voltage, current, accumulated current and temperature. It implements 00008 capacity estimation algorithms for rechargeable batteries. However, this 00009 file only includes routines to access the electrical parameters and not 00010 the age-estimation registers. 00011 -------------------------------------------------------------------------- 00012 ---------------------- 00013 NOTE_1: The functions that return parameters, do so in the units reported 00014 in the description of each function. The user should implement the scaling 00015 on his/her own. 00016 -------------------------------------------------------------------------- */ 00017 00018 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00019 * Function name : ReadVoltage * 00020 * Overview : Returns the voltage measured at the VIN input of the DS2781 * 00021 * in units of 9.76mV * 00022 * Return type : 16-bit unsigned int * 00023 * Parameters : None * 00024 * Time : < 4.3ms * 00025 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00026 00027 int ReadVoltage (void) 00028 { 00029 uint16_t result = 0; 00030 if( OneWire_Reset() == true) 00031 { 00032 OneWire_WriteByte( SKIP_NETADDRESS ); 00033 OneWire_WriteByte( READ_DATA ); 00034 OneWire_WriteByte( 0x0C ); //Register Address 00035 result = OneWire_ReadByte() << 8; //MSB 00036 result |= OneWire_ReadByte() ; //LSB 00037 } 00038 return (result >> 5); 00039 } 00040 00041 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00042 * Function name : ReadCurrent * 00043 * Overview : Returns the current measured through Rsns external to DS2781 in * 00044 * units of 1.5625uV/Rsns. Positive current indicates discharge * 00045 * Return type : 16-bit unsigned int * 00046 * Parameters : None * 00047 * Time : < 4.3ms * 00048 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00049 00050 uint16_t ReadCurrent (void) 00051 { 00052 uint16_t result = 0; 00053 if( OneWire_Reset() == true) 00054 { 00055 OneWire_WriteByte( SKIP_NETADDRESS ); 00056 OneWire_WriteByte( READ_DATA ); 00057 OneWire_WriteByte( 0x0E ); //Register Address 00058 result = ((uint16_t)OneWire_ReadByte() ) << 8; //MSB 00059 result |= ((uint16_t)OneWire_ReadByte() ); //LSB 00060 } 00061 return result; 00062 } 00063 00064 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00065 * Function name : ReadAccumulatedCurrent * 00066 * Overview : Returns the accumulated current at the DS2781 in units of * 00067 * 1.526nVhr/Rsns * 00068 * Return type : 32-bit unsigned long * 00069 * Parameters : None * 00070 * Time : < 5.8ms * 00071 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00072 00073 uint32_t ReadAccumulatedCurrent (void) 00074 { 00075 unsigned long result = 0; 00076 if( OneWire_Reset() == true) 00077 { 00078 OneWire_WriteByte( SKIP_NETADDRESS ); 00079 OneWire_WriteByte( READ_DATA ); 00080 OneWire_WriteByte( 0x10 ); //Register Address 00081 result = ((unsigned long)OneWire_ReadByte() ) << 24; //MSB 00082 result |= ((unsigned long)OneWire_ReadByte() ) << 16; 00083 result |= ((unsigned long)OneWire_ReadByte() ) << 8; 00084 result |= ((unsigned long)OneWire_ReadByte() ); //LSB 00085 } 00086 return (result >> 4); 00087 } 00088 00089 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00090 * Function name : ResetAccumulatedCurrent * 00091 * Overview : Resets the accumulated current register at the DS2781 * 00092 * Return type : Void * 00093 * Parameters : None * 00094 * Time : < 4.2ms * 00095 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00096 00097 void ResetAccumulatedCurrent (void) 00098 { 00099 if( OneWire_Reset() == true) 00100 { 00101 OneWire_WriteByte( SKIP_NETADDRESS ); 00102 OneWire_WriteByte( WRITE_DATA ); 00103 OneWire_WriteByte( 0x10 ); //Register Address 00104 OneWire_WriteByte( 0x00 ); //MSB 00105 OneWire_WriteByte( 0x00 ); //LSB 00106 } 00107 } 00108 00109 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00110 * Function name : ReadNetAddress * 00111 * Overview : Returns the net address of the DS2781 * 00112 * Return type : 64-bit unsigned long long * 00113 * Parameters : None * 00114 * Time : < 7.3ms * 00115 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00116 00117 uint32_t ReadNetAddress (void) 00118 { 00119 uint16_t result = 0; 00120 if( OneWire_Reset() == true) 00121 { 00122 OneWire_WriteByte( READ_NETADDRESS ); 00123 //result = ((Quint16_t)OneWire_ReadByte() ); //MSB 00124 //result |= ((Quint16_t)OneWire_ReadByte() ) << 8; 00125 //result |= ((Quint16_t)OneWire_ReadByte() ) << 16; 00126 //result |= ((Quint16_t)OneWire_ReadByte() ) << 24; 00127 //result |= ((Quint16_t)OneWire_ReadByte() ) << 32; 00128 //result |= ((Quint16_t)OneWire_ReadByte() ) << 40; 00129 //result |= ((Quint16_t)OneWire_ReadByte() ) << 48; 00130 //result |= ((Quint16_t)OneWire_ReadByte() ) <<56; //LSB 00131 } 00132 return result; 00133 } 00134 00135 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00136 * Function name : ReadTemperature * 00137 * Overview : Returns the temperature measured by the DS2781 in units of * 00138 * 0.125°C * 00139 * Return type : 16-bit unsigned int * 00140 * Parameters : None * 00141 * Time : < 4.3ms * 00142 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00143 00144 uint16_t ReadTemperature (void) 00145 { 00146 uint16_t result = 0; 00147 if( OneWire_Reset() == true) 00148 { 00149 OneWire_WriteByte( SKIP_NETADDRESS ); 00150 OneWire_WriteByte( READ_DATA ); 00151 OneWire_WriteByte( 0x0A ); //Register Address 00152 result = ((uint16_t)OneWire_ReadByte() ) << 8; //MSB 00153 result |= ((uint16_t)OneWire_ReadByte() ); //LSB 00154 } 00155 return (result >> 5); 00156 } 00157 00158 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00159 * Function name : ReadCurrentOffset * 00160 * Overview : Returns the value of the current offset register of the DS2781 * 00161 * in units of 1.56uV/Rsns * 00162 * Return type : 8-bit uint8_t * 00163 * Parameters : None * 00164 * Time : < 3.6ms * 00165 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00166 00167 uint8_t ReadCurrentOffset (void) 00168 { 00169 uint8_t result = 0; 00170 if( OneWire_Reset() == true) 00171 { 00172 OneWire_WriteByte( SKIP_NETADDRESS ); 00173 OneWire_WriteByte( READ_DATA ); 00174 OneWire_WriteByte( 0x7B ); //Register Address 00175 result = OneWire_ReadByte(); 00176 } 00177 return result; 00178 } 00179 00180 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00181 * Function name : WriteCurrentOffset * 00182 * Overview : Writes to the current offset register of the DS2781 in units of * 00183 * 1.56uV/Rsns * 00184 * Return type : Void * 00185 * Parameters : Byte to be written to the register in 2's complement * 00186 * Time : < 3.6ms * 00187 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00188 00189 void WriteCurrentOffset (uint8_t offset) 00190 { 00191 if( OneWire_Reset() == true) 00192 { 00193 OneWire_WriteByte( SKIP_NETADDRESS ); 00194 OneWire_WriteByte( WRITE_DATA ); 00195 OneWire_WriteByte( 0x7B ); //Register Address 00196 OneWire_WriteByte( offset ); 00197 } 00198 } 00199 00200 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00201 * Function name : AdjustCurrentOffset * 00202 * Overview : Adjusts the value of the current offset register of the DS2781 * 00203 * by taking into account the offset at no current. Should only * 00204 * be called when the battery is supplying no current * 00205 * Return type : Void * 00206 * Parameters : None * 00207 * Time : < 3.62s * 00208 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00209 00210 void AdjustCurrentOffset (void) 00211 { 00212 char offset = 0; 00213 00214 WriteCurrentOffset ( 0x0 ); //Reset Current Offset Register 00215 00216 //Delay100MSx(36); //Wait 3.6s for current register to update 00217 00218 if( OneWire_Reset() == true) 00219 { 00220 OneWire_WriteByte( SKIP_NETADDRESS ); 00221 OneWire_WriteByte( READ_DATA ); 00222 OneWire_WriteByte( 0x0F ); //Current Register LSB 00223 offset = OneWire_ReadByte(); 00224 } 00225 00226 offset = 256 - offset; //2's complement Negating 00227 00228 if( OneWire_Reset() == true) 00229 { 00230 OneWire_WriteByte( SKIP_NETADDRESS ); 00231 OneWire_WriteByte( WRITE_DATA ); 00232 OneWire_WriteByte( 0x7B ); //Current Offset Register 00233 OneWire_WriteByte( offset ); 00234 } 00235 } 00236 00237 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00238 * Function name : UpdateControlRegister * 00239 * Overview : Writes to the Control register of the DS2781 using the values * 00240 * supplied as a byte parameter. Writes to EEPROM addresses are * 00241 * ignored for up to 15ms after this function is called. * 00242 * Return type : Void * 00243 * Parameters : None * 00244 * Time : < 6.4ms * 00245 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00246 00247 void UpdateControlRegister (uint8_t control) 00248 { 00249 if( OneWire_Reset() == true ) 00250 { 00251 OneWire_WriteByte( SKIP_NETADDRESS ); 00252 OneWire_WriteByte( WRITE_DATA ); 00253 OneWire_WriteByte( 0x60 ); //Register Address 00254 OneWire_WriteByte( control ); 00255 } 00256 00257 if( OneWire_Reset() == true ) 00258 { 00259 OneWire_WriteByte( SKIP_NETADDRESS ); 00260 OneWire_WriteByte( COPY_DATA ); 00261 OneWire_WriteByte( 0x60 ); //Register Address 00262 } 00263 } 00264 00265 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00266 * Function name : ReadRAM * 00267 * Overview : Reads a byte from the shadow RAM of the DS2781 at the given * 00268 * memory address * 00269 * Return type : 8-bit uint8_t * 00270 * Parameters : Address of register to be read * 00271 * Time : < 3.6ms * 00272 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00273 00274 uint8_t ReadRAM (uint8_t addr) 00275 { 00276 uint8_t result = 0; 00277 if( OneWire_Reset() == true) 00278 { 00279 OneWire_WriteByte( SKIP_NETADDRESS ); 00280 OneWire_WriteByte( READ_DATA ); 00281 OneWire_WriteByte( addr ); //Register Address 00282 result = OneWire_ReadByte(); 00283 } 00284 return result; 00285 } 00286 00287 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00288 * Function name : WriteRAM * 00289 * Overview : Writes the given byte to the shadow RAM of the DS2781 at the * 00290 * given memory address * 00291 * Return type : Void * 00292 * Parameters : Byte to be written, address of register * 00293 * Time : < 3.6ms * 00294 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00295 00296 void WriteRAM (uint8_t byte, uint8_t addr) 00297 { 00298 if( OneWire_Reset() == true) 00299 { 00300 OneWire_WriteByte( SKIP_NETADDRESS ); 00301 OneWire_WriteByte( WRITE_DATA ); 00302 OneWire_WriteByte( addr ); //Register Address 00303 OneWire_WriteByte( byte ); 00304 } 00305 } 00306 00307 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00308 * Function name : CopyEEPROM * 00309 * Overview : This function copies the contents of the EEPROM shadow RAM to * 00310 * EEPROM cells for the EEPROM block containing thr given address. * 00311 * Writes to EEPROM addresses are ignored for up to 15ms after this * 00312 * function is called. * 00313 * Return type : Void * 00314 * Parameters : Memory address of shadow RAM to be copied * 00315 * Time : < 2.9ms * 00316 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00317 00318 void CopyEEPROM (uint8_t addr) 00319 { 00320 if( OneWire_Reset() == true) 00321 { 00322 OneWire_WriteByte( SKIP_NETADDRESS ); 00323 OneWire_WriteByte( COPY_DATA ); 00324 OneWire_WriteByte( addr ); 00325 } 00326 } 00327 00328 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 00329 * Function name : RecallEEPROM * 00330 * Overview : This function copies the contents of the EEPROM cells to the * 00331 * shadow RAM for the EEPROM block containing the given address. * * 00332 * Return type : Void * 00333 * Parameters : Memory address of EEPROM to be copied * 00334 * Time : < 2.9ms * 00335 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ 00336 00337 void RecallEEPROM (uint8_t addr) 00338 { 00339 if( OneWire_Reset() == true) 00340 { 00341 OneWire_WriteByte( SKIP_NETADDRESS ); 00342 OneWire_WriteByte( RECALL_DATA ); 00343 OneWire_WriteByte( addr ); 00344 } 00345 } 00346 /* EOF */
Generated on Fri Jul 29 2022 00:12:49 by 1.7.2