ACS data acq changed completely. Tested and working. Deals all faults.
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of QM_BAE_review_1 by
EPS.cpp@0:7b4c00e3912f, 2015-12-24 (annotated)
- Committer:
- sakthipriya
- Date:
- Thu Dec 24 19:15:43 2015 +0000
- Revision:
- 0:7b4c00e3912f
- Child:
- 1:446a959e36ce
EPS CONOPS BTRY GAUGE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:7b4c00e3912f | 1 | #include "EPS.h" |
sakthipriya | 0:7b4c00e3912f | 2 | #include "pin_config.h" |
sakthipriya | 0:7b4c00e3912f | 3 | /***********************************************global variable declaration***************************************************************/ |
sakthipriya | 0:7b4c00e3912f | 4 | extern uint32_t BAE_STATUS; |
sakthipriya | 0:7b4c00e3912f | 5 | extern uint32_t BAE_ENABLE; |
sakthipriya | 0:7b4c00e3912f | 6 | |
sakthipriya | 0:7b4c00e3912f | 7 | |
sakthipriya | 0:7b4c00e3912f | 8 | //m_I2C.frequency(10000) |
sakthipriya | 0:7b4c00e3912f | 9 | const char RCOMP0= 0x97; |
sakthipriya | 0:7b4c00e3912f | 10 | BAE_HK_actual actual_data; |
sakthipriya | 0:7b4c00e3912f | 11 | BAE_HK_quant quant_data; |
sakthipriya | 0:7b4c00e3912f | 12 | BAE_HK_min_max bae_HK_minmax; |
sakthipriya | 0:7b4c00e3912f | 13 | BAE_HK_arch arch_data; |
sakthipriya | 0:7b4c00e3912f | 14 | |
sakthipriya | 0:7b4c00e3912f | 15 | //......................................Peripheral declarations.........................................................// |
sakthipriya | 0:7b4c00e3912f | 16 | Serial pc_eps(USBTX,USBRX); |
sakthipriya | 0:7b4c00e3912f | 17 | I2C m_I2C(PIN85,PIN84); |
sakthipriya | 0:7b4c00e3912f | 18 | DigitalOut TRXY(TRXY_DR_EN); //active high |
sakthipriya | 0:7b4c00e3912f | 19 | DigitalOut TRZ(TRZ_DR_EN); //active high |
sakthipriya | 0:7b4c00e3912f | 20 | DigitalOut EN3V3A(ENBL3V3A); |
sakthipriya | 0:7b4c00e3912f | 21 | DigitalOut EN_BTRY_HT(BATT_HEAT); |
sakthipriya | 0:7b4c00e3912f | 22 | //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT); |
sakthipriya | 0:7b4c00e3912f | 23 | AnalogIn Vbatt_ang(VBATT); |
sakthipriya | 0:7b4c00e3912f | 24 | |
sakthipriya | 0:7b4c00e3912f | 25 | //*********************************************************flags********************************************************// |
sakthipriya | 0:7b4c00e3912f | 26 | extern char EPS_INIT_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 27 | extern char EPS_BATTERY_GAUGE_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 28 | extern char EPS_MAIN_STATUS; |
sakthipriya | 0:7b4c00e3912f | 29 | extern char EPS_BATTERY_TEMP_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 30 | extern char EPS_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 31 | |
sakthipriya | 0:7b4c00e3912f | 32 | extern char EPS_BATTERY_HEAT_ENABLE ; |
sakthipriya | 0:7b4c00e3912f | 33 | |
sakthipriya | 0:7b4c00e3912f | 34 | //........................................... FUCTIONS.................................................// |
sakthipriya | 0:7b4c00e3912f | 35 | |
sakthipriya | 0:7b4c00e3912f | 36 | void FCTN_EPS_INIT() |
sakthipriya | 0:7b4c00e3912f | 37 | { |
sakthipriya | 0:7b4c00e3912f | 38 | printf("\n\r eps init \n"); |
sakthipriya | 0:7b4c00e3912f | 39 | EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag |
sakthipriya | 0:7b4c00e3912f | 40 | // FLAG(); |
sakthipriya | 0:7b4c00e3912f | 41 | FCTN_BATTERYGAUGE_INIT(); |
sakthipriya | 0:7b4c00e3912f | 42 | //FCTN_EPS_BTEMP_INIT(); |
sakthipriya | 0:7b4c00e3912f | 43 | EN3V3A = 1; //enable dc dc converter A |
sakthipriya | 0:7b4c00e3912f | 44 | char value=alertFlags(); |
sakthipriya | 0:7b4c00e3912f | 45 | unsigned short value_u= (short int )value; |
sakthipriya | 0:7b4c00e3912f | 46 | value_u &=0x0001; |
sakthipriya | 0:7b4c00e3912f | 47 | if(value_u ==0x0001) // battery gauge not initialised |
sakthipriya | 0:7b4c00e3912f | 48 | { |
sakthipriya | 0:7b4c00e3912f | 49 | actual_data.power_mode = 1; |
sakthipriya | 0:7b4c00e3912f | 50 | EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 0:7b4c00e3912f | 51 | } |
sakthipriya | 0:7b4c00e3912f | 52 | else |
sakthipriya | 0:7b4c00e3912f | 53 | { |
sakthipriya | 0:7b4c00e3912f | 54 | actual_data.Batt_gauge_actual[1] = soc(); |
sakthipriya | 0:7b4c00e3912f | 55 | actual_data.Batt_voltage_actual = Vbatt_ang.read()*3.3; |
sakthipriya | 0:7b4c00e3912f | 56 | FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]); |
sakthipriya | 0:7b4c00e3912f | 57 | EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 0:7b4c00e3912f | 58 | } |
sakthipriya | 0:7b4c00e3912f | 59 | |
sakthipriya | 0:7b4c00e3912f | 60 | EPS_INIT_STATUS = 'c' ; //clear EPS_INIT_STATUS flag |
sakthipriya | 0:7b4c00e3912f | 61 | |
sakthipriya | 0:7b4c00e3912f | 62 | } |
sakthipriya | 0:7b4c00e3912f | 63 | |
sakthipriya | 0:7b4c00e3912f | 64 | //----------------------------------------------------Power algo code--------------------------------------------------------------------// |
sakthipriya | 0:7b4c00e3912f | 65 | void FCTN_EPS_POWERMODE(float soc) //dummy algo |
sakthipriya | 0:7b4c00e3912f | 66 | { |
sakthipriya | 0:7b4c00e3912f | 67 | if(soc >= 80) |
sakthipriya | 0:7b4c00e3912f | 68 | actual_data.power_mode = 4; |
sakthipriya | 0:7b4c00e3912f | 69 | else if(soc >= 70 & soc < 80) |
sakthipriya | 0:7b4c00e3912f | 70 | actual_data.power_mode = 3; |
sakthipriya | 0:7b4c00e3912f | 71 | else if(soc >= 60 & soc < 70) |
sakthipriya | 0:7b4c00e3912f | 72 | actual_data.power_mode = 2; |
sakthipriya | 0:7b4c00e3912f | 73 | else if(soc < 60) |
sakthipriya | 0:7b4c00e3912f | 74 | actual_data.power_mode = 1; |
sakthipriya | 0:7b4c00e3912f | 75 | } |
sakthipriya | 0:7b4c00e3912f | 76 | |
sakthipriya | 0:7b4c00e3912f | 77 | //...................................................HK...........................................// |
sakthipriya | 0:7b4c00e3912f | 78 | |
sakthipriya | 0:7b4c00e3912f | 79 | int quantiz(float start,float step,float x) |
sakthipriya | 0:7b4c00e3912f | 80 | { |
sakthipriya | 0:7b4c00e3912f | 81 | int y=(x-start)/step; |
sakthipriya | 0:7b4c00e3912f | 82 | if(y<=0)y=0; |
sakthipriya | 0:7b4c00e3912f | 83 | if(y>=255)y=255; |
sakthipriya | 0:7b4c00e3912f | 84 | return y; |
sakthipriya | 0:7b4c00e3912f | 85 | } |
sakthipriya | 0:7b4c00e3912f | 86 | |
sakthipriya | 0:7b4c00e3912f | 87 | void HK_main() |
sakthipriya | 0:7b4c00e3912f | 88 | { |
sakthipriya | 0:7b4c00e3912f | 89 | |
sakthipriya | 0:7b4c00e3912f | 90 | } |
sakthipriya | 0:7b4c00e3912f | 91 | |
sakthipriya | 0:7b4c00e3912f | 92 | //............................................BATTERY GAUGE......................................// |
sakthipriya | 0:7b4c00e3912f | 93 | void FCTN_BATTERYGAUGE_INIT() |
sakthipriya | 0:7b4c00e3912f | 94 | { |
sakthipriya | 0:7b4c00e3912f | 95 | disable_sleep(); |
sakthipriya | 0:7b4c00e3912f | 96 | disable_hibernate(); |
sakthipriya | 0:7b4c00e3912f | 97 | socChangeAlertEnabled(true); //enabling alert on soc changing by 1% |
sakthipriya | 0:7b4c00e3912f | 98 | emptyAlertThreshold(32);//setting empty alert threshold to 32% soc |
sakthipriya | 0:7b4c00e3912f | 99 | vAlertMinMaxThreshold();//set min, max value of Valrt register |
sakthipriya | 0:7b4c00e3912f | 100 | vResetThresholdSet();//set threshold voltage for reset |
sakthipriya | 0:7b4c00e3912f | 101 | vResetAlertEnabled(true);//enable alert on reset for V < Vreset |
sakthipriya | 0:7b4c00e3912f | 102 | } |
sakthipriya | 0:7b4c00e3912f | 103 | |
sakthipriya | 0:7b4c00e3912f | 104 | void FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4]) |
sakthipriya | 0:7b4c00e3912f | 105 | { |
sakthipriya | 0:7b4c00e3912f | 106 | |
sakthipriya | 0:7b4c00e3912f | 107 | |
sakthipriya | 0:7b4c00e3912f | 108 | float temp=25; //=Battery_temp (from temp sensor on battery board) //value of battery temperature in C currently given a dummy value. Should be updated everytime. |
sakthipriya | 0:7b4c00e3912f | 109 | tempCompensation(temp); |
sakthipriya | 0:7b4c00e3912f | 110 | |
sakthipriya | 0:7b4c00e3912f | 111 | |
sakthipriya | 0:7b4c00e3912f | 112 | Battery_parameters[0]=vcell(); |
sakthipriya | 0:7b4c00e3912f | 113 | Battery_parameters[1]=soc(); |
sakthipriya | 0:7b4c00e3912f | 114 | Battery_parameters[2]=crate(); |
sakthipriya | 0:7b4c00e3912f | 115 | |
sakthipriya | 0:7b4c00e3912f | 116 | printf("\nVcell=%f",vcell()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 117 | printf("\nSOC=%f",soc()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 118 | printf("\nC_rate=%f",crate()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 119 | |
sakthipriya | 0:7b4c00e3912f | 120 | |
sakthipriya | 0:7b4c00e3912f | 121 | if (alerting()== true) //alert is on |
sakthipriya | 0:7b4c00e3912f | 122 | { |
sakthipriya | 0:7b4c00e3912f | 123 | Battery_parameters[3]=alertFlags(); |
sakthipriya | 0:7b4c00e3912f | 124 | clearAlert();//clear alert |
sakthipriya | 0:7b4c00e3912f | 125 | clearAlertFlags();//clear all alert flags |
sakthipriya | 0:7b4c00e3912f | 126 | } |
sakthipriya | 0:7b4c00e3912f | 127 | |
sakthipriya | 0:7b4c00e3912f | 128 | } |
sakthipriya | 0:7b4c00e3912f | 129 | |
sakthipriya | 0:7b4c00e3912f | 130 | unsigned short read(char reg) |
sakthipriya | 0:7b4c00e3912f | 131 | { |
sakthipriya | 0:7b4c00e3912f | 132 | |
sakthipriya | 0:7b4c00e3912f | 133 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 134 | char buff[2]; |
sakthipriya | 0:7b4c00e3912f | 135 | |
sakthipriya | 0:7b4c00e3912f | 136 | //Select the register |
sakthipriya | 0:7b4c00e3912f | 137 | m_I2C.write(m_ADDR, ®, 1, true); |
sakthipriya | 0:7b4c00e3912f | 138 | |
sakthipriya | 0:7b4c00e3912f | 139 | //Read the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 140 | m_I2C.read(m_ADDR, buff, 2); |
sakthipriya | 0:7b4c00e3912f | 141 | |
sakthipriya | 0:7b4c00e3912f | 142 | //Return the combined 16-bit value |
sakthipriya | 0:7b4c00e3912f | 143 | return (buff[0] << 8) | buff[1]; |
sakthipriya | 0:7b4c00e3912f | 144 | } |
sakthipriya | 0:7b4c00e3912f | 145 | |
sakthipriya | 0:7b4c00e3912f | 146 | |
sakthipriya | 0:7b4c00e3912f | 147 | void write(char reg, unsigned short data) |
sakthipriya | 0:7b4c00e3912f | 148 | { |
sakthipriya | 0:7b4c00e3912f | 149 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 150 | char buff[3]; |
sakthipriya | 0:7b4c00e3912f | 151 | |
sakthipriya | 0:7b4c00e3912f | 152 | //Load the register address and 16-bit data |
sakthipriya | 0:7b4c00e3912f | 153 | buff[0] = reg; |
sakthipriya | 0:7b4c00e3912f | 154 | buff[1] = data >> 8; |
sakthipriya | 0:7b4c00e3912f | 155 | buff[2] = data; |
sakthipriya | 0:7b4c00e3912f | 156 | |
sakthipriya | 0:7b4c00e3912f | 157 | //Write the data |
sakthipriya | 0:7b4c00e3912f | 158 | m_I2C.write(m_ADDR, buff, 3); |
sakthipriya | 0:7b4c00e3912f | 159 | } |
sakthipriya | 0:7b4c00e3912f | 160 | |
sakthipriya | 0:7b4c00e3912f | 161 | |
sakthipriya | 0:7b4c00e3912f | 162 | |
sakthipriya | 0:7b4c00e3912f | 163 | // Command the MAX17049 to perform a power-on reset |
sakthipriya | 0:7b4c00e3912f | 164 | void reset() |
sakthipriya | 0:7b4c00e3912f | 165 | { |
sakthipriya | 0:7b4c00e3912f | 166 | //Write the POR command |
sakthipriya | 0:7b4c00e3912f | 167 | write(REG_CMD, 0x5400); |
sakthipriya | 0:7b4c00e3912f | 168 | } |
sakthipriya | 0:7b4c00e3912f | 169 | |
sakthipriya | 0:7b4c00e3912f | 170 | // Command the MAX17049 to perform a QuickStart |
sakthipriya | 0:7b4c00e3912f | 171 | void quickStart() |
sakthipriya | 0:7b4c00e3912f | 172 | { |
sakthipriya | 0:7b4c00e3912f | 173 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 174 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 175 | |
sakthipriya | 0:7b4c00e3912f | 176 | //Set the QuickStart bit |
sakthipriya | 0:7b4c00e3912f | 177 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 178 | |
sakthipriya | 0:7b4c00e3912f | 179 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 180 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 181 | } |
sakthipriya | 0:7b4c00e3912f | 182 | |
sakthipriya | 0:7b4c00e3912f | 183 | |
sakthipriya | 0:7b4c00e3912f | 184 | //disable sleep |
sakthipriya | 0:7b4c00e3912f | 185 | void disable_sleep() |
sakthipriya | 0:7b4c00e3912f | 186 | { |
sakthipriya | 0:7b4c00e3912f | 187 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 188 | value &= ~(1 << 13); |
sakthipriya | 0:7b4c00e3912f | 189 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 190 | } |
sakthipriya | 0:7b4c00e3912f | 191 | |
sakthipriya | 0:7b4c00e3912f | 192 | //disable the hibernate of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 193 | void disable_hibernate() |
sakthipriya | 0:7b4c00e3912f | 194 | { |
sakthipriya | 0:7b4c00e3912f | 195 | write(REG_HIBRT, 0x0000); |
sakthipriya | 0:7b4c00e3912f | 196 | } |
sakthipriya | 0:7b4c00e3912f | 197 | |
sakthipriya | 0:7b4c00e3912f | 198 | |
sakthipriya | 0:7b4c00e3912f | 199 | // Enable or disable the SOC 1% change alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 200 | void socChangeAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 201 | { |
sakthipriya | 0:7b4c00e3912f | 202 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 203 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 204 | |
sakthipriya | 0:7b4c00e3912f | 205 | //Set or clear the ALSC bit |
sakthipriya | 0:7b4c00e3912f | 206 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 207 | value |= (1 << 6); |
sakthipriya | 0:7b4c00e3912f | 208 | else |
sakthipriya | 0:7b4c00e3912f | 209 | value &= ~(1 << 6); |
sakthipriya | 0:7b4c00e3912f | 210 | |
sakthipriya | 0:7b4c00e3912f | 211 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 212 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 213 | } |
sakthipriya | 0:7b4c00e3912f | 214 | |
sakthipriya | 0:7b4c00e3912f | 215 | |
sakthipriya | 0:7b4c00e3912f | 216 | void compensation(char rcomp) |
sakthipriya | 0:7b4c00e3912f | 217 | { |
sakthipriya | 0:7b4c00e3912f | 218 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 219 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 220 | |
sakthipriya | 0:7b4c00e3912f | 221 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 222 | value &= 0x00FF; |
sakthipriya | 0:7b4c00e3912f | 223 | value |= rcomp << 8; |
sakthipriya | 0:7b4c00e3912f | 224 | |
sakthipriya | 0:7b4c00e3912f | 225 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 226 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 227 | } |
sakthipriya | 0:7b4c00e3912f | 228 | |
sakthipriya | 0:7b4c00e3912f | 229 | |
sakthipriya | 0:7b4c00e3912f | 230 | void tempCompensation(float temp) |
sakthipriya | 0:7b4c00e3912f | 231 | { |
sakthipriya | 0:7b4c00e3912f | 232 | //Calculate the new RCOMP value |
sakthipriya | 0:7b4c00e3912f | 233 | char rcomp; |
sakthipriya | 0:7b4c00e3912f | 234 | if (temp > 20.0) { |
sakthipriya | 0:7b4c00e3912f | 235 | rcomp = RCOMP0 + (temp - 20.0) * -0.5; |
sakthipriya | 0:7b4c00e3912f | 236 | } else { |
sakthipriya | 0:7b4c00e3912f | 237 | rcomp = RCOMP0 + (temp - 20.0) * -5.0; |
sakthipriya | 0:7b4c00e3912f | 238 | } |
sakthipriya | 0:7b4c00e3912f | 239 | |
sakthipriya | 0:7b4c00e3912f | 240 | //Update the RCOMP value |
sakthipriya | 0:7b4c00e3912f | 241 | compensation(rcomp); |
sakthipriya | 0:7b4c00e3912f | 242 | } |
sakthipriya | 0:7b4c00e3912f | 243 | |
sakthipriya | 0:7b4c00e3912f | 244 | // Command the MAX17049 to de-assert the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 245 | void clearAlert() |
sakthipriya | 0:7b4c00e3912f | 246 | { |
sakthipriya | 0:7b4c00e3912f | 247 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 248 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 249 | |
sakthipriya | 0:7b4c00e3912f | 250 | //Clear the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 251 | value &= ~(1 << 5); |
sakthipriya | 0:7b4c00e3912f | 252 | |
sakthipriya | 0:7b4c00e3912f | 253 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 254 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 255 | } |
sakthipriya | 0:7b4c00e3912f | 256 | |
sakthipriya | 0:7b4c00e3912f | 257 | |
sakthipriya | 0:7b4c00e3912f | 258 | //Set the SOC empty alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 259 | void emptyAlertThreshold(char threshold) |
sakthipriya | 0:7b4c00e3912f | 260 | { |
sakthipriya | 0:7b4c00e3912f | 261 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 262 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 263 | |
sakthipriya | 0:7b4c00e3912f | 264 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 265 | value &= 0xFFE0; |
sakthipriya | 0:7b4c00e3912f | 266 | value |= 32 - threshold; |
sakthipriya | 0:7b4c00e3912f | 267 | |
sakthipriya | 0:7b4c00e3912f | 268 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 269 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 270 | } |
sakthipriya | 0:7b4c00e3912f | 271 | |
sakthipriya | 0:7b4c00e3912f | 272 | // Set the low and high voltage alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 273 | void vAlertMinMaxThreshold() |
sakthipriya | 0:7b4c00e3912f | 274 | { |
sakthipriya | 0:7b4c00e3912f | 275 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 276 | unsigned short value = read(REG_VALRT); |
sakthipriya | 0:7b4c00e3912f | 277 | |
sakthipriya | 0:7b4c00e3912f | 278 | //Mask off the old value |
sakthipriya | 0:7b4c00e3912f | 279 | |
sakthipriya | 0:7b4c00e3912f | 280 | value = 0x96D2; |
sakthipriya | 0:7b4c00e3912f | 281 | |
sakthipriya | 0:7b4c00e3912f | 282 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 283 | write(REG_VALRT, value); |
sakthipriya | 0:7b4c00e3912f | 284 | } |
sakthipriya | 0:7b4c00e3912f | 285 | |
sakthipriya | 0:7b4c00e3912f | 286 | |
sakthipriya | 0:7b4c00e3912f | 287 | // Set the reset voltage threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 288 | void vResetThresholdSet() |
sakthipriya | 0:7b4c00e3912f | 289 | { |
sakthipriya | 0:7b4c00e3912f | 290 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 291 | unsigned short value = read(REG_VRESET_ID); |
sakthipriya | 0:7b4c00e3912f | 292 | |
sakthipriya | 0:7b4c00e3912f | 293 | //Mask off the old //value |
sakthipriya | 0:7b4c00e3912f | 294 | value &= 0x00FF;//Dis=0 |
sakthipriya | 0:7b4c00e3912f | 295 | |
sakthipriya | 0:7b4c00e3912f | 296 | value |= 0x9400;//corresponding to 2.5 V |
sakthipriya | 0:7b4c00e3912f | 297 | |
sakthipriya | 0:7b4c00e3912f | 298 | |
sakthipriya | 0:7b4c00e3912f | 299 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 300 | write(REG_VRESET_ID, value); |
sakthipriya | 0:7b4c00e3912f | 301 | } |
sakthipriya | 0:7b4c00e3912f | 302 | |
sakthipriya | 0:7b4c00e3912f | 303 | |
sakthipriya | 0:7b4c00e3912f | 304 | // Enable or disable the voltage reset alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 305 | void vResetAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 306 | { |
sakthipriya | 0:7b4c00e3912f | 307 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 308 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 309 | |
sakthipriya | 0:7b4c00e3912f | 310 | //Set or clear the EnVR bit |
sakthipriya | 0:7b4c00e3912f | 311 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 312 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 313 | else |
sakthipriya | 0:7b4c00e3912f | 314 | value &= ~(1 << 14); |
sakthipriya | 0:7b4c00e3912f | 315 | |
sakthipriya | 0:7b4c00e3912f | 316 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 317 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 318 | } |
sakthipriya | 0:7b4c00e3912f | 319 | |
sakthipriya | 0:7b4c00e3912f | 320 | //Get the current alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 321 | //refer datasheet-status registers section to decode it. |
sakthipriya | 0:7b4c00e3912f | 322 | char alertFlags() |
sakthipriya | 0:7b4c00e3912f | 323 | { |
sakthipriya | 0:7b4c00e3912f | 324 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 325 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 326 | |
sakthipriya | 0:7b4c00e3912f | 327 | //Return only the flag bits |
sakthipriya | 0:7b4c00e3912f | 328 | return (value >> 8) & 0x3F; |
sakthipriya | 0:7b4c00e3912f | 329 | } |
sakthipriya | 0:7b4c00e3912f | 330 | |
sakthipriya | 0:7b4c00e3912f | 331 | // Clear all the alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 332 | void clearAlertFlags() |
sakthipriya | 0:7b4c00e3912f | 333 | { |
sakthipriya | 0:7b4c00e3912f | 334 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 335 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 336 | |
sakthipriya | 0:7b4c00e3912f | 337 | //Clear the specified flag bits |
sakthipriya | 0:7b4c00e3912f | 338 | value &= ~( 0x3F<< 8); |
sakthipriya | 0:7b4c00e3912f | 339 | |
sakthipriya | 0:7b4c00e3912f | 340 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 341 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 342 | } |
sakthipriya | 0:7b4c00e3912f | 343 | |
sakthipriya | 0:7b4c00e3912f | 344 | // Get the current cell voltage measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 345 | float vcell() |
sakthipriya | 0:7b4c00e3912f | 346 | { |
sakthipriya | 0:7b4c00e3912f | 347 | //Read the 16-bit raw Vcell value |
sakthipriya | 0:7b4c00e3912f | 348 | unsigned short value = read(REG_VCELL); |
sakthipriya | 0:7b4c00e3912f | 349 | |
sakthipriya | 0:7b4c00e3912f | 350 | //Return Vcell in volts |
sakthipriya | 0:7b4c00e3912f | 351 | return value * 0.000078125*2; |
sakthipriya | 0:7b4c00e3912f | 352 | } |
sakthipriya | 0:7b4c00e3912f | 353 | |
sakthipriya | 0:7b4c00e3912f | 354 | // Get the current state of charge measurement of the MAX17049 as a float |
sakthipriya | 0:7b4c00e3912f | 355 | float soc() |
sakthipriya | 0:7b4c00e3912f | 356 | { |
sakthipriya | 0:7b4c00e3912f | 357 | unsigned short value; |
sakthipriya | 0:7b4c00e3912f | 358 | char buff[2]; |
sakthipriya | 0:7b4c00e3912f | 359 | |
sakthipriya | 0:7b4c00e3912f | 360 | //Select the register |
sakthipriya | 0:7b4c00e3912f | 361 | m_I2C.write(m_ADDR, ®_SOC, 1, true); |
sakthipriya | 0:7b4c00e3912f | 362 | |
sakthipriya | 0:7b4c00e3912f | 363 | //Read the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 364 | bool ack = true; |
sakthipriya | 0:7b4c00e3912f | 365 | ack = m_I2C.read(m_ADDR, buff, 2); |
sakthipriya | 0:7b4c00e3912f | 366 | |
sakthipriya | 0:7b4c00e3912f | 367 | //Return the combined 16-bit value |
sakthipriya | 0:7b4c00e3912f | 368 | value = (buff[0] << 8) | buff[1]; |
sakthipriya | 0:7b4c00e3912f | 369 | /* |
sakthipriya | 0:7b4c00e3912f | 370 | //Read the 16-bit raw SOC value |
sakthipriya | 0:7b4c00e3912f | 371 | unsigned short value = read(REG_SOC);*/ |
sakthipriya | 0:7b4c00e3912f | 372 | |
sakthipriya | 0:7b4c00e3912f | 373 | //Return SOC in percent |
sakthipriya | 0:7b4c00e3912f | 374 | if(ack == 0) |
sakthipriya | 0:7b4c00e3912f | 375 | return value * 0.00390625; |
sakthipriya | 0:7b4c00e3912f | 376 | else |
sakthipriya | 0:7b4c00e3912f | 377 | return 200; |
sakthipriya | 0:7b4c00e3912f | 378 | } |
sakthipriya | 0:7b4c00e3912f | 379 | |
sakthipriya | 0:7b4c00e3912f | 380 | |
sakthipriya | 0:7b4c00e3912f | 381 | |
sakthipriya | 0:7b4c00e3912f | 382 | // Get the current C rate measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 383 | float crate() |
sakthipriya | 0:7b4c00e3912f | 384 | { |
sakthipriya | 0:7b4c00e3912f | 385 | //Read the 16-bit raw C/Rate value |
sakthipriya | 0:7b4c00e3912f | 386 | short value = read(REG_CRATE); |
sakthipriya | 0:7b4c00e3912f | 387 | |
sakthipriya | 0:7b4c00e3912f | 388 | //Return C/Rate in %/hr |
sakthipriya | 0:7b4c00e3912f | 389 | return value * 0.208; |
sakthipriya | 0:7b4c00e3912f | 390 | } |
sakthipriya | 0:7b4c00e3912f | 391 | |
sakthipriya | 0:7b4c00e3912f | 392 | // Determine whether or not the MAX17049 is asserting the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 393 | bool alerting() |
sakthipriya | 0:7b4c00e3912f | 394 | { |
sakthipriya | 0:7b4c00e3912f | 395 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 396 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 397 | |
sakthipriya | 0:7b4c00e3912f | 398 | //Return the status of the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 399 | if (value & (1 << 5)) |
sakthipriya | 0:7b4c00e3912f | 400 | return true; |
sakthipriya | 0:7b4c00e3912f | 401 | else |
sakthipriya | 0:7b4c00e3912f | 402 | return false; |
sakthipriya | 0:7b4c00e3912f | 403 | } |