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