green rosh
/
BAE_FRDM_INTEGRATION
bae integrated final (may be)
Fork of BAE_FRDMTESIN2 by
Embed:
(wiki syntax)
Show/hide line numbers
HK.h
00001 00002 //to be saved as HK.h 00003 00004 #include "mbed.h" 00005 #define tstart -40 00006 #define tstep 8 00007 #define tstep_thermistor 8//verify!! 00008 #define tstart_thermistor -40 00009 #define vstart 3.3 00010 #define vstep 0.84667 00011 #define cstart 0.0691 00012 #define cstep 0.09133 00013 #define rsens 0.095 00014 00015 struct SensorData { 00016 char Voltage[10]; 00017 char Current[10]; 00018 char Temperature[10]; 00019 char PanelTemperature[2];//read by the 4 thermistors on solar panels 00020 char Vcell;//unquantised 00021 char soc;//unquantised 00022 char alerts;//UNQUANTISED 00023 //(alerts[0]=1)-> reset indicator=>dont care 00024 //(alerts[1]=1)-> Vcell>ValrtMax(5.1V)->will always be on->dont care 00025 //(alerts[2]=1)-> Vcell<ValrtMin(5.1V)->indicates deep discharge 00026 //(alerts[3]=1)-> Vcell<Vreset(2.5V) 00027 //(alerts[5]=1)-> Soc CROSSES the threshold value 00028 //(alerts[6]=1)-> alert on (alerts[3]) enabled when Vcell<Vreset(here we set it to be 2.5V) 00029 char crate;//unquantised 00030 char faultpoll; //polled faults 00031 char faultir; //interupted faults 00032 char power_mode; //power modes 00033 00034 00035 //float magnetometer,gyro=>to be addes 00036 }; 00037 00038 typedef struct ShortBeacon { 00039 char Voltage[1]; //battery voltage from gauge 00040 char AngularSpeed[2]; //all the 3 data 00041 char SubsystemStatus[1]; //power modes 00042 char Temp[3]; //temp of solar panel 00043 char ErrorFlag[1]; //fault 00044 }ShortBeacy; 00045 00046 00047 00048 void FUNC_HK_MAIN(); 00049 00050 int quantiz(float start,float step,float x); 00051 00052 00053 //--------------------------------following is header details for battery gauge------------------------------------------- 00054 00055 00056 00057 #define MAX17048_H 00058 00059 class MAX17048 00060 { 00061 public: 00062 /** The default compensation value for the MAX17048 00063 */ 00064 static const char RCOMP0= 0x97; 00065 00066 /** Represents the different alert flags for the MAX17048 00067 */ 00068 enum AlertFlags { 00069 ALERT_RI = (1 << 0), /**< Reset indicator */ 00070 ALERT_VH = (1 << 1), /**< Voltage high alert */ 00071 ALERT_VL = (1 << 2), /**< Voltage low alert */ 00072 ALERT_VR = (1 << 3), /**< Voltage reset alert */ 00073 ALERT_HD = (1 << 4), /**< SOC low alert */ 00074 ALERT_SC = (1 << 5) /**< SOC change alert */ 00075 }; 00076 00077 //parametrised constructor 00078 MAX17048(PinName sda, PinName scl, int hz = 400000): m_I2C(sda, scl)//should it be same as the uC clock freq 00079 { 00080 //Set the I2C bus frequency 00081 m_I2C.frequency(hz); 00082 } 00083 00084 // Probe for the MAX17048 and indicate if it's present on the bus 00085 bool open() 00086 { 00087 //Probe for the MAX17048 using a Zero Length Transfer 00088 if (!m_I2C.write(m_ADDR, NULL, 0)) { 00089 //Return success 00090 return true; 00091 } else { 00092 //Return failure 00093 return false; 00094 } 00095 } 00096 00097 00098 // Command the MAX17048 to perform a power-on reset 00099 void reset() 00100 { 00101 //Write the POR command 00102 write(REG_CMD, 0x5400); 00103 } 00104 00105 // Command the MAX17048 to perform a QuickStart 00106 void quickStart() 00107 { 00108 //Read the current 16-bit register value 00109 unsigned short value = read(REG_MODE); 00110 00111 //Set the QuickStart bit 00112 value |= (1 << 14); 00113 00114 //Write the value back out 00115 write(REG_MODE, value); 00116 } 00117 00118 //disable sleep 00119 void disable_sleep() 00120 { 00121 unsigned short value = read(REG_MODE); 00122 value &= ~(1 << 13); 00123 write(REG_MODE, value); 00124 } 00125 00126 //disable the hibernate of the MAX17048 00127 void disable_hibernate() 00128 { 00129 write(REG_HIBRT, 0x0000); 00130 } 00131 00132 // Determine whether or not the SOC 1% change alert is enabled on the MAX17048 00133 bool socChangeAlertEnabled() 00134 { 00135 //Read the 16-bit register value 00136 unsigned short value = read(REG_CONFIG); 00137 00138 //Return the status of the ALSC bit 00139 if (value & (1 << 6)) 00140 return true; 00141 else 00142 return false; 00143 } 00144 00145 // Enable or disable the SOC 1% change alert on the MAX17048 00146 void socChangeAlertEnabled(bool enabled) 00147 { 00148 //Read the current 16-bit register value 00149 unsigned short value = read(REG_CONFIG); 00150 00151 //Set or clear the ALSC bit 00152 if (enabled) 00153 value |= (1 << 6); 00154 else 00155 value &= ~(1 << 6); 00156 00157 //Write the value back out 00158 write(REG_CONFIG, value); 00159 } 00160 00161 // Determine whether or not the MAX17048 is asserting the ALRT pin 00162 bool alerting() 00163 { 00164 //Read the 16-bit register value 00165 unsigned short value = read(REG_CONFIG); 00166 00167 //Return the status of the ALRT bit 00168 if (value & (1 << 5)) 00169 return true; 00170 else 00171 return false; 00172 } 00173 00174 // Command the MAX17048 to de-assert the ALRT pin 00175 void clearAlert() 00176 { 00177 //Read the current 16-bit register value 00178 unsigned short value = read(REG_CONFIG); 00179 00180 //Clear the ALRT bit 00181 value &= ~(1 << 5); 00182 00183 //Write the value back out 00184 write(REG_CONFIG, value); 00185 } 00186 // return The current SOC empty alert threshold in %. 00187 char emptyAlertThreshold() 00188 { 00189 //Read the 16-bit register value 00190 unsigned short value = read(REG_CONFIG); 00191 00192 //Extract the threshold 00193 return 32 - (value & 0x001F); 00194 } 00195 00196 //Set the SOC empty alert threshold of the MAX17048 00197 void emptyAlertThreshold(char threshold) 00198 { 00199 //Read the current 16-bit register value 00200 unsigned short value = read(REG_CONFIG); 00201 00202 //Update the register value 00203 value &= 0xFFE0; 00204 value |= 32 - threshold; 00205 00206 //Write the 16-bit register 00207 write(REG_CONFIG, value); 00208 } 00209 //return The current low voltage alert threshold in volts. 00210 float vAlertMinThreshold() 00211 { 00212 //Read the 16-bit register value 00213 unsigned short value = read(REG_VALRT); 00214 00215 //Extract the alert threshold 00216 return (value >> 8) * 0.02;//least count is 20mV 00217 } 00218 // Set the low and high voltage alert threshold of the MAX17048 00219 void vAlertMinMaxThreshold() 00220 { 00221 //Read the current 16-bit register value 00222 unsigned short value = read(REG_VALRT); 00223 00224 //Mask off the old value 00225 00226 value = 0xFFFF; 00227 00228 //Write the 16-bit register 00229 write(REG_VALRT, value); 00230 } 00231 00232 //return The current high voltage alert threshold in volts. 00233 float vAlertMaxThreshold() 00234 { 00235 //Read the 16-bit register value 00236 unsigned short value = read(REG_VALRT); 00237 00238 //Extract the active threshold 00239 return (value & 0x00FF) * 0.02; 00240 } 00241 00242 //return The current reset voltage threshold in volts. 00243 float vResetThreshold() 00244 { 00245 //Read the 16-bit register value 00246 unsigned short value = read(REG_VRESET_ID); 00247 00248 //Extract the threshold 00249 return (value >> 9) * 0.04; 00250 } 00251 00252 // Set the reset voltage threshold of the MAX17048 00253 void vResetThresholdSet() 00254 { 00255 //Read the current 16-bit register value 00256 unsigned short value = read(REG_VRESET_ID); 00257 00258 //Mask off the old //value 00259 value &= 0x00FF;//Dis=0 00260 00261 value |= 0x7C00;//corresponding to 2.5 V 00262 00263 00264 //Write the 16-bit register 00265 write(REG_VRESET_ID, value); 00266 } 00267 00268 // Get the factory programmed 8-bit ID of the MAX17048 00269 char id() 00270 { 00271 //Read the 16-bit register value 00272 unsigned short value = read(REG_VRESET_ID); 00273 00274 //Return only the ID bits 00275 return value; 00276 } 00277 00278 // Determine whether or not the voltage reset alert is enabled on the MAX17048 00279 bool vResetAlertEnabled() 00280 { 00281 //Read the 16-bit register value 00282 unsigned short value = read(REG_STATUS); 00283 00284 //Return the status of the EnVR bit 00285 if (value & (1 << 14)) 00286 return true; 00287 else 00288 return false; 00289 } 00290 00291 // Enable or disable the voltage reset alert on the MAX17048 00292 void vResetAlertEnabled(bool enabled) 00293 { 00294 //Read the current 16-bit register value 00295 unsigned short value = read(REG_STATUS); 00296 00297 //Set or clear the EnVR bit 00298 if (enabled) 00299 value |= (1 << 14); 00300 else 00301 value &= ~(1 << 14); 00302 00303 //Write the value back out 00304 write(REG_STATUS, value); 00305 } 00306 00307 //Get the current alert flags on the MAX17048 00308 //refer datasheet-status registers section to decode it. 00309 char alertFlags() 00310 { 00311 //Read the 16-bit register value 00312 unsigned short value = read(REG_STATUS); 00313 00314 //Return only the flag bits 00315 return (value >> 8) & 0x3F; 00316 } 00317 00318 // Clear all the alert flags on the MAX17048 00319 void clearAlertFlags() 00320 { 00321 //Read the current 16-bit register value 00322 unsigned short value = read(REG_STATUS); 00323 00324 //Clear the specified flag bits 00325 value &= ~( 0x3F<< 8); 00326 00327 //Write the value back out 00328 write(REG_STATUS, value); 00329 } 00330 00331 // Get the current cell voltage measurement of the MAX17048 00332 float vcell() 00333 { 00334 //Read the 16-bit raw Vcell value 00335 unsigned short value = read(REG_VCELL); 00336 00337 //Return Vcell in volts 00338 return value * 0.000078125; 00339 } 00340 00341 // Get the current state of charge measurement of the MAX17048 as a float 00342 float soc() 00343 { 00344 //Read the 16-bit raw SOC value 00345 unsigned short value = read(REG_SOC); 00346 00347 //Return SOC in percent 00348 return value * 0.00390625; 00349 } 00350 00351 // Get the current state of charge measurement of the MAX17048 as an int 00352 int soc_int() 00353 { 00354 //Read the 16-bit raw SOC value 00355 unsigned short value = read(REG_SOC); 00356 00357 //Return only the top byte 00358 return value >> 8; 00359 } 00360 00361 // Get the current C rate measurement of the MAX17048 00362 float crate() 00363 { 00364 //Read the 16-bit raw C/Rate value 00365 short value = read(REG_CRATE); 00366 00367 //Return C/Rate in %/hr 00368 return value * 0.208; 00369 } 00370 00371 00372 //I2C register addresses 00373 enum Register { 00374 REG_VCELL = 0x02, 00375 REG_SOC = 0x04, 00376 REG_MODE = 0x06, 00377 REG_VERSION = 0x08, 00378 REG_HIBRT = 0x0A, 00379 REG_CONFIG = 0x0C, 00380 REG_VALRT = 0x14, 00381 REG_CRATE = 0x16, 00382 REG_VRESET_ID = 0x18, 00383 REG_STATUS = 0x1A, 00384 REG_TABLE = 0x40, 00385 REG_CMD = 0xFE 00386 }; 00387 00388 //Member constants 00389 static const int m_ADDR=(0x36 << 1); 00390 00391 //Member variables 00392 I2C m_I2C; 00393 00394 //Internal functions 00395 unsigned short read(char reg) 00396 { 00397 //Create a temporary buffer 00398 char buff[2]; 00399 00400 //Select the register 00401 m_I2C.write(m_ADDR, ®, 1, true); 00402 00403 //Read the 16-bit register 00404 m_I2C.read(m_ADDR, buff, 2); 00405 00406 //Return the combined 16-bit value 00407 return (buff[0] << 8) | buff[1]; 00408 } 00409 00410 00411 void write(char reg, unsigned short data) 00412 { 00413 //Create a temporary buffer 00414 char buff[3]; 00415 00416 //Load the register address and 16-bit data 00417 buff[0] = reg; 00418 buff[1] = data >> 8; 00419 buff[2] = data; 00420 00421 //Write the data 00422 m_I2C.write(m_ADDR, buff, 3); 00423 } 00424 }; 00425 00426 00427 void FUNC_BATTERYGAUGE_MAIN(float[]); 00428 00429
Generated on Sat Jul 16 2022 17:56:57 by 1.7.2