![](/media/cache/group/nine_tails_kurama_by_zubair43-d4zyf5s.jpg.50x50_q85.jpg)
ACS completed fully. All cases to be tested
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of ACS_Flowchart_BAE by
EPS.cpp@2:c823d84b4cb0, 2015-12-25 (annotated)
- Committer:
- sakthipriya
- Date:
- Fri Dec 25 15:25:44 2015 +0000
- Revision:
- 2:c823d84b4cb0
- Parent:
- 1:446a959e36ce
- Child:
- 3:07e15677a75c
bg working
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 | 2:c823d84b4cb0 | 24 | SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK |
sakthipriya | 2:c823d84b4cb0 | 25 | DigitalOut ssn1(PIN19); //Slave select1 |
sakthipriya | 2:c823d84b4cb0 | 26 | DigitalOut ssn2(PIN21);//Slave select2 |
sakthipriya | 2:c823d84b4cb0 | 27 | //DigitalOut PS(PTB0); |
sakthipriya | 2:c823d84b4cb0 | 28 | //DigitalOut HS(PTB1); |
sakthipriya | 2:c823d84b4cb0 | 29 | |
sakthipriya | 0:7b4c00e3912f | 30 | |
sakthipriya | 0:7b4c00e3912f | 31 | //*********************************************************flags********************************************************// |
sakthipriya | 0:7b4c00e3912f | 32 | extern char EPS_INIT_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 33 | extern char EPS_BATTERY_GAUGE_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 34 | extern char EPS_MAIN_STATUS; |
sakthipriya | 0:7b4c00e3912f | 35 | extern char EPS_BATTERY_TEMP_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 36 | extern char EPS_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 37 | |
sakthipriya | 0:7b4c00e3912f | 38 | extern char EPS_BATTERY_HEAT_ENABLE ; |
sakthipriya | 0:7b4c00e3912f | 39 | |
sakthipriya | 0:7b4c00e3912f | 40 | //........................................... FUCTIONS.................................................// |
sakthipriya | 0:7b4c00e3912f | 41 | |
sakthipriya | 0:7b4c00e3912f | 42 | void FCTN_EPS_INIT() |
sakthipriya | 0:7b4c00e3912f | 43 | { |
sakthipriya | 0:7b4c00e3912f | 44 | printf("\n\r eps init \n"); |
sakthipriya | 0:7b4c00e3912f | 45 | EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag |
sakthipriya | 0:7b4c00e3912f | 46 | // FLAG(); |
sakthipriya | 0:7b4c00e3912f | 47 | FCTN_BATTERYGAUGE_INIT(); |
sakthipriya | 2:c823d84b4cb0 | 48 | //FCTN_BATTTEMP_INIT(); |
sakthipriya | 0:7b4c00e3912f | 49 | EN3V3A = 1; //enable dc dc converter A |
sakthipriya | 0:7b4c00e3912f | 50 | char value=alertFlags(); |
sakthipriya | 0:7b4c00e3912f | 51 | unsigned short value_u= (short int )value; |
sakthipriya | 0:7b4c00e3912f | 52 | value_u &=0x0001; |
sakthipriya | 0:7b4c00e3912f | 53 | if(value_u ==0x0001) // battery gauge not initialised |
sakthipriya | 0:7b4c00e3912f | 54 | { |
sakthipriya | 0:7b4c00e3912f | 55 | actual_data.power_mode = 1; |
sakthipriya | 0:7b4c00e3912f | 56 | EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 0:7b4c00e3912f | 57 | } |
sakthipriya | 0:7b4c00e3912f | 58 | else |
sakthipriya | 0:7b4c00e3912f | 59 | { |
sakthipriya | 0:7b4c00e3912f | 60 | actual_data.Batt_gauge_actual[1] = soc(); |
sakthipriya | 0:7b4c00e3912f | 61 | actual_data.Batt_voltage_actual = Vbatt_ang.read()*3.3; |
sakthipriya | 0:7b4c00e3912f | 62 | FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]); |
sakthipriya | 0:7b4c00e3912f | 63 | EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 0:7b4c00e3912f | 64 | } |
sakthipriya | 0:7b4c00e3912f | 65 | |
sakthipriya | 0:7b4c00e3912f | 66 | EPS_INIT_STATUS = 'c' ; //clear EPS_INIT_STATUS flag |
sakthipriya | 0:7b4c00e3912f | 67 | |
sakthipriya | 0:7b4c00e3912f | 68 | } |
sakthipriya | 0:7b4c00e3912f | 69 | |
sakthipriya | 0:7b4c00e3912f | 70 | //----------------------------------------------------Power algo code--------------------------------------------------------------------// |
sakthipriya | 0:7b4c00e3912f | 71 | void FCTN_EPS_POWERMODE(float soc) //dummy algo |
sakthipriya | 0:7b4c00e3912f | 72 | { |
sakthipriya | 0:7b4c00e3912f | 73 | if(soc >= 80) |
sakthipriya | 0:7b4c00e3912f | 74 | actual_data.power_mode = 4; |
sakthipriya | 0:7b4c00e3912f | 75 | else if(soc >= 70 & soc < 80) |
sakthipriya | 0:7b4c00e3912f | 76 | actual_data.power_mode = 3; |
sakthipriya | 0:7b4c00e3912f | 77 | else if(soc >= 60 & soc < 70) |
sakthipriya | 0:7b4c00e3912f | 78 | actual_data.power_mode = 2; |
sakthipriya | 0:7b4c00e3912f | 79 | else if(soc < 60) |
sakthipriya | 0:7b4c00e3912f | 80 | actual_data.power_mode = 1; |
sakthipriya | 0:7b4c00e3912f | 81 | } |
sakthipriya | 0:7b4c00e3912f | 82 | |
sakthipriya | 0:7b4c00e3912f | 83 | //...................................................HK...........................................// |
sakthipriya | 0:7b4c00e3912f | 84 | |
sakthipriya | 0:7b4c00e3912f | 85 | int quantiz(float start,float step,float x) |
sakthipriya | 0:7b4c00e3912f | 86 | { |
sakthipriya | 0:7b4c00e3912f | 87 | int y=(x-start)/step; |
sakthipriya | 0:7b4c00e3912f | 88 | if(y<=0)y=0; |
sakthipriya | 0:7b4c00e3912f | 89 | if(y>=255)y=255; |
sakthipriya | 0:7b4c00e3912f | 90 | return y; |
sakthipriya | 0:7b4c00e3912f | 91 | } |
sakthipriya | 0:7b4c00e3912f | 92 | |
sakthipriya | 0:7b4c00e3912f | 93 | void HK_main() |
sakthipriya | 0:7b4c00e3912f | 94 | { |
sakthipriya | 0:7b4c00e3912f | 95 | |
sakthipriya | 2:c823d84b4cb0 | 96 | |
sakthipriya | 0:7b4c00e3912f | 97 | } |
sakthipriya | 0:7b4c00e3912f | 98 | |
sakthipriya | 0:7b4c00e3912f | 99 | //............................................BATTERY GAUGE......................................// |
sakthipriya | 0:7b4c00e3912f | 100 | void FCTN_BATTERYGAUGE_INIT() |
sakthipriya | 0:7b4c00e3912f | 101 | { |
sakthipriya | 0:7b4c00e3912f | 102 | disable_sleep(); |
sakthipriya | 0:7b4c00e3912f | 103 | disable_hibernate(); |
sakthipriya | 0:7b4c00e3912f | 104 | socChangeAlertEnabled(true); //enabling alert on soc changing by 1% |
sakthipriya | 0:7b4c00e3912f | 105 | emptyAlertThreshold(32);//setting empty alert threshold to 32% soc |
sakthipriya | 0:7b4c00e3912f | 106 | vAlertMinMaxThreshold();//set min, max value of Valrt register |
sakthipriya | 0:7b4c00e3912f | 107 | vResetThresholdSet();//set threshold voltage for reset |
sakthipriya | 0:7b4c00e3912f | 108 | vResetAlertEnabled(true);//enable alert on reset for V < Vreset |
sakthipriya | 0:7b4c00e3912f | 109 | } |
sakthipriya | 0:7b4c00e3912f | 110 | |
sakthipriya | 0:7b4c00e3912f | 111 | void FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4]) |
sakthipriya | 0:7b4c00e3912f | 112 | { |
sakthipriya | 1:446a959e36ce | 113 | printf("\n\r battery gauge \n"); |
sakthipriya | 0:7b4c00e3912f | 114 | |
sakthipriya | 0:7b4c00e3912f | 115 | 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 | 116 | tempCompensation(temp); |
sakthipriya | 0:7b4c00e3912f | 117 | |
sakthipriya | 0:7b4c00e3912f | 118 | |
sakthipriya | 0:7b4c00e3912f | 119 | Battery_parameters[0]=vcell(); |
sakthipriya | 0:7b4c00e3912f | 120 | Battery_parameters[1]=soc(); |
sakthipriya | 0:7b4c00e3912f | 121 | Battery_parameters[2]=crate(); |
sakthipriya | 0:7b4c00e3912f | 122 | |
sakthipriya | 0:7b4c00e3912f | 123 | printf("\nVcell=%f",vcell()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 124 | printf("\nSOC=%f",soc()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 125 | printf("\nC_rate=%f",crate()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 126 | |
sakthipriya | 0:7b4c00e3912f | 127 | |
sakthipriya | 0:7b4c00e3912f | 128 | if (alerting()== true) //alert is on |
sakthipriya | 0:7b4c00e3912f | 129 | { |
sakthipriya | 0:7b4c00e3912f | 130 | Battery_parameters[3]=alertFlags(); |
sakthipriya | 0:7b4c00e3912f | 131 | clearAlert();//clear alert |
sakthipriya | 0:7b4c00e3912f | 132 | clearAlertFlags();//clear all alert flags |
sakthipriya | 0:7b4c00e3912f | 133 | } |
sakthipriya | 0:7b4c00e3912f | 134 | |
sakthipriya | 0:7b4c00e3912f | 135 | } |
sakthipriya | 0:7b4c00e3912f | 136 | |
sakthipriya | 0:7b4c00e3912f | 137 | unsigned short read(char reg) |
sakthipriya | 0:7b4c00e3912f | 138 | { |
sakthipriya | 0:7b4c00e3912f | 139 | |
sakthipriya | 0:7b4c00e3912f | 140 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 141 | char buff[2]; |
sakthipriya | 0:7b4c00e3912f | 142 | |
sakthipriya | 0:7b4c00e3912f | 143 | //Select the register |
sakthipriya | 0:7b4c00e3912f | 144 | m_I2C.write(m_ADDR, ®, 1, true); |
sakthipriya | 0:7b4c00e3912f | 145 | |
sakthipriya | 0:7b4c00e3912f | 146 | //Read the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 147 | m_I2C.read(m_ADDR, buff, 2); |
sakthipriya | 0:7b4c00e3912f | 148 | |
sakthipriya | 0:7b4c00e3912f | 149 | //Return the combined 16-bit value |
sakthipriya | 0:7b4c00e3912f | 150 | return (buff[0] << 8) | buff[1]; |
sakthipriya | 0:7b4c00e3912f | 151 | } |
sakthipriya | 0:7b4c00e3912f | 152 | |
sakthipriya | 1:446a959e36ce | 153 | unsigned short read_soc(char reg , bool ack = true) |
sakthipriya | 1:446a959e36ce | 154 | { |
sakthipriya | 1:446a959e36ce | 155 | |
sakthipriya | 1:446a959e36ce | 156 | //Create a temporary buffer |
sakthipriya | 1:446a959e36ce | 157 | char buff[2]; |
sakthipriya | 1:446a959e36ce | 158 | |
sakthipriya | 1:446a959e36ce | 159 | //Select the register |
sakthipriya | 1:446a959e36ce | 160 | m_I2C.write(m_ADDR, ®, 1, true); |
sakthipriya | 1:446a959e36ce | 161 | |
sakthipriya | 1:446a959e36ce | 162 | //Read the 16-bit register |
sakthipriya | 1:446a959e36ce | 163 | ack = m_I2C.read(m_ADDR, buff, 2); |
sakthipriya | 1:446a959e36ce | 164 | |
sakthipriya | 1:446a959e36ce | 165 | //Return the combined 16-bit value |
sakthipriya | 1:446a959e36ce | 166 | return (buff[0] << 8) | buff[1]; |
sakthipriya | 1:446a959e36ce | 167 | } |
sakthipriya | 0:7b4c00e3912f | 168 | |
sakthipriya | 0:7b4c00e3912f | 169 | void write(char reg, unsigned short data) |
sakthipriya | 0:7b4c00e3912f | 170 | { |
sakthipriya | 0:7b4c00e3912f | 171 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 172 | char buff[3]; |
sakthipriya | 0:7b4c00e3912f | 173 | |
sakthipriya | 0:7b4c00e3912f | 174 | //Load the register address and 16-bit data |
sakthipriya | 0:7b4c00e3912f | 175 | buff[0] = reg; |
sakthipriya | 0:7b4c00e3912f | 176 | buff[1] = data >> 8; |
sakthipriya | 0:7b4c00e3912f | 177 | buff[2] = data; |
sakthipriya | 0:7b4c00e3912f | 178 | |
sakthipriya | 0:7b4c00e3912f | 179 | //Write the data |
sakthipriya | 0:7b4c00e3912f | 180 | m_I2C.write(m_ADDR, buff, 3); |
sakthipriya | 0:7b4c00e3912f | 181 | } |
sakthipriya | 0:7b4c00e3912f | 182 | |
sakthipriya | 0:7b4c00e3912f | 183 | |
sakthipriya | 0:7b4c00e3912f | 184 | |
sakthipriya | 0:7b4c00e3912f | 185 | // Command the MAX17049 to perform a power-on reset |
sakthipriya | 0:7b4c00e3912f | 186 | void reset() |
sakthipriya | 0:7b4c00e3912f | 187 | { |
sakthipriya | 0:7b4c00e3912f | 188 | //Write the POR command |
sakthipriya | 0:7b4c00e3912f | 189 | write(REG_CMD, 0x5400); |
sakthipriya | 0:7b4c00e3912f | 190 | } |
sakthipriya | 0:7b4c00e3912f | 191 | |
sakthipriya | 0:7b4c00e3912f | 192 | // Command the MAX17049 to perform a QuickStart |
sakthipriya | 0:7b4c00e3912f | 193 | void quickStart() |
sakthipriya | 0:7b4c00e3912f | 194 | { |
sakthipriya | 0:7b4c00e3912f | 195 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 196 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 197 | |
sakthipriya | 0:7b4c00e3912f | 198 | //Set the QuickStart bit |
sakthipriya | 0:7b4c00e3912f | 199 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 200 | |
sakthipriya | 0:7b4c00e3912f | 201 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 202 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 203 | } |
sakthipriya | 0:7b4c00e3912f | 204 | |
sakthipriya | 0:7b4c00e3912f | 205 | |
sakthipriya | 0:7b4c00e3912f | 206 | //disable sleep |
sakthipriya | 0:7b4c00e3912f | 207 | void disable_sleep() |
sakthipriya | 0:7b4c00e3912f | 208 | { |
sakthipriya | 0:7b4c00e3912f | 209 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 210 | value &= ~(1 << 13); |
sakthipriya | 0:7b4c00e3912f | 211 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 212 | } |
sakthipriya | 0:7b4c00e3912f | 213 | |
sakthipriya | 0:7b4c00e3912f | 214 | //disable the hibernate of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 215 | void disable_hibernate() |
sakthipriya | 0:7b4c00e3912f | 216 | { |
sakthipriya | 0:7b4c00e3912f | 217 | write(REG_HIBRT, 0x0000); |
sakthipriya | 0:7b4c00e3912f | 218 | } |
sakthipriya | 0:7b4c00e3912f | 219 | |
sakthipriya | 0:7b4c00e3912f | 220 | |
sakthipriya | 0:7b4c00e3912f | 221 | // Enable or disable the SOC 1% change alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 222 | void socChangeAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 223 | { |
sakthipriya | 0:7b4c00e3912f | 224 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 225 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 226 | |
sakthipriya | 0:7b4c00e3912f | 227 | //Set or clear the ALSC bit |
sakthipriya | 0:7b4c00e3912f | 228 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 229 | value |= (1 << 6); |
sakthipriya | 0:7b4c00e3912f | 230 | else |
sakthipriya | 0:7b4c00e3912f | 231 | value &= ~(1 << 6); |
sakthipriya | 0:7b4c00e3912f | 232 | |
sakthipriya | 0:7b4c00e3912f | 233 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 234 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 235 | } |
sakthipriya | 0:7b4c00e3912f | 236 | |
sakthipriya | 0:7b4c00e3912f | 237 | |
sakthipriya | 0:7b4c00e3912f | 238 | void compensation(char rcomp) |
sakthipriya | 0:7b4c00e3912f | 239 | { |
sakthipriya | 0:7b4c00e3912f | 240 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 241 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 242 | |
sakthipriya | 0:7b4c00e3912f | 243 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 244 | value &= 0x00FF; |
sakthipriya | 0:7b4c00e3912f | 245 | value |= rcomp << 8; |
sakthipriya | 0:7b4c00e3912f | 246 | |
sakthipriya | 0:7b4c00e3912f | 247 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 248 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 249 | } |
sakthipriya | 0:7b4c00e3912f | 250 | |
sakthipriya | 0:7b4c00e3912f | 251 | |
sakthipriya | 0:7b4c00e3912f | 252 | void tempCompensation(float temp) |
sakthipriya | 0:7b4c00e3912f | 253 | { |
sakthipriya | 0:7b4c00e3912f | 254 | //Calculate the new RCOMP value |
sakthipriya | 0:7b4c00e3912f | 255 | char rcomp; |
sakthipriya | 0:7b4c00e3912f | 256 | if (temp > 20.0) { |
sakthipriya | 0:7b4c00e3912f | 257 | rcomp = RCOMP0 + (temp - 20.0) * -0.5; |
sakthipriya | 0:7b4c00e3912f | 258 | } else { |
sakthipriya | 0:7b4c00e3912f | 259 | rcomp = RCOMP0 + (temp - 20.0) * -5.0; |
sakthipriya | 0:7b4c00e3912f | 260 | } |
sakthipriya | 0:7b4c00e3912f | 261 | |
sakthipriya | 0:7b4c00e3912f | 262 | //Update the RCOMP value |
sakthipriya | 0:7b4c00e3912f | 263 | compensation(rcomp); |
sakthipriya | 0:7b4c00e3912f | 264 | } |
sakthipriya | 0:7b4c00e3912f | 265 | |
sakthipriya | 0:7b4c00e3912f | 266 | // Command the MAX17049 to de-assert the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 267 | void clearAlert() |
sakthipriya | 0:7b4c00e3912f | 268 | { |
sakthipriya | 0:7b4c00e3912f | 269 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 270 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 271 | |
sakthipriya | 0:7b4c00e3912f | 272 | //Clear the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 273 | value &= ~(1 << 5); |
sakthipriya | 0:7b4c00e3912f | 274 | |
sakthipriya | 0:7b4c00e3912f | 275 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 276 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 277 | } |
sakthipriya | 0:7b4c00e3912f | 278 | |
sakthipriya | 0:7b4c00e3912f | 279 | |
sakthipriya | 0:7b4c00e3912f | 280 | //Set the SOC empty alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 281 | void emptyAlertThreshold(char threshold) |
sakthipriya | 0:7b4c00e3912f | 282 | { |
sakthipriya | 0:7b4c00e3912f | 283 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 284 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 285 | |
sakthipriya | 0:7b4c00e3912f | 286 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 287 | value &= 0xFFE0; |
sakthipriya | 0:7b4c00e3912f | 288 | value |= 32 - threshold; |
sakthipriya | 0:7b4c00e3912f | 289 | |
sakthipriya | 0:7b4c00e3912f | 290 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 291 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 292 | } |
sakthipriya | 0:7b4c00e3912f | 293 | |
sakthipriya | 0:7b4c00e3912f | 294 | // Set the low and high voltage alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 295 | void vAlertMinMaxThreshold() |
sakthipriya | 0:7b4c00e3912f | 296 | { |
sakthipriya | 0:7b4c00e3912f | 297 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 298 | unsigned short value = read(REG_VALRT); |
sakthipriya | 0:7b4c00e3912f | 299 | |
sakthipriya | 0:7b4c00e3912f | 300 | //Mask off the old value |
sakthipriya | 0:7b4c00e3912f | 301 | |
sakthipriya | 0:7b4c00e3912f | 302 | value = 0x96D2; |
sakthipriya | 0:7b4c00e3912f | 303 | |
sakthipriya | 0:7b4c00e3912f | 304 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 305 | write(REG_VALRT, value); |
sakthipriya | 0:7b4c00e3912f | 306 | } |
sakthipriya | 0:7b4c00e3912f | 307 | |
sakthipriya | 0:7b4c00e3912f | 308 | |
sakthipriya | 0:7b4c00e3912f | 309 | // Set the reset voltage threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 310 | void vResetThresholdSet() |
sakthipriya | 0:7b4c00e3912f | 311 | { |
sakthipriya | 0:7b4c00e3912f | 312 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 313 | unsigned short value = read(REG_VRESET_ID); |
sakthipriya | 0:7b4c00e3912f | 314 | |
sakthipriya | 0:7b4c00e3912f | 315 | //Mask off the old //value |
sakthipriya | 0:7b4c00e3912f | 316 | value &= 0x00FF;//Dis=0 |
sakthipriya | 0:7b4c00e3912f | 317 | |
sakthipriya | 0:7b4c00e3912f | 318 | value |= 0x9400;//corresponding to 2.5 V |
sakthipriya | 0:7b4c00e3912f | 319 | |
sakthipriya | 0:7b4c00e3912f | 320 | |
sakthipriya | 0:7b4c00e3912f | 321 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 322 | write(REG_VRESET_ID, value); |
sakthipriya | 0:7b4c00e3912f | 323 | } |
sakthipriya | 0:7b4c00e3912f | 324 | |
sakthipriya | 0:7b4c00e3912f | 325 | |
sakthipriya | 0:7b4c00e3912f | 326 | // Enable or disable the voltage reset alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 327 | void vResetAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 328 | { |
sakthipriya | 0:7b4c00e3912f | 329 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 330 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 331 | |
sakthipriya | 0:7b4c00e3912f | 332 | //Set or clear the EnVR bit |
sakthipriya | 0:7b4c00e3912f | 333 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 334 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 335 | else |
sakthipriya | 0:7b4c00e3912f | 336 | value &= ~(1 << 14); |
sakthipriya | 0:7b4c00e3912f | 337 | |
sakthipriya | 0:7b4c00e3912f | 338 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 339 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 340 | } |
sakthipriya | 0:7b4c00e3912f | 341 | |
sakthipriya | 0:7b4c00e3912f | 342 | //Get the current alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 343 | //refer datasheet-status registers section to decode it. |
sakthipriya | 0:7b4c00e3912f | 344 | char alertFlags() |
sakthipriya | 0:7b4c00e3912f | 345 | { |
sakthipriya | 0:7b4c00e3912f | 346 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 347 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 348 | |
sakthipriya | 0:7b4c00e3912f | 349 | //Return only the flag bits |
sakthipriya | 0:7b4c00e3912f | 350 | return (value >> 8) & 0x3F; |
sakthipriya | 0:7b4c00e3912f | 351 | } |
sakthipriya | 0:7b4c00e3912f | 352 | |
sakthipriya | 0:7b4c00e3912f | 353 | // Clear all the alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 354 | void clearAlertFlags() |
sakthipriya | 0:7b4c00e3912f | 355 | { |
sakthipriya | 0:7b4c00e3912f | 356 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 357 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 358 | |
sakthipriya | 0:7b4c00e3912f | 359 | //Clear the specified flag bits |
sakthipriya | 0:7b4c00e3912f | 360 | value &= ~( 0x3F<< 8); |
sakthipriya | 0:7b4c00e3912f | 361 | |
sakthipriya | 0:7b4c00e3912f | 362 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 363 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 364 | } |
sakthipriya | 0:7b4c00e3912f | 365 | |
sakthipriya | 0:7b4c00e3912f | 366 | // Get the current cell voltage measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 367 | float vcell() |
sakthipriya | 0:7b4c00e3912f | 368 | { |
sakthipriya | 1:446a959e36ce | 369 | |
sakthipriya | 0:7b4c00e3912f | 370 | //Read the 16-bit raw Vcell value |
sakthipriya | 0:7b4c00e3912f | 371 | unsigned short value = read(REG_VCELL); |
sakthipriya | 0:7b4c00e3912f | 372 | |
sakthipriya | 0:7b4c00e3912f | 373 | //Return Vcell in volts |
sakthipriya | 0:7b4c00e3912f | 374 | return value * 0.000078125*2; |
sakthipriya | 0:7b4c00e3912f | 375 | } |
sakthipriya | 0:7b4c00e3912f | 376 | |
sakthipriya | 0:7b4c00e3912f | 377 | // Get the current state of charge measurement of the MAX17049 as a float |
sakthipriya | 0:7b4c00e3912f | 378 | float soc() |
sakthipriya | 0:7b4c00e3912f | 379 | { |
sakthipriya | 1:446a959e36ce | 380 | //unsigned short value; |
sakthipriya | 0:7b4c00e3912f | 381 | char buff[2]; |
sakthipriya | 1:446a959e36ce | 382 | bool ack; |
sakthipriya | 0:7b4c00e3912f | 383 | //Read the 16-bit raw SOC value |
sakthipriya | 1:446a959e36ce | 384 | unsigned short value = read_soc(REG_SOC, ack); |
sakthipriya | 0:7b4c00e3912f | 385 | |
sakthipriya | 0:7b4c00e3912f | 386 | //Return SOC in percent |
sakthipriya | 2:c823d84b4cb0 | 387 | if(ack == 0) |
sakthipriya | 0:7b4c00e3912f | 388 | return value * 0.00390625; |
sakthipriya | 2:c823d84b4cb0 | 389 | else |
sakthipriya | 2:c823d84b4cb0 | 390 | return 200; |
sakthipriya | 0:7b4c00e3912f | 391 | } |
sakthipriya | 0:7b4c00e3912f | 392 | |
sakthipriya | 0:7b4c00e3912f | 393 | |
sakthipriya | 0:7b4c00e3912f | 394 | |
sakthipriya | 0:7b4c00e3912f | 395 | // Get the current C rate measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 396 | float crate() |
sakthipriya | 0:7b4c00e3912f | 397 | { |
sakthipriya | 0:7b4c00e3912f | 398 | //Read the 16-bit raw C/Rate value |
sakthipriya | 0:7b4c00e3912f | 399 | short value = read(REG_CRATE); |
sakthipriya | 0:7b4c00e3912f | 400 | |
sakthipriya | 0:7b4c00e3912f | 401 | //Return C/Rate in %/hr |
sakthipriya | 0:7b4c00e3912f | 402 | return value * 0.208; |
sakthipriya | 0:7b4c00e3912f | 403 | } |
sakthipriya | 0:7b4c00e3912f | 404 | |
sakthipriya | 0:7b4c00e3912f | 405 | // Determine whether or not the MAX17049 is asserting the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 406 | bool alerting() |
sakthipriya | 0:7b4c00e3912f | 407 | { |
sakthipriya | 0:7b4c00e3912f | 408 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 409 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 410 | |
sakthipriya | 0:7b4c00e3912f | 411 | //Return the status of the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 412 | if (value & (1 << 5)) |
sakthipriya | 0:7b4c00e3912f | 413 | return true; |
sakthipriya | 0:7b4c00e3912f | 414 | else |
sakthipriya | 0:7b4c00e3912f | 415 | return false; |
sakthipriya | 2:c823d84b4cb0 | 416 | } |
sakthipriya | 2:c823d84b4cb0 | 417 | |
sakthipriya | 2:c823d84b4cb0 | 418 | //.............................Battery board Temp sensor........................// |
sakthipriya | 2:c823d84b4cb0 | 419 | void FCTN_BATTTEMP_INIT() |
sakthipriya | 2:c823d84b4cb0 | 420 | { |
sakthipriya | 2:c823d84b4cb0 | 421 | ssn1=1;ssn2=1; |
sakthipriya | 2:c823d84b4cb0 | 422 | //PS=0; |
sakthipriya | 2:c823d84b4cb0 | 423 | //HS=0; |
sakthipriya | 2:c823d84b4cb0 | 424 | spi_bt.format(8,3); |
sakthipriya | 2:c823d84b4cb0 | 425 | spi_bt.frequency(1000000); |
sakthipriya | 2:c823d84b4cb0 | 426 | } |
sakthipriya | 2:c823d84b4cb0 | 427 | |
sakthipriya | 2:c823d84b4cb0 | 428 | void FCTN_BATT_TEMP_SENSOR_MAIN(float temp[2]) |
sakthipriya | 2:c823d84b4cb0 | 429 | { |
sakthipriya | 2:c823d84b4cb0 | 430 | uint8_t MSB, LSB; |
sakthipriya | 2:c823d84b4cb0 | 431 | int16_t bit_data; |
sakthipriya | 2:c823d84b4cb0 | 432 | float sensitivity=0.0078125; //1 LSB = sensitivity degree celcius |
sakthipriya | 2:c823d84b4cb0 | 433 | wait_ms(320); |
sakthipriya | 2:c823d84b4cb0 | 434 | ssn1=0; |
sakthipriya | 2:c823d84b4cb0 | 435 | |
sakthipriya | 2:c823d84b4cb0 | 436 | spi_bt.write(0x80);//Reading digital data from Sensor 1 |
sakthipriya | 2:c823d84b4cb0 | 437 | LSB = spi_bt.write(0x00);//LSB first |
sakthipriya | 2:c823d84b4cb0 | 438 | wait_ms(0); |
sakthipriya | 2:c823d84b4cb0 | 439 | MSB = spi_bt.write(0x00); |
sakthipriya | 2:c823d84b4cb0 | 440 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 441 | pc_eps.printf("%d %d\n",MSB,LSB); |
sakthipriya | 2:c823d84b4cb0 | 442 | bit_data= ((uint16_t)MSB<<8)|LSB; |
sakthipriya | 2:c823d84b4cb0 | 443 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 444 | temp[0]=(float)bit_data*sensitivity;//Converting into decimal value |
sakthipriya | 2:c823d84b4cb0 | 445 | ssn1=1; |
sakthipriya | 2:c823d84b4cb0 | 446 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 447 | ssn2=0;//Reading data from sensor 2 |
sakthipriya | 2:c823d84b4cb0 | 448 | spi_bt.write(0x80); |
sakthipriya | 2:c823d84b4cb0 | 449 | LSB = spi_bt.write(0x00); |
sakthipriya | 2:c823d84b4cb0 | 450 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 451 | MSB = spi_bt.write(0x00); |
sakthipriya | 2:c823d84b4cb0 | 452 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 453 | bit_data= ((int16_t)MSB<<8)|LSB; |
sakthipriya | 2:c823d84b4cb0 | 454 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 455 | temp[1]=(float)bit_data*sensitivity; |
sakthipriya | 2:c823d84b4cb0 | 456 | ssn2=1; |
sakthipriya | 2:c823d84b4cb0 | 457 | |
sakthipriya | 2:c823d84b4cb0 | 458 | } |