Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
DC1894.ino
Go to the documentation of this file.
1 /*!
2 DC1894B
3 LTC6804-1: Battery stack monitor
4 
5 @verbatim
6 
7 NOTES
8  Setup:
9  Set the terminal baud rate to 115200 and select the newline terminator.
10  Ensure all jumpers on the demo board are installed in their default positions from the factory.
11  Refer to Demo Manual D1894B.
12 
13 
14  Menu Entry 1: Write Configuration
15  Writes the configuration register of the LTC6804s on the stack. This command can be used to turn on
16  the reference and shorten ADC conversion Times.
17 
18  Menu Entry 2: Read Configuration
19  Reads the configuration register of the LTC6804, the read configuration can differ from the written configuration.
20  The GPIO pins will reflect the state of the pin
21 
22  Menu Entry 3: Start Cell voltage conversion
23  Starts a LTC6804 cell channel adc conversion.
24 
25  Menu Entry 4: Read cell voltages
26  Reads the LTC6804 cell voltage registers and prints the results to the serial port.
27 
28  Menu Entry 5: Start Auxiliary voltage conversion
29  Starts a LTC6804 GPIO channel adc conversion.
30 
31  Menu Entry 6: Read Auxiliary voltages
32  Reads the LTC6804 axiliary registers and prints the GPIO voltages to the serial port.
33 
34  Menu Entry 7: Start cell voltage measurement loop
35  The command will continuously measure the LTC6804 cell voltages and print the results to the serial port.
36  The loop can be exited by sending the MCU a 'm' character over the serial link.
37 
38 USER INPUT DATA FORMAT:
39  decimal : 1024
40  hex : 0x400
41  octal : 02000 (leading 0)
42  binary : B10000000000
43  float : 1024.0
44 @endverbatim
45 
46 http://www.linear.com/product/LTC6804-1
47 
48 http://www.linear.com/product/LTC6804-1#demoboards
49 
50 
51 Copyright 2018(c) Analog Devices, Inc.
52 
53 All rights reserved.
54 
55 Redistribution and use in source and binary forms, with or without
56 modification, are permitted provided that the following conditions are met:
57  - Redistributions of source code must retain the above copyright
58  notice, this list of conditions and the following disclaimer.
59  - Redistributions in binary form must reproduce the above copyright
60  notice, this list of conditions and the following disclaimer in
61  the documentation and/or other materials provided with the
62  distribution.
63  - Neither the name of Analog Devices, Inc. nor the names of its
64  contributors may be used to endorse or promote products derived
65  from this software without specific prior written permission.
66  - The use of this software may or may not infringe the patent rights
67  of one or more patent holders. This license does not release you
68  from the requirement that you obtain separate licenses from these
69  patent holders to use this software.
70  - Use of the software either in source or binary form, must be run
71  on or directly connected to an Analog Devices Inc. component.
72 
73 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
74 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
75 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
76 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
77 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
78 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
79 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
80 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
81 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
82 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 
84 Copyright 2013 Linear Technology Corp. (LTC)
85  */
86 
87 
88 /*! @file
89  @ingroup LTC68041
90 */
91 
92 #include <Arduino.h>
93 #include <stdint.h>
94 #include "Linduino.h"
95 #include "LT_SPI.h"
96 #include "UserInterface.h"
97 #include "LTC68041.h"
98 #include <SPI.h>
99 
100 const uint8_t TOTAL_IC = 1;//!<number of ICs in the daisy chain
101 
102 /******************************************************
103  *** Global Battery Variables received from 6804 commands
104  These variables store the results from the LTC6804
105  register reads and the array lengths must be based
106  on the number of ICs on the stack
107  ******************************************************/
108 uint16_t cell_codes[TOTAL_IC][12];
109 /*!<
110  The cell codes will be stored in the cell_codes[][12] array in the following format:
111 
112  | cell_codes[0][0]| cell_codes[0][1] | cell_codes[0][2]| ..... | cell_codes[0][11]| cell_codes[1][0] | cell_codes[1][1]| ..... |
113  |------------------|------------------|------------------|--------------|-------------------|-------------------|-----------------|----------|
114  |IC1 Cell 1 |IC1 Cell 2 |IC1 Cell 3 | ..... | IC1 Cell 12 |IC2 Cell 1 |IC2 Cell 2 | ..... |
115 ****/
116 
117 uint16_t aux_codes[TOTAL_IC][6];
118 /*!<
119  The GPIO codes will be stored in the aux_codes[][6] array in the following format:
120 
121  | aux_codes[0][0]| aux_codes[0][1] | aux_codes[0][2]| aux_codes[0][3]| aux_codes[0][4]| aux_codes[0][5]| aux_codes[1][0] |aux_codes[1][1]| ..... |
122  |-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|---------------|-----------|
123  |IC1 GPIO1 |IC1 GPIO2 |IC1 GPIO3 |IC1 GPIO4 |IC1 GPIO5 |IC1 Vref2 |IC2 GPIO1 |IC2 GPIO2 | ..... |
124 */
125 
126 uint8_t tx_cfg[TOTAL_IC][6];
127 /*!<
128  The tx_cfg[][6] stores the LTC6804 configuration data that is going to be written
129  to the LTC6804 ICs on the daisy chain. The LTC6804 configuration data that will be
130  written should be stored in blocks of 6 bytes. The array should have the following format:
131 
132  | tx_cfg[0][0]| tx_cfg[0][1] | tx_cfg[0][2]| tx_cfg[0][3]| tx_cfg[0][4]| tx_cfg[0][5]| tx_cfg[1][0] | tx_cfg[1][1]| tx_cfg[1][2]| ..... |
133  |--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|-----------|
134  |IC1 CFGR0 |IC1 CFGR1 |IC1 CFGR2 |IC1 CFGR3 |IC1 CFGR4 |IC1 CFGR5 |IC2 CFGR0 |IC2 CFGR1 | IC2 CFGR2 | ..... |
135 
136 */
137 
138 uint8_t rx_cfg[TOTAL_IC][8];
139 /*!<
140  the rx_cfg[][8] array stores the data that is read back from a LTC6804-1 daisy chain.
141  The configuration data for each IC is stored in blocks of 8 bytes. Below is an table illustrating the array organization:
142 
143 |rx_config[0][0]|rx_config[0][1]|rx_config[0][2]|rx_config[0][3]|rx_config[0][4]|rx_config[0][5]|rx_config[0][6] |rx_config[0][7] |rx_config[1][0]|rx_config[1][1]| ..... |
144 |---------------|---------------|---------------|---------------|---------------|---------------|-----------------|----------------|---------------|---------------|-----------|
145 |IC1 CFGR0 |IC1 CFGR1 |IC1 CFGR2 |IC1 CFGR3 |IC1 CFGR4 |IC1 CFGR5 |IC1 PEC High |IC1 PEC Low |IC2 CFGR0 |IC2 CFGR1 | ..... |
146 */
147 
148 /*!**********************************************************************
149  \brief Inititializes hardware and variables
150  ***********************************************************************/
151 void setup()
152 {
153  Serial.begin(115200);
154  LTC6804_initialize(); //Initialize LTC6804 hardware
155  init_cfg(); //initialize the 6804 configuration array to be written
156  print_menu();
157 }
158 
159 /*!*********************************************************************
160  \brief main loop
161 
162 ***********************************************************************/
163 void loop()
164 {
165 
166  if (Serial.available()) // Check for user input
167  {
168  uint32_t user_command;
169  user_command = read_int(); // Read the user command
170  Serial.println(user_command);
171  run_command(user_command);
172  }
173 }
174 
175 
176 /*!*****************************************
177  \brief executes the user inputted command
178 
179  Menu Entry 1: Write Configuration \n
180  Writes the configuration register of the LTC6804. This command can be used to turn on the reference
181  and increase the speed of the ADC conversions.
182 
183  Menu Entry 2: Read Configuration \n
184  Reads the configuration register of the LTC6804, the read configuration can differ from the written configuration.
185  The GPIO pins will reflect the state of the pin
186 
187  Menu Entry 3: Start Cell voltage conversion \n
188  Starts a LTC6804 cell channel adc conversion.
189 
190  Menu Entry 4: Read cell voltages
191  Reads the LTC6804 cell voltage registers and prints the results to the serial port.
192 
193  Menu Entry 5: Start Auxiliary voltage conversion
194  Starts a LTC6804 GPIO channel adc conversion.
195 
196  Menu Entry 6: Read Auxiliary voltages6118
197  Reads the LTC6804 axiliary registers and prints the GPIO voltages to the serial port.
198 
199  Menu Entry 7: Start cell voltage measurement loop
200  The command will continuously measure the LTC6804 cell voltages and print the results to the serial port.
201  The loop can be exited by sending the MCU a 'm' character over the serial link.
202 
203 *******************************************/
204 void run_command(uint32_t cmd)
205 {
206  int8_t error = 0;
207 
208  char input = 0;
209  switch (cmd)
210  {
211 
212  case 1:
213  wakeup_sleep();
215  print_config();
216  break;
217 
218  case 2:
219  wakeup_sleep();
220  error = LTC6804_rdcfg(TOTAL_IC,rx_cfg);
221  if (error == -1)
222  {
223  Serial.println("A PEC error was detected in the received data");
224  }
225  print_rxconfig();
226  break;
227 
228  case 3:
229  wakeup_sleep();
230  LTC6804_adcv();
231  delay(3);
232  Serial.println("cell conversion completed");
233  Serial.println();
234  break;
235 
236  case 4:
237  wakeup_sleep();
238  error = LTC6804_rdcv(0, TOTAL_IC,cell_codes); // Set to read back all cell voltage registers
239  if (error == -1)
240  {
241  Serial.println("A PEC error was detected in the received data");
242  }
243  print_cells();
244  break;
245 
246  case 5:
247  wakeup_sleep();
248  LTC6804_adax();
249  delay(3);
250  Serial.println("aux conversion completed");
251  Serial.println();
252  break;
253 
254  case 6:
255  wakeup_sleep();
256  error = LTC6804_rdaux(0,TOTAL_IC,aux_codes); // Set to read back all aux registers
257  if (error == -1)
258  {
259  Serial.println("A PEC error was detected in the received data");
260  }
261  print_aux();
262  break;
263 
264  case 7:
265  Serial.println("transmit 'm' to quit");
266  wakeup_sleep();
268  while (input != 'm')
269  {
270  if (Serial.available() > 0)
271  {
272  input = read_char();
273  }
274  wakeup_idle();
275  LTC6804_adcv();
276  delay(10);
277  wakeup_idle();
278  error = LTC6804_rdcv(0, TOTAL_IC,cell_codes);
279  if (error == -1)
280  {
281  Serial.println("A PEC error was detected in the received data");
282  }
283  print_cells();
284  delay(500);
285  }
286  print_menu();
287  break;
288 
289  default:
290  Serial.println("Incorrect Option");
291  break;
292  }
293 }
294 
295 /*!***********************************
296  \brief Initializes the configuration array
297  **************************************/
298 void init_cfg()
299 {
300  for (int i = 0; i<TOTAL_IC; i++)
301  {
302  tx_cfg[i][0] = 0xFE;
303  tx_cfg[i][1] = 0x00 ;
304  tx_cfg[i][2] = 0x00 ;
305  tx_cfg[i][3] = 0x00 ;
306  tx_cfg[i][4] = 0x00 ;
307  tx_cfg[i][5] = 0x00 ;
308  }
309 
310 }
311 
312 /*!*********************************
313  \brief Prints the main menu
314 ***********************************/
316 {
317  Serial.println("Please enter LTC6804 Command");
318  Serial.println("Write Configuration: 1");
319  Serial.println("Read Configuration: 2");
320  Serial.println("Start Cell Voltage Conversion: 3");
321  Serial.println("Read Cell Voltages: 4");
322  Serial.println("Start Aux Voltage Conversion: 5");
323  Serial.println("Read Aux Voltages: 6");
324  Serial.println("loop cell voltages: 7");
325  Serial.println("Please enter command: ");
326  Serial.println();
327 }
328 
329 
330 
331 /*!************************************************************
332  \brief Prints cell coltage codes to the serial port
333  *************************************************************/
335 {
336 
337 
338  for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++)
339  {
340  Serial.print(" IC ");
341  Serial.print(current_ic+1,DEC);
342  for (int i=0; i<12; i++)
343  {
344  Serial.print(" C");
345  Serial.print(i+1,DEC);
346  Serial.print(":");
347  Serial.print(cell_codes[current_ic][i]*0.0001,4);
348  Serial.print(",");
349  }
350  Serial.println();
351  }
352  Serial.println();
353 }
354 
355 /*!****************************************************************************
356  \brief Prints GPIO voltage codes and Vref2 voltage code onto the serial port
357  *****************************************************************************/
358 void print_aux()
359 {
360 
361  for (int current_ic =0 ; current_ic < TOTAL_IC; current_ic++)
362  {
363  Serial.print(" IC ");
364  Serial.print(current_ic+1,DEC);
365  for (int i=0; i < 5; i++)
366  {
367  Serial.print(" GPIO-");
368  Serial.print(i+1,DEC);
369  Serial.print(":");
370  Serial.print(aux_codes[current_ic][i]*0.0001,4);
371  Serial.print(",");
372  }
373  Serial.print(" Vref2");
374  Serial.print(":");
375  Serial.print(aux_codes[current_ic][5]*0.0001,4);
376  Serial.println();
377  }
378  Serial.println();
379 }
380 /*!******************************************************************************
381  \brief Prints the configuration data that is going to be written to the LTC6804
382  to the serial port.
383  ********************************************************************************/
385 {
386  int cfg_pec;
387 
388  Serial.println("Written Configuration: ");
389  for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++)
390  {
391  Serial.print(" IC ");
392  Serial.print(current_ic+1,DEC);
393  Serial.print(": ");
394  Serial.print("0x");
395  serial_print_hex(tx_cfg[current_ic][0]);
396  Serial.print(", 0x");
397  serial_print_hex(tx_cfg[current_ic][1]);
398  Serial.print(", 0x");
399  serial_print_hex(tx_cfg[current_ic][2]);
400  Serial.print(", 0x");
401  serial_print_hex(tx_cfg[current_ic][3]);
402  Serial.print(", 0x");
403  serial_print_hex(tx_cfg[current_ic][4]);
404  Serial.print(", 0x");
405  serial_print_hex(tx_cfg[current_ic][5]);
406  Serial.print(", Calculated PEC: 0x");
407  cfg_pec = pec15_calc(6,&tx_cfg[current_ic][0]);
408  serial_print_hex((uint8_t)(cfg_pec>>8));
409  Serial.print(", 0x");
410  serial_print_hex((uint8_t)(cfg_pec));
411  Serial.println();
412  }
413  Serial.println();
414 }
415 
416 /*!*****************************************************************
417  \brief Prints the configuration data that was read back from the
418  LTC6804 to the serial port.
419  *******************************************************************/
421 {
422  Serial.println("Received Configuration ");
423  for (int current_ic=0; current_ic<TOTAL_IC; current_ic++)
424  {
425  Serial.print(" IC ");
426  Serial.print(current_ic+1,DEC);
427  Serial.print(": 0x");
428  serial_print_hex(rx_cfg[current_ic][0]);
429  Serial.print(", 0x");
430  serial_print_hex(rx_cfg[current_ic][1]);
431  Serial.print(", 0x");
432  serial_print_hex(rx_cfg[current_ic][2]);
433  Serial.print(", 0x");
434  serial_print_hex(rx_cfg[current_ic][3]);
435  Serial.print(", 0x");
436  serial_print_hex(rx_cfg[current_ic][4]);
437  Serial.print(", 0x");
438  serial_print_hex(rx_cfg[current_ic][5]);
439  Serial.print(", Received PEC: 0x");
440  serial_print_hex(rx_cfg[current_ic][6]);
441  Serial.print(", 0x");
442  serial_print_hex(rx_cfg[current_ic][7]);
443  Serial.println();
444  }
445  Serial.println();
446 }
447 
448 void serial_print_hex(uint8_t data)
449 {
450  if (data< 16)
451  {
452  Serial.print("0");
453  Serial.print((byte)data,HEX);
454  }
455  else
456  Serial.print((byte)data,HEX);
457 }
int8_t LTC6804_rdcfg(uint8_t total_ic, uint8_t r_config[][8])
Definition: LTC68041.cpp:891
static uint8_t tx_cfg[TOTAL_IC][6]
The tx_cfg[][6] stores the LTC6804 configuration data that is going to be written to the LTC6804 ICs ...
Definition: DC1894.ino:126
static void print_rxconfig()
Definition: DC1894.ino:420
int8_t LTC6804_rdaux(uint8_t reg, uint8_t total_ic, uint16_t aux_codes[][6])
Reads and parses the LTC6804 auxiliary registers.
Definition: LTC68041.cpp:495
void LTC6804_adcv()
Definition: LTC68041.cpp:154
void wakeup_sleep()
Definition: LTC68041.cpp:969
unsigned char user_command
static void print_cells()
Definition: DC1894.ino:334
Header File for Linduino Libraries and Demo Code.
static void loop()
Definition: DC1894.ino:163
void LTC6804_initialize()
This function will initialize all 6804 variables and the SPI port.
Definition: LTC68041.cpp:92
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
static void setup()
Definition: DC1894.ino:151
static void print_config()
Definition: DC1894.ino:384
void wakeup_idle()
Definition: LTC68041.cpp:957
static void print_aux()
Definition: DC1894.ino:358
#define input(pin)
Return the state of pin "pin".
Definition: Linduino.h:79
static int error
uint16_t pec15_calc(uint8_t len, uint8_t *data)
Definition: LTC68041.cpp:985
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
static void print_menu()
Definition: DC1894.ino:315
const uint8_t TOTAL_IC
number of ICs in the daisy chain
Definition: DC1894.ino:100
int32_t read_int()
static uint16_t aux_codes[TOTAL_IC][6]
The GPIO codes will be stored in the aux_codes[][6] array in the following format: ...
Definition: DC1894.ino:117
void LTC6804_adax()
Definition: LTC68041.cpp:206
static void run_command(uint32_t cmd)
Definition: DC1894.ino:204
static uint16_t cell_codes[TOTAL_IC][12]
The cell codes will be stored in the cell_codes[][12] array in the following format: ...
Definition: DC1894.ino:108
static void init_cfg()
Definition: DC1894.ino:298
LTC6804-1 Multicell Battery Monitor.
uint8_t LTC6804_rdcv(uint8_t reg, uint8_t total_ic, uint16_t cell_codes[][12])
Reads and parses the LTC6804 cell voltage registers.
Definition: LTC68041.cpp:268
static int i
Definition: DC2430A.ino:184
void LTC6804_wrcfg(uint8_t total_ic, uint8_t config[][6])
Write the LTC6804 configuration register.
Definition: LTC68041.cpp:805
static void serial_print_hex(uint8_t data)
Definition: DC1894.ino:448
int8_t read_char()
static uint8_t rx_cfg[TOTAL_IC][8]
the rx_cfg[][8] array stores the data that is read back from a LTC6804-1 daisy chain.
Definition: DC1894.ino:138