Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
DC1208A.ino
Go to the documentation of this file.
1 /*!
2 DC1208A
3 High Voltage I2C Current and Voltage Monitor
4 
5 @verbatim
6 
7  Setup:
8  Set the terminal baud rate to 115200 and select the newline terminator. Refer
9  to Demo Manual DC1208A. Ensure all jumpers are installed in the factory default
10  positions. A power supply and load resistor are required. A DVM may be used to
11  verify displayed voltage results. A precision voltage source (preferably
12  low-noise) may be used to apply a voltage to the ADIN pin. For all tests,
13  connect power supply between VIN and GND, 7V-75V. Connect a load resistor to
14  Vout and GND.
15 
16  Command Description:
17 
18  *****Main Menu*****
19 
20  1- Read Continuous Mode- If selected, program keeps taking readings every second
21  or so. In this mode, the program displays Load Current, VIN Voltage and ADIN
22  voltage.
23 
24  2- Snapshot Mode- Selecting this option causes display of the Snapshot Mode
25  Menu. Readings are taken just once for each Snapshot Mode menu entry.
26 
27  ***** SNAPSHOT MODE MENU COMMANDS *****
28 
29  1- Load Current- Selecting this option causes load current to be displayed.
30 
31  2- V_IN Voltage- Selecting this option causes VIN voltage to be displayed.
32 
33  3- ADIN Voltage- Selecting this option causes the voltage at the ADIN pin to
34  be displayed.
35 
36 USER INPUT DATA FORMAT:
37  decimal : 1024
38  hex : 0x400
39  octal : 02000 (leading 0 "zero")
40  binary : B10000000000
41  float : 1024.0
42 
43 @endverbatim
44 
45 
46 Copyright 2018(c) Analog Devices, Inc.
47 
48 All rights reserved.
49 
50 Redistribution and use in source and binary forms, with or without
51 modification, are permitted provided that the following conditions are met:
52  - Redistributions of source code must retain the above copyright
53  notice, this list of conditions and the following disclaimer.
54  - Redistributions in binary form must reproduce the above copyright
55  notice, this list of conditions and the following disclaimer in
56  the documentation and/or other materials provided with the
57  distribution.
58  - Neither the name of Analog Devices, Inc. nor the names of its
59  contributors may be used to endorse or promote products derived
60  from this software without specific prior written permission.
61  - The use of this software may or may not infringe the patent rights
62  of one or more patent holders. This license does not release you
63  from the requirement that you obtain separate licenses from these
64  patent holders to use this software.
65  - Use of the software either in source or binary form, must be run
66  on or directly connected to an Analog Devices Inc. component.
67 
68 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
69 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
70 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
71 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
72 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
73 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
74 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
75 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
76 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
77 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
78 */
79 
80 /*! @file
81  @ingroup LTC4151
82 */
83 
84 #include <Arduino.h>
85 #include <stdint.h>
86 #include "Linduino.h"
87 #include "LT_I2C.h"
88 #include "LT_SPI.h"
89 #include "UserInterface.h"
90 #include "QuikEval_EEPROM.h"
91 #include "LTC4151.h"
92 #include <Wire.h>
93 #include <SPI.h>
94 
95 // Function Declaration
96 void print_title(); // Print the title block
97 void print_prompt(); // Print the main menu
98 int8_t LTC4151_snapshot(uint8_t i2c_address, uint8_t channel); // Build and send the snapshot command
99 
100 int8_t menu_1_continuous_mode(); // Sub-menu functions
101 int8_t menu_2_snapshot_mode();
102 
103 // Global variables
104 static int8_t demo_board_connected; //!< Set to 1 if the board is connected
105 
106 const float resistor = .02; //!< Sense resistor value
107 
108 const float LTC4151_sense_lsb = 20e-6; //!< Typical sense lsb weight in volts
109 const float LTC4151_vin_lsb = 25e-3; //!< Typical Vin lsb weight in volts
110 const float LTC4151_adin_lsb = 0.5; //!< Typical ADIN lsb weight in mV
111 
112 //! Initialize Linduino
113 void setup()
114 {
115  char demo_name[] = "DC1208"; // Demo Board Name stored in QuikEval EEPROM
116  quikeval_I2C_init(); //! Initializes Linduino I2C port to 100kHz
117  quikeval_I2C_connect(); //! Connects I2C port to the QuikEval connector
118  Serial.begin(115200); //! Initialize the serial port to the PC
119  print_title();
120  demo_board_connected = discover_demo_board(demo_name); //! Checks if correct demo board is connected.
121  if (demo_board_connected) // Do nothing if the demo board is not connected
122  print_prompt();
123 }
124 
125 //! Repeats Linduino loop
126 void loop()
127 {
128  int8_t ack = 0;
129  uint8_t user_command;
130  if (demo_board_connected) //! Does nothing if the demo board is not connected
131  {
132  if (Serial.available()) // Checks for user input
133  {
134  user_command = read_int(); //! Reads the user command
135  if (user_command != 'm')
136  Serial.println(user_command);
137  ack = 0;
138  switch (user_command) //! Prints the appropriate submenu
139  {
140  case 1:
141  ack = menu_1_continuous_mode(); // Execute continuous mode
142  break;
143  case 2:
144  ack = menu_2_snapshot_mode(); // Execute snapshot mode
145  break;
146  default:
147  Serial.println("Incorrect Option");
148  break;
149  }
150  if (ack != 0)
151  Serial.println("Error: No Acknowledge. Check I2C Address.");
152  Serial.println();
153  Serial.println("***************************");
154  print_prompt();
155  }
156  }
157 }
158 
159 // Function Definitions
160 
161 //! Prints the title block when program first starts.
163 {
164  Serial.println("");
165  Serial.println("*****************************************************************");
166  Serial.println("* DC1208 Demonstration Program *");
167  Serial.println("* *");
168  Serial.println("* This program communicates with the LTC4151 High Voltage I2C *");
169  Serial.println("* Current and Voltage Monitor found on the DC1208A demo board. *");
170  Serial.println("* Set the baud rate to 115200 and select the newline terminator.*");
171  Serial.println("* *");
172  Serial.println("*****************************************************************");
173 }
174 
175 //! Prints main menu.
177 {
178  Serial.println();
179  Serial.println("1-Read Continuous Mode");
180  Serial.println("2-Read Snapshot Mode");
181  Serial.println();
182  Serial.print("Enter a command: ");
183 }
184 
185 //! Build and send the snapshot command
186 //! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
187 int8_t LTC4151_snapshot(uint8_t i2c_address, //!< I2C address of the LTC4151.
188  uint8_t channel) //!< Desired channel from the header
189 {
190  int8_t ack = 0;
191  ack |= LTC4151_write(i2c_address, LTC4151_CONTROL_REG, LTC4151_CONTINUOUS_MODE); //! Disable previous snapshot mode to allow a new snapshot
192  ack |= LTC4151_write(i2c_address, LTC4151_CONTROL_REG, (channel | LTC4151_SNAPSHOT_MODE)); //! Re-enable snapshot mode on selected channel
193  return(ack);
194 }
195 
196 //! Reads all inputs in continuous mode
197 //! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
199 {
200  int8_t ack = 0;
201 
202  // Read Continuous Mode
203  ack |= LTC4151_write(LTC4151_I2C_ADDRESS, LTC4151_CONTROL_REG, LTC4151_CONTINUOUS_MODE); //! Set LTC4151 to continuous mode
204  do
205  {
206  uint16_t current_sense_adc_code, vin_adc_code, adin_adc_code;
207 
208  ack |= LTC4151_read_12_bits(LTC4151_I2C_ADDRESS, LTC4151_SENSE_MSB_REG, &current_sense_adc_code); //! Reads two bytes for SENSE, MSB register followed by LSB register
209  ack |= LTC4151_read_12_bits(LTC4151_I2C_ADDRESS, LTC4151_VIN_MSB_REG, &vin_adc_code); //! Reads two bytes for VIN, MSB register followed by LSB register
210  ack |= LTC4151_read_12_bits(LTC4151_I2C_ADDRESS, LTC4151_ADIN_MSB_REG, &adin_adc_code); //! Reads two bytes for ADIN, MSB register followed by LSB register
211 
212  float current, VIN_voltage, ADIN_voltage;
213  current = LTC4151_code_to_sense_current(current_sense_adc_code, resistor, LTC4151_sense_lsb);
214  VIN_voltage = LTC4151_code_to_vin_voltage(vin_adc_code, LTC4151_vin_lsb);
215  ADIN_voltage = LTC4151_code_to_ADIN_voltage(adin_adc_code, LTC4151_adin_lsb);
216 
217  //! Display current, VIN voltage, and ADIN Voltage
218 
219  Serial.println();
220  Serial.println("***************************");
221  Serial.print("Load Current: ");
222  Serial.print(current);
223  Serial.println(" A\n");
224 
225  Serial.print("VIN Voltage: ");
226  Serial.print(VIN_voltage);
227  Serial.println(" V\n");
228 
229  Serial.print("ADIN Voltage: ");
230  Serial.print(ADIN_voltage);
231  Serial.println(" mV\n");
232 
233  Serial.println("m-Main Menu");
234  delay(2000);
235  }
236  while (Serial.available() == false); //! Repeats until user enters a character
237  read_int(); // Clears Serial.available
238  return(ack);
239 }
240 
241 //! Reads inputs in snapshot mode
242 //! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
244 {
245  int8_t ack = 0;
246  uint8_t user_command;
247 
248  // Snapshot Mode
249  do
250  {
251  //! Displays the Snapshot Mode menu
252  Serial.println("***************************");
253  Serial.println("Snapshot Mode");
254  Serial.println("1-Load Current:");
255  Serial.println("2-V_IN Voltage:");
256  Serial.println("3-ADIN Voltage:");
257  Serial.println("m-Main Menu");
258  Serial.print("Enter a command: ");
259 
260  user_command = read_int(); //! Reads the user command
261 
262  if (user_command == 'm') // Print m if it is entered
263  Serial.println("m");
264  else
265  Serial.println(user_command); // Print user command
266  Serial.println();
267 
268  uint16_t current_sense_adc_code, vin_adc_code, adin_adc_code;
269  float current, VIN_voltage, ADIN_voltage;
270 
271  //! Reads sense resistor current, Vin voltage, or ADIN voltage
272  switch (user_command)
273  {
274  case 1:
276  delay(200); //Wait for conversion to finish. Could watch busy bit instead to reduce wait time.
277  ack |= LTC4151_read_12_bits(LTC4151_I2C_ADDRESS, LTC4151_SENSE_MSB_REG, &current_sense_adc_code);
278  current = LTC4151_code_to_sense_current(current_sense_adc_code, resistor, LTC4151_sense_lsb);
279  Serial.print("Load Current: ");
280  Serial.print(current);
281  Serial.println(" A\n");
282  break;
283  case 2:
285  delay(100); //Wait for conversion to finish. Could watch busy bit instead to reduce wait time.
287  VIN_voltage = LTC4151_code_to_vin_voltage(vin_adc_code, LTC4151_vin_lsb);
288  Serial.print("VIN Voltage: ");
289  Serial.print(VIN_voltage);
290  Serial.println(" V\n");
291  break;
292  case 3:
294  delay(100); //Wait for conversion to finish. Could watch busy bit instead to reduce wait time.
296  ADIN_voltage = LTC4151_code_to_ADIN_voltage(adin_adc_code, LTC4151_adin_lsb);
297  Serial.print("ADIN Voltage: ");
298  Serial.print(ADIN_voltage);
299  Serial.println(" mV\n");
300  break;
301  default:
302  if (user_command != 'm')
303  Serial.println("Incorrect Option");
304  break;
305  }
307  }
308  while ((user_command != 'm') && (ack == 0));
309  return(ack);
310 }
static void loop()
Repeats Linduino loop.
Definition: DC1208A.ino:126
static void print_prompt()
Prints main menu.
Definition: DC1208A.ino:176
uint8_t i2c_address
unsigned char user_command
const float resistor
Sense resistor value.
Definition: DC1208A.ino:106
#define LTC4151_ADIN_MSB_REG
Definition: LTC4151.h:159
const float LTC4151_adin_lsb
Typical ADIN lsb weight in mV.
Definition: DC1208A.ino:110
static int8_t menu_2_snapshot_mode()
Reads inputs in snapshot mode.
Definition: DC1208A.ino:243
Header File for Linduino Libraries and Demo Code.
static uint8_t channel
LTC2305 Channel selection.
Definition: DC1444A.ino:127
float LTC4151_code_to_ADIN_voltage(uint16_t adc_code, float LTC4151_adin_lsb)
Calculates the LTC4151 ADIN voltage in mV given "LTC4151_adin_lsb" LSB weight in mV.
Definition: LTC4151.cpp:117
int8_t LTC4151_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
Write one byte to an LTC4151 register.
Definition: LTC4151.cpp:73
const float LTC4151_sense_lsb
Typical sense lsb weight in volts.
Definition: DC1208A.ino:108
LTC4151: High Voltage I2C Current and Voltage Monitor.
static int8_t demo_board_connected
Set to 1 if the board is connected.
Definition: DC1208A.ino:104
int8_t LTC4151_read_12_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
Reads a 12-bit value from LTC4151.
Definition: LTC4151.cpp:83
QuikEval EEPROM Library.
float LTC4151_code_to_vin_voltage(uint16_t adc_code, float LTC4151_vin_lsb)
Calculates the LTC4151 V_IN voltage given "LTC_vin_lsb" LSB weight in volts.
Definition: LTC4151.cpp:109
static int8_t menu_1_continuous_mode()
Reads all inputs in continuous mode.
Definition: DC1208A.ino:198
int8_t discover_demo_board(char *demo_name)
Read the ID string from the EEPROM and determine if the correct board is connected.
static void setup()
Initialize Linduino.
Definition: DC1208A.ino:113
#define LTC4151_I2C_ADDRESS
Definition: LTC4151.h:131
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
const float LTC4151_vin_lsb
Typical Vin lsb weight in volts.
Definition: DC1208A.ino:109
static void print_title()
Prints the title block when program first starts.
Definition: DC1208A.ino:162
#define LTC4151_SENSE_MSB_REG
Definition: LTC4151.h:155
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
char demo_name[]
Demo Board Name stored in QuikEval EEPROM.
Definition: DC1880A.ino:97
int32_t read_int()
#define LTC4151_VIN_CHANNEL_REG
Definition: LTC4151.h:179
static int8_t LTC4151_snapshot(uint8_t i2c_address, uint8_t channel)
Build and send the snapshot command.
Definition: DC1208A.ino:187
#define LTC4151_SENSE_CHANNEL_REG
Definition: LTC4151.h:178
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
float LTC4151_code_to_sense_current(uint16_t adc_code, float resistor, float LTC4151_sense_lsb)
Calculates the LTC4151 sense current in Amps given "resistor" value in ohms and "LTC4151_sense_lsb" L...
Definition: LTC4151.cpp:100
void quikeval_I2C_connect(void)
Switch MUX to connect I2C pins to QuikEval connector.
Definition: LT_I2C.cpp:401
#define LTC4151_CONTINUOUS_MODE
Definition: LTC4151.h:182
#define LTC4151_VIN_MSB_REG
Definition: LTC4151.h:157
static uint16_t current
the current measurement from the LTC3335&#39;s counter test mode.
Definition: DC2343A.ino:114
#define LTC4151_CONTROL_REG
Definition: LTC4151.h:161
#define LTC4151_SNAPSHOT_MODE
Definition: LTC4151.h:183
#define LTC4151_ADIN_CHANNEL_REG
Definition: LTC4151.h:180