ESP Line Following Buggy
2nd Year Embedded System Project (Group 48 - 2023/24)
Loading...
Searching...
No Matches
ds2781.cpp
Go to the documentation of this file.
1#include "ds2781.h"
2#include "OneWire_Methods.h"
3
4/* --------------------------------------------------------------------------
5 This file includes the functions needed to access and modify the registers
6 in a DS2781 using the 1-Wire protocol. The DS2781 is an IC that measures
7 voltage, current, accumulated current and temperature. It implements
8 capacity estimation algorithms for rechargeable batteries. However, this
9 file only includes routines to access the electrical parameters and not
10 the age-estimation registers.
11 --------------------------------------------------------------------------
12----------------------
13 NOTE_1: The functions that return parameters, do so in the units reported
14 in the description of each function. The user should implement the scaling
15 on his/her own.
16 -------------------------------------------------------------------------- */
17
18/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
19 * Function name : ReadVoltage *
20 * Overview : Returns the voltage measured at the VIN input of the DS2781 *
21 * in units of 9.76mV *
22 * Return type : 16-bit unsigned int *
23 * Parameters : None *
24 * Time : < 4.3ms *
25 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
26
27int ReadVoltage (void)
28{
29 uint16_t result = 0;
30 if( OneWire_Reset() == true)
31 {
34 OneWire_WriteByte( 0x0C ); //Register Address
35 result = OneWire_ReadByte() << 8; //MSB
36 result |= OneWire_ReadByte() ; //LSB
37 }
38 return (result >> 5);
39}
40
41/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
42 * Function name : ReadCurrent *
43 * Overview : Returns the current measured through Rsns external to DS2781 in *
44 * units of 1.5625uV/Rsns. Positive current indicates discharge *
45 * Return type : 16-bit unsigned int *
46 * Parameters : None *
47 * Time : < 4.3ms *
48 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
49
50uint16_t ReadCurrent (void)
51{
52 uint16_t result = 0;
53 if( OneWire_Reset() == true)
54 {
57 OneWire_WriteByte( 0x0E ); //Register Address
58 result = ((uint16_t)OneWire_ReadByte() ) << 8; //MSB
59 result |= ((uint16_t)OneWire_ReadByte() ); //LSB
60 }
61 return result;
62}
63
64/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
65 * Function name : ReadAccumulatedCurrent *
66 * Overview : Returns the accumulated current at the DS2781 in units of *
67 * 1.526nVhr/Rsns *
68 * Return type : 32-bit unsigned long *
69 * Parameters : None *
70 * Time : < 5.8ms *
71 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
72
74{
75 unsigned long result = 0;
76 if( OneWire_Reset() == true)
77 {
80 OneWire_WriteByte( 0x10 ); //Register Address
81 result = ((unsigned long)OneWire_ReadByte() ) << 24; //MSB
82 result |= ((unsigned long)OneWire_ReadByte() ) << 16;
83 result |= ((unsigned long)OneWire_ReadByte() ) << 8;
84 result |= ((unsigned long)OneWire_ReadByte() ); //LSB
85 }
86 return (result >> 4);
87}
88
89/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
90 * Function name : ResetAccumulatedCurrent *
91 * Overview : Resets the accumulated current register at the DS2781 *
92 * Return type : Void *
93 * Parameters : None *
94 * Time : < 4.2ms *
95 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
96
98{
99 if( OneWire_Reset() == true)
100 {
103 OneWire_WriteByte( 0x10 ); //Register Address
104 OneWire_WriteByte( 0x00 ); //MSB
105 OneWire_WriteByte( 0x00 ); //LSB
106 }
107}
108
109/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
110 * Function name : ReadNetAddress *
111 * Overview : Returns the net address of the DS2781 *
112 * Return type : 64-bit unsigned long long *
113 * Parameters : None *
114 * Time : < 7.3ms *
115 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
116
117uint32_t ReadNetAddress (void)
118{
119 uint16_t result = 0;
120 if( OneWire_Reset() == true)
121 {
123 //result = ((Quint16_t)OneWire_ReadByte() ); //MSB
124 //result |= ((Quint16_t)OneWire_ReadByte() ) << 8;
125 //result |= ((Quint16_t)OneWire_ReadByte() ) << 16;
126 //result |= ((Quint16_t)OneWire_ReadByte() ) << 24;
127 //result |= ((Quint16_t)OneWire_ReadByte() ) << 32;
128 //result |= ((Quint16_t)OneWire_ReadByte() ) << 40;
129 //result |= ((Quint16_t)OneWire_ReadByte() ) << 48;
130 //result |= ((Quint16_t)OneWire_ReadByte() ) <<56; //LSB
131 }
132 return result;
133}
134
135/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
136 * Function name : ReadTemperature *
137 * Overview : Returns the temperature measured by the DS2781 in units of *
138 * 0.125°C *
139 * Return type : 16-bit unsigned int *
140 * Parameters : None *
141 * Time : < 4.3ms *
142 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
143
144uint16_t ReadTemperature (void)
145{
146 uint16_t result = 0;
147 if( OneWire_Reset() == true)
148 {
151 OneWire_WriteByte( 0x0A ); //Register Address
152 result = ((uint16_t)OneWire_ReadByte() ) << 8; //MSB
153 result |= ((uint16_t)OneWire_ReadByte() ); //LSB
154 }
155 return (result >> 5);
156}
157
158/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
159 * Function name : ReadCurrentOffset *
160 * Overview : Returns the value of the current offset register of the DS2781 *
161 * in units of 1.56uV/Rsns *
162 * Return type : 8-bit uint8_t *
163 * Parameters : None *
164 * Time : < 3.6ms *
165 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
166
167uint8_t ReadCurrentOffset (void)
168{
169 uint8_t result = 0;
170 if( OneWire_Reset() == true)
171 {
174 OneWire_WriteByte( 0x7B ); //Register Address
175 result = OneWire_ReadByte();
176 }
177 return result;
178}
179
180/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
181 * Function name : WriteCurrentOffset *
182 * Overview : Writes to the current offset register of the DS2781 in units of *
183 * 1.56uV/Rsns *
184 * Return type : Void *
185 * Parameters : Byte to be written to the register in 2's complement *
186 * Time : < 3.6ms *
187 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
188
189void WriteCurrentOffset (uint8_t offset)
190{
191 if( OneWire_Reset() == true)
192 {
195 OneWire_WriteByte( 0x7B ); //Register Address
196 OneWire_WriteByte( offset );
197 }
198}
199
200/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
201 * Function name : AdjustCurrentOffset *
202 * Overview : Adjusts the value of the current offset register of the DS2781 *
203 * by taking into account the offset at no current. Should only *
204 * be called when the battery is supplying no current *
205 * Return type : Void *
206 * Parameters : None *
207 * Time : < 3.62s *
208 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
209
211{
212 char offset = 0;
213
214 WriteCurrentOffset ( 0x0 ); //Reset Current Offset Register
215
216 //Delay100MSx(36); //Wait 3.6s for current register to update
217
218 if( OneWire_Reset() == true)
219 {
222 OneWire_WriteByte( 0x0F ); //Current Register LSB
223 offset = OneWire_ReadByte();
224 }
225
226 offset = 256 - offset; //2's complement Negating
227
228 if( OneWire_Reset() == true)
229 {
232 OneWire_WriteByte( 0x7B ); //Current Offset Register
233 OneWire_WriteByte( offset );
234 }
235}
236
237/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
238 * Function name : UpdateControlRegister *
239 * Overview : Writes to the Control register of the DS2781 using the values *
240 * supplied as a byte parameter. Writes to EEPROM addresses are *
241 * ignored for up to 15ms after this function is called. *
242 * Return type : Void *
243 * Parameters : None *
244 * Time : < 6.4ms *
245 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
246
247void UpdateControlRegister (uint8_t control)
248{
249 if( OneWire_Reset() == true )
250 {
253 OneWire_WriteByte( 0x60 ); //Register Address
254 OneWire_WriteByte( control );
255 }
256
257 if( OneWire_Reset() == true )
258 {
261 OneWire_WriteByte( 0x60 ); //Register Address
262 }
263}
264
265/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
266 * Function name : ReadRAM *
267 * Overview : Reads a byte from the shadow RAM of the DS2781 at the given *
268 * memory address *
269 * Return type : 8-bit uint8_t *
270 * Parameters : Address of register to be read *
271 * Time : < 3.6ms *
272 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
273
274uint8_t ReadRAM (uint8_t addr)
275{
276 uint8_t result = 0;
277 if( OneWire_Reset() == true)
278 {
281 OneWire_WriteByte( addr ); //Register Address
282 result = OneWire_ReadByte();
283 }
284 return result;
285}
286
287/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
288 * Function name : WriteRAM *
289 * Overview : Writes the given byte to the shadow RAM of the DS2781 at the *
290 * given memory address *
291 * Return type : Void *
292 * Parameters : Byte to be written, address of register *
293 * Time : < 3.6ms *
294 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
295
296void WriteRAM (uint8_t byte, uint8_t addr)
297{
298 if( OneWire_Reset() == true)
299 {
302 OneWire_WriteByte( addr ); //Register Address
303 OneWire_WriteByte( byte );
304 }
305}
306
307/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
308 * Function name : CopyEEPROM *
309 * Overview : This function copies the contents of the EEPROM shadow RAM to *
310 * EEPROM cells for the EEPROM block containing thr given address. *
311 * Writes to EEPROM addresses are ignored for up to 15ms after this *
312 * function is called. *
313 * Return type : Void *
314 * Parameters : Memory address of shadow RAM to be copied *
315 * Time : < 2.9ms *
316 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
317
318void CopyEEPROM (uint8_t addr)
319{
320 if( OneWire_Reset() == true)
321 {
324 OneWire_WriteByte( addr );
325 }
326}
327
328/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
329 * Function name : RecallEEPROM *
330 * Overview : This function copies the contents of the EEPROM cells to the *
331 * shadow RAM for the EEPROM block containing the given address. * *
332 * Return type : Void *
333 * Parameters : Memory address of EEPROM to be copied *
334 * Time : < 2.9ms *
335 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
336
337void RecallEEPROM (uint8_t addr)
338{
339 if( OneWire_Reset() == true)
340 {
343 OneWire_WriteByte( addr );
344 }
345}
346/* EOF */
bool OneWire_Reset(void)
unsigned char OneWire_ReadByte(void)
void OneWire_WriteByte(unsigned char byte_to_write)
void RecallEEPROM(uint8_t addr)
Definition ds2781.cpp:337
int ReadVoltage(void)
Definition ds2781.cpp:27
void UpdateControlRegister(uint8_t control)
Definition ds2781.cpp:247
void AdjustCurrentOffset(void)
Definition ds2781.cpp:210
uint32_t ReadNetAddress(void)
Definition ds2781.cpp:117
void WriteCurrentOffset(uint8_t offset)
Definition ds2781.cpp:189
void ResetAccumulatedCurrent(void)
Definition ds2781.cpp:97
uint32_t ReadAccumulatedCurrent(void)
Definition ds2781.cpp:73
uint8_t ReadRAM(uint8_t addr)
Definition ds2781.cpp:274
void WriteRAM(uint8_t byte, uint8_t addr)
Definition ds2781.cpp:296
void CopyEEPROM(uint8_t addr)
Definition ds2781.cpp:318
uint16_t ReadTemperature(void)
Definition ds2781.cpp:144
uint8_t ReadCurrentOffset(void)
Definition ds2781.cpp:167
uint16_t ReadCurrent(void)
Definition ds2781.cpp:50
#define WRITE_DATA
Definition ds2781.h:10
#define READ_DATA
Definition ds2781.h:9
#define RECALL_DATA
Definition ds2781.h:12
#define COPY_DATA
Definition ds2781.h:11
#define READ_NETADDRESS
Definition ds2781.h:4
#define SKIP_NETADDRESS
Definition ds2781.h:5