Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of QM_BAE_review_1 by
EPS.cpp
00001 #include "EPS.h" 00002 #include "pin_config.h" 00003 #include "iostream" 00004 /***********************************************global variable declaration***************************************************************/ 00005 extern uint32_t BAE_STATUS; 00006 extern uint32_t BAE_ENABLE; 00007 extern uint8_t BAE_data[74]; 00008 extern char BAE_chardata[74]; 00009 00010 //m_I2C.frequency(10000) 00011 const char RCOMP0= 0x97;// don't know what it is now 00012 BAE_HK_actual actual_data; 00013 BAE_HK_quant quant_data; 00014 BAE_HK_min_max bae_HK_minmax; 00015 BAE_HK_arch arch_data; 00016 00017 00018 //......................................Peripheral declarations.........................................................// 00019 Serial pc_eps(USBTX,USBRX); 00020 00021 I2C m_I2C(PIN85,PIN84); 00022 DigitalOut TRXY(TRXY_DR_EN); //active high 00023 DigitalOut TRZ(TRZ_DR_EN); //active high 00024 DigitalOut EN3V3A(ENBL3V3A); 00025 DigitalOut EN_BTRY_HT(BATT_HEAT); 00026 //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT); 00027 //AnalogIn Vbatt_ang(VBATT); 00028 AnalogIn Batt_voltage(PIN20); //Battery voltage 00029 00030 SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK // battery temp something 3 00031 DigitalOut ssn1(PIN19); //Slave select1 // low line master talks 00032 DigitalOut ssn2(PIN21);//Slave select2 00033 //DigitalOut PS(PTB0); 00034 //DigitalOut HS(PTB1); 00035 00036 AnalogIn CurrentInput(PIN54); // Input from Current Multiplexer //PIN54 00037 AnalogIn VoltageInput(PIN53); // Input from Voltage Multiplexer //PIN53 00038 AnalogIn BAE_temp_sensor(PIN55); //Input from BAE temp sensor 00039 00040 /*mux for reading value one by one*/ 00041 DigitalOut SelectLinea3 (PIN46); // MSB of Select Lines 00042 DigitalOut SelectLinea2 (PIN45); 00043 DigitalOut SelectLinea1 (PIN44); 00044 DigitalOut SelectLinea0 (PIN43); // LSB of Select Lines 00045 00046 DigitalOut SelectLineb3 (PIN56); // MSB of Select Lines 00047 DigitalOut SelectLineb2 (PIN57); 00048 DigitalOut SelectLineb1 (PIN58); 00049 DigitalOut SelectLineb0 (PIN59); // LSB of Select Lines 00050 00051 //*********************************************************flags********************************************************// 00052 extern char EPS_INIT_STATUS ; 00053 extern char EPS_BATTERY_GAUGE_STATUS ; 00054 extern char EPS_MAIN_STATUS; 00055 extern char EPS_BATTERY_TEMP_STATUS ; 00056 extern char EPS_STATUS ; 00057 00058 extern char EPS_BATTERY_HEAT_ENABLE ; 00059 00060 //........................................... FUCTIONS.................................................// 00061 00062 void FCTN_EPS_INIT() 00063 { 00064 printf("\n\r eps init \n"); 00065 EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag 00066 // FLAG(); 00067 FCTN_BATTERYGAUGE_INIT(); 00068 FCTN_BATTTEMP_INIT(); 00069 EN3V3A = 1; //enable dc dc converter A 00070 char value=alertFlags(); // initialization part of battery gauge 00071 unsigned short value_u= (short int )value; 00072 value_u &=0x0001; 00073 if(value_u ==0x0001) // battery gauge not initialised 00074 { 00075 actual_data.power_mode = 1; 00076 EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS 00077 } 00078 else 00079 { 00080 actual_data.Batt_gauge_actual[1] = soc(); 00081 actual_data.Batt_voltage_actual = Batt_voltage.read()*3.3; //1 corresponds to 3.3 scaling factor 00082 FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]); 00083 EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS 00084 } 00085 00086 EPS_INIT_STATUS = 'c' ; //clear EPS_INIT_STATUS flag 00087 00088 } 00089 00090 //----------------------------------------------------Power algo code--------------------------------------------------------------------// 00091 /*update the power modes*/ 00092 00093 void FCTN_EPS_POWERMODE(float soc) //dummy algo 00094 { 00095 if(soc >= 80) 00096 actual_data.power_mode = 4; 00097 else if(soc >= 70 & soc < 80) 00098 actual_data.power_mode = 3; 00099 else if(soc >= 60 & soc < 70) 00100 actual_data.power_mode = 2; 00101 else if(soc < 60) 00102 actual_data.power_mode = 1; 00103 } 00104 00105 //...................................................HK...........................................// 00106 /*reading values*/ 00107 00108 void FCTN_HK_MAIN() 00109 { 00110 int Iteration=0; 00111 00112 SelectLinea0=0; 00113 SelectLinea1=0; 00114 SelectLinea2=0; 00115 SelectLinea3=0; 00116 00117 SelectLineb0=0; 00118 SelectLineb1=0; 00119 SelectLineb2=0; 00120 SelectLineb3=0; 00121 00122 //collecting data 00123 for(Iteration=0; Iteration<16; Iteration++){ 00124 00125 actual_data.voltage_actual[Iteration]=VoltageInput.read(); 00126 actual_data.current_actual[Iteration]=CurrentInput.read(); 00127 00128 SelectLinea0=!(SelectLinea0); 00129 if(Iteration%2==1) 00130 SelectLinea1=!(SelectLinea1); 00131 if(Iteration%4==3) 00132 SelectLinea2=!(SelectLinea2); 00133 if(Iteration%8==7) 00134 SelectLinea3=!(SelectLinea3); 00135 int s0,s1,s2,s3; 00136 s0=SelectLineb0=SelectLinea0; 00137 s1=SelectLineb1=SelectLinea2; 00138 s2=SelectLineb2=SelectLinea2; 00139 s3=SelectLineb3=SelectLinea3; 00140 printf("\n\r %d %d %d %d", s0,s1,s2,s3); 00141 00142 } 00143 for(Iteration=0; Iteration<16; Iteration++){ 00144 00145 if(Iteration==14) 00146 actual_data.voltage_actual[Iteration]= (-90.7*3.3*actual_data.voltage_actual[Iteration])+190.1543; 00147 else 00148 actual_data.voltage_actual[Iteration]= actual_data.voltage_actual[Iteration]*3.3*5.63; 00149 } 00150 00151 for(Iteration=0;Iteration<12;Iteration++){ 00152 if(Iteration<8) 00153 actual_data.current_actual[Iteration]= actual_data.current_actual[Iteration]*3.3/(50*rsens); 00154 else 00155 actual_data.current_actual[Iteration]=actual_data.current_actual[Iteration]*3.3; 00156 int resistance; 00157 00158 resistance=24000*actual_data.current_actual[Iteration]/(3.3-actual_data.current_actual[Iteration]); 00159 if(actual_data.current_actual[Iteration]>1.47) 00160 { 00161 actual_data.current_actual[Iteration]=3694/log(24.032242*resistance); 00162 } 00163 else{ 00164 00165 actual_data.current_actual[Iteration]=3365.4/log(7.60573*resistance); 00166 } 00167 } 00168 actual_data.BAE_temp_actual=(-90.7*3.3*actual_data.BAE_temp_actual)+190.1543; 00169 00170 actual_data.Batt_voltage_actual=Batt_voltage.read()*3.3*5.63; 00171 00172 //quantizing data 00173 for(Iteration=0; Iteration<16; Iteration++){ 00174 00175 if(Iteration==14) 00176 quant_data.voltage_quant[Iteration]=quantiz(tstart,tstep,actual_data.voltage_actual[Iteration]); 00177 else 00178 quant_data.voltage_quant[Iteration]=quantiz(vstart,vstep,actual_data.voltage_actual[Iteration]); 00179 00180 } 00181 for(Iteration=0;Iteration<12;Iteration++){ 00182 if(Iteration<8) 00183 quant_data.current_quant[Iteration]=quantiz(cstart,cstep,actual_data.current_actual[Iteration]); 00184 else 00185 quant_data.current_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.current_actual[Iteration]); 00186 } 00187 for(Iteration=0;Iteration<2;Iteration++){ 00188 00189 quant_data.Batt_temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.Batt_temp_actual[Iteration]); 00190 } 00191 00192 quant_data.Batt_gauge_quant[0]=quantiz(vcell_start,vcell_step,actual_data.Batt_gauge_actual[0]); 00193 quant_data.Batt_gauge_quant[1]=quantiz(soc_start,soc_step,actual_data.Batt_gauge_actual[1]); 00194 quant_data.Batt_gauge_quant[2]=quantiz(crate_start,crate_step,actual_data.Batt_gauge_actual[2]); 00195 quant_data.Batt_gauge_alerts=actual_data.Batt_gauge_actual[3]; 00196 00197 quant_data.BAE_temp_quant=quantiz(tstart,tstep,actual_data.BAE_temp_actual); 00198 00199 for(Iteration=0;Iteration<3;Iteration++){ 00200 quant_data.AngularSpeed_quant[Iteration]=actual_data.AngularSpeed_actual[Iteration]; 00201 printf("\n\r w value %f",quant_data.AngularSpeed_quant[Iteration]); 00202 } 00203 00204 for(Iteration=0;Iteration<3;Iteration++){ 00205 quant_data.Bvalue_quant[Iteration]=actual_data.Bvalue_actual[Iteration]; 00206 printf("\n\r b value %f",quant_data.Bvalue_quant[Iteration]); 00207 } 00208 00209 quant_data.Batt_voltage_quant=quantiz(vstart,vstep,actual_data.Batt_voltage_actual); 00210 00211 00212 arch_data.Batt_1_temp=quant_data.Batt_temp_quant[0]; 00213 arch_data.Batt_2_temp=quant_data.Batt_temp_quant[1]; 00214 arch_data.EPS_PCB_temp=quant_data.voltage_quant[14]; 00215 arch_data.Batt_SOC=quant_data.Batt_gauge_quant[1]; 00216 arch_data.power_mode=actual_data.power_mode; 00217 arch_data.faultPoll_status=actual_data.faultPoll_status; 00218 arch_data.faultIr_status=actual_data.faultIr_status; 00219 arch_data.Batt_voltage=quant_data.Batt_voltage_quant; 00220 printf("\n\r in hk"); 00221 00222 } 00223 00224 void FCTN_APPEND_HKDATA() 00225 { 00226 // quantized data 00227 for (int i=0;i<16;i++) 00228 BAE_data[i] = quant_data.voltage_quant[i]; 00229 for (int i=16;i<28;i++) 00230 BAE_data[i] = quant_data.current_quant[i-16]; 00231 BAE_data[28] = quant_data.Batt_temp_quant[0]; 00232 BAE_data[29] = quant_data.Batt_temp_quant[1]; 00233 BAE_data[30] = quant_data.Batt_gauge_quant[1]; 00234 BAE_data[31] = quant_data.Batt_gauge_quant[1]; 00235 BAE_data[32] = quant_data.Batt_gauge_quant[1]; 00236 FCTN_CONVERT_FLOAT(quant_data.Batt_gauge_alerts,&BAE_data[33]); 00237 BAE_data[37] = quant_data.BAE_temp_quant; 00238 FCTN_CONVERT_FLOAT(quant_data.AngularSpeed_quant[0],&BAE_data[38]); 00239 //printf("\n\r outside %d %d %d %d", BAE_data[38],BAE_data[39],BAE_data[40],BAE_data[41]); 00240 //std:: cout << "\n\r uint8 outside " << BAE_data[38] << '\t' << BAE_data[39] << '\t' << BAE_data[40] << '\t' << BAE_data[41] <<std::endl; 00241 FCTN_CONVERT_FLOAT(quant_data.AngularSpeed_quant[1],&BAE_data[42]); 00242 //std:: cout << "\n\r uint8 outside " << BAE_data[42] << '\t' << BAE_data[43] << '\t' << BAE_data[44] << '\t' << BAE_data[45] <<std::endl; 00243 // printf("\n\r outside %d %d %d %d", BAE_data[42],BAE_data[43],BAE_data[44],BAE_data[45]); 00244 FCTN_CONVERT_FLOAT(quant_data.AngularSpeed_quant[2],&BAE_data[46]); 00245 //printf("\n\r outside %d %d %d %d", BAE_data[46],BAE_data[47],BAE_data[48],BAE_data[49]); 00246 //std:: cout << "\n\r uint8 outside " << BAE_data[46] << '\t' << BAE_data[47] << '\t' << BAE_data[48] << '\t' << BAE_data[49] <<std::endl; 00247 FCTN_CONVERT_FLOAT(quant_data.Bvalue_quant[0],&BAE_data[50]); 00248 FCTN_CONVERT_FLOAT(quant_data.Bvalue_quant[1],&BAE_data[54]); 00249 FCTN_CONVERT_FLOAT(quant_data.Bvalue_quant[2],&BAE_data[58]); 00250 BAE_data[62] = quant_data.Batt_voltage_quant; 00251 BAE_data[63] = (uint8_t)actual_data.power_mode; 00252 BAE_data[64] = actual_data.faultPoll_status; 00253 BAE_data[65] = actual_data.faultIr_status; 00254 // archived data 00255 BAE_data[66] = arch_data.Batt_1_temp; //verify if uint8_t is right 00256 BAE_data[67] = arch_data.Batt_2_temp; 00257 BAE_data[68] = arch_data.EPS_PCB_temp; 00258 BAE_data[69] = arch_data.Batt_SOC; 00259 BAE_data[70] = (uint8_t)arch_data.power_mode; 00260 BAE_data[71] = arch_data.faultPoll_status; 00261 BAE_data[72] = arch_data.faultIr_status; 00262 BAE_data[73] = arch_data.Batt_voltage; 00263 for(int i=0; i<73;i++) 00264 BAE_chardata[i] = (char)BAE_data[i]; 00265 BAE_chardata[73] = '\0'; 00266 printf("\n\r bae ats data %c %c %c", BAE_chardata[38],BAE_chardata[39],BAE_chardata[40]); 00267 printf("\n\r BAE data is %s", BAE_chardata); 00268 00269 } 00270 00271 uint8_t quantiz(float start,float step,float x) 00272 { 00273 int y=(x-start)/step; 00274 if(y<=0)y=0; 00275 if(y>=255)y=255; 00276 return y; 00277 } 00278 00279 bool firstCount=true; // goes to EPS init 00280 00281 00282 void saveMin(char x,char y){ 00283 if(y<x){ 00284 x=y; 00285 } 00286 00287 } 00288 void saveMax(char x,char y){ 00289 if (y>x) 00290 { 00291 x=y; 00292 } 00293 } 00294 00295 00296 void minMaxHkData(){ 00297 if(firstCount==true){ 00298 for (int i = 0; i < 16; ++i){ 00299 bae_HK_minmax.voltage_min[i] = quant_data.voltage_quant[i]; 00300 bae_HK_minmax.voltage_max[i] = quant_data.voltage_quant[i]; 00301 } 00302 for (int i = 0; i < 12; ++i){ 00303 bae_HK_minmax.current_min[i] = quant_data.current_quant[i]; 00304 bae_HK_minmax.current_max[i] = quant_data.current_quant[i]; 00305 } 00306 00307 for (int i = 0; i < 2; ++i){ 00308 bae_HK_minmax.Batt_temp_min[i] = quant_data.Batt_temp_quant[i]; 00309 bae_HK_minmax.Batt_temp_max[i] = quant_data.Batt_temp_quant[i]; 00310 } 00311 for (int i = 0; i < 3; ++i){ 00312 bae_HK_minmax.Batt_gauge_min[i] = quant_data.Batt_gauge_quant[i]; 00313 bae_HK_minmax.Batt_gauge_max[i] = quant_data.Batt_gauge_quant[i]; 00314 } 00315 for (int i = 0; i < 3; ++i){ 00316 bae_HK_minmax.AngularSpeed_min[i] = quant_data.AngularSpeed_quant[i]; 00317 bae_HK_minmax.AngularSpeed_max[i] = quant_data.AngularSpeed_quant[i]; 00318 } 00319 for (int i = 0; i < 3; ++i){ 00320 bae_HK_minmax.Bvalue_min[i] = quant_data.Bvalue_quant[i]; 00321 bae_HK_minmax.Bvalue_max[i] = quant_data.Bvalue_quant[i]; 00322 } 00323 bae_HK_minmax.BAE_temp_min=quant_data.BAE_temp_quant; 00324 bae_HK_minmax.BAE_temp_max=quant_data.BAE_temp_quant; 00325 bae_HK_minmax.Batt_voltage_min=quant_data.Batt_voltage_quant; 00326 bae_HK_minmax.Batt_voltage_max=quant_data.Batt_voltage_quant; 00327 00328 } 00329 else { 00330 for (int i = 0; i < 16; ++i) 00331 { 00332 saveMin(bae_HK_minmax.voltage_min[i],quant_data.voltage_quant[i]); 00333 saveMax(bae_HK_minmax.voltage_max[i],quant_data.voltage_quant[i]); 00334 } 00335 for (int i = 0; i < 12; ++i) 00336 { 00337 saveMin(bae_HK_minmax.current_min[i],quant_data.current_quant[i]); 00338 saveMax(bae_HK_minmax.current_max[i],quant_data.current_quant[i]); 00339 } 00340 00341 for (int i = 0; i < 2; ++i) 00342 { 00343 saveMin(bae_HK_minmax.Batt_temp_min[i],quant_data.Batt_temp_quant[i]); 00344 saveMax(bae_HK_minmax.Batt_temp_max[i],quant_data.Batt_temp_quant[i]); 00345 } 00346 for (int i = 0; i < 3; ++i) 00347 { 00348 saveMin(bae_HK_minmax.Batt_gauge_min[i], quant_data.Batt_gauge_quant[i]); 00349 saveMax(bae_HK_minmax.Batt_gauge_max[i], quant_data.Batt_gauge_quant[i]); 00350 } 00351 for (int i = 0; i < 3; ++i) 00352 { 00353 saveMin(bae_HK_minmax.AngularSpeed_min[i], quant_data.AngularSpeed_quant[i]); 00354 saveMax(bae_HK_minmax.AngularSpeed_max[i], quant_data.AngularSpeed_quant[i]); 00355 } 00356 for (int i = 0; i < 3; ++i) 00357 { 00358 saveMin(bae_HK_minmax.Bvalue_min[i], quant_data.Bvalue_quant[i]); 00359 saveMax(bae_HK_minmax.Bvalue_max[i], quant_data.Bvalue_quant[i]); 00360 } 00361 saveMin(bae_HK_minmax.BAE_temp_min,quant_data.BAE_temp_quant); 00362 saveMax(bae_HK_minmax.BAE_temp_max,quant_data.BAE_temp_quant); 00363 saveMin(bae_HK_minmax.Batt_voltage_min,quant_data.Batt_voltage_quant); 00364 saveMin(bae_HK_minmax.Batt_voltage_max,quant_data.Batt_voltage_quant); 00365 00366 00367 } 00368 firstCount=false; 00369 } 00370 00371 00372 //............................................BATTERY GAUGE......................................// 00373 void FCTN_BATTERYGAUGE_INIT() 00374 { 00375 disable_sleep(); 00376 disable_hibernate(); 00377 socChangeAlertEnabled(true); //enabling alert on soc changing by 1% 00378 emptyAlertThreshold(32);//setting empty alert threshold to 32% soc 00379 vAlertMinMaxThreshold();//set min, max value of Valrt register 00380 vResetThresholdSet();//set threshold voltage for reset 00381 vResetAlertEnabled(true);//enable alert on reset for V < Vreset 00382 } 00383 00384 void FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4]) 00385 { 00386 printf("\n\r battery gauge \n"); 00387 00388 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. 00389 tempCompensation(temp); 00390 00391 00392 Battery_parameters[0]=vcell(); 00393 Battery_parameters[1]=soc(); 00394 Battery_parameters[2]=crate(); 00395 00396 printf("\nVcell=%f",vcell()); //remove this for final code 00397 printf("\nSOC=%f",soc()); //remove this for final code 00398 printf("\nC_rate=%f",crate()); //remove this for final code 00399 00400 00401 if (alerting()== true) //alert is on 00402 { 00403 Battery_parameters[3]=alertFlags(); 00404 clearAlert();//clear alert 00405 clearAlertFlags();//clear all alert flags 00406 } 00407 00408 } 00409 00410 unsigned short read(char reg) 00411 { 00412 00413 //Create a temporary buffer 00414 char buff[2]; 00415 00416 //Select the register 00417 m_I2C.write(m_ADDR, ®, 1, true); 00418 00419 //Read the 16-bit register 00420 m_I2C.read(m_ADDR, buff, 2); 00421 00422 //Return the combined 16-bit value 00423 return (buff[0] << 8) | buff[1]; 00424 } 00425 00426 00427 00428 00429 00430 void write(char reg, unsigned short data) 00431 { 00432 //Create a temporary buffer 00433 char buff[3]; 00434 00435 //Load the register address and 16-bit data 00436 buff[0] = reg; 00437 buff[1] = data >> 8; 00438 buff[2] = data; 00439 00440 //Write the data 00441 m_I2C.write(m_ADDR, buff, 3); 00442 } 00443 00444 00445 00446 // Command the MAX17049 to perform a power-on reset 00447 void reset() 00448 { 00449 //Write the POR command 00450 write(REG_CMD, 0x5400); 00451 } 00452 00453 // Command the MAX17049 to perform a QuickStart 00454 void quickStart() 00455 { 00456 //Read the current 16-bit register value 00457 unsigned short value = read(REG_MODE); 00458 00459 //Set the QuickStart bit 00460 value |= (1 << 14); 00461 00462 //Write the value back out 00463 write(REG_MODE, value); 00464 } 00465 00466 00467 //disable sleep 00468 void disable_sleep() 00469 { 00470 unsigned short value = read(REG_MODE); 00471 value &= ~(1 << 13); 00472 write(REG_MODE, value); 00473 } 00474 00475 //disable the hibernate of the MAX17049 00476 void disable_hibernate() 00477 { 00478 write(REG_HIBRT, 0x0000); 00479 } 00480 00481 00482 // Enable or disable the SOC 1% change alert on the MAX17049 00483 void socChangeAlertEnabled(bool enabled) 00484 { 00485 //Read the current 16-bit register value 00486 unsigned short value = read(REG_CONFIG); 00487 00488 //Set or clear the ALSC bit 00489 if (enabled) 00490 value |= (1 << 6); 00491 else 00492 value &= ~(1 << 6); 00493 00494 //Write the value back out 00495 write(REG_CONFIG, value); 00496 } 00497 00498 00499 void compensation(char rcomp) 00500 { 00501 //Read the current 16-bit register value 00502 unsigned short value = read(REG_CONFIG); 00503 00504 //Update the register value 00505 value &= 0x00FF; 00506 value |= rcomp << 8; 00507 00508 //Write the value back out 00509 write(REG_CONFIG, value); 00510 } 00511 00512 00513 void tempCompensation(float temp) 00514 { 00515 //Calculate the new RCOMP value 00516 char rcomp; 00517 if (temp > 20.0) { 00518 rcomp = RCOMP0 + (temp - 20.0) * -0.5; 00519 } else { 00520 rcomp = RCOMP0 + (temp - 20.0) * -5.0; 00521 } 00522 00523 //Update the RCOMP value 00524 compensation(rcomp); 00525 } 00526 00527 // Command the MAX17049 to de-assert the ALRT pin 00528 void clearAlert() 00529 { 00530 //Read the current 16-bit register value 00531 unsigned short value = read(REG_CONFIG); 00532 00533 //Clear the ALRT bit 00534 value &= ~(1 << 5); 00535 00536 //Write the value back out 00537 write(REG_CONFIG, value); 00538 } 00539 00540 00541 //Set the SOC empty alert threshold of the MAX17049 00542 void emptyAlertThreshold(char threshold) 00543 { 00544 //Read the current 16-bit register value 00545 unsigned short value = read(REG_CONFIG); 00546 00547 //Update the register value 00548 value &= 0xFFE0; 00549 value |= 32 - threshold; 00550 00551 //Write the 16-bit register 00552 write(REG_CONFIG, value); 00553 } 00554 00555 // Set the low and high voltage alert threshold of the MAX17049 00556 void vAlertMinMaxThreshold() 00557 { 00558 //Read the current 16-bit register value 00559 unsigned short value = read(REG_VALRT); 00560 00561 //Mask off the old value 00562 00563 value = 0x96D2; 00564 00565 //Write the 16-bit register 00566 write(REG_VALRT, value); 00567 } 00568 00569 00570 // Set the reset voltage threshold of the MAX17049 00571 void vResetThresholdSet() 00572 { 00573 //Read the current 16-bit register value 00574 unsigned short value = read(REG_VRESET_ID); 00575 00576 //Mask off the old //value 00577 value &= 0x00FF;//Dis=0 00578 00579 value |= 0x9400;//corresponding to 2.5 V 00580 00581 00582 //Write the 16-bit register 00583 write(REG_VRESET_ID, value); 00584 } 00585 00586 00587 // Enable or disable the voltage reset alert on the MAX17049 00588 void vResetAlertEnabled(bool enabled) 00589 { 00590 //Read the current 16-bit register value 00591 unsigned short value = read(REG_STATUS); 00592 00593 //Set or clear the EnVR bit 00594 if (enabled) 00595 value |= (1 << 14); 00596 else 00597 value &= ~(1 << 14); 00598 00599 //Write the value back out 00600 write(REG_STATUS, value); 00601 } 00602 00603 //Get the current alert flags on the MAX17049 00604 //refer datasheet-status registers section to decode it. 00605 char alertFlags() 00606 { 00607 //Read the 16-bit register value 00608 unsigned short value = read(REG_STATUS); 00609 00610 //Return only the flag bits 00611 return (value >> 8) & 0x3F; 00612 } 00613 00614 // Clear all the alert flags on the MAX17049 00615 void clearAlertFlags() 00616 { 00617 //Read the current 16-bit register value 00618 unsigned short value = read(REG_STATUS); 00619 00620 //Clear the specified flag bits 00621 value &= ~( 0x3F<< 8); 00622 00623 //Write the value back out 00624 write(REG_STATUS, value); 00625 } 00626 00627 // Get the current cell voltage measurement of the MAX17049 00628 float vcell() 00629 { 00630 00631 //Read the 16-bit raw Vcell value 00632 unsigned short value = read(REG_VCELL); 00633 00634 //Return Vcell in volts 00635 return value * 0.000078125*2; 00636 } 00637 00638 // Get the current state of charge measurement of the MAX17049 as a float 00639 float soc() 00640 { 00641 00642 //Create a temporary buffer 00643 char buff[2]; 00644 int ack = 1; 00645 //Select the register 00646 char reg = REG_SOC; // cannot pass the hash defined values directly 00647 m_I2C.write(m_ADDR, ®, 1, true); 00648 00649 00650 //Read the 16-bit register 00651 00652 ack = m_I2C.read(m_ADDR, buff, 2); 00653 00654 printf("\n\r acknow %d", ack); 00655 00656 //Return SOC in percent 00657 if(ack == 0) 00658 return ((buff[0] << 8) | buff[1]) * 0.00390625; 00659 else 00660 return 200; 00661 } 00662 00663 00664 00665 // Get the current C rate measurement of the MAX17049 00666 float crate() 00667 { 00668 //Read the 16-bit raw C/Rate value 00669 short value = read(REG_CRATE); 00670 00671 //Return C/Rate in %/hr 00672 return value * 0.208; 00673 } 00674 00675 // Determine whether or not the MAX17049 is asserting the ALRT pin 00676 bool alerting() 00677 { 00678 //Read the 16-bit register value 00679 unsigned short value = read(REG_CONFIG); 00680 00681 //Return the status of the ALRT bit 00682 if (value & (1 << 5)) 00683 return true; 00684 else 00685 return false; 00686 } 00687 00688 //.............................Battery board Temp sensor........................// 00689 void FCTN_BATTTEMP_INIT() 00690 { 00691 ssn1=1;ssn2=1; 00692 //PS=0; 00693 //HS=0; 00694 spi_bt.format(8,3); 00695 spi_bt.frequency(1000000); 00696 } 00697 00698 void FCTN_BATT_TEMP_SENSOR_MAIN(float temp[2]) 00699 { 00700 uint8_t MSB, LSB; 00701 int16_t bit_data; 00702 float sensitivity=0.0078125; //1 LSB = sensitivity degree celcius 00703 wait_ms(320); 00704 ssn1=0; 00705 00706 spi_bt.write(0x80);//Reading digital data from Sensor 1 00707 LSB = spi_bt.write(0x00);//LSB first 00708 wait_ms(0); 00709 MSB = spi_bt.write(0x00); 00710 wait_ms(10); 00711 pc_eps.printf("%d %d\n",MSB,LSB); 00712 bit_data= ((uint16_t)MSB<<8)|LSB; 00713 wait_ms(10); 00714 temp[0]=(float)bit_data*sensitivity;//Converting into decimal value 00715 ssn1=1; 00716 wait_ms(10); 00717 ssn2=0;//Reading data from sensor 2 00718 spi_bt.write(0x80); 00719 LSB = spi_bt.write(0x00); 00720 wait_ms(10); 00721 MSB = spi_bt.write(0x00); 00722 wait_ms(10); 00723 bit_data= ((int16_t)MSB<<8)|LSB; 00724 wait_ms(10); 00725 temp[1]=(float)bit_data*sensitivity; 00726 ssn2=1; 00727 00728 }
Generated on Tue Jul 12 2022 17:24:01 by
1.7.2
