cc_cv_charging.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "cc_cv_charging.h"
  2. #include "battery.h"
  3. #include "dac.h"
  4. #include "adc.h"
  5. #include "ti/devices/msp/m0p/mspm0g350x.h"
  6. #include "ti/driverlib/dl_gpio.h"
  7. #include <stdio.h>
  8. //#define BATTERY_CAPACITY_MAH (2000)
  9. #define MAX_VOLTAGE_MV (4200)
  10. #define MIN_VOLTAGE_MV (3000)
  11. #define CC_CURRENT_LIMIT_MA (500)
  12. #define CUTOFF_CURRENT_MA (50)
  13. #define MAX_CYCLES (2)
  14. #define CONSTANT_VALUE (1)
  15. #define SHUNT_RESISTOR (0.1)
  16. //********** Not used **************************
  17. // if the battery is deeply discharged then 50mA of trickle charge is given for a set timer
  18. //#define TRICKLE_CHARGE_CURRENT_MA (50)
  19. //#define TRICKLE_CHARGE_VOLTAGE_MV (2100)
  20. //#define TRICKLE_CHARGE_TIMEOUT_MS (5000) //5 mseconds
  21. //Pre Charge
  22. //#define PRE_CHARGE_CURRENT_MA (BATTERY_CAPACITY_MAH/ 10)
  23. static ChargingState charging_state= STATE_PRE_CHARGE;
  24. static uint16_t cycle_count= 0;
  25. static uint32_t pre_charge_start_time= 0;
  26. //Functions for Charging and Discharging
  27. void CC_CV_UpdateChargingState(uint8_t slot_id){
  28. uint16_t batt_voltage= batteries[slot_id].voltage;
  29. int16_t batt_current= batteries[slot_id].current;
  30. if(batt_voltage < MIN_VOLTAGE_MV){
  31. charging_state= STATE_PRE_CHARGE;
  32. }
  33. else if(batt_voltage >= MIN_VOLTAGE_MV && batt_voltage < MAX_VOLTAGE_MV - BATTERY_THRESHOLD){
  34. charging_state= STATE_CC_CHARGING;
  35. }
  36. else if(batt_voltage >= MAX_VOLTAGE_MV - BATTERY_THRESHOLD){
  37. charging_state= STATE_CV_CHARGING;
  38. }
  39. else if(charging_state == STATE_CV_CHARGING && batt_current <= CUTOFF_CURRENT_MA + BATTERY_THRESHOLD){
  40. if(cycle_count < MAX_CYCLES){
  41. charging_state= STATE_DISCHARGING;
  42. }
  43. else{
  44. charging_state = STATE_FINAL_DISCHARGE;
  45. }
  46. }
  47. else if(charging_state == STATE_DISCHARGING && batt_voltage <= MIN_VOLTAGE_MV + BATTERY_THRESHOLD) {
  48. charging_state= STATE_CC_CHARGING;
  49. }
  50. else{
  51. charging_state= STATE_ERROR;
  52. }
  53. }
  54. /*
  55. Function for Battery Charging and Discharging:
  56. Trickle Charge: Only used when the battery voltage < ~2.1 V.
  57. - In this state, the batteries internal protection may have been previously disconnected.
  58. Pre Charge: Once the battery is in discharged state, pre-charging begins.
  59. - Starts charging safely with a typically low current C/10
  60. Constant Current: Continues until the battery voltage has reached the "full" or floating voltage level
  61. Constant Voltage: CV volatge starts once the maximum voltage threshold is obtained
  62. */
  63. void CC_CV_ControlCharging(uint8_t slot_id){
  64. //Calling function to get all the conditional states
  65. CC_CV_UpdateChargingState(slot_id);
  66. uint16_t batt_voltage= batteries[slot_id].voltage;
  67. int16_t batt_current= batteries[slot_id].current;
  68. uint16_t batt_cutoff_current= batteries[slot_id].cut_off_current;
  69. uint16_t charge_current_mA= (batteries[slot_id].capacitance * batteries[slot_id].charge_fraction)/100;
  70. int16_t dac_channel_VoutA;
  71. switch(charging_state){
  72. case STATE_PRE_CHARGE:
  73. DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  74. uint16_t pre_charge_current_limit_ma = charge_current_mA/10;
  75. DAC_fastWrite(pre_charge_current_limit_ma);
  76. printf("[CC-CV] Pre Charging: Slot %d at %d mA.\n", slot_id, pre_charge_current_limit_ma);
  77. break;
  78. case STATE_CC_CHARGING:
  79. DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  80. DAC_fastWrite(charge_current_mA);
  81. printf("[CC-CV] CC Charging: Slot %d at %d mA.\n", slot_id, CC_CURRENT_LIMIT_MA);
  82. break;
  83. case STATE_CV_CHARGING:
  84. DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  85. if(batt_current >= batt_cutoff_current+BATTERY_THRESHOLD){
  86. //Reducing by 5%
  87. dac_channel_VoutA= (batt_current - (batt_current-0.05));
  88. }
  89. else{
  90. dac_channel_VoutA= 0;
  91. charging_state= STATE_FINAL_DISCHARGE;
  92. }
  93. DAC_fastWrite(dac_channel_VoutA);
  94. printf("[CC-CV] CV Charging: Slot %d at %d mA.\n", slot_id, CUTOFF_CURRENT_MA);
  95. break;
  96. case STATE_DISCHARGING:
  97. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  98. DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
  99. //still unsure about the value for channel VoutA:
  100. DAC_fastWrite(batt_voltage);
  101. printf("[CC-CV] Discharging: Slot %d at %d mA.\n", slot_id, batt_voltage);
  102. break;
  103. case STATE_FINAL_DISCHARGE:
  104. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  105. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
  106. printf("[CC-CV] Final Discharge: Slot %d..\n", slot_id);
  107. DAC_fastWrite(0);
  108. case STATE_ERROR:
  109. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  110. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
  111. printf("[CC-CV] Error: Slot %d.\n", slot_id);
  112. default:
  113. break;
  114. }
  115. }