ACS completed fully. All cases to be tested

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of ACS_Flowchart_BAE by Team Fox

Committer:
sakthipriya
Date:
Thu Dec 31 17:12:52 2015 +0000
Revision:
5:bb592f3185cc
Parent:
3:07e15677a75c
Child:
6:036d08b62785
final version of BAE

Who changed what in which revision?

UserRevisionLine numberNew 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 5:bb592f3185cc 6 extern uint8_t BAE_data[73];
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 5:bb592f3185cc 15
sakthipriya 0:7b4c00e3912f 16 //......................................Peripheral declarations.........................................................//
sakthipriya 0:7b4c00e3912f 17 Serial pc_eps(USBTX,USBRX);
sakthipriya 3:07e15677a75c 18
sakthipriya 0:7b4c00e3912f 19 I2C m_I2C(PIN85,PIN84);
sakthipriya 0:7b4c00e3912f 20 DigitalOut TRXY(TRXY_DR_EN); //active high
sakthipriya 0:7b4c00e3912f 21 DigitalOut TRZ(TRZ_DR_EN); //active high
sakthipriya 0:7b4c00e3912f 22 DigitalOut EN3V3A(ENBL3V3A);
sakthipriya 0:7b4c00e3912f 23 DigitalOut EN_BTRY_HT(BATT_HEAT);
sakthipriya 0:7b4c00e3912f 24 //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT);
sakthipriya 3:07e15677a75c 25 //AnalogIn Vbatt_ang(VBATT);
sakthipriya 3:07e15677a75c 26 AnalogIn Batt_voltage(PIN20); //Battery voltage
sakthipriya 3:07e15677a75c 27
sakthipriya 2:c823d84b4cb0 28 SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK
sakthipriya 2:c823d84b4cb0 29 DigitalOut ssn1(PIN19); //Slave select1
sakthipriya 2:c823d84b4cb0 30 DigitalOut ssn2(PIN21);//Slave select2
sakthipriya 2:c823d84b4cb0 31 //DigitalOut PS(PTB0);
sakthipriya 2:c823d84b4cb0 32 //DigitalOut HS(PTB1);
sakthipriya 2:c823d84b4cb0 33
sakthipriya 3:07e15677a75c 34 AnalogIn CurrentInput(PIN54); // Input from Current Multiplexer //PIN54
sakthipriya 3:07e15677a75c 35 AnalogIn VoltageInput(PIN53); // Input from Voltage Multiplexer //PIN53
sakthipriya 3:07e15677a75c 36 AnalogIn BAE_temp_sensor(PIN55); //Input from BAE temp sensor
sakthipriya 3:07e15677a75c 37
sakthipriya 3:07e15677a75c 38 DigitalOut SelectLinea3 (PIN46); // MSB of Select Lines
sakthipriya 3:07e15677a75c 39 DigitalOut SelectLinea2 (PIN45);
sakthipriya 3:07e15677a75c 40 DigitalOut SelectLinea1 (PIN44);
sakthipriya 3:07e15677a75c 41 DigitalOut SelectLinea0 (PIN43); // LSB of Select Lines
sakthipriya 3:07e15677a75c 42
sakthipriya 3:07e15677a75c 43 DigitalOut SelectLineb3 (PIN56); // MSB of Select Lines
sakthipriya 3:07e15677a75c 44 DigitalOut SelectLineb2 (PIN57);
sakthipriya 3:07e15677a75c 45 DigitalOut SelectLineb1 (PIN58);
sakthipriya 3:07e15677a75c 46 DigitalOut SelectLineb0 (PIN59); // LSB of Select Lines
sakthipriya 0:7b4c00e3912f 47
sakthipriya 0:7b4c00e3912f 48 //*********************************************************flags********************************************************//
sakthipriya 0:7b4c00e3912f 49 extern char EPS_INIT_STATUS ;
sakthipriya 0:7b4c00e3912f 50 extern char EPS_BATTERY_GAUGE_STATUS ;
sakthipriya 0:7b4c00e3912f 51 extern char EPS_MAIN_STATUS;
sakthipriya 0:7b4c00e3912f 52 extern char EPS_BATTERY_TEMP_STATUS ;
sakthipriya 0:7b4c00e3912f 53 extern char EPS_STATUS ;
sakthipriya 0:7b4c00e3912f 54
sakthipriya 0:7b4c00e3912f 55 extern char EPS_BATTERY_HEAT_ENABLE ;
sakthipriya 0:7b4c00e3912f 56
sakthipriya 0:7b4c00e3912f 57 //........................................... FUCTIONS.................................................//
sakthipriya 0:7b4c00e3912f 58
sakthipriya 0:7b4c00e3912f 59 void FCTN_EPS_INIT()
sakthipriya 0:7b4c00e3912f 60 {
sakthipriya 0:7b4c00e3912f 61 printf("\n\r eps init \n");
sakthipriya 0:7b4c00e3912f 62 EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag
sakthipriya 3:07e15677a75c 63 // FLAG();
sakthipriya 0:7b4c00e3912f 64 FCTN_BATTERYGAUGE_INIT();
sakthipriya 3:07e15677a75c 65 FCTN_BATTTEMP_INIT();
sakthipriya 0:7b4c00e3912f 66 EN3V3A = 1; //enable dc dc converter A
sakthipriya 0:7b4c00e3912f 67 char value=alertFlags();
sakthipriya 0:7b4c00e3912f 68 unsigned short value_u= (short int )value;
sakthipriya 0:7b4c00e3912f 69 value_u &=0x0001;
sakthipriya 0:7b4c00e3912f 70 if(value_u ==0x0001) // battery gauge not initialised
sakthipriya 0:7b4c00e3912f 71 {
sakthipriya 0:7b4c00e3912f 72 actual_data.power_mode = 1;
sakthipriya 0:7b4c00e3912f 73 EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS
sakthipriya 0:7b4c00e3912f 74 }
sakthipriya 0:7b4c00e3912f 75 else
sakthipriya 0:7b4c00e3912f 76 {
sakthipriya 0:7b4c00e3912f 77 actual_data.Batt_gauge_actual[1] = soc();
sakthipriya 3:07e15677a75c 78 actual_data.Batt_voltage_actual = Batt_voltage.read()*3.3;
sakthipriya 0:7b4c00e3912f 79 FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);
sakthipriya 0:7b4c00e3912f 80 EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS
sakthipriya 0:7b4c00e3912f 81 }
sakthipriya 0:7b4c00e3912f 82
sakthipriya 0:7b4c00e3912f 83 EPS_INIT_STATUS = 'c' ; //clear EPS_INIT_STATUS flag
sakthipriya 0:7b4c00e3912f 84
sakthipriya 0:7b4c00e3912f 85 }
sakthipriya 0:7b4c00e3912f 86
sakthipriya 0:7b4c00e3912f 87 //----------------------------------------------------Power algo code--------------------------------------------------------------------//
sakthipriya 0:7b4c00e3912f 88 void FCTN_EPS_POWERMODE(float soc) //dummy algo
sakthipriya 0:7b4c00e3912f 89 {
sakthipriya 0:7b4c00e3912f 90 if(soc >= 80)
sakthipriya 0:7b4c00e3912f 91 actual_data.power_mode = 4;
sakthipriya 0:7b4c00e3912f 92 else if(soc >= 70 & soc < 80)
sakthipriya 0:7b4c00e3912f 93 actual_data.power_mode = 3;
sakthipriya 0:7b4c00e3912f 94 else if(soc >= 60 & soc < 70)
sakthipriya 0:7b4c00e3912f 95 actual_data.power_mode = 2;
sakthipriya 0:7b4c00e3912f 96 else if(soc < 60)
sakthipriya 0:7b4c00e3912f 97 actual_data.power_mode = 1;
sakthipriya 0:7b4c00e3912f 98 }
sakthipriya 0:7b4c00e3912f 99
sakthipriya 0:7b4c00e3912f 100 //...................................................HK...........................................//
sakthipriya 0:7b4c00e3912f 101
sakthipriya 3:07e15677a75c 102
sakthipriya 3:07e15677a75c 103 void FCTN_HK_MAIN()
sakthipriya 3:07e15677a75c 104 {
sakthipriya 3:07e15677a75c 105 int Iteration=0;
sakthipriya 3:07e15677a75c 106
sakthipriya 3:07e15677a75c 107 SelectLinea0=0;
sakthipriya 3:07e15677a75c 108 SelectLinea1=0;
sakthipriya 3:07e15677a75c 109 SelectLinea2=0;
sakthipriya 3:07e15677a75c 110 SelectLinea3=0;
sakthipriya 3:07e15677a75c 111
sakthipriya 3:07e15677a75c 112 SelectLineb0=0;
sakthipriya 3:07e15677a75c 113 SelectLineb1=0;
sakthipriya 3:07e15677a75c 114 SelectLineb2=0;
sakthipriya 3:07e15677a75c 115 SelectLineb3=0;
sakthipriya 3:07e15677a75c 116
sakthipriya 3:07e15677a75c 117 //collecting data
sakthipriya 3:07e15677a75c 118 for(Iteration=0; Iteration<16; Iteration++){
sakthipriya 3:07e15677a75c 119
sakthipriya 3:07e15677a75c 120 actual_data.voltage_actual[Iteration]=VoltageInput.read();
sakthipriya 3:07e15677a75c 121 actual_data.current_actual[Iteration]=CurrentInput.read();
sakthipriya 3:07e15677a75c 122
sakthipriya 3:07e15677a75c 123 SelectLinea0=!(SelectLinea0);
sakthipriya 3:07e15677a75c 124 if(Iteration%2==1)
sakthipriya 3:07e15677a75c 125 SelectLinea1=!(SelectLinea1);
sakthipriya 3:07e15677a75c 126 if(Iteration%4==3)
sakthipriya 3:07e15677a75c 127 SelectLinea2=!(SelectLinea2);
sakthipriya 3:07e15677a75c 128 if(Iteration%8==7)
sakthipriya 3:07e15677a75c 129 SelectLinea3=!(SelectLinea3);
sakthipriya 3:07e15677a75c 130 int s0,s1,s2,s3;
sakthipriya 3:07e15677a75c 131 s0=SelectLineb0=SelectLinea0;
sakthipriya 3:07e15677a75c 132 s1=SelectLineb1=SelectLinea2;
sakthipriya 3:07e15677a75c 133 s2=SelectLineb2=SelectLinea2;
sakthipriya 3:07e15677a75c 134 s3=SelectLineb3=SelectLinea3;
sakthipriya 3:07e15677a75c 135 printf("\n\r %d %d %d %d", s0,s1,s2,s3);
sakthipriya 3:07e15677a75c 136
sakthipriya 3:07e15677a75c 137 }
sakthipriya 3:07e15677a75c 138 for(Iteration=0; Iteration<16; Iteration++){
sakthipriya 3:07e15677a75c 139
sakthipriya 3:07e15677a75c 140 if(Iteration==14)
sakthipriya 3:07e15677a75c 141 actual_data.voltage_actual[Iteration]= (-90.7*3.3*actual_data.voltage_actual[Iteration])+190.1543;
sakthipriya 3:07e15677a75c 142 else
sakthipriya 3:07e15677a75c 143 actual_data.voltage_actual[Iteration]= actual_data.voltage_actual[Iteration]*3.3*5.63;
sakthipriya 3:07e15677a75c 144 }
sakthipriya 3:07e15677a75c 145
sakthipriya 3:07e15677a75c 146 for(Iteration=0;Iteration<12;Iteration++){
sakthipriya 3:07e15677a75c 147 if(Iteration<8)
sakthipriya 3:07e15677a75c 148 actual_data.current_actual[Iteration]= actual_data.current_actual[Iteration]*3.3/(50*rsens);
sakthipriya 3:07e15677a75c 149 else
sakthipriya 3:07e15677a75c 150 actual_data.current_actual[Iteration]=actual_data.current_actual[Iteration]*3.3;
sakthipriya 3:07e15677a75c 151 int resistance;
sakthipriya 3:07e15677a75c 152
sakthipriya 3:07e15677a75c 153 resistance=24000*actual_data.current_actual[Iteration]/(3.3-actual_data.current_actual[Iteration]);
sakthipriya 3:07e15677a75c 154 if(actual_data.current_actual[Iteration]>1.47)
sakthipriya 3:07e15677a75c 155 {
sakthipriya 3:07e15677a75c 156 actual_data.current_actual[Iteration]=3694/log(24.032242*resistance);
sakthipriya 3:07e15677a75c 157 }
sakthipriya 3:07e15677a75c 158 else{
sakthipriya 3:07e15677a75c 159
sakthipriya 3:07e15677a75c 160 actual_data.current_actual[Iteration]=3365.4/log(7.60573*resistance);
sakthipriya 3:07e15677a75c 161 }
sakthipriya 3:07e15677a75c 162 }
sakthipriya 3:07e15677a75c 163 actual_data.BAE_temp_actual=(-90.7*3.3*actual_data.BAE_temp_actual)+190.1543;
sakthipriya 3:07e15677a75c 164
sakthipriya 3:07e15677a75c 165 actual_data.Batt_voltage_actual=Batt_voltage.read()*3.3*5.63;
sakthipriya 3:07e15677a75c 166
sakthipriya 3:07e15677a75c 167 //quantizing data
sakthipriya 3:07e15677a75c 168 for(Iteration=0; Iteration<16; Iteration++){
sakthipriya 3:07e15677a75c 169
sakthipriya 3:07e15677a75c 170 if(Iteration==14)
sakthipriya 3:07e15677a75c 171 quant_data.voltage_quant[Iteration]=quantiz(tstart,tstep,actual_data.voltage_actual[Iteration]);
sakthipriya 3:07e15677a75c 172 else
sakthipriya 3:07e15677a75c 173 quant_data.voltage_quant[Iteration]=quantiz(vstart,vstep,actual_data.voltage_actual[Iteration]);
sakthipriya 3:07e15677a75c 174
sakthipriya 3:07e15677a75c 175 }
sakthipriya 3:07e15677a75c 176 for(Iteration=0;Iteration<12;Iteration++){
sakthipriya 3:07e15677a75c 177 if(Iteration<8)
sakthipriya 3:07e15677a75c 178 quant_data.current_quant[Iteration]=quantiz(cstart,cstep,actual_data.current_actual[Iteration]);
sakthipriya 3:07e15677a75c 179 else
sakthipriya 3:07e15677a75c 180 quant_data.current_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.current_actual[Iteration]);
sakthipriya 3:07e15677a75c 181 }
sakthipriya 3:07e15677a75c 182 for(Iteration=0;Iteration<2;Iteration++){
sakthipriya 3:07e15677a75c 183
sakthipriya 3:07e15677a75c 184 quant_data.Batt_temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.Batt_temp_actual[Iteration]);
sakthipriya 3:07e15677a75c 185 }
sakthipriya 3:07e15677a75c 186
sakthipriya 3:07e15677a75c 187 quant_data.Batt_gauge_quant[0]=quantiz(vcell_start,vcell_step,actual_data.Batt_gauge_actual[0]);
sakthipriya 3:07e15677a75c 188 quant_data.Batt_gauge_quant[1]=quantiz(soc_start,soc_step,actual_data.Batt_gauge_actual[1]);
sakthipriya 3:07e15677a75c 189 quant_data.Batt_gauge_quant[2]=quantiz(crate_start,crate_step,actual_data.Batt_gauge_actual[2]);
sakthipriya 3:07e15677a75c 190 quant_data.Batt_gauge_alerts=actual_data.Batt_gauge_actual[3];
sakthipriya 3:07e15677a75c 191
sakthipriya 3:07e15677a75c 192 quant_data.BAE_temp_quant=quantiz(tstart,tstep,actual_data.BAE_temp_actual);
sakthipriya 3:07e15677a75c 193
sakthipriya 3:07e15677a75c 194 for(Iteration=0;Iteration<3;Iteration++){
sakthipriya 5:bb592f3185cc 195 quant_data.AngularSpeed_quant[Iteration]=actual_data.AngularSpeed_actual[Iteration];
sakthipriya 3:07e15677a75c 196 }
sakthipriya 3:07e15677a75c 197
sakthipriya 3:07e15677a75c 198 for(Iteration=0;Iteration<3;Iteration++){
sakthipriya 3:07e15677a75c 199 quant_data.Bvalue_quant[Iteration]=actual_data.Bvalue_actual[Iteration];
sakthipriya 3:07e15677a75c 200 }
sakthipriya 3:07e15677a75c 201
sakthipriya 3:07e15677a75c 202 quant_data.Batt_voltage_quant=quantiz(vstart,vstep,actual_data.Batt_voltage_actual);
sakthipriya 3:07e15677a75c 203
sakthipriya 3:07e15677a75c 204
sakthipriya 3:07e15677a75c 205 arch_data.Batt_1_temp=quant_data.Batt_temp_quant[0];
sakthipriya 3:07e15677a75c 206 arch_data.Batt_2_temp=quant_data.Batt_temp_quant[1];
sakthipriya 3:07e15677a75c 207 arch_data.EPS_PCB_temp=quant_data.voltage_quant[14];
sakthipriya 3:07e15677a75c 208 arch_data.Batt_SOC=quant_data.Batt_gauge_quant[1];
sakthipriya 3:07e15677a75c 209 arch_data.power_mode=actual_data.power_mode;
sakthipriya 5:bb592f3185cc 210 arch_data.faultPoll_status=actual_data.faultPoll_status;
sakthipriya 5:bb592f3185cc 211 arch_data.faultIr_status=actual_data.faultIr_status;
sakthipriya 3:07e15677a75c 212 arch_data.Batt_voltage=quant_data.Batt_voltage_quant;
sakthipriya 3:07e15677a75c 213
sakthipriya 3:07e15677a75c 214
sakthipriya 3:07e15677a75c 215 }
sakthipriya 3:07e15677a75c 216
sakthipriya 5:bb592f3185cc 217 void FCTN_APPEND_HKDATA()
sakthipriya 5:bb592f3185cc 218 {
sakthipriya 5:bb592f3185cc 219 // quantized data
sakthipriya 5:bb592f3185cc 220 for (int i=0;i<16;i++)
sakthipriya 5:bb592f3185cc 221 BAE_data[i] = quant_data.voltage_quant[i];
sakthipriya 5:bb592f3185cc 222 for (int i=16;i<28;i++)
sakthipriya 5:bb592f3185cc 223 BAE_data[i] = quant_data.current_quant[i-16];
sakthipriya 5:bb592f3185cc 224 BAE_data[28] = quant_data.Batt_temp_quant[0];
sakthipriya 5:bb592f3185cc 225 BAE_data[29] = quant_data.Batt_temp_quant[1];
sakthipriya 5:bb592f3185cc 226 BAE_data[30] = quant_data.Batt_gauge_quant[1];
sakthipriya 5:bb592f3185cc 227 BAE_data[31] = quant_data.Batt_gauge_quant[1];
sakthipriya 5:bb592f3185cc 228 BAE_data[32] = quant_data.Batt_gauge_quant[1];
sakthipriya 5:bb592f3185cc 229 FCTN_CONVERT_FLOAT(quant_data.Batt_gauge_alerts,&BAE_data[33]);
sakthipriya 5:bb592f3185cc 230 BAE_data[37] = quant_data.BAE_temp_quant;
sakthipriya 5:bb592f3185cc 231 FCTN_CONVERT_FLOAT(quant_data.AngularSpeed_quant[0],&BAE_data[38]);
sakthipriya 5:bb592f3185cc 232 FCTN_CONVERT_FLOAT(quant_data.AngularSpeed_quant[1],&BAE_data[42]);
sakthipriya 5:bb592f3185cc 233 FCTN_CONVERT_FLOAT(quant_data.AngularSpeed_quant[2],&BAE_data[46]);
sakthipriya 5:bb592f3185cc 234 FCTN_CONVERT_FLOAT(quant_data.Bvalue_quant[0],&BAE_data[50]);
sakthipriya 5:bb592f3185cc 235 FCTN_CONVERT_FLOAT(quant_data.Bvalue_quant[1],&BAE_data[54]);
sakthipriya 5:bb592f3185cc 236 FCTN_CONVERT_FLOAT(quant_data.Bvalue_quant[2],&BAE_data[58]);
sakthipriya 5:bb592f3185cc 237 BAE_data[62] = quant_data.Batt_voltage_quant;
sakthipriya 5:bb592f3185cc 238 BAE_data[63] = (uint8_t)actual_data.power_mode;
sakthipriya 5:bb592f3185cc 239 BAE_data[64] = actual_data.faultPoll_status;
sakthipriya 5:bb592f3185cc 240 BAE_data[65] = actual_data.faultIr_status;
sakthipriya 5:bb592f3185cc 241 // archived data
sakthipriya 5:bb592f3185cc 242 BAE_data[66] = arch_data.Batt_1_temp; //verify if uint8_t is right
sakthipriya 5:bb592f3185cc 243 BAE_data[67] = arch_data.Batt_2_temp;
sakthipriya 5:bb592f3185cc 244 BAE_data[68] = arch_data.EPS_PCB_temp;
sakthipriya 5:bb592f3185cc 245 BAE_data[69] = arch_data.Batt_SOC;
sakthipriya 5:bb592f3185cc 246 BAE_data[70] = (uint8_t)arch_data.power_mode;
sakthipriya 5:bb592f3185cc 247 BAE_data[71] = arch_data.faultPoll_status;
sakthipriya 5:bb592f3185cc 248 BAE_data[72] = arch_data.faultIr_status;
sakthipriya 5:bb592f3185cc 249 BAE_data[73] = arch_data.Batt_voltage;
sakthipriya 5:bb592f3185cc 250
sakthipriya 5:bb592f3185cc 251 }
sakthipriya 5:bb592f3185cc 252
sakthipriya 5:bb592f3185cc 253 uint8_t quantiz(float start,float step,float x)
sakthipriya 0:7b4c00e3912f 254 {
sakthipriya 0:7b4c00e3912f 255 int y=(x-start)/step;
sakthipriya 0:7b4c00e3912f 256 if(y<=0)y=0;
sakthipriya 0:7b4c00e3912f 257 if(y>=255)y=255;
sakthipriya 0:7b4c00e3912f 258 return y;
sakthipriya 0:7b4c00e3912f 259 }
sakthipriya 0:7b4c00e3912f 260
sakthipriya 3:07e15677a75c 261 bool firstCount=true; // goes to EPS init
sakthipriya 3:07e15677a75c 262
sakthipriya 3:07e15677a75c 263
sakthipriya 3:07e15677a75c 264 void saveMin(char x,char y){
sakthipriya 3:07e15677a75c 265 if(y<x){
sakthipriya 3:07e15677a75c 266 x=y;
sakthipriya 3:07e15677a75c 267 }
sakthipriya 3:07e15677a75c 268
sakthipriya 3:07e15677a75c 269 }
sakthipriya 3:07e15677a75c 270 void saveMax(char x,char y){
sakthipriya 3:07e15677a75c 271 if (y>x)
sakthipriya 3:07e15677a75c 272 {
sakthipriya 3:07e15677a75c 273 x=y;
sakthipriya 3:07e15677a75c 274 }
sakthipriya 0:7b4c00e3912f 275 }
sakthipriya 0:7b4c00e3912f 276
sakthipriya 3:07e15677a75c 277
sakthipriya 3:07e15677a75c 278 void minMaxHkData(){
sakthipriya 3:07e15677a75c 279 if(firstCount==true){
sakthipriya 3:07e15677a75c 280 for (int i = 0; i < 16; ++i){
sakthipriya 3:07e15677a75c 281 bae_HK_minmax.voltage_min[i] = quant_data.voltage_quant[i];
sakthipriya 3:07e15677a75c 282 bae_HK_minmax.voltage_max[i] = quant_data.voltage_quant[i];
sakthipriya 3:07e15677a75c 283 }
sakthipriya 3:07e15677a75c 284 for (int i = 0; i < 12; ++i){
sakthipriya 3:07e15677a75c 285 bae_HK_minmax.current_min[i] = quant_data.current_quant[i];
sakthipriya 3:07e15677a75c 286 bae_HK_minmax.current_max[i] = quant_data.current_quant[i];
sakthipriya 3:07e15677a75c 287 }
sakthipriya 3:07e15677a75c 288
sakthipriya 3:07e15677a75c 289 for (int i = 0; i < 2; ++i){
sakthipriya 3:07e15677a75c 290 bae_HK_minmax.Batt_temp_min[i] = quant_data.Batt_temp_quant[i];
sakthipriya 3:07e15677a75c 291 bae_HK_minmax.Batt_temp_max[i] = quant_data.Batt_temp_quant[i];
sakthipriya 3:07e15677a75c 292 }
sakthipriya 3:07e15677a75c 293 for (int i = 0; i < 3; ++i){
sakthipriya 3:07e15677a75c 294 bae_HK_minmax.Batt_gauge_min[i] = quant_data.Batt_gauge_quant[i];
sakthipriya 3:07e15677a75c 295 bae_HK_minmax.Batt_gauge_max[i] = quant_data.Batt_gauge_quant[i];
sakthipriya 3:07e15677a75c 296 }
sakthipriya 3:07e15677a75c 297 for (int i = 0; i < 3; ++i){
sakthipriya 3:07e15677a75c 298 bae_HK_minmax.AngularSpeed_min[i] = quant_data.AngularSpeed_quant[i];
sakthipriya 3:07e15677a75c 299 bae_HK_minmax.AngularSpeed_max[i] = quant_data.AngularSpeed_quant[i];
sakthipriya 3:07e15677a75c 300 }
sakthipriya 3:07e15677a75c 301 for (int i = 0; i < 3; ++i){
sakthipriya 3:07e15677a75c 302 bae_HK_minmax.Bvalue_min[i] = quant_data.Bvalue_quant[i];
sakthipriya 3:07e15677a75c 303 bae_HK_minmax.Bvalue_max[i] = quant_data.Bvalue_quant[i];
sakthipriya 3:07e15677a75c 304 }
sakthipriya 3:07e15677a75c 305 bae_HK_minmax.BAE_temp_min=quant_data.BAE_temp_quant;
sakthipriya 3:07e15677a75c 306 bae_HK_minmax.BAE_temp_max=quant_data.BAE_temp_quant;
sakthipriya 3:07e15677a75c 307 bae_HK_minmax.Batt_voltage_min=quant_data.Batt_voltage_quant;
sakthipriya 3:07e15677a75c 308 bae_HK_minmax.Batt_voltage_max=quant_data.Batt_voltage_quant;
sakthipriya 3:07e15677a75c 309
sakthipriya 3:07e15677a75c 310 }
sakthipriya 3:07e15677a75c 311 else {
sakthipriya 3:07e15677a75c 312 for (int i = 0; i < 16; ++i)
sakthipriya 3:07e15677a75c 313 {
sakthipriya 3:07e15677a75c 314 saveMin(bae_HK_minmax.voltage_min[i],quant_data.voltage_quant[i]);
sakthipriya 3:07e15677a75c 315 saveMax(bae_HK_minmax.voltage_max[i],quant_data.voltage_quant[i]);
sakthipriya 3:07e15677a75c 316 }
sakthipriya 3:07e15677a75c 317 for (int i = 0; i < 12; ++i)
sakthipriya 3:07e15677a75c 318 {
sakthipriya 3:07e15677a75c 319 saveMin(bae_HK_minmax.current_min[i],quant_data.current_quant[i]);
sakthipriya 3:07e15677a75c 320 saveMax(bae_HK_minmax.current_max[i],quant_data.current_quant[i]);
sakthipriya 3:07e15677a75c 321 }
sakthipriya 3:07e15677a75c 322
sakthipriya 3:07e15677a75c 323 for (int i = 0; i < 2; ++i)
sakthipriya 3:07e15677a75c 324 {
sakthipriya 3:07e15677a75c 325 saveMin(bae_HK_minmax.Batt_temp_min[i],quant_data.Batt_temp_quant[i]);
sakthipriya 3:07e15677a75c 326 saveMax(bae_HK_minmax.Batt_temp_max[i],quant_data.Batt_temp_quant[i]);
sakthipriya 3:07e15677a75c 327 }
sakthipriya 3:07e15677a75c 328 for (int i = 0; i < 3; ++i)
sakthipriya 3:07e15677a75c 329 {
sakthipriya 3:07e15677a75c 330 saveMin(bae_HK_minmax.Batt_gauge_min[i], quant_data.Batt_gauge_quant[i]);
sakthipriya 3:07e15677a75c 331 saveMax(bae_HK_minmax.Batt_gauge_max[i], quant_data.Batt_gauge_quant[i]);
sakthipriya 3:07e15677a75c 332 }
sakthipriya 3:07e15677a75c 333 for (int i = 0; i < 3; ++i)
sakthipriya 3:07e15677a75c 334 {
sakthipriya 3:07e15677a75c 335 saveMin(bae_HK_minmax.AngularSpeed_min[i], quant_data.AngularSpeed_quant[i]);
sakthipriya 3:07e15677a75c 336 saveMax(bae_HK_minmax.AngularSpeed_max[i], quant_data.AngularSpeed_quant[i]);
sakthipriya 3:07e15677a75c 337 }
sakthipriya 3:07e15677a75c 338 for (int i = 0; i < 3; ++i)
sakthipriya 3:07e15677a75c 339 {
sakthipriya 3:07e15677a75c 340 saveMin(bae_HK_minmax.Bvalue_min[i], quant_data.Bvalue_quant[i]);
sakthipriya 3:07e15677a75c 341 saveMax(bae_HK_minmax.Bvalue_max[i], quant_data.Bvalue_quant[i]);
sakthipriya 3:07e15677a75c 342 }
sakthipriya 3:07e15677a75c 343 saveMin(bae_HK_minmax.BAE_temp_min,quant_data.BAE_temp_quant);
sakthipriya 3:07e15677a75c 344 saveMax(bae_HK_minmax.BAE_temp_max,quant_data.BAE_temp_quant);
sakthipriya 3:07e15677a75c 345 saveMin(bae_HK_minmax.Batt_voltage_min,quant_data.Batt_voltage_quant);
sakthipriya 3:07e15677a75c 346 saveMin(bae_HK_minmax.Batt_voltage_max,quant_data.Batt_voltage_quant);
sakthipriya 3:07e15677a75c 347
sakthipriya 3:07e15677a75c 348
sakthipriya 3:07e15677a75c 349 }
sakthipriya 3:07e15677a75c 350 firstCount=false;
sakthipriya 3:07e15677a75c 351 }
sakthipriya 3:07e15677a75c 352
sakthipriya 3:07e15677a75c 353
sakthipriya 0:7b4c00e3912f 354 //............................................BATTERY GAUGE......................................//
sakthipriya 0:7b4c00e3912f 355 void FCTN_BATTERYGAUGE_INIT()
sakthipriya 0:7b4c00e3912f 356 {
sakthipriya 0:7b4c00e3912f 357 disable_sleep();
sakthipriya 0:7b4c00e3912f 358 disable_hibernate();
sakthipriya 0:7b4c00e3912f 359 socChangeAlertEnabled(true); //enabling alert on soc changing by 1%
sakthipriya 0:7b4c00e3912f 360 emptyAlertThreshold(32);//setting empty alert threshold to 32% soc
sakthipriya 0:7b4c00e3912f 361 vAlertMinMaxThreshold();//set min, max value of Valrt register
sakthipriya 0:7b4c00e3912f 362 vResetThresholdSet();//set threshold voltage for reset
sakthipriya 0:7b4c00e3912f 363 vResetAlertEnabled(true);//enable alert on reset for V < Vreset
sakthipriya 0:7b4c00e3912f 364 }
sakthipriya 0:7b4c00e3912f 365
sakthipriya 0:7b4c00e3912f 366 void FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4])
sakthipriya 0:7b4c00e3912f 367 {
sakthipriya 1:446a959e36ce 368 printf("\n\r battery gauge \n");
sakthipriya 0:7b4c00e3912f 369
sakthipriya 3:07e15677a75c 370 float temp=30; //=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 371 tempCompensation(temp);
sakthipriya 0:7b4c00e3912f 372
sakthipriya 0:7b4c00e3912f 373
sakthipriya 0:7b4c00e3912f 374 Battery_parameters[0]=vcell();
sakthipriya 0:7b4c00e3912f 375 Battery_parameters[1]=soc();
sakthipriya 0:7b4c00e3912f 376 Battery_parameters[2]=crate();
sakthipriya 0:7b4c00e3912f 377
sakthipriya 0:7b4c00e3912f 378 printf("\nVcell=%f",vcell()); //remove this for final code
sakthipriya 0:7b4c00e3912f 379 printf("\nSOC=%f",soc()); //remove this for final code
sakthipriya 0:7b4c00e3912f 380 printf("\nC_rate=%f",crate()); //remove this for final code
sakthipriya 0:7b4c00e3912f 381
sakthipriya 0:7b4c00e3912f 382
sakthipriya 0:7b4c00e3912f 383 if (alerting()== true) //alert is on
sakthipriya 0:7b4c00e3912f 384 {
sakthipriya 0:7b4c00e3912f 385 Battery_parameters[3]=alertFlags();
sakthipriya 0:7b4c00e3912f 386 clearAlert();//clear alert
sakthipriya 0:7b4c00e3912f 387 clearAlertFlags();//clear all alert flags
sakthipriya 0:7b4c00e3912f 388 }
sakthipriya 0:7b4c00e3912f 389
sakthipriya 0:7b4c00e3912f 390 }
sakthipriya 0:7b4c00e3912f 391
sakthipriya 0:7b4c00e3912f 392 unsigned short read(char reg)
sakthipriya 0:7b4c00e3912f 393 {
sakthipriya 0:7b4c00e3912f 394
sakthipriya 0:7b4c00e3912f 395 //Create a temporary buffer
sakthipriya 0:7b4c00e3912f 396 char buff[2];
sakthipriya 0:7b4c00e3912f 397
sakthipriya 0:7b4c00e3912f 398 //Select the register
sakthipriya 0:7b4c00e3912f 399 m_I2C.write(m_ADDR, &reg, 1, true);
sakthipriya 0:7b4c00e3912f 400
sakthipriya 0:7b4c00e3912f 401 //Read the 16-bit register
sakthipriya 0:7b4c00e3912f 402 m_I2C.read(m_ADDR, buff, 2);
sakthipriya 0:7b4c00e3912f 403
sakthipriya 0:7b4c00e3912f 404 //Return the combined 16-bit value
sakthipriya 0:7b4c00e3912f 405 return (buff[0] << 8) | buff[1];
sakthipriya 0:7b4c00e3912f 406 }
sakthipriya 0:7b4c00e3912f 407
sakthipriya 3:07e15677a75c 408
sakthipriya 1:446a959e36ce 409
sakthipriya 3:07e15677a75c 410
sakthipriya 0:7b4c00e3912f 411
sakthipriya 0:7b4c00e3912f 412 void write(char reg, unsigned short data)
sakthipriya 0:7b4c00e3912f 413 {
sakthipriya 0:7b4c00e3912f 414 //Create a temporary buffer
sakthipriya 0:7b4c00e3912f 415 char buff[3];
sakthipriya 0:7b4c00e3912f 416
sakthipriya 0:7b4c00e3912f 417 //Load the register address and 16-bit data
sakthipriya 0:7b4c00e3912f 418 buff[0] = reg;
sakthipriya 0:7b4c00e3912f 419 buff[1] = data >> 8;
sakthipriya 0:7b4c00e3912f 420 buff[2] = data;
sakthipriya 0:7b4c00e3912f 421
sakthipriya 0:7b4c00e3912f 422 //Write the data
sakthipriya 0:7b4c00e3912f 423 m_I2C.write(m_ADDR, buff, 3);
sakthipriya 0:7b4c00e3912f 424 }
sakthipriya 0:7b4c00e3912f 425
sakthipriya 0:7b4c00e3912f 426
sakthipriya 0:7b4c00e3912f 427
sakthipriya 0:7b4c00e3912f 428 // Command the MAX17049 to perform a power-on reset
sakthipriya 0:7b4c00e3912f 429 void reset()
sakthipriya 0:7b4c00e3912f 430 {
sakthipriya 0:7b4c00e3912f 431 //Write the POR command
sakthipriya 0:7b4c00e3912f 432 write(REG_CMD, 0x5400);
sakthipriya 0:7b4c00e3912f 433 }
sakthipriya 0:7b4c00e3912f 434
sakthipriya 0:7b4c00e3912f 435 // Command the MAX17049 to perform a QuickStart
sakthipriya 0:7b4c00e3912f 436 void quickStart()
sakthipriya 0:7b4c00e3912f 437 {
sakthipriya 0:7b4c00e3912f 438 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 439 unsigned short value = read(REG_MODE);
sakthipriya 0:7b4c00e3912f 440
sakthipriya 0:7b4c00e3912f 441 //Set the QuickStart bit
sakthipriya 0:7b4c00e3912f 442 value |= (1 << 14);
sakthipriya 0:7b4c00e3912f 443
sakthipriya 0:7b4c00e3912f 444 //Write the value back out
sakthipriya 0:7b4c00e3912f 445 write(REG_MODE, value);
sakthipriya 0:7b4c00e3912f 446 }
sakthipriya 0:7b4c00e3912f 447
sakthipriya 0:7b4c00e3912f 448
sakthipriya 0:7b4c00e3912f 449 //disable sleep
sakthipriya 0:7b4c00e3912f 450 void disable_sleep()
sakthipriya 0:7b4c00e3912f 451 {
sakthipriya 0:7b4c00e3912f 452 unsigned short value = read(REG_MODE);
sakthipriya 0:7b4c00e3912f 453 value &= ~(1 << 13);
sakthipriya 0:7b4c00e3912f 454 write(REG_MODE, value);
sakthipriya 0:7b4c00e3912f 455 }
sakthipriya 0:7b4c00e3912f 456
sakthipriya 0:7b4c00e3912f 457 //disable the hibernate of the MAX17049
sakthipriya 0:7b4c00e3912f 458 void disable_hibernate()
sakthipriya 0:7b4c00e3912f 459 {
sakthipriya 0:7b4c00e3912f 460 write(REG_HIBRT, 0x0000);
sakthipriya 0:7b4c00e3912f 461 }
sakthipriya 0:7b4c00e3912f 462
sakthipriya 0:7b4c00e3912f 463
sakthipriya 0:7b4c00e3912f 464 // Enable or disable the SOC 1% change alert on the MAX17049
sakthipriya 0:7b4c00e3912f 465 void socChangeAlertEnabled(bool enabled)
sakthipriya 0:7b4c00e3912f 466 {
sakthipriya 0:7b4c00e3912f 467 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 468 unsigned short value = read(REG_CONFIG);
sakthipriya 0:7b4c00e3912f 469
sakthipriya 0:7b4c00e3912f 470 //Set or clear the ALSC bit
sakthipriya 0:7b4c00e3912f 471 if (enabled)
sakthipriya 0:7b4c00e3912f 472 value |= (1 << 6);
sakthipriya 0:7b4c00e3912f 473 else
sakthipriya 0:7b4c00e3912f 474 value &= ~(1 << 6);
sakthipriya 0:7b4c00e3912f 475
sakthipriya 0:7b4c00e3912f 476 //Write the value back out
sakthipriya 0:7b4c00e3912f 477 write(REG_CONFIG, value);
sakthipriya 0:7b4c00e3912f 478 }
sakthipriya 0:7b4c00e3912f 479
sakthipriya 0:7b4c00e3912f 480
sakthipriya 0:7b4c00e3912f 481 void compensation(char rcomp)
sakthipriya 0:7b4c00e3912f 482 {
sakthipriya 0:7b4c00e3912f 483 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 484 unsigned short value = read(REG_CONFIG);
sakthipriya 0:7b4c00e3912f 485
sakthipriya 0:7b4c00e3912f 486 //Update the register value
sakthipriya 0:7b4c00e3912f 487 value &= 0x00FF;
sakthipriya 0:7b4c00e3912f 488 value |= rcomp << 8;
sakthipriya 0:7b4c00e3912f 489
sakthipriya 0:7b4c00e3912f 490 //Write the value back out
sakthipriya 0:7b4c00e3912f 491 write(REG_CONFIG, value);
sakthipriya 0:7b4c00e3912f 492 }
sakthipriya 0:7b4c00e3912f 493
sakthipriya 0:7b4c00e3912f 494
sakthipriya 0:7b4c00e3912f 495 void tempCompensation(float temp)
sakthipriya 0:7b4c00e3912f 496 {
sakthipriya 0:7b4c00e3912f 497 //Calculate the new RCOMP value
sakthipriya 0:7b4c00e3912f 498 char rcomp;
sakthipriya 0:7b4c00e3912f 499 if (temp > 20.0) {
sakthipriya 0:7b4c00e3912f 500 rcomp = RCOMP0 + (temp - 20.0) * -0.5;
sakthipriya 0:7b4c00e3912f 501 } else {
sakthipriya 0:7b4c00e3912f 502 rcomp = RCOMP0 + (temp - 20.0) * -5.0;
sakthipriya 0:7b4c00e3912f 503 }
sakthipriya 0:7b4c00e3912f 504
sakthipriya 0:7b4c00e3912f 505 //Update the RCOMP value
sakthipriya 0:7b4c00e3912f 506 compensation(rcomp);
sakthipriya 0:7b4c00e3912f 507 }
sakthipriya 0:7b4c00e3912f 508
sakthipriya 0:7b4c00e3912f 509 // Command the MAX17049 to de-assert the ALRT pin
sakthipriya 0:7b4c00e3912f 510 void clearAlert()
sakthipriya 0:7b4c00e3912f 511 {
sakthipriya 0:7b4c00e3912f 512 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 513 unsigned short value = read(REG_CONFIG);
sakthipriya 0:7b4c00e3912f 514
sakthipriya 0:7b4c00e3912f 515 //Clear the ALRT bit
sakthipriya 0:7b4c00e3912f 516 value &= ~(1 << 5);
sakthipriya 0:7b4c00e3912f 517
sakthipriya 0:7b4c00e3912f 518 //Write the value back out
sakthipriya 0:7b4c00e3912f 519 write(REG_CONFIG, value);
sakthipriya 0:7b4c00e3912f 520 }
sakthipriya 0:7b4c00e3912f 521
sakthipriya 0:7b4c00e3912f 522
sakthipriya 0:7b4c00e3912f 523 //Set the SOC empty alert threshold of the MAX17049
sakthipriya 0:7b4c00e3912f 524 void emptyAlertThreshold(char threshold)
sakthipriya 0:7b4c00e3912f 525 {
sakthipriya 0:7b4c00e3912f 526 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 527 unsigned short value = read(REG_CONFIG);
sakthipriya 0:7b4c00e3912f 528
sakthipriya 0:7b4c00e3912f 529 //Update the register value
sakthipriya 0:7b4c00e3912f 530 value &= 0xFFE0;
sakthipriya 0:7b4c00e3912f 531 value |= 32 - threshold;
sakthipriya 0:7b4c00e3912f 532
sakthipriya 0:7b4c00e3912f 533 //Write the 16-bit register
sakthipriya 0:7b4c00e3912f 534 write(REG_CONFIG, value);
sakthipriya 0:7b4c00e3912f 535 }
sakthipriya 0:7b4c00e3912f 536
sakthipriya 0:7b4c00e3912f 537 // Set the low and high voltage alert threshold of the MAX17049
sakthipriya 0:7b4c00e3912f 538 void vAlertMinMaxThreshold()
sakthipriya 0:7b4c00e3912f 539 {
sakthipriya 0:7b4c00e3912f 540 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 541 unsigned short value = read(REG_VALRT);
sakthipriya 0:7b4c00e3912f 542
sakthipriya 0:7b4c00e3912f 543 //Mask off the old value
sakthipriya 0:7b4c00e3912f 544
sakthipriya 0:7b4c00e3912f 545 value = 0x96D2;
sakthipriya 0:7b4c00e3912f 546
sakthipriya 0:7b4c00e3912f 547 //Write the 16-bit register
sakthipriya 0:7b4c00e3912f 548 write(REG_VALRT, value);
sakthipriya 0:7b4c00e3912f 549 }
sakthipriya 0:7b4c00e3912f 550
sakthipriya 0:7b4c00e3912f 551
sakthipriya 0:7b4c00e3912f 552 // Set the reset voltage threshold of the MAX17049
sakthipriya 0:7b4c00e3912f 553 void vResetThresholdSet()
sakthipriya 0:7b4c00e3912f 554 {
sakthipriya 0:7b4c00e3912f 555 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 556 unsigned short value = read(REG_VRESET_ID);
sakthipriya 0:7b4c00e3912f 557
sakthipriya 0:7b4c00e3912f 558 //Mask off the old //value
sakthipriya 0:7b4c00e3912f 559 value &= 0x00FF;//Dis=0
sakthipriya 0:7b4c00e3912f 560
sakthipriya 0:7b4c00e3912f 561 value |= 0x9400;//corresponding to 2.5 V
sakthipriya 0:7b4c00e3912f 562
sakthipriya 0:7b4c00e3912f 563
sakthipriya 0:7b4c00e3912f 564 //Write the 16-bit register
sakthipriya 0:7b4c00e3912f 565 write(REG_VRESET_ID, value);
sakthipriya 0:7b4c00e3912f 566 }
sakthipriya 0:7b4c00e3912f 567
sakthipriya 0:7b4c00e3912f 568
sakthipriya 0:7b4c00e3912f 569 // Enable or disable the voltage reset alert on the MAX17049
sakthipriya 0:7b4c00e3912f 570 void vResetAlertEnabled(bool enabled)
sakthipriya 0:7b4c00e3912f 571 {
sakthipriya 0:7b4c00e3912f 572 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 573 unsigned short value = read(REG_STATUS);
sakthipriya 0:7b4c00e3912f 574
sakthipriya 0:7b4c00e3912f 575 //Set or clear the EnVR bit
sakthipriya 0:7b4c00e3912f 576 if (enabled)
sakthipriya 0:7b4c00e3912f 577 value |= (1 << 14);
sakthipriya 0:7b4c00e3912f 578 else
sakthipriya 0:7b4c00e3912f 579 value &= ~(1 << 14);
sakthipriya 0:7b4c00e3912f 580
sakthipriya 0:7b4c00e3912f 581 //Write the value back out
sakthipriya 0:7b4c00e3912f 582 write(REG_STATUS, value);
sakthipriya 0:7b4c00e3912f 583 }
sakthipriya 0:7b4c00e3912f 584
sakthipriya 0:7b4c00e3912f 585 //Get the current alert flags on the MAX17049
sakthipriya 0:7b4c00e3912f 586 //refer datasheet-status registers section to decode it.
sakthipriya 0:7b4c00e3912f 587 char alertFlags()
sakthipriya 0:7b4c00e3912f 588 {
sakthipriya 0:7b4c00e3912f 589 //Read the 16-bit register value
sakthipriya 0:7b4c00e3912f 590 unsigned short value = read(REG_STATUS);
sakthipriya 0:7b4c00e3912f 591
sakthipriya 0:7b4c00e3912f 592 //Return only the flag bits
sakthipriya 0:7b4c00e3912f 593 return (value >> 8) & 0x3F;
sakthipriya 0:7b4c00e3912f 594 }
sakthipriya 0:7b4c00e3912f 595
sakthipriya 0:7b4c00e3912f 596 // Clear all the alert flags on the MAX17049
sakthipriya 0:7b4c00e3912f 597 void clearAlertFlags()
sakthipriya 0:7b4c00e3912f 598 {
sakthipriya 0:7b4c00e3912f 599 //Read the current 16-bit register value
sakthipriya 0:7b4c00e3912f 600 unsigned short value = read(REG_STATUS);
sakthipriya 0:7b4c00e3912f 601
sakthipriya 0:7b4c00e3912f 602 //Clear the specified flag bits
sakthipriya 0:7b4c00e3912f 603 value &= ~( 0x3F<< 8);
sakthipriya 0:7b4c00e3912f 604
sakthipriya 0:7b4c00e3912f 605 //Write the value back out
sakthipriya 0:7b4c00e3912f 606 write(REG_STATUS, value);
sakthipriya 0:7b4c00e3912f 607 }
sakthipriya 0:7b4c00e3912f 608
sakthipriya 0:7b4c00e3912f 609 // Get the current cell voltage measurement of the MAX17049
sakthipriya 0:7b4c00e3912f 610 float vcell()
sakthipriya 0:7b4c00e3912f 611 {
sakthipriya 1:446a959e36ce 612
sakthipriya 0:7b4c00e3912f 613 //Read the 16-bit raw Vcell value
sakthipriya 0:7b4c00e3912f 614 unsigned short value = read(REG_VCELL);
sakthipriya 0:7b4c00e3912f 615
sakthipriya 0:7b4c00e3912f 616 //Return Vcell in volts
sakthipriya 0:7b4c00e3912f 617 return value * 0.000078125*2;
sakthipriya 0:7b4c00e3912f 618 }
sakthipriya 0:7b4c00e3912f 619
sakthipriya 0:7b4c00e3912f 620 // Get the current state of charge measurement of the MAX17049 as a float
sakthipriya 0:7b4c00e3912f 621 float soc()
sakthipriya 0:7b4c00e3912f 622 {
sakthipriya 3:07e15677a75c 623
sakthipriya 3:07e15677a75c 624 //Create a temporary buffer
sakthipriya 0:7b4c00e3912f 625 char buff[2];
sakthipriya 3:07e15677a75c 626 int ack = 1;
sakthipriya 3:07e15677a75c 627 //Select the register
sakthipriya 3:07e15677a75c 628 char reg = REG_SOC; // cannot pass the hash defined values directly
sakthipriya 3:07e15677a75c 629 m_I2C.write(m_ADDR, &reg, 1, true);
sakthipriya 3:07e15677a75c 630
sakthipriya 3:07e15677a75c 631
sakthipriya 3:07e15677a75c 632 //Read the 16-bit register
sakthipriya 3:07e15677a75c 633
sakthipriya 3:07e15677a75c 634 ack = m_I2C.read(m_ADDR, buff, 2);
sakthipriya 3:07e15677a75c 635
sakthipriya 3:07e15677a75c 636 printf("\n\r acknow %d", ack);
sakthipriya 0:7b4c00e3912f 637
sakthipriya 0:7b4c00e3912f 638 //Return SOC in percent
sakthipriya 2:c823d84b4cb0 639 if(ack == 0)
sakthipriya 3:07e15677a75c 640 return ((buff[0] << 8) | buff[1]) * 0.00390625;
sakthipriya 2:c823d84b4cb0 641 else
sakthipriya 2:c823d84b4cb0 642 return 200;
sakthipriya 0:7b4c00e3912f 643 }
sakthipriya 0:7b4c00e3912f 644
sakthipriya 0:7b4c00e3912f 645
sakthipriya 0:7b4c00e3912f 646
sakthipriya 0:7b4c00e3912f 647 // Get the current C rate measurement of the MAX17049
sakthipriya 0:7b4c00e3912f 648 float crate()
sakthipriya 0:7b4c00e3912f 649 {
sakthipriya 0:7b4c00e3912f 650 //Read the 16-bit raw C/Rate value
sakthipriya 0:7b4c00e3912f 651 short value = read(REG_CRATE);
sakthipriya 0:7b4c00e3912f 652
sakthipriya 0:7b4c00e3912f 653 //Return C/Rate in %/hr
sakthipriya 0:7b4c00e3912f 654 return value * 0.208;
sakthipriya 0:7b4c00e3912f 655 }
sakthipriya 0:7b4c00e3912f 656
sakthipriya 0:7b4c00e3912f 657 // Determine whether or not the MAX17049 is asserting the ALRT pin
sakthipriya 0:7b4c00e3912f 658 bool alerting()
sakthipriya 0:7b4c00e3912f 659 {
sakthipriya 0:7b4c00e3912f 660 //Read the 16-bit register value
sakthipriya 0:7b4c00e3912f 661 unsigned short value = read(REG_CONFIG);
sakthipriya 0:7b4c00e3912f 662
sakthipriya 0:7b4c00e3912f 663 //Return the status of the ALRT bit
sakthipriya 0:7b4c00e3912f 664 if (value & (1 << 5))
sakthipriya 0:7b4c00e3912f 665 return true;
sakthipriya 0:7b4c00e3912f 666 else
sakthipriya 0:7b4c00e3912f 667 return false;
sakthipriya 2:c823d84b4cb0 668 }
sakthipriya 2:c823d84b4cb0 669
sakthipriya 2:c823d84b4cb0 670 //.............................Battery board Temp sensor........................//
sakthipriya 2:c823d84b4cb0 671 void FCTN_BATTTEMP_INIT()
sakthipriya 2:c823d84b4cb0 672 {
sakthipriya 2:c823d84b4cb0 673 ssn1=1;ssn2=1;
sakthipriya 2:c823d84b4cb0 674 //PS=0;
sakthipriya 2:c823d84b4cb0 675 //HS=0;
sakthipriya 2:c823d84b4cb0 676 spi_bt.format(8,3);
sakthipriya 2:c823d84b4cb0 677 spi_bt.frequency(1000000);
sakthipriya 2:c823d84b4cb0 678 }
sakthipriya 2:c823d84b4cb0 679
sakthipriya 2:c823d84b4cb0 680 void FCTN_BATT_TEMP_SENSOR_MAIN(float temp[2])
sakthipriya 2:c823d84b4cb0 681 {
sakthipriya 2:c823d84b4cb0 682 uint8_t MSB, LSB;
sakthipriya 2:c823d84b4cb0 683 int16_t bit_data;
sakthipriya 2:c823d84b4cb0 684 float sensitivity=0.0078125; //1 LSB = sensitivity degree celcius
sakthipriya 2:c823d84b4cb0 685 wait_ms(320);
sakthipriya 2:c823d84b4cb0 686 ssn1=0;
sakthipriya 2:c823d84b4cb0 687
sakthipriya 2:c823d84b4cb0 688 spi_bt.write(0x80);//Reading digital data from Sensor 1
sakthipriya 2:c823d84b4cb0 689 LSB = spi_bt.write(0x00);//LSB first
sakthipriya 2:c823d84b4cb0 690 wait_ms(0);
sakthipriya 2:c823d84b4cb0 691 MSB = spi_bt.write(0x00);
sakthipriya 2:c823d84b4cb0 692 wait_ms(10);
sakthipriya 2:c823d84b4cb0 693 pc_eps.printf("%d %d\n",MSB,LSB);
sakthipriya 2:c823d84b4cb0 694 bit_data= ((uint16_t)MSB<<8)|LSB;
sakthipriya 2:c823d84b4cb0 695 wait_ms(10);
sakthipriya 2:c823d84b4cb0 696 temp[0]=(float)bit_data*sensitivity;//Converting into decimal value
sakthipriya 2:c823d84b4cb0 697 ssn1=1;
sakthipriya 2:c823d84b4cb0 698 wait_ms(10);
sakthipriya 2:c823d84b4cb0 699 ssn2=0;//Reading data from sensor 2
sakthipriya 2:c823d84b4cb0 700 spi_bt.write(0x80);
sakthipriya 2:c823d84b4cb0 701 LSB = spi_bt.write(0x00);
sakthipriya 2:c823d84b4cb0 702 wait_ms(10);
sakthipriya 2:c823d84b4cb0 703 MSB = spi_bt.write(0x00);
sakthipriya 2:c823d84b4cb0 704 wait_ms(10);
sakthipriya 2:c823d84b4cb0 705 bit_data= ((int16_t)MSB<<8)|LSB;
sakthipriya 2:c823d84b4cb0 706 wait_ms(10);
sakthipriya 2:c823d84b4cb0 707 temp[1]=(float)bit_data*sensitivity;
sakthipriya 2:c823d84b4cb0 708 ssn2=1;
sakthipriya 2:c823d84b4cb0 709
sakthipriya 2:c823d84b4cb0 710 }