![](/media/cache/group/logo-maxim-integrated.png.50x50_q85.png)
Sample code for the MAX17055 Fuel Gauge with EZ config Initialization API implemented. The MAX17055 is part of the MAX32620FTHR
Dependencies: MAX17055_EZconfig
main.cpp@0:bec440cb42e5, 2019-02-07 (annotated)
- Committer:
- fneirab
- Date:
- Thu Feb 07 21:50:03 2019 +0000
- Revision:
- 0:bec440cb42e5
This is the sample code for the MAX17055 which is included in the MAX32620FTHR.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fneirab | 0:bec440cb42e5 | 1 | #include "mbed.h" |
fneirab | 0:bec440cb42e5 | 2 | #include "max17055.h" |
fneirab | 0:bec440cb42e5 | 3 | |
fneirab | 0:bec440cb42e5 | 4 | |
fneirab | 0:bec440cb42e5 | 5 | //LED indication |
fneirab | 0:bec440cb42e5 | 6 | DigitalOut rled(LED1); |
fneirab | 0:bec440cb42e5 | 7 | |
fneirab | 0:bec440cb42e5 | 8 | //I2C communications |
fneirab | 0:bec440cb42e5 | 9 | I2C i2cBus(P5_7, P6_0); |
fneirab | 0:bec440cb42e5 | 10 | |
fneirab | 0:bec440cb42e5 | 11 | //Fuel Gauge |
fneirab | 0:bec440cb42e5 | 12 | MAX17055 fuelGauge(i2cBus); |
fneirab | 0:bec440cb42e5 | 13 | MAX17055::platform_data design_param; |
fneirab | 0:bec440cb42e5 | 14 | MAX17055::saved_FG_params_t saved_param; |
fneirab | 0:bec440cb42e5 | 15 | |
fneirab | 0:bec440cb42e5 | 16 | //MAX77650 PMIC |
fneirab | 0:bec440cb42e5 | 17 | #define POWER_HOLD_ON 1 |
fneirab | 0:bec440cb42e5 | 18 | #define POWER_HOLD_OFF 0 |
fneirab | 0:bec440cb42e5 | 19 | |
fneirab | 0:bec440cb42e5 | 20 | DigitalOut pw_Hold(P2_2, POWER_HOLD_ON); //To enable battery operation from the PMIC |
fneirab | 0:bec440cb42e5 | 21 | |
fneirab | 0:bec440cb42e5 | 22 | //Battery Parameters Storage from the Fuel Gauge MAX17055 |
fneirab | 0:bec440cb42e5 | 23 | union max17055_u { |
fneirab | 0:bec440cb42e5 | 24 | struct battery { |
fneirab | 0:bec440cb42e5 | 25 | uint8_t avg_soc_FG; // in Battery Percent |
fneirab | 0:bec440cb42e5 | 26 | float tte_FG; // in seconds |
fneirab | 0:bec440cb42e5 | 27 | float ttf_FG; // in seconds |
fneirab | 0:bec440cb42e5 | 28 | int avg_vcell_FG; // in 78.125uV per bit |
fneirab | 0:bec440cb42e5 | 29 | float avg_curr_FG; // in uAmps |
fneirab | 0:bec440cb42e5 | 30 | float curr_FG; // in uAmps |
fneirab | 0:bec440cb42e5 | 31 | int rep_cap; // in mAh |
fneirab | 0:bec440cb42e5 | 32 | int rep_SOC; // in % |
fneirab | 0:bec440cb42e5 | 33 | } battery; |
fneirab | 0:bec440cb42e5 | 34 | } max17055_u; |
fneirab | 0:bec440cb42e5 | 35 | |
fneirab | 0:bec440cb42e5 | 36 | |
fneirab | 0:bec440cb42e5 | 37 | int main(void) |
fneirab | 0:bec440cb42e5 | 38 | { |
fneirab | 0:bec440cb42e5 | 39 | rled = true; |
fneirab | 0:bec440cb42e5 | 40 | //These are the calculated parameters for rsense 5mOhm found in the MAX32620FTHR and a 350mAh Li+ Cell. |
fneirab | 0:bec440cb42e5 | 41 | design_param.designcap = 0x015E; //Design Battery Capacity mAh thic can change depending on the batteries implemented see battery data sheet for details. |
fneirab | 0:bec440cb42e5 | 42 | design_param.ichgterm = 0x0070; // Charge Termination Current for the Battery This is specified by the manufacturer. |
fneirab | 0:bec440cb42e5 | 43 | design_param.vempty = 0x9600; // 3V Battery Empty Voltage This is sepecified by design, but manufacturer has a min Empy voltage specification. |
fneirab | 0:bec440cb42e5 | 44 | design_param.vcharge = 4200; // 4.2V Battery Charge Voltage can be obtained from MAX77650 configuration |
fneirab | 0:bec440cb42e5 | 45 | design_param.rsense = 5; //5mOhms for MAX32620, keep in mind the MAX17055EVKIT has a 10mOhm resistor. This is a design specific value. Used Rsense for resgiter calculation results. |
fneirab | 0:bec440cb42e5 | 46 | |
fneirab | 0:bec440cb42e5 | 47 | //Saved Parameters |
fneirab | 0:bec440cb42e5 | 48 | //saved_param.cycles = 0; //This value is used for the save parameters function. |
fneirab | 0:bec440cb42e5 | 49 | |
fneirab | 0:bec440cb42e5 | 50 | int status; // stores return value of the status of setting up the MAX17055 |
fneirab | 0:bec440cb42e5 | 51 | MAX17055::Registers_e array_addresses[7] = {MAX17055::MODELCFG_REG, |
fneirab | 0:bec440cb42e5 | 52 | MAX17055::DESIGNCAP_REG, |
fneirab | 0:bec440cb42e5 | 53 | MAX17055::FULLCAPNOM_REG, |
fneirab | 0:bec440cb42e5 | 54 | MAX17055::DPACC_REG, |
fneirab | 0:bec440cb42e5 | 55 | MAX17055::DQACC_REG, |
fneirab | 0:bec440cb42e5 | 56 | MAX17055::VEMPTY_REG, |
fneirab | 0:bec440cb42e5 | 57 | MAX17055::ICHGTERM_REG |
fneirab | 0:bec440cb42e5 | 58 | }; |
fneirab | 0:bec440cb42e5 | 59 | |
fneirab | 0:bec440cb42e5 | 60 | status = fuelGauge.init(design_param); |
fneirab | 0:bec440cb42e5 | 61 | printf("Init FuelGauge Function Status= %X \r\n",status); // Status shoudl retun Zero if the are no issues with the initialization. |
fneirab | 0:bec440cb42e5 | 62 | |
fneirab | 0:bec440cb42e5 | 63 | if (status == 0) |
fneirab | 0:bec440cb42e5 | 64 | fuelGauge.save_Params(saved_param); |
fneirab | 0:bec440cb42e5 | 65 | |
fneirab | 0:bec440cb42e5 | 66 | //Read DesignCap, Ichagterm, Vempty, dqacc, dpacc, MODELCFG, FullCapNom to verify values correspond to the expected stored Values. |
fneirab | 0:bec440cb42e5 | 67 | // FullCapNom[mAh] = (dqacc[mAh]/dpacc[%])*100% |
fneirab | 0:bec440cb42e5 | 68 | uint16_t regValue[7]; |
fneirab | 0:bec440cb42e5 | 69 | for(int i = 0 ; i < 7 ; i++) { |
fneirab | 0:bec440cb42e5 | 70 | regValue[i] = fuelGauge.get_regInfo(array_addresses[i]); |
fneirab | 0:bec440cb42e5 | 71 | printf("%X" ,regValue[i]); |
fneirab | 0:bec440cb42e5 | 72 | printf(" "); |
fneirab | 0:bec440cb42e5 | 73 | } |
fneirab | 0:bec440cb42e5 | 74 | rled = false; |
fneirab | 0:bec440cb42e5 | 75 | wait(2); |
fneirab | 0:bec440cb42e5 | 76 | |
fneirab | 0:bec440cb42e5 | 77 | while (1) { |
fneirab | 0:bec440cb42e5 | 78 | //This code is an example to ilustrate the performance or the Fuel Gauge. This can change with the design requirements. Use this as a way to troubleshoot the FuelGauge. |
fneirab | 0:bec440cb42e5 | 79 | rled = !rled; |
fneirab | 0:bec440cb42e5 | 80 | |
fneirab | 0:bec440cb42e5 | 81 | max17055_u.battery.avg_vcell_FG = fuelGauge.get_avgVcell(); |
fneirab | 0:bec440cb42e5 | 82 | max17055_u.battery.avg_curr_FG = fuelGauge.get_AvgCurrent(design_param); |
fneirab | 0:bec440cb42e5 | 83 | max17055_u.battery.curr_FG = fuelGauge.get_Current(design_param); |
fneirab | 0:bec440cb42e5 | 84 | max17055_u.battery.rep_cap = fuelGauge.get_battCAP(design_param); |
fneirab | 0:bec440cb42e5 | 85 | max17055_u.battery.rep_SOC = fuelGauge.get_SOC(); |
fneirab | 0:bec440cb42e5 | 86 | |
fneirab | 0:bec440cb42e5 | 87 | //This code should work with Arduino Serial Plotter to visualize data |
fneirab | 0:bec440cb42e5 | 88 | printf("%f " ,(max17055_u.battery.avg_vcell_FG/1000000.0)); //V |
fneirab | 0:bec440cb42e5 | 89 | printf("%f " ,max17055_u.battery.avg_curr_FG); //uA |
fneirab | 0:bec440cb42e5 | 90 | printf("%f " ,max17055_u.battery.curr_FG); //uA |
fneirab | 0:bec440cb42e5 | 91 | printf("%d " ,max17055_u.battery.rep_cap); //mAh |
fneirab | 0:bec440cb42e5 | 92 | printf("%d " ,max17055_u.battery.rep_SOC); //% |
fneirab | 0:bec440cb42e5 | 93 | printf("\n"); |
fneirab | 0:bec440cb42e5 | 94 | |
fneirab | 0:bec440cb42e5 | 95 | wait(1); |
fneirab | 0:bec440cb42e5 | 96 | } |
fneirab | 0:bec440cb42e5 | 97 | } |