90 #include <LiquidCrystal.h> 104 Serial.begin(115200);
109 Serial.print(F(
"Found correct demo board"));
113 Serial.println(F(
"Will attempt to proceed"));
124 if (Serial.available())
127 Serial.println(command);
157 LTC2947_DoubleToBytes_Test();
161 Serial.println(F(
"Unknown command!"));
172 Serial.println(F(
"*****************************************************************"));
173 Serial.println(F(
"* DC2334A Demonstration Program *"));
174 Serial.println(F(
"* *"));
175 Serial.println(F(
"* This program demonstrates the basic features of the LTC2947 *"));
176 Serial.println(F(
"* a high-precision power and energy monitor with an internal *"));
177 Serial.println(F(
"* sense resistor supporting up to 30A. *"));
178 Serial.println(F(
"* Set the baud rate to 115200 and select the newline terminator.*"));
179 Serial.println(F(
"*****************************************************************"));
185 Serial.println(F(
"\n\nMain Menu"));
186 Serial.println(F(
" 1. Operation control menu"));
187 Serial.println(F(
" 2. Read Status and Alert Registers"));
188 Serial.println(F(
" 3. Read measurements (I,P,V,TEMP,VCC)"));
189 Serial.println(F(
" 4. Read accumulated measurements (C,E,TB)"));
190 Serial.println(F(
" 5. Init accumulated measurements (C,E,TB)"));
191 Serial.println(F(
" 6. Read min/max values of I,V,P,TEMP"));
192 Serial.println(F(
" 7. GPIO control"));
194 Serial.println(F(
" 8. Send ARA"));
197 Serial.println(F(
" 99. LTC2947_DoubleToBytes_Test"));
199 Serial.print(F(
"Enter: "));
205 Serial.println(F(
"\n\nOperation control"));
206 Serial.println(F(
" 1. Continuous measurement"));
207 Serial.println(F(
" 2. Single measurement"));
208 Serial.println(F(
" 3. Clear measurement results"));
209 Serial.println(F(
" 4. Stop measurement"));
210 Serial.println(F(
" 5. Shutdown"));
211 Serial.println(F(
" 6. Wakeup"));
212 Serial.println(F(
" 7. Read current operation mode"));
213 Serial.println(F(
" 8. RESET"));
214 Serial.println(F(
" Any other number to cancel"));
215 Serial.print(F(
"Enter: "));
217 while (!Serial.available());
248 Serial.println(F(
"Failed to wakeup LTC2947 due to timeout"));
251 Serial.print(F(
"LTC2947 woke up after "));
252 Serial.print(wakeupTime);
253 Serial.println(F(
" milliseconds"));
262 Serial.println(F(
"Continuous measurement enabled"));
264 Serial.println(F(
"LTC2947 was in shutdown mode and should\nhave been woken up by this read operation"));
266 Serial.println(F(
"Idle mode"));
275 Serial.println(F(
"Unsupported command, canceled!"));
283 Serial.println(F(
"\nSTATUS / ALERT registers"));
286 for (uint8_t
i = 0;
i < 8;
i++)
293 Serial.println(F(
"\nMeasurements"));
299 Serial.print(F(
"Current(A): "));
300 Serial.println(current, 3);
302 Serial.print(F(
"Power(W) : "));
303 Serial.println(power, 2);
305 Serial.print(F(
"Voltage(V): "));
306 Serial.println(voltage, 3);
308 Serial.print(F(
"Temp.(C) : "));
309 Serial.println(temp, 1);
311 Serial.print(F(
"Supply(V) : "));
312 Serial.println(vcc, 2);
320 Serial.println(F(
"\nAccumulated measurements"));
322 boolean accuSet1 =
true;
327 Serial.print(F(
"\nAccu set "));
328 Serial.println(accuSet1 ? 1 : 2);
330 Serial.print(F(
"Charge(As): "));
331 Serial.println(C, 4);
333 Serial.print(F(
"Energy(Ws): "));
334 Serial.println(E, 3);
336 Serial.print(F(
"Time(s) : "));
337 Serial.println(TB, 1);
354 uint8_t accu_base_addr;
357 Serial.println(F(
"\nInit Accumulator set 1 or 2?"));
358 Serial.print(F(
"Enter: "));
359 while (!Serial.available());
361 accuSet1 = (sel == 1);
363 Serial.println(F(
"1 (C1,E1,TB1)"));
365 Serial.println(F(
"2 (C2,E2,TB2)"));
367 Serial.println(F(
"Unknown selection, will use C2,E2,TB2"));
373 Serial.println(F(
"\nInput LSBs or real values in As/Ws/s?"));
374 Serial.println(F(
" 1. LSBs"));
375 Serial.println(F(
" 2. Real Values in As/Ws/s"));
376 Serial.print(F(
"Enter: "));
377 while (!Serial.available());
381 Serial.println(F(
"1 (LSBs)"));
383 Serial.println(F(
"2 (As/Ws/s)"));
385 Serial.println(F(
"Unknown selection, will use 1 (LSBs)"));
389 Serial.print(accuSet1 ? F(
"C1: ") : F(
"C2: "));
390 while (!Serial.available());
403 Serial.print(cetb, 8);
404 Serial.println(F(
" As"));
409 Serial.print(accuSet1 ? F(
"E1: ") : F(
"E2: "));
410 while (!Serial.available());
415 Serial.print(cetb, 8);
416 Serial.println(F(
" Ws"));
421 Serial.print(accuSet1 ? F(
"TB1: ") : F(
"TB2: "));
422 while (!Serial.available());
427 Serial.print(cetb, 8);
428 Serial.println(F(
" s"));
431 Serial.print(F(
"\nC,E,TB will be written with following values starting at address 0x"));
436 for (int8_t
i = 0;
i < 16;
i++)
439 Serial.print(F(
":0x"));
442 Serial.print(F(
" (MSB)"));
444 Serial.print(F(
" (LSB)"));
452 Serial.println(F(
"\nRead back of both accu sets from device:"));
461 Serial.println(F(
"\nMIN/MAX measurements"));
469 Serial.print(F(
"Current(A) "));
474 Serial.print(F(
"Power(W) "));
482 Serial.print(F(
"Voltage(V) "));
487 Serial.print(F(
"Temp.(C) "));
496 Serial.println(F(
"\n\nGPIO control"));
497 Serial.println(F(
" 1. Disable output driver"));
498 Serial.println(F(
" 2. Enable output driver"));
499 Serial.println(F(
" 3. Set GPO"));
500 Serial.println(F(
" 4. Clr GPO"));
501 Serial.println(F(
" 5. Read GPIO pin state"));
502 Serial.print(F(
"Enter: "));
504 while (!Serial.available());
511 Serial.print(F(
"GPIO output driver "));
515 Serial.println((opt == 1)
522 Serial.print(F(
"GPIO pin state set "));
526 Serial.println((opt == 3)
532 Serial.print(F(
"GPIO pin "));
539 Serial.println(F(
"Unsupported command, canceled!"));
549 Serial.print(F(
"\nSend ARA, slave response:0x"));
555 Serial.println(F(
" (ERROR: I2C communication failed)"));
558 Serial.println(F(
" (LTC2947 response)"));
561 Serial.println(F(
" (other slave response)"));
564 Serial.println(F(
" (no slave response)"));
568 Serial.println(F(
" (ERROR: LTC2947 response without WR bit)"));
570 Serial.println(F(
" (ERROR: other slave response without WR bit)"));
578 Serial.print(F(
"MIN: "));
581 Serial.print(min, digits);
582 Serial.print(F(
" (0x"));
585 Serial.print(F(
") / MAX: "));
588 Serial.print(max, digits);
589 Serial.print(F(
" (0x"));
603 Serial.println(F(
"Will set LTC2947 to IDLE mode now!"));
610 Serial.println(F(
"LTC2947 in IDLE mode now!"));
614 Serial.println(F(
"Unable to set LTC2947 to IDLE mode!"));
627 if (tbctl == data[0])
628 Serial.println(F(
"Successfully configured LTC2947's TBCTL"));
630 Serial.println(F(
"Failed to configured LTC2947's TBCTL"));
637 switch (register_num)
640 Serial.println(F(
"STATUS:"));
641 Serial.print(F(
" UPDATE: "));
643 Serial.print(F(
" ADCERR: "));
645 Serial.print(F(
" TBERR: "));
647 Serial.print(F(
" PORA: "));
649 Serial.print(F(
" UVLOA: "));
651 Serial.print(F(
" UVLOD: "));
653 Serial.print(F(
" UVLOSTBY:"));
657 Serial.println(F(
"STATVT:"));
658 Serial.print(F(
" FANH: "));
660 Serial.print(F(
" FANL: "));
662 Serial.print(F(
" TEMPH: "));
664 Serial.print(F(
" TEMPL: "));
666 Serial.print(F(
" VH: "));
668 Serial.print(F(
" VL: "));
672 Serial.println(F(
"STATIP:"));
673 Serial.print(F(
" IH: "));
675 Serial.print(F(
" IL: "));
677 Serial.print(F(
" PH: "));
679 Serial.print(F(
" PL: "));
683 Serial.println(F(
"STATC:"));
684 Serial.print(F(
" C1H "));
686 Serial.print(F(
" C1L "));
688 Serial.print(F(
" C2H "));
690 Serial.print(F(
" C2L "));
694 Serial.println(F(
"STATE:"));
695 Serial.print(F(
" E1H "));
697 Serial.print(F(
" E1L "));
699 Serial.print(F(
" E2H "));
701 Serial.print(F(
" E2L "));
705 Serial.println(F(
"STATCEOF:"));
706 Serial.print(F(
" C1OF "));
708 Serial.print(F(
" C2OF "));
710 Serial.print(F(
" E1OF "));
712 Serial.print(F(
" E2OF "));
716 Serial.println(F(
"STATTB:"));
717 Serial.print(F(
" TB1OF "));
719 Serial.print(F(
" TB2OF "));
721 Serial.print(F(
" TB1TH "));
723 Serial.print(F(
" TB2TH "));
727 Serial.println(F(
"STATVDVCC:"));
728 Serial.print(F(
" VDVCCH "));
730 Serial.print(F(
" VDVCCL "));
734 Serial.print(register_num);
735 Serial.println(F(
" is not available!"));
745 Serial.println(F(
"\n\nSelect serial mode for LTC2947"));
746 Serial.println(F(
" 1. I2C (DEFAULT)"));
747 Serial.println(F(
" 2. SPI"));
748 Serial.print(F(
"Enter: "));
749 while (Serial.available())
755 Serial.println(F(
"Will use DEFAULT (I2C)"));
762 Serial.println(F(
"\n\nEnter 7-bit I2C slave address for LTC2947 (DEFAULT:0x5C)"));
763 while (Serial.available())
768 Serial.println(F(
"Will use DEFAULT (0x5C)"));
771 Serial.print(F(
"Slave address: 0x"));
772 Serial.println(slv, HEX);
786 Serial.println(F(
"Unsupported slave address, please try again!"));
802 Serial.println(F(
"Please enter 1 or 2!"));
static void mm7_gpio_ctrl()
LTC2947's GPIO control menu.
#define LTC2947_BM_TBCTL_PRE_0
byte statusRegs[8]
array to store all status / alert registers
#define LTC2947_I2C_ADDR_LL
static void print_min_max(float min, float max, uint8_t digits, byte *minBytes, byte *maxBytes)
prints two (min/max) values with label including the 16 bit raw values
#define LTC2947_ARA_ERROR
general I2C communication error
#define LTC2947_BM_STATC_C1L
#define LTC2947_OFFS_TEMPMAX
#define LTC2947_BM_STATUS_ADCERR
#define LTC2947_BM_STATTB_TB1TH
#define LTC2947_BM_STATIP_PL
#define LTC2947_BM_TBCTL_DIV_0
void LTC2947_Read_I_P_V_TEMP_VCC(float *I, float *P, float *V, float *TEMP, float *VCC)
Reads current (I), power (P), voltage (V), temperature (TEMP) and supply voltage (VCC) from the devic...
#define LTC2947_BM_STATC_C2L
#define LTC2947_BM_STATIP_IL
#define LTC2947_BM_STATTB_TB2TH
static int8_t LTC2947_RD_BYTES(uint8_t REG_ADDR, uint8_t LENGTH, uint8_t *BYTES)
read multiple bytes via I2C/SPI
uint8_t LTC2947_Ara(uint8_t *svlAddr)
Sends the Alert Response address to the I2C bus and reads the response If two or more devices on the ...
void LTC2947_InitSPI()
Initializes the LTC2947 library for SPI mode operation.
static void print_title()
Print the title block.
#define LTC2947_BM_STATVT_VH
#define LTC2947_REG_STATUS
void LTC2947_Read_C_E_TB(boolean accuSet1, double *C, double *E, double *TB)
Reads charge (C), energy (E) and time (TB) from the device.
#define LTC2947_BM_STATVDVCC_VDVCCH
#define LTC2947_BM_STATC_C2H
#define LTC2947_BM_STATC_C1H
#define LTC2947_BM_STATIP_IH
Header File for Linduino Libraries and Demo Code.
#define LTC2947_ARA_OTHER_RESPONSE
got ARA response from some other I2C slave
#define LTC2947_BM_STATE_E2H
static void loop()
Repeats Linduino loop.
static void mm3_measurements()
Read measurement results.
void spi_enable(uint8_t spi_clock_divider)
Setup the processor for hardware SPI communication.
#define LTC2947_BM_STATUS_TBERR
#define LTC2947_BM_STATVT_FANH
static uint8_t demo_board_connected
Global variables.
#define LTC2947_BM_STATVT_TEMPH
void LTC2947_SetPageSelect(boolean page)
write LTC2947's page control register to selected one of two memory pages
#define LTC2947_BM_STATE_E1L
#define LTC2947_ARA_NO_RESPONSE
got NO ARA response
#define LTC2947_BM_STATUS_UVLOA
static void mm1_operation_control()
LTC2947's operation control menu.
void LTC2947_SerialPrint8hex(uint8_t val)
Prints a 8-bit value in 2-character hexadecimal format with left padded zeros.
#define LTC2947_I2C_ADDR_RR
union LT_union_int32_4bytes data
static void setup_ser_mode()
Setup LTC2947's serial interface mode.
#define LTC2947_I2C_ADDR_RL
#define LTC2947_BM_STATCEOF_E1OF
#define LTC2947_BM_STATUS_PORA
#define LTC2947_BM_OPCTL_SHDN
#define LTC2947_BM_STATUS_UVLOD
#define LTC2947_LSB_TEMPMAX
#define LTC2947_BM_STATTB_TB1OF
#define LTC2947_BM_STATUS_UPDATE
static void mm2_read_status_alert_registers()
Read and print all status / alert registers.
boolean LTC2947_SPI_Mode_Enabled
controlled by LTC2947_InitI2C / LTC2947_InitSPI to switch between I2C / SPI mode
static void mm6_min_max()
Read tracking (min/max) measurement values.
#define LTC2947_BM_OPCTL_SSHOT
#define LTC2947_BM_STATCEOF_E2OF
#define LTC2947_BM_OPCTL_RST
int16_t LTC2947_2BytesToInt16(byte *bytes)
converts an array of 2 bytes to 16-bit signed integer
#define LTC2947_REG_TBCTL
boolean LTC2947_GPIO_Read()
reads the current GPIO pin state Make sure LTC2947's page 0 is selected before calling this function...
#define LTC2947_BM_STATCEOF_C1OF
#define LTC2947_BM_OPCTL_CLR
#define bitMaskSetChk(value, bitMask)
int8_t discover_demo_board(char *demo_name)
Read the ID string from the EEPROM and determine if the correct board is connected.
#define LTC2947_BM_STATE_E1H
static void print_main_menu()
Print the main menu.
void LTC2947_InitI2C(uint8_t slvAddr)
Initializes the LTC2947 library for I2C mode operation and configures the slave address see defines L...
static void setup()
Initialize Linduino.
#define LTC2947_OFFS_TEMPMIN
LT_SPI: Routines to communicate with ATmega328P's hardware SPI port.
uint8_t LTC2947_I2C_Slave_Addr
set by LTC2947_InitI2C to set slave address for I2C operation
static void go_idle_cfg_tbctl()
put LTC2947 into idle mode and configure TBCTL
#define LTC2947_BM_STATVT_VL
void LTC2947_GPIO_SetPinState(uint8_t val)
Sets the level of the output driver on the GPIO pin This has only an effect if the output driver is e...
LT_I2C: Routines to communicate with ATmega328P's hardware I2C port.
#define LTC2947_BM_STATCEOF_C2OF
#define LTC2947_BM_STATE_E2L
char demo_name[]
Demo Board Name stored in QuikEval EEPROM.
void quikeval_SPI_connect()
Connect SPI pins to QuikEval connector through the Linduino MUX. This will disconnect I2C...
#define LTC2947_REG_OPCTL
static void mm4_accu_measurements()
Read accumulated measurement results.
int16_t LTC2947_wake_up()
Wake up LTC2947 from shutdown mode and measure the wakeup time.
void LTC2947_SerialPrint16hex(uint16_t val)
Prints a 16-bit value in 4-character hexadecimal format with left padded zeros.
static void parse_status_alert_regs(uint8_t statusRegs[8], uint8_t register_num)
parse status and alert registers.
#define LTC2947_ARA_LTC2947_RESPONSE
got ARA response from LTC2947
static void mm8_ara()
SMBus Alert Response Protocol.
void LTC2947_GPIO_PinMode(uint8_t mode)
Enables/disables the output driver on the GPIO pin Make sure LTC2947's page 0 is selected before call...
#define LTC2947_LSB_TEMPMIN
#define LTC2947_I2C_ADDR_LH
#define LTC2947_BM_STATVT_TEMPL
#define LTC2947_I2C_ADDR_LR
void LTC2947_DoubleToBytes(double value, double lsb, uint8_t *bytes, uint8_t length)
Converts a floating point number that was scaled with a given LSB to an integer representation that w...
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
LTC2947: LTC2947 a high-precision power and energy monitor with an internal sense resistor supporting...
static int8_t LTC2947_WR_BYTES(uint8_t REG_ADDR, uint8_t LENGTH, uint8_t *BYTES)
write byte array via I2C/SPI interface
static int8_t LTC2947_WR_BYTE(uint8_t REG_ADDR, uint8_t VALUE)
write single byte via I2C
#define LTC2947_BM_STATVDVCC_VDVCCL
void quikeval_I2C_connect(void)
Switch MUX to connect I2C pins to QuikEval connector.
static uint16_t current
the current measurement from the LTC3335's counter test mode.
static void mm5_accu_init()
Initialize accumulators with user defined values.
#define LTC2947_BM_STATTB_TB2OF
#define LTC2947_BM_OPCTL_CONT
#define LTC2947_BM_STATIP_PH
#define LTC2947_BM_STATVT_FANL
#define LTC2947_BM_STATUS_UVLOSTBY
#define LTC2947_ARA_RESPONSE_WO_WR
got ARA response from any slave but wihtout the expected WR bit
#define LTC2947_I2C_ADDR_RH
static int8_t LTC2947_RD_BYTE(uint8_t REG_ADDR, uint8_t *RESULT)
read single byte via I2C/SPI