114 #define AUTOMATIC_MODE_DISPLAY_DELAY 1000 115 #define SCAN_MODE_DISPLAY_DELAY 10000
116 const float resistor = .100;
119 const char ack_error[] =
"Error: No Acknowledge. Check I2C Address.";
134 Serial.begin(115200);
144 Serial.println(
"Did not read ID String, attempting to proceed anyway...\nPlease ensure I2C lines of Linduino are connected to the LTC device");
157 static uint8_t mAh_or_Coulombs = 0;
158 static uint8_t celcius_or_kelvin = 0;
160 static uint16_t prescalarValue = 128;
165 if (Serial.available())
168 if (user_command !=
'm')
169 Serial.println(user_command);
172 switch (user_command)
175 ack |=
menu_1_automatic_mode(mAh_or_Coulombs, celcius_or_kelvin, prescalar_mode, prescalarValue, alcc_mode);
184 ack |=
menu_4_sleep_mode(mAh_or_Coulombs, prescalar_mode, prescalarValue, alcc_mode);
190 ack |=
menu_6_settings(&mAh_or_Coulombs, &celcius_or_kelvin, &prescalar_mode, &prescalarValue, &alcc_mode);
195 Serial.print(F(
"*************************"));
205 Serial.println(F(
"\n*****************************************************************"));
206 Serial.print(F(
"* DC1496B-B Demonstration Program *\n"));
207 Serial.print(F(
"* *\n"));
208 Serial.print(F(
"* This program communicates with the LTC2942 Coulomb Counter *\n"));
209 Serial.print(F(
"* found on the DC1496B-B demo board. *\n"));
210 Serial.print(F(
"* Set the baud rate to 115200 and select the newline terminator.*\n"));
211 Serial.print(F(
"* *\n"));
212 Serial.print(F(
"*****************************************************************\n"));
218 Serial.print(F(
"\n1-Automatic Mode\n"));
219 Serial.print(F(
"2-Manual Voltage Mode\n"));
220 Serial.print(F(
"3-Manual Temperature Mode\n"));
221 Serial.print(F(
"4-Sleep Mode\n"));
222 Serial.print(F(
"5-Shutdown Mode\n"));
223 Serial.print(F(
"6-Settings\n"));
224 Serial.print(F(
"Enter a command: "));
228 int8_t
menu_1_automatic_mode(int8_t mAh_or_Coulombs, int8_t celcius_or_kelvin ,uint16_t prescalar_mode, uint16_t prescalarValue, uint16_t alcc_mode)
238 Serial.println(F(
"could not configure LTC2942"));
243 Serial.print(F(
"*************************\n\n"));
245 uint8_t status_code, hightemp_code, lowtemp_code;
246 uint16_t charge_code, current_code, voltage_code, temperature_code;
253 Serial.print(F(
"CHARGE: "));
254 Serial.println(charge_code,HEX);
255 Serial.print(F(
"VOLTAGE: "));
256 Serial.println(voltage_code, HEX);
257 Serial.print(F(
"TEMPERATURE: "));
258 Serial.println(temperature_code,HEX);
260 float charge,
voltage, temperature;
264 Serial.print(
"Coulombs: ");
265 Serial.print(charge, 4);
266 Serial.print(F(
" C\n"));
271 Serial.print(
"mAh: ");
272 Serial.print(charge, 4);
273 Serial.print(F(
" mAh\n"));
280 Serial.print(F(
"Voltage "));
281 Serial.print(voltage, 4);
282 Serial.print(F(
" V\n"));
285 if (celcius_or_kelvin)
288 Serial.print(F(
"Temperature "));
289 Serial.print(temperature, 4);
290 Serial.print(F(
" K\n"));
295 Serial.print(F(
"Temperature "));
296 Serial.print(temperature, 4);
297 Serial.print(F(
" C\n"));
302 Serial.print(F(
"m-Main Menu\n\n"));
307 while (!Serial.available() && !(ack));
314 int8_t
menu_2_manual_voltage_mode(int8_t mAh_or_Coulombs ,int8_t celcius_or_kelvin ,uint16_t prescalar_mode, uint16_t prescalarValue, uint16_t alcc_mode)
327 Serial.print(F(
"*************************\n\n"));
330 uint16_t charge_code, voltage_code;
338 float charge,
voltage, temperature;
342 Serial.print(
"Coulombs: ");
343 Serial.print(charge, 4);
344 Serial.print(F(
" C\n"));
349 Serial.print(
"mAh: ");
350 Serial.print(charge, 4);
351 Serial.print(F(
" mAh\n"));
358 Serial.print(F(
"Voltage "));
359 Serial.print(voltage, 4);
360 Serial.print(F(
" V"));
361 if (staleData) Serial.print(F(
" ***** Stale Data ******\n"));
362 else Serial.println(
"");
365 if (staleData) Serial.print(F(
" ***** Stale Data ******\n"));
366 else Serial.println(
"");
372 Serial.print(F(
"m-Main Menu\n\n"));
378 while (!Serial.available() && !(ack));
397 Serial.print(F(
"*************************\n\n"));
400 uint16_t charge_code, temperature_code;
408 float charge, temperature;
412 Serial.print(
"Coulombs: ");
413 Serial.print(charge, 4);
414 Serial.print(F(
" C\n"));
419 Serial.print(
"mAh: ");
420 Serial.print(charge, 4);
421 Serial.print(F(
" mAh\n"));
427 if (celcius_or_kelvin)
430 Serial.print(F(
"Temperature "));
431 Serial.print(temperature, 4);
432 Serial.print(F(
" K"));
437 Serial.print(F(
"Temperature "));
438 Serial.print(temperature, 4);
439 Serial.print(F(
" C"));
441 if (staleData) Serial.print(F(
" ***** Stale Data ******\n"));
442 else Serial.println(
"");
448 Serial.print(F(
"m-Main Menu\n\n"));
454 while (!Serial.available() && !(ack));
460 int8_t
menu_4_sleep_mode(int8_t mAh_or_Coulombs ,uint16_t prescalar_mode, uint16_t prescalarValue, uint16_t alcc_mode)
472 Serial.print(F(
"*************************\n\n"));
475 uint16_t charge_code;
486 Serial.print(
"Coulombs: ");
487 Serial.print(charge, 4);
488 Serial.print(F(
" C\n"));
493 Serial.print(
"mAh: ");
494 Serial.print(charge, 4);
495 Serial.print(F(
" mAh\n"));
499 Serial.print(F(
"Voltage "));
500 Serial.print(F(
" ADC Sleep...\n"));
502 Serial.print(F(
"Temperature "));
503 Serial.print(F(
" ADC Sleep...\n"));
505 Serial.print(F(
"m-Main Menu\n\n"));
512 while (!Serial.available() && !(ack));
523 Serial.print(
"LTC2942 Has Been ShutDown\n");
527 int8_t
menu_6_settings(uint8_t *mAh_or_Coulombs, uint8_t *celcius_or_kelvin, uint16_t *prescalar_mode, uint16_t *prescalarValue, uint16_t *alcc_mode)
534 Serial.print(F(
"*************************\n\n"));
535 Serial.print(F(
"1-Set Alert Thresholds\n"));
536 Serial.print(F(
"2-Set Prescalar Value\n"));
537 Serial.print(F(
"3-Set AL#/CC# Pin State\n"));
538 Serial.print(F(
"4-Set Units\n"));
539 Serial.print(F(
"m-Main Menu\n\n"));
540 Serial.print(F(
"Enter a command: "));
543 if (user_command ==
'm')
546 Serial.println(user_command);
548 switch (user_command)
567 if (user_command !=
'm')
568 Serial.println(
"Incorrect Option");
572 while (!((user_command ==
'm') || (ack)));
585 Serial.print(F(
"*************************\n\n"));
586 Serial.print(F(
"1-Set Charge Thresholds\n"));
587 Serial.print(F(
"2-Set Voltage Thresholds\n"));
588 Serial.print(F(
"3-Set Temperature Thresholds\n"));
589 Serial.print(F(
"m-Main Menu\n\n"));
590 Serial.print(F(
"Enter a command: "));
593 if (user_command ==
'm')
596 Serial.println(user_command);
598 switch (user_command)
610 if (user_command !=
'm')
611 Serial.println(
"Incorrect Option");
615 while (!((user_command ==
'm') || (ack)));
624 Serial.print(F(
"Enter RAW Max Charge Threshold:"));
626 uint16_t max_charge_threshold;
628 Serial.println(max_charge_threshold);
632 Serial.print(F(
"Enter RAW Min Charge Threshold:"));
634 float min_charge_threshold;
636 Serial.println(min_charge_threshold);
648 Serial.print(F(
"Enter Max Voltage Threshold:"));
650 float max_voltage_threshold;
652 Serial.print(max_voltage_threshold, 3);
656 uint8_t max_voltage_threshold_code = max_voltage_threshold_code_16>>8;
660 Serial.print(F(
"Enter Min Voltage Threshold:"));
662 float min_voltage_threshold;
664 Serial.println(min_voltage_threshold, 3);
668 uint8_t min_voltage_threshold_code = min_voltage_threshold_code_16>>8;
679 Serial.print(F(
"Enter Max Temperature Threshold in Celcius:"));
681 float max_temperature_threshold;
683 Serial.print(max_temperature_threshold, 2);
687 uint8_t max_temperature_threshold_code = max_temperature_threshold_code_16>>8;
692 Serial.print(F(
"Enter Min Temperature Threshold in Celcius:"));
694 float min_temperature_threshold;
696 Serial.print(min_temperature_threshold, 2);
699 uint8_t min_temperature_threshold_code = min_temperature_threshold_code_16>>8;
714 Serial.print(F(
"*************************\n\n"));
715 Serial.print(F(
"1-Set Prescalar M = 1\n"));
716 Serial.print(F(
"2-Set Prescalar M = 2\n"));
717 Serial.print(F(
"3-Set Prescalar M = 4\n"));
718 Serial.print(F(
"4-Set Prescalar M = 8\n"));
719 Serial.print(F(
"5-Set Prescalar M = 16\n"));
720 Serial.print(F(
"6-Set Prescalar M = 32\n"));
721 Serial.print(F(
"7-Set Prescalar M = 64\n"));
722 Serial.print(F(
"8-Set Prescalar M = 128\n"));
723 Serial.print(F(
"m-Main Menu\n\n"));
724 Serial.print(F(
"Enter a command: "));
727 if (user_command ==
'm')
730 Serial.println(user_command);
732 switch (user_command)
737 Serial.println(F(
"\nPrescalar Set to 1\n"));
742 Serial.println(F(
"\nPrescalar Set to 2\n"));
747 Serial.println(F(
"\nPrescalar Set to 4\n"));
752 Serial.println(F(
"\nPrescalar Set to 8\n"));
756 *prescalarValue = 16;
757 Serial.println(F(
"\nPrescalar Set to 16\n"));
761 *prescalarValue = 32;
763 Serial.println(F(
"\nPrescalar Set to 32\n"));
767 *prescalarValue = 64;
768 Serial.println(F(
"\nPrescalar Set to 64\n"));
772 *prescalarValue =128;
773 Serial.println(F(
"\nPrescalar Set to 128\n"));
775 if (user_command !=
'm')
776 Serial.println(
"Incorrect Option");
780 while (!((user_command ==
'm') || (ack)));
792 Serial.print(F(
"*************************\n\n"));
793 Serial.print(F(
"1-Enable Alert Mode\n"));
794 Serial.print(F(
"2-Enable Charge Complete Mode\n"));
795 Serial.print(F(
"3-Disable AL#/CC# Pin\n"));
796 Serial.print(F(
"m-Main Menu\n\n"));
797 Serial.print(F(
"Enter a command: "));
800 if (user_command ==
'm')
803 Serial.println(user_command);
805 switch (user_command)
809 Serial.println(F(
"\nAlert Mode Enabled\n"));
813 Serial.println(F(
"\nCharge Mode Enabled\n"));
817 Serial.println(F(
"\nAL#/CC# Pin Disabled\n"));
820 if (user_command !=
'm')
821 Serial.println(
"Incorrect Option");
825 while (!((user_command ==
'm') || (ack)));
837 Serial.print(F(
"*************************\n\n"));
838 Serial.print(F(
"1-Set Charge Units to mAh\n"));
839 Serial.print(F(
"2-Set Charge Units to Coulombs\n"));
840 Serial.print(F(
"3-Set Temperature Units to Celsius\n"));
841 Serial.print(F(
"4-Set Temperature Units to Kelvin\n"));
842 Serial.print(F(
"m-Main Menu\n\n"));
843 Serial.print(F(
"Enter a command: "));
846 if (user_command ==
'm')
849 Serial.println(user_command);
851 switch (user_command)
854 *mAh_or_Coulombs = 0;
855 Serial.println(F(
"\nCharge Units Set to mAh\n"));
858 *mAh_or_Coulombs = 1;
859 Serial.println(F(
"\nCharge Units Set to Coulombs\n"));
862 *celcius_or_kelvin = 0;
863 Serial.println(F(
"\nTemperature Units Set to Celcius\n"));
866 *celcius_or_kelvin = 1;
867 Serial.println(F(
"\nTemperature Units Set to Kelvin\n"));
870 if (user_command !=
'm')
871 Serial.println(
"Incorrect Option");
876 while (!((user_command ==
'm') || (ack)));
885 return((1<<position)&value);
894 Serial.print(F(
"\n***********************\n"));
895 Serial.print(F(
"Alert: "));
896 Serial.print(F(
"Charge Over/Under Flow Alert\n"));
897 Serial.print(F(
"***********************\n"));
901 Serial.print(F(
"\n***********************\n"));
902 Serial.print(F(
"Alert: "));
903 Serial.print(F(
"Temperature Alert\n"));
904 Serial.print(F(
"***********************\n"));
908 Serial.print(F(
"\n***********************\n"));
909 Serial.print(F(
"Alert: "));
910 Serial.print(F(
"Charge High Alert\n"));
911 Serial.print(F(
"***********************\n"));
915 Serial.print(F(
"\n***********************\n"));
916 Serial.print(F(
"Alert: "));
917 Serial.print(F(
"Charge Low Alert\n"));
918 Serial.print(F(
"***********************\n"));
922 Serial.print(F(
"\n***********************\n"));
923 Serial.print(F(
"Alert: "));
924 Serial.print(F(
"Voltage Alert\n"));
925 Serial.print(F(
"***********************\n"));
929 Serial.print(F(
"\n***********************\n"));
930 Serial.print(F(
"Alert: "));
931 Serial.print(F(
"UVLO Alert\n"));
932 Serial.print(F(
"***********************\n"));
static int8_t menu_4_sleep_mode(int8_t mAh_or_Coulombs, uint16_t prescalar_mode, uint16_t prescalarValue, uint16_t alcc_mode)
Sleep Mode.
float LTC2942_code_to_voltage(uint16_t adc_code)
Calculate the LTC2942 SENSE+ voltage.
#define LTC2942_PRESCALAR_M_32
#define LTC2942_PRESCALAR_M_2
#define LTC2942_SLEEP_MODE
unsigned char user_command
LTC2942: Battery Gas Gauge with Temperature, Voltage Measurement.
const float LTC2942_FULLSCALE_TEMPERATURE
#define LTC2942_PRESCALAR_M_1
#define LTC2942_MANUAL_TEMPERATURE
#define LTC2942_TEMPERATURE_THRESH_LOW_REG
const float resistor
resistor value on demo board
static void store_alert_settings()
int8_t LTC2942_read_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t *adc_code)
Reads a 16-bit adc_code from LTC2942.
Header File for Linduino Libraries and Demo Code.
#define LTC2942_I2C_ADDRESS
#define LTC2942_SHUTDOWN_MODE
#define LTC2942_VOLTAGE_THRESH_HIGH_REG
#define LTC2942_TEMPERATURE_MSB_REG
#define LTC2942_PRESCALAR_M_8
static int8_t menu_3_manual_temperature_mode(int8_t mAh_or_Coulombs, int8_t celcius_or_kelvin, uint16_t prescalar_mode, uint16_t prescalarValue, uint16_t alcc_mode)
Manual Voltage Mode.
static void print_prompt()
Print the Prompt.
#define LTC2942_STATUS_REG
int8_t LTC2942_write(uint8_t i2c_address, uint8_t adc_command, uint8_t code)
Write an 8-bit code to the LTC2942.
int8_t LTC2942_write_16_bits(uint8_t i2c_address, uint8_t adc_command, uint16_t code)
Write a 16-bit code to the LTC2942.
#define LTC2942_PRESCALAR_M_4
const float LTC2942_FULLSCALE_VOLTAGE
#define LTC2942_PRESCALAR_M_64
static int8_t menu_6_alert_menu_1_set_charge_thresholds()
Set Charge Threshold Function.
static uint8_t alert_code
Value stored or read from ALERT register.
#define LTC2942_MANUAL_VOLTAGE
static uint8_t menu_6_alert_menu_3_set_allcc_state(uint16_t *alcc_mode)
AL#/CC# Pin Mode Menu.
#define LTC2942_PRESCALAR_M_16
#define LTC2942_ACCUM_CHARGE_MSB_REG
float LTC2942_code_to_coulombs(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2942 charge in Coulombs.
bool isBitSet(uint8_t value, uint8_t position)
Checks to see if a bit in a certain position is set.
static int8_t menu_6_settings_menu_2_set_prescalar_values(uint16_t *prescalar_mode, uint16_t *prescalarValue)
Prescalar Menu.
static int8_t menu_5_shutdown_mode()
Shutdown Mode.
static int8_t menu_2_manual_voltage_mode(int8_t mAh_or_Coulombs, int8_t celcius_or_kelvin, uint16_t prescalar_mode, uint16_t prescalarValue, uint16_t alcc_mode)
Manual Voltage Mode.
static int8_t menu_6_settings(uint8_t *mAh_or_Coulombs, uint8_t *celcius_or_kelvin, uint16_t *prescalar_mode, uint16_t *prescalarValue, uint16_t *alcc_mode)
Settings Menu.
int8_t discover_demo_board(char *demo_name)
Read the ID string from the EEPROM and determine if the correct board is connected.
#define LTC2942_CHARGE_COMPLETE_MODE
static void checkAlerts(uint8_t status_code)
Check Alerts Function - Checks to see if an alert has been set in the status register. If an alert has been set, it prints out the appropriate message.
#define LTC2942_DISABLE_ALCC_PIN
static void loop()
Repeats Linduino loop.
static uint8_t menu_6_alert_menu_4_set_units(uint8_t *mAh_or_Coulombs, uint8_t *celcius_or_kelvin)
Set Charge and Temperature Units Menu.
static int8_t menu_1_automatic_mode(int8_t mAh_or_Coulombs, int8_t celcius_or_kelvin, uint16_t prescalar_mode, uint16_t prescalarValue, uint16_t alcc_mode)
Automatic Mode.
#define LTC2942_VOLTAGE_THRESH_LOW_REG
static int8_t menu_6_alert_menu_3_set_temperature_thresholds()
Set Temperature Thresholds.
LT_I2C: Routines to communicate with ATmega328P's hardware I2C port.
static void print_title()
Print the title block.
#define LTC2942_TEMPERATURE_THRESH_HIGH_REG
static int8_t menu_6_settings_menu_1_set_alert_thresholds()
Alert Threshold Menu.
char demo_name[]
Demo Board Name stored in QuikEval EEPROM.
static int8_t menu_6_alert_menu_2_set_voltage_thresholds()
Set Voltage Thresholds.
#define AUTOMATIC_MODE_DISPLAY_DELAY
The delay between readings in automatic mode.
#define LTC2942_PRESCALAR_M_128
#define LTC2942_AUTOMATIC_MODE
#define LTC2942_VOLTAGE_MSB_REG
#define LTC2942_CHARGE_THRESH_LOW_MSB_REG
#define LTC2942_CONTROL_REG
float LTC2942_code_to_mAh(uint16_t adc_code, float resistor, uint16_t prescalar)
Calculate the LTC2942 charge in mAh.
static int8_t demo_board_connected
Set to 1 if the board is connected.
static void setup()
Initialize Linduino.
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
void quikeval_I2C_connect(void)
Switch MUX to connect I2C pins to QuikEval connector.
static int8_t restore_alert_settings()
float LTC2942_code_to_celcius_temperature(uint16_t adc_code)
Calculate the LTC2942 temperature.
const char ack_error[]
Error message.
int8_t LTC2942_read(uint8_t i2c_address, uint8_t adc_command, uint8_t *adc_code)
Reads an 8-bit adc_code from LTC2942.
float LTC2942_code_to_kelvin_temperature(uint16_t adc_code)
Calculate the LTC2942 temperature.
#define LTC2942_CHARGE_THRESH_HIGH_MSB_REG
#define LTC2942_ALERT_MODE