ACS completed fully. All cases to be tested
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of ACS_Flowchart_BAE by
EPS.cpp@1:446a959e36ce, 2015-12-24 (annotated)
- Committer:
- sakthipriya
- Date:
- Thu Dec 24 20:01:31 2015 +0000
- Revision:
- 1:446a959e36ce
- Parent:
- 0:7b4c00e3912f
- Child:
- 2:c823d84b4cb0
updated
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 | 1:446a959e36ce | 106 | printf("\n\r battery gauge \n"); |
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 | 1:446a959e36ce | 146 | unsigned short read_soc(char reg , bool ack = true) |
sakthipriya | 1:446a959e36ce | 147 | { |
sakthipriya | 1:446a959e36ce | 148 | |
sakthipriya | 1:446a959e36ce | 149 | //Create a temporary buffer |
sakthipriya | 1:446a959e36ce | 150 | char buff[2]; |
sakthipriya | 1:446a959e36ce | 151 | |
sakthipriya | 1:446a959e36ce | 152 | //Select the register |
sakthipriya | 1:446a959e36ce | 153 | m_I2C.write(m_ADDR, ®, 1, true); |
sakthipriya | 1:446a959e36ce | 154 | |
sakthipriya | 1:446a959e36ce | 155 | //Read the 16-bit register |
sakthipriya | 1:446a959e36ce | 156 | ack = m_I2C.read(m_ADDR, buff, 2); |
sakthipriya | 1:446a959e36ce | 157 | |
sakthipriya | 1:446a959e36ce | 158 | //Return the combined 16-bit value |
sakthipriya | 1:446a959e36ce | 159 | return (buff[0] << 8) | buff[1]; |
sakthipriya | 1:446a959e36ce | 160 | } |
sakthipriya | 0:7b4c00e3912f | 161 | |
sakthipriya | 0:7b4c00e3912f | 162 | void write(char reg, unsigned short data) |
sakthipriya | 0:7b4c00e3912f | 163 | { |
sakthipriya | 0:7b4c00e3912f | 164 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 165 | char buff[3]; |
sakthipriya | 0:7b4c00e3912f | 166 | |
sakthipriya | 0:7b4c00e3912f | 167 | //Load the register address and 16-bit data |
sakthipriya | 0:7b4c00e3912f | 168 | buff[0] = reg; |
sakthipriya | 0:7b4c00e3912f | 169 | buff[1] = data >> 8; |
sakthipriya | 0:7b4c00e3912f | 170 | buff[2] = data; |
sakthipriya | 0:7b4c00e3912f | 171 | |
sakthipriya | 0:7b4c00e3912f | 172 | //Write the data |
sakthipriya | 0:7b4c00e3912f | 173 | m_I2C.write(m_ADDR, buff, 3); |
sakthipriya | 0:7b4c00e3912f | 174 | } |
sakthipriya | 0:7b4c00e3912f | 175 | |
sakthipriya | 0:7b4c00e3912f | 176 | |
sakthipriya | 0:7b4c00e3912f | 177 | |
sakthipriya | 0:7b4c00e3912f | 178 | // Command the MAX17049 to perform a power-on reset |
sakthipriya | 0:7b4c00e3912f | 179 | void reset() |
sakthipriya | 0:7b4c00e3912f | 180 | { |
sakthipriya | 0:7b4c00e3912f | 181 | //Write the POR command |
sakthipriya | 0:7b4c00e3912f | 182 | write(REG_CMD, 0x5400); |
sakthipriya | 0:7b4c00e3912f | 183 | } |
sakthipriya | 0:7b4c00e3912f | 184 | |
sakthipriya | 0:7b4c00e3912f | 185 | // Command the MAX17049 to perform a QuickStart |
sakthipriya | 0:7b4c00e3912f | 186 | void quickStart() |
sakthipriya | 0:7b4c00e3912f | 187 | { |
sakthipriya | 0:7b4c00e3912f | 188 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 189 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 190 | |
sakthipriya | 0:7b4c00e3912f | 191 | //Set the QuickStart bit |
sakthipriya | 0:7b4c00e3912f | 192 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 193 | |
sakthipriya | 0:7b4c00e3912f | 194 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 195 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 196 | } |
sakthipriya | 0:7b4c00e3912f | 197 | |
sakthipriya | 0:7b4c00e3912f | 198 | |
sakthipriya | 0:7b4c00e3912f | 199 | //disable sleep |
sakthipriya | 0:7b4c00e3912f | 200 | void disable_sleep() |
sakthipriya | 0:7b4c00e3912f | 201 | { |
sakthipriya | 0:7b4c00e3912f | 202 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 203 | value &= ~(1 << 13); |
sakthipriya | 0:7b4c00e3912f | 204 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 205 | } |
sakthipriya | 0:7b4c00e3912f | 206 | |
sakthipriya | 0:7b4c00e3912f | 207 | //disable the hibernate of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 208 | void disable_hibernate() |
sakthipriya | 0:7b4c00e3912f | 209 | { |
sakthipriya | 0:7b4c00e3912f | 210 | write(REG_HIBRT, 0x0000); |
sakthipriya | 0:7b4c00e3912f | 211 | } |
sakthipriya | 0:7b4c00e3912f | 212 | |
sakthipriya | 0:7b4c00e3912f | 213 | |
sakthipriya | 0:7b4c00e3912f | 214 | // Enable or disable the SOC 1% change alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 215 | void socChangeAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 216 | { |
sakthipriya | 0:7b4c00e3912f | 217 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 218 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 219 | |
sakthipriya | 0:7b4c00e3912f | 220 | //Set or clear the ALSC bit |
sakthipriya | 0:7b4c00e3912f | 221 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 222 | value |= (1 << 6); |
sakthipriya | 0:7b4c00e3912f | 223 | else |
sakthipriya | 0:7b4c00e3912f | 224 | value &= ~(1 << 6); |
sakthipriya | 0:7b4c00e3912f | 225 | |
sakthipriya | 0:7b4c00e3912f | 226 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 227 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 228 | } |
sakthipriya | 0:7b4c00e3912f | 229 | |
sakthipriya | 0:7b4c00e3912f | 230 | |
sakthipriya | 0:7b4c00e3912f | 231 | void compensation(char rcomp) |
sakthipriya | 0:7b4c00e3912f | 232 | { |
sakthipriya | 0:7b4c00e3912f | 233 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 234 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 235 | |
sakthipriya | 0:7b4c00e3912f | 236 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 237 | value &= 0x00FF; |
sakthipriya | 0:7b4c00e3912f | 238 | value |= rcomp << 8; |
sakthipriya | 0:7b4c00e3912f | 239 | |
sakthipriya | 0:7b4c00e3912f | 240 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 241 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 242 | } |
sakthipriya | 0:7b4c00e3912f | 243 | |
sakthipriya | 0:7b4c00e3912f | 244 | |
sakthipriya | 0:7b4c00e3912f | 245 | void tempCompensation(float temp) |
sakthipriya | 0:7b4c00e3912f | 246 | { |
sakthipriya | 0:7b4c00e3912f | 247 | //Calculate the new RCOMP value |
sakthipriya | 0:7b4c00e3912f | 248 | char rcomp; |
sakthipriya | 0:7b4c00e3912f | 249 | if (temp > 20.0) { |
sakthipriya | 0:7b4c00e3912f | 250 | rcomp = RCOMP0 + (temp - 20.0) * -0.5; |
sakthipriya | 0:7b4c00e3912f | 251 | } else { |
sakthipriya | 0:7b4c00e3912f | 252 | rcomp = RCOMP0 + (temp - 20.0) * -5.0; |
sakthipriya | 0:7b4c00e3912f | 253 | } |
sakthipriya | 0:7b4c00e3912f | 254 | |
sakthipriya | 0:7b4c00e3912f | 255 | //Update the RCOMP value |
sakthipriya | 0:7b4c00e3912f | 256 | compensation(rcomp); |
sakthipriya | 0:7b4c00e3912f | 257 | } |
sakthipriya | 0:7b4c00e3912f | 258 | |
sakthipriya | 0:7b4c00e3912f | 259 | // Command the MAX17049 to de-assert the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 260 | void clearAlert() |
sakthipriya | 0:7b4c00e3912f | 261 | { |
sakthipriya | 0:7b4c00e3912f | 262 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 263 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 264 | |
sakthipriya | 0:7b4c00e3912f | 265 | //Clear the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 266 | value &= ~(1 << 5); |
sakthipriya | 0:7b4c00e3912f | 267 | |
sakthipriya | 0:7b4c00e3912f | 268 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 269 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 270 | } |
sakthipriya | 0:7b4c00e3912f | 271 | |
sakthipriya | 0:7b4c00e3912f | 272 | |
sakthipriya | 0:7b4c00e3912f | 273 | //Set the SOC empty alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 274 | void emptyAlertThreshold(char threshold) |
sakthipriya | 0:7b4c00e3912f | 275 | { |
sakthipriya | 0:7b4c00e3912f | 276 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 277 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 278 | |
sakthipriya | 0:7b4c00e3912f | 279 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 280 | value &= 0xFFE0; |
sakthipriya | 0:7b4c00e3912f | 281 | value |= 32 - threshold; |
sakthipriya | 0:7b4c00e3912f | 282 | |
sakthipriya | 0:7b4c00e3912f | 283 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 284 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 285 | } |
sakthipriya | 0:7b4c00e3912f | 286 | |
sakthipriya | 0:7b4c00e3912f | 287 | // Set the low and high voltage alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 288 | void vAlertMinMaxThreshold() |
sakthipriya | 0:7b4c00e3912f | 289 | { |
sakthipriya | 0:7b4c00e3912f | 290 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 291 | unsigned short value = read(REG_VALRT); |
sakthipriya | 0:7b4c00e3912f | 292 | |
sakthipriya | 0:7b4c00e3912f | 293 | //Mask off the old value |
sakthipriya | 0:7b4c00e3912f | 294 | |
sakthipriya | 0:7b4c00e3912f | 295 | value = 0x96D2; |
sakthipriya | 0:7b4c00e3912f | 296 | |
sakthipriya | 0:7b4c00e3912f | 297 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 298 | write(REG_VALRT, value); |
sakthipriya | 0:7b4c00e3912f | 299 | } |
sakthipriya | 0:7b4c00e3912f | 300 | |
sakthipriya | 0:7b4c00e3912f | 301 | |
sakthipriya | 0:7b4c00e3912f | 302 | // Set the reset voltage threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 303 | void vResetThresholdSet() |
sakthipriya | 0:7b4c00e3912f | 304 | { |
sakthipriya | 0:7b4c00e3912f | 305 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 306 | unsigned short value = read(REG_VRESET_ID); |
sakthipriya | 0:7b4c00e3912f | 307 | |
sakthipriya | 0:7b4c00e3912f | 308 | //Mask off the old //value |
sakthipriya | 0:7b4c00e3912f | 309 | value &= 0x00FF;//Dis=0 |
sakthipriya | 0:7b4c00e3912f | 310 | |
sakthipriya | 0:7b4c00e3912f | 311 | value |= 0x9400;//corresponding to 2.5 V |
sakthipriya | 0:7b4c00e3912f | 312 | |
sakthipriya | 0:7b4c00e3912f | 313 | |
sakthipriya | 0:7b4c00e3912f | 314 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 315 | write(REG_VRESET_ID, value); |
sakthipriya | 0:7b4c00e3912f | 316 | } |
sakthipriya | 0:7b4c00e3912f | 317 | |
sakthipriya | 0:7b4c00e3912f | 318 | |
sakthipriya | 0:7b4c00e3912f | 319 | // Enable or disable the voltage reset alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 320 | void vResetAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 321 | { |
sakthipriya | 0:7b4c00e3912f | 322 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 323 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 324 | |
sakthipriya | 0:7b4c00e3912f | 325 | //Set or clear the EnVR bit |
sakthipriya | 0:7b4c00e3912f | 326 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 327 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 328 | else |
sakthipriya | 0:7b4c00e3912f | 329 | value &= ~(1 << 14); |
sakthipriya | 0:7b4c00e3912f | 330 | |
sakthipriya | 0:7b4c00e3912f | 331 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 332 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 333 | } |
sakthipriya | 0:7b4c00e3912f | 334 | |
sakthipriya | 0:7b4c00e3912f | 335 | //Get the current alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 336 | //refer datasheet-status registers section to decode it. |
sakthipriya | 0:7b4c00e3912f | 337 | char alertFlags() |
sakthipriya | 0:7b4c00e3912f | 338 | { |
sakthipriya | 0:7b4c00e3912f | 339 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 340 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 341 | |
sakthipriya | 0:7b4c00e3912f | 342 | //Return only the flag bits |
sakthipriya | 0:7b4c00e3912f | 343 | return (value >> 8) & 0x3F; |
sakthipriya | 0:7b4c00e3912f | 344 | } |
sakthipriya | 0:7b4c00e3912f | 345 | |
sakthipriya | 0:7b4c00e3912f | 346 | // Clear all the alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 347 | void clearAlertFlags() |
sakthipriya | 0:7b4c00e3912f | 348 | { |
sakthipriya | 0:7b4c00e3912f | 349 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 350 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 351 | |
sakthipriya | 0:7b4c00e3912f | 352 | //Clear the specified flag bits |
sakthipriya | 0:7b4c00e3912f | 353 | value &= ~( 0x3F<< 8); |
sakthipriya | 0:7b4c00e3912f | 354 | |
sakthipriya | 0:7b4c00e3912f | 355 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 356 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 357 | } |
sakthipriya | 0:7b4c00e3912f | 358 | |
sakthipriya | 0:7b4c00e3912f | 359 | // Get the current cell voltage measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 360 | float vcell() |
sakthipriya | 0:7b4c00e3912f | 361 | { |
sakthipriya | 1:446a959e36ce | 362 | |
sakthipriya | 0:7b4c00e3912f | 363 | //Read the 16-bit raw Vcell value |
sakthipriya | 0:7b4c00e3912f | 364 | unsigned short value = read(REG_VCELL); |
sakthipriya | 0:7b4c00e3912f | 365 | |
sakthipriya | 0:7b4c00e3912f | 366 | //Return Vcell in volts |
sakthipriya | 0:7b4c00e3912f | 367 | return value * 0.000078125*2; |
sakthipriya | 0:7b4c00e3912f | 368 | } |
sakthipriya | 0:7b4c00e3912f | 369 | |
sakthipriya | 0:7b4c00e3912f | 370 | // Get the current state of charge measurement of the MAX17049 as a float |
sakthipriya | 0:7b4c00e3912f | 371 | float soc() |
sakthipriya | 0:7b4c00e3912f | 372 | { |
sakthipriya | 1:446a959e36ce | 373 | //unsigned short value; |
sakthipriya | 0:7b4c00e3912f | 374 | char buff[2]; |
sakthipriya | 1:446a959e36ce | 375 | bool ack; |
sakthipriya | 0:7b4c00e3912f | 376 | //Read the 16-bit raw SOC value |
sakthipriya | 1:446a959e36ce | 377 | unsigned short value = read_soc(REG_SOC, ack); |
sakthipriya | 0:7b4c00e3912f | 378 | |
sakthipriya | 0:7b4c00e3912f | 379 | //Return SOC in percent |
sakthipriya | 1:446a959e36ce | 380 | // if(ack == 0) |
sakthipriya | 0:7b4c00e3912f | 381 | return value * 0.00390625; |
sakthipriya | 1:446a959e36ce | 382 | //else |
sakthipriya | 1:446a959e36ce | 383 | //return 200; |
sakthipriya | 0:7b4c00e3912f | 384 | } |
sakthipriya | 0:7b4c00e3912f | 385 | |
sakthipriya | 0:7b4c00e3912f | 386 | |
sakthipriya | 0:7b4c00e3912f | 387 | |
sakthipriya | 0:7b4c00e3912f | 388 | // Get the current C rate measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 389 | float crate() |
sakthipriya | 0:7b4c00e3912f | 390 | { |
sakthipriya | 0:7b4c00e3912f | 391 | //Read the 16-bit raw C/Rate value |
sakthipriya | 0:7b4c00e3912f | 392 | short value = read(REG_CRATE); |
sakthipriya | 0:7b4c00e3912f | 393 | |
sakthipriya | 0:7b4c00e3912f | 394 | //Return C/Rate in %/hr |
sakthipriya | 0:7b4c00e3912f | 395 | return value * 0.208; |
sakthipriya | 0:7b4c00e3912f | 396 | } |
sakthipriya | 0:7b4c00e3912f | 397 | |
sakthipriya | 0:7b4c00e3912f | 398 | // Determine whether or not the MAX17049 is asserting the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 399 | bool alerting() |
sakthipriya | 0:7b4c00e3912f | 400 | { |
sakthipriya | 0:7b4c00e3912f | 401 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 402 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 403 | |
sakthipriya | 0:7b4c00e3912f | 404 | //Return the status of the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 405 | if (value & (1 << 5)) |
sakthipriya | 0:7b4c00e3912f | 406 | return true; |
sakthipriya | 0:7b4c00e3912f | 407 | else |
sakthipriya | 0:7b4c00e3912f | 408 | return false; |
sakthipriya | 0:7b4c00e3912f | 409 | } |