![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
bae integrated final (may be)
Fork of BAE_FRDMTESIN2 by
HK.h@8:667fbc82d634, 2014-12-15 (annotated)
- Committer:
- greenroshks
- Date:
- Mon Dec 15 05:58:23 2014 +0000
- Revision:
- 8:667fbc82d634
- Parent:
- 7:f06840d848e3
BAE FINAL INTEGRATED (may be)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
greenroshks | 8:667fbc82d634 | 1 | |
greenroshks | 8:667fbc82d634 | 2 | //to be saved as HK.h |
greenroshks | 8:667fbc82d634 | 3 | |
greenroshks | 0:8b0d43fe6c05 | 4 | #include "mbed.h" |
greenroshks | 0:8b0d43fe6c05 | 5 | #define tstart -40 |
greenroshks | 0:8b0d43fe6c05 | 6 | #define tstep 8 |
greenroshks | 8:667fbc82d634 | 7 | #define tstep_thermistor 8//verify!! |
greenroshks | 8:667fbc82d634 | 8 | #define tstart_thermistor -40 |
greenroshks | 0:8b0d43fe6c05 | 9 | #define vstart 3.3 |
greenroshks | 0:8b0d43fe6c05 | 10 | #define vstep 0.84667 |
greenroshks | 0:8b0d43fe6c05 | 11 | #define cstart 0.0691 |
greenroshks | 0:8b0d43fe6c05 | 12 | #define cstep 0.09133 |
greenroshks | 0:8b0d43fe6c05 | 13 | #define rsens 0.095 |
greenroshks | 0:8b0d43fe6c05 | 14 | |
greenroshks | 0:8b0d43fe6c05 | 15 | struct SensorData { |
greenroshks | 0:8b0d43fe6c05 | 16 | char Voltage[10]; |
greenroshks | 0:8b0d43fe6c05 | 17 | char Current[10]; |
greenroshks | 0:8b0d43fe6c05 | 18 | char Temperature[10]; |
greenroshks | 8:667fbc82d634 | 19 | char PanelTemperature[2];//read by the 4 thermistors on solar panels |
greenroshks | 8:667fbc82d634 | 20 | char Vcell;//unquantised |
greenroshks | 8:667fbc82d634 | 21 | char soc;//unquantised |
greenroshks | 8:667fbc82d634 | 22 | char alerts;//UNQUANTISED |
greenroshks | 8:667fbc82d634 | 23 | //(alerts[0]=1)-> reset indicator=>dont care |
greenroshks | 8:667fbc82d634 | 24 | //(alerts[1]=1)-> Vcell>ValrtMax(5.1V)->will always be on->dont care |
greenroshks | 8:667fbc82d634 | 25 | //(alerts[2]=1)-> Vcell<ValrtMin(5.1V)->indicates deep discharge |
greenroshks | 8:667fbc82d634 | 26 | //(alerts[3]=1)-> Vcell<Vreset(2.5V) |
greenroshks | 8:667fbc82d634 | 27 | //(alerts[5]=1)-> Soc CROSSES the threshold value |
greenroshks | 8:667fbc82d634 | 28 | //(alerts[6]=1)-> alert on (alerts[3]) enabled when Vcell<Vreset(here we set it to be 2.5V) |
greenroshks | 8:667fbc82d634 | 29 | char crate;//unquantised |
greenroshks | 8:667fbc82d634 | 30 | char faultpoll; //polled faults |
greenroshks | 8:667fbc82d634 | 31 | char faultir; //interupted faults |
greenroshks | 8:667fbc82d634 | 32 | char power_mode; //power modes |
greenroshks | 8:667fbc82d634 | 33 | |
greenroshks | 8:667fbc82d634 | 34 | |
greenroshks | 8:667fbc82d634 | 35 | //float magnetometer,gyro=>to be addes |
greenroshks | 0:8b0d43fe6c05 | 36 | }; |
greenroshks | 0:8b0d43fe6c05 | 37 | |
greenroshks | 0:8b0d43fe6c05 | 38 | typedef struct ShortBeacon { |
greenroshks | 8:667fbc82d634 | 39 | char Voltage[1]; //battery voltage from gauge |
greenroshks | 8:667fbc82d634 | 40 | char AngularSpeed[2]; //all the 3 data |
greenroshks | 8:667fbc82d634 | 41 | char SubsystemStatus[1]; //power modes |
greenroshks | 8:667fbc82d634 | 42 | char Temp[3]; //temp of solar panel |
greenroshks | 8:667fbc82d634 | 43 | char ErrorFlag[1]; //fault |
greenroshks | 8:667fbc82d634 | 44 | }ShortBeacy; |
greenroshks | 8:667fbc82d634 | 45 | |
greenroshks | 8:667fbc82d634 | 46 | |
greenroshks | 0:8b0d43fe6c05 | 47 | |
greenroshks | 0:8b0d43fe6c05 | 48 | void FUNC_HK_MAIN(); |
greenroshks | 0:8b0d43fe6c05 | 49 | |
greenroshks | 0:8b0d43fe6c05 | 50 | int quantiz(float start,float step,float x); |
greenroshks | 8:667fbc82d634 | 51 | |
greenroshks | 8:667fbc82d634 | 52 | |
greenroshks | 8:667fbc82d634 | 53 | //--------------------------------following is header details for battery gauge------------------------------------------- |
greenroshks | 8:667fbc82d634 | 54 | |
greenroshks | 8:667fbc82d634 | 55 | |
greenroshks | 8:667fbc82d634 | 56 | |
greenroshks | 8:667fbc82d634 | 57 | #define MAX17048_H |
greenroshks | 8:667fbc82d634 | 58 | |
greenroshks | 8:667fbc82d634 | 59 | class MAX17048 |
greenroshks | 8:667fbc82d634 | 60 | { |
greenroshks | 8:667fbc82d634 | 61 | public: |
greenroshks | 8:667fbc82d634 | 62 | /** The default compensation value for the MAX17048 |
greenroshks | 8:667fbc82d634 | 63 | */ |
greenroshks | 8:667fbc82d634 | 64 | static const char RCOMP0= 0x97; |
greenroshks | 8:667fbc82d634 | 65 | |
greenroshks | 8:667fbc82d634 | 66 | /** Represents the different alert flags for the MAX17048 |
greenroshks | 8:667fbc82d634 | 67 | */ |
greenroshks | 8:667fbc82d634 | 68 | enum AlertFlags { |
greenroshks | 8:667fbc82d634 | 69 | ALERT_RI = (1 << 0), /**< Reset indicator */ |
greenroshks | 8:667fbc82d634 | 70 | ALERT_VH = (1 << 1), /**< Voltage high alert */ |
greenroshks | 8:667fbc82d634 | 71 | ALERT_VL = (1 << 2), /**< Voltage low alert */ |
greenroshks | 8:667fbc82d634 | 72 | ALERT_VR = (1 << 3), /**< Voltage reset alert */ |
greenroshks | 8:667fbc82d634 | 73 | ALERT_HD = (1 << 4), /**< SOC low alert */ |
greenroshks | 8:667fbc82d634 | 74 | ALERT_SC = (1 << 5) /**< SOC change alert */ |
greenroshks | 8:667fbc82d634 | 75 | }; |
greenroshks | 8:667fbc82d634 | 76 | |
greenroshks | 8:667fbc82d634 | 77 | //parametrised constructor |
greenroshks | 8:667fbc82d634 | 78 | MAX17048(PinName sda, PinName scl, int hz = 400000): m_I2C(sda, scl)//should it be same as the uC clock freq |
greenroshks | 8:667fbc82d634 | 79 | { |
greenroshks | 8:667fbc82d634 | 80 | //Set the I2C bus frequency |
greenroshks | 8:667fbc82d634 | 81 | m_I2C.frequency(hz); |
greenroshks | 8:667fbc82d634 | 82 | } |
greenroshks | 8:667fbc82d634 | 83 | |
greenroshks | 8:667fbc82d634 | 84 | // Probe for the MAX17048 and indicate if it's present on the bus |
greenroshks | 8:667fbc82d634 | 85 | bool open() |
greenroshks | 8:667fbc82d634 | 86 | { |
greenroshks | 8:667fbc82d634 | 87 | //Probe for the MAX17048 using a Zero Length Transfer |
greenroshks | 8:667fbc82d634 | 88 | if (!m_I2C.write(m_ADDR, NULL, 0)) { |
greenroshks | 8:667fbc82d634 | 89 | //Return success |
greenroshks | 8:667fbc82d634 | 90 | return true; |
greenroshks | 8:667fbc82d634 | 91 | } else { |
greenroshks | 8:667fbc82d634 | 92 | //Return failure |
greenroshks | 8:667fbc82d634 | 93 | return false; |
greenroshks | 8:667fbc82d634 | 94 | } |
greenroshks | 8:667fbc82d634 | 95 | } |
greenroshks | 8:667fbc82d634 | 96 | |
greenroshks | 8:667fbc82d634 | 97 | |
greenroshks | 8:667fbc82d634 | 98 | // Command the MAX17048 to perform a power-on reset |
greenroshks | 8:667fbc82d634 | 99 | void reset() |
greenroshks | 8:667fbc82d634 | 100 | { |
greenroshks | 8:667fbc82d634 | 101 | //Write the POR command |
greenroshks | 8:667fbc82d634 | 102 | write(REG_CMD, 0x5400); |
greenroshks | 8:667fbc82d634 | 103 | } |
greenroshks | 8:667fbc82d634 | 104 | |
greenroshks | 8:667fbc82d634 | 105 | // Command the MAX17048 to perform a QuickStart |
greenroshks | 8:667fbc82d634 | 106 | void quickStart() |
greenroshks | 8:667fbc82d634 | 107 | { |
greenroshks | 8:667fbc82d634 | 108 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 109 | unsigned short value = read(REG_MODE); |
greenroshks | 8:667fbc82d634 | 110 | |
greenroshks | 8:667fbc82d634 | 111 | //Set the QuickStart bit |
greenroshks | 8:667fbc82d634 | 112 | value |= (1 << 14); |
greenroshks | 8:667fbc82d634 | 113 | |
greenroshks | 8:667fbc82d634 | 114 | //Write the value back out |
greenroshks | 8:667fbc82d634 | 115 | write(REG_MODE, value); |
greenroshks | 8:667fbc82d634 | 116 | } |
greenroshks | 8:667fbc82d634 | 117 | |
greenroshks | 8:667fbc82d634 | 118 | //disable sleep |
greenroshks | 8:667fbc82d634 | 119 | void disable_sleep() |
greenroshks | 8:667fbc82d634 | 120 | { |
greenroshks | 8:667fbc82d634 | 121 | unsigned short value = read(REG_MODE); |
greenroshks | 8:667fbc82d634 | 122 | value &= ~(1 << 13); |
greenroshks | 8:667fbc82d634 | 123 | write(REG_MODE, value); |
greenroshks | 8:667fbc82d634 | 124 | } |
greenroshks | 8:667fbc82d634 | 125 | |
greenroshks | 8:667fbc82d634 | 126 | //disable the hibernate of the MAX17048 |
greenroshks | 8:667fbc82d634 | 127 | void disable_hibernate() |
greenroshks | 8:667fbc82d634 | 128 | { |
greenroshks | 8:667fbc82d634 | 129 | write(REG_HIBRT, 0x0000); |
greenroshks | 8:667fbc82d634 | 130 | } |
greenroshks | 8:667fbc82d634 | 131 | |
greenroshks | 8:667fbc82d634 | 132 | // Determine whether or not the SOC 1% change alert is enabled on the MAX17048 |
greenroshks | 8:667fbc82d634 | 133 | bool socChangeAlertEnabled() |
greenroshks | 8:667fbc82d634 | 134 | { |
greenroshks | 8:667fbc82d634 | 135 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 136 | unsigned short value = read(REG_CONFIG); |
greenroshks | 8:667fbc82d634 | 137 | |
greenroshks | 8:667fbc82d634 | 138 | //Return the status of the ALSC bit |
greenroshks | 8:667fbc82d634 | 139 | if (value & (1 << 6)) |
greenroshks | 8:667fbc82d634 | 140 | return true; |
greenroshks | 8:667fbc82d634 | 141 | else |
greenroshks | 8:667fbc82d634 | 142 | return false; |
greenroshks | 8:667fbc82d634 | 143 | } |
greenroshks | 8:667fbc82d634 | 144 | |
greenroshks | 8:667fbc82d634 | 145 | // Enable or disable the SOC 1% change alert on the MAX17048 |
greenroshks | 8:667fbc82d634 | 146 | void socChangeAlertEnabled(bool enabled) |
greenroshks | 8:667fbc82d634 | 147 | { |
greenroshks | 8:667fbc82d634 | 148 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 149 | unsigned short value = read(REG_CONFIG); |
greenroshks | 8:667fbc82d634 | 150 | |
greenroshks | 8:667fbc82d634 | 151 | //Set or clear the ALSC bit |
greenroshks | 8:667fbc82d634 | 152 | if (enabled) |
greenroshks | 8:667fbc82d634 | 153 | value |= (1 << 6); |
greenroshks | 8:667fbc82d634 | 154 | else |
greenroshks | 8:667fbc82d634 | 155 | value &= ~(1 << 6); |
greenroshks | 8:667fbc82d634 | 156 | |
greenroshks | 8:667fbc82d634 | 157 | //Write the value back out |
greenroshks | 8:667fbc82d634 | 158 | write(REG_CONFIG, value); |
greenroshks | 8:667fbc82d634 | 159 | } |
greenroshks | 0:8b0d43fe6c05 | 160 | |
greenroshks | 8:667fbc82d634 | 161 | // Determine whether or not the MAX17048 is asserting the ALRT pin |
greenroshks | 8:667fbc82d634 | 162 | bool alerting() |
greenroshks | 8:667fbc82d634 | 163 | { |
greenroshks | 8:667fbc82d634 | 164 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 165 | unsigned short value = read(REG_CONFIG); |
greenroshks | 8:667fbc82d634 | 166 | |
greenroshks | 8:667fbc82d634 | 167 | //Return the status of the ALRT bit |
greenroshks | 8:667fbc82d634 | 168 | if (value & (1 << 5)) |
greenroshks | 8:667fbc82d634 | 169 | return true; |
greenroshks | 8:667fbc82d634 | 170 | else |
greenroshks | 8:667fbc82d634 | 171 | return false; |
greenroshks | 8:667fbc82d634 | 172 | } |
greenroshks | 8:667fbc82d634 | 173 | |
greenroshks | 8:667fbc82d634 | 174 | // Command the MAX17048 to de-assert the ALRT pin |
greenroshks | 8:667fbc82d634 | 175 | void clearAlert() |
greenroshks | 8:667fbc82d634 | 176 | { |
greenroshks | 8:667fbc82d634 | 177 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 178 | unsigned short value = read(REG_CONFIG); |
greenroshks | 8:667fbc82d634 | 179 | |
greenroshks | 8:667fbc82d634 | 180 | //Clear the ALRT bit |
greenroshks | 8:667fbc82d634 | 181 | value &= ~(1 << 5); |
greenroshks | 8:667fbc82d634 | 182 | |
greenroshks | 8:667fbc82d634 | 183 | //Write the value back out |
greenroshks | 8:667fbc82d634 | 184 | write(REG_CONFIG, value); |
greenroshks | 8:667fbc82d634 | 185 | } |
greenroshks | 8:667fbc82d634 | 186 | // return The current SOC empty alert threshold in %. |
greenroshks | 8:667fbc82d634 | 187 | char emptyAlertThreshold() |
greenroshks | 8:667fbc82d634 | 188 | { |
greenroshks | 8:667fbc82d634 | 189 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 190 | unsigned short value = read(REG_CONFIG); |
greenroshks | 8:667fbc82d634 | 191 | |
greenroshks | 8:667fbc82d634 | 192 | //Extract the threshold |
greenroshks | 8:667fbc82d634 | 193 | return 32 - (value & 0x001F); |
greenroshks | 8:667fbc82d634 | 194 | } |
greenroshks | 8:667fbc82d634 | 195 | |
greenroshks | 8:667fbc82d634 | 196 | //Set the SOC empty alert threshold of the MAX17048 |
greenroshks | 8:667fbc82d634 | 197 | void emptyAlertThreshold(char threshold) |
greenroshks | 8:667fbc82d634 | 198 | { |
greenroshks | 8:667fbc82d634 | 199 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 200 | unsigned short value = read(REG_CONFIG); |
greenroshks | 8:667fbc82d634 | 201 | |
greenroshks | 8:667fbc82d634 | 202 | //Update the register value |
greenroshks | 8:667fbc82d634 | 203 | value &= 0xFFE0; |
greenroshks | 8:667fbc82d634 | 204 | value |= 32 - threshold; |
greenroshks | 8:667fbc82d634 | 205 | |
greenroshks | 8:667fbc82d634 | 206 | //Write the 16-bit register |
greenroshks | 8:667fbc82d634 | 207 | write(REG_CONFIG, value); |
greenroshks | 8:667fbc82d634 | 208 | } |
greenroshks | 8:667fbc82d634 | 209 | //return The current low voltage alert threshold in volts. |
greenroshks | 8:667fbc82d634 | 210 | float vAlertMinThreshold() |
greenroshks | 8:667fbc82d634 | 211 | { |
greenroshks | 8:667fbc82d634 | 212 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 213 | unsigned short value = read(REG_VALRT); |
greenroshks | 8:667fbc82d634 | 214 | |
greenroshks | 8:667fbc82d634 | 215 | //Extract the alert threshold |
greenroshks | 8:667fbc82d634 | 216 | return (value >> 8) * 0.02;//least count is 20mV |
greenroshks | 8:667fbc82d634 | 217 | } |
greenroshks | 8:667fbc82d634 | 218 | // Set the low and high voltage alert threshold of the MAX17048 |
greenroshks | 8:667fbc82d634 | 219 | void vAlertMinMaxThreshold() |
greenroshks | 8:667fbc82d634 | 220 | { |
greenroshks | 8:667fbc82d634 | 221 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 222 | unsigned short value = read(REG_VALRT); |
greenroshks | 8:667fbc82d634 | 223 | |
greenroshks | 8:667fbc82d634 | 224 | //Mask off the old value |
greenroshks | 8:667fbc82d634 | 225 | |
greenroshks | 8:667fbc82d634 | 226 | value = 0xFFFF; |
greenroshks | 8:667fbc82d634 | 227 | |
greenroshks | 8:667fbc82d634 | 228 | //Write the 16-bit register |
greenroshks | 8:667fbc82d634 | 229 | write(REG_VALRT, value); |
greenroshks | 8:667fbc82d634 | 230 | } |
greenroshks | 8:667fbc82d634 | 231 | |
greenroshks | 8:667fbc82d634 | 232 | //return The current high voltage alert threshold in volts. |
greenroshks | 8:667fbc82d634 | 233 | float vAlertMaxThreshold() |
greenroshks | 8:667fbc82d634 | 234 | { |
greenroshks | 8:667fbc82d634 | 235 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 236 | unsigned short value = read(REG_VALRT); |
greenroshks | 8:667fbc82d634 | 237 | |
greenroshks | 8:667fbc82d634 | 238 | //Extract the active threshold |
greenroshks | 8:667fbc82d634 | 239 | return (value & 0x00FF) * 0.02; |
greenroshks | 8:667fbc82d634 | 240 | } |
greenroshks | 8:667fbc82d634 | 241 | |
greenroshks | 8:667fbc82d634 | 242 | //return The current reset voltage threshold in volts. |
greenroshks | 8:667fbc82d634 | 243 | float vResetThreshold() |
greenroshks | 8:667fbc82d634 | 244 | { |
greenroshks | 8:667fbc82d634 | 245 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 246 | unsigned short value = read(REG_VRESET_ID); |
greenroshks | 8:667fbc82d634 | 247 | |
greenroshks | 8:667fbc82d634 | 248 | //Extract the threshold |
greenroshks | 8:667fbc82d634 | 249 | return (value >> 9) * 0.04; |
greenroshks | 8:667fbc82d634 | 250 | } |
greenroshks | 8:667fbc82d634 | 251 | |
greenroshks | 8:667fbc82d634 | 252 | // Set the reset voltage threshold of the MAX17048 |
greenroshks | 8:667fbc82d634 | 253 | void vResetThresholdSet() |
greenroshks | 8:667fbc82d634 | 254 | { |
greenroshks | 8:667fbc82d634 | 255 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 256 | unsigned short value = read(REG_VRESET_ID); |
greenroshks | 8:667fbc82d634 | 257 | |
greenroshks | 8:667fbc82d634 | 258 | //Mask off the old //value |
greenroshks | 8:667fbc82d634 | 259 | value &= 0x00FF;//Dis=0 |
greenroshks | 8:667fbc82d634 | 260 | |
greenroshks | 8:667fbc82d634 | 261 | value |= 0x7C00;//corresponding to 2.5 V |
greenroshks | 8:667fbc82d634 | 262 | |
greenroshks | 8:667fbc82d634 | 263 | |
greenroshks | 8:667fbc82d634 | 264 | //Write the 16-bit register |
greenroshks | 8:667fbc82d634 | 265 | write(REG_VRESET_ID, value); |
greenroshks | 8:667fbc82d634 | 266 | } |
greenroshks | 8:667fbc82d634 | 267 | |
greenroshks | 8:667fbc82d634 | 268 | // Get the factory programmed 8-bit ID of the MAX17048 |
greenroshks | 8:667fbc82d634 | 269 | char id() |
greenroshks | 8:667fbc82d634 | 270 | { |
greenroshks | 8:667fbc82d634 | 271 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 272 | unsigned short value = read(REG_VRESET_ID); |
greenroshks | 8:667fbc82d634 | 273 | |
greenroshks | 8:667fbc82d634 | 274 | //Return only the ID bits |
greenroshks | 8:667fbc82d634 | 275 | return value; |
greenroshks | 8:667fbc82d634 | 276 | } |
greenroshks | 8:667fbc82d634 | 277 | |
greenroshks | 8:667fbc82d634 | 278 | // Determine whether or not the voltage reset alert is enabled on the MAX17048 |
greenroshks | 8:667fbc82d634 | 279 | bool vResetAlertEnabled() |
greenroshks | 8:667fbc82d634 | 280 | { |
greenroshks | 8:667fbc82d634 | 281 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 282 | unsigned short value = read(REG_STATUS); |
greenroshks | 8:667fbc82d634 | 283 | |
greenroshks | 8:667fbc82d634 | 284 | //Return the status of the EnVR bit |
greenroshks | 8:667fbc82d634 | 285 | if (value & (1 << 14)) |
greenroshks | 8:667fbc82d634 | 286 | return true; |
greenroshks | 8:667fbc82d634 | 287 | else |
greenroshks | 8:667fbc82d634 | 288 | return false; |
greenroshks | 8:667fbc82d634 | 289 | } |
greenroshks | 8:667fbc82d634 | 290 | |
greenroshks | 8:667fbc82d634 | 291 | // Enable or disable the voltage reset alert on the MAX17048 |
greenroshks | 8:667fbc82d634 | 292 | void vResetAlertEnabled(bool enabled) |
greenroshks | 8:667fbc82d634 | 293 | { |
greenroshks | 8:667fbc82d634 | 294 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 295 | unsigned short value = read(REG_STATUS); |
greenroshks | 8:667fbc82d634 | 296 | |
greenroshks | 8:667fbc82d634 | 297 | //Set or clear the EnVR bit |
greenroshks | 8:667fbc82d634 | 298 | if (enabled) |
greenroshks | 8:667fbc82d634 | 299 | value |= (1 << 14); |
greenroshks | 8:667fbc82d634 | 300 | else |
greenroshks | 8:667fbc82d634 | 301 | value &= ~(1 << 14); |
greenroshks | 8:667fbc82d634 | 302 | |
greenroshks | 8:667fbc82d634 | 303 | //Write the value back out |
greenroshks | 8:667fbc82d634 | 304 | write(REG_STATUS, value); |
greenroshks | 8:667fbc82d634 | 305 | } |
greenroshks | 8:667fbc82d634 | 306 | |
greenroshks | 8:667fbc82d634 | 307 | //Get the current alert flags on the MAX17048 |
greenroshks | 8:667fbc82d634 | 308 | //refer datasheet-status registers section to decode it. |
greenroshks | 8:667fbc82d634 | 309 | char alertFlags() |
greenroshks | 8:667fbc82d634 | 310 | { |
greenroshks | 8:667fbc82d634 | 311 | //Read the 16-bit register value |
greenroshks | 8:667fbc82d634 | 312 | unsigned short value = read(REG_STATUS); |
greenroshks | 8:667fbc82d634 | 313 | |
greenroshks | 8:667fbc82d634 | 314 | //Return only the flag bits |
greenroshks | 8:667fbc82d634 | 315 | return (value >> 8) & 0x3F; |
greenroshks | 8:667fbc82d634 | 316 | } |
greenroshks | 8:667fbc82d634 | 317 | |
greenroshks | 8:667fbc82d634 | 318 | // Clear all the alert flags on the MAX17048 |
greenroshks | 8:667fbc82d634 | 319 | void clearAlertFlags() |
greenroshks | 8:667fbc82d634 | 320 | { |
greenroshks | 8:667fbc82d634 | 321 | //Read the current 16-bit register value |
greenroshks | 8:667fbc82d634 | 322 | unsigned short value = read(REG_STATUS); |
greenroshks | 8:667fbc82d634 | 323 | |
greenroshks | 8:667fbc82d634 | 324 | //Clear the specified flag bits |
greenroshks | 8:667fbc82d634 | 325 | value &= ~( 0x3F<< 8); |
greenroshks | 8:667fbc82d634 | 326 | |
greenroshks | 8:667fbc82d634 | 327 | //Write the value back out |
greenroshks | 8:667fbc82d634 | 328 | write(REG_STATUS, value); |
greenroshks | 8:667fbc82d634 | 329 | } |
greenroshks | 8:667fbc82d634 | 330 | |
greenroshks | 8:667fbc82d634 | 331 | // Get the current cell voltage measurement of the MAX17048 |
greenroshks | 8:667fbc82d634 | 332 | float vcell() |
greenroshks | 8:667fbc82d634 | 333 | { |
greenroshks | 8:667fbc82d634 | 334 | //Read the 16-bit raw Vcell value |
greenroshks | 8:667fbc82d634 | 335 | unsigned short value = read(REG_VCELL); |
greenroshks | 8:667fbc82d634 | 336 | |
greenroshks | 8:667fbc82d634 | 337 | //Return Vcell in volts |
greenroshks | 8:667fbc82d634 | 338 | return value * 0.000078125; |
greenroshks | 8:667fbc82d634 | 339 | } |
greenroshks | 8:667fbc82d634 | 340 | |
greenroshks | 8:667fbc82d634 | 341 | // Get the current state of charge measurement of the MAX17048 as a float |
greenroshks | 8:667fbc82d634 | 342 | float soc() |
greenroshks | 8:667fbc82d634 | 343 | { |
greenroshks | 8:667fbc82d634 | 344 | //Read the 16-bit raw SOC value |
greenroshks | 8:667fbc82d634 | 345 | unsigned short value = read(REG_SOC); |
greenroshks | 8:667fbc82d634 | 346 | |
greenroshks | 8:667fbc82d634 | 347 | //Return SOC in percent |
greenroshks | 8:667fbc82d634 | 348 | return value * 0.00390625; |
greenroshks | 8:667fbc82d634 | 349 | } |
greenroshks | 8:667fbc82d634 | 350 | |
greenroshks | 8:667fbc82d634 | 351 | // Get the current state of charge measurement of the MAX17048 as an int |
greenroshks | 8:667fbc82d634 | 352 | int soc_int() |
greenroshks | 8:667fbc82d634 | 353 | { |
greenroshks | 8:667fbc82d634 | 354 | //Read the 16-bit raw SOC value |
greenroshks | 8:667fbc82d634 | 355 | unsigned short value = read(REG_SOC); |
greenroshks | 8:667fbc82d634 | 356 | |
greenroshks | 8:667fbc82d634 | 357 | //Return only the top byte |
greenroshks | 8:667fbc82d634 | 358 | return value >> 8; |
greenroshks | 8:667fbc82d634 | 359 | } |
greenroshks | 8:667fbc82d634 | 360 | |
greenroshks | 8:667fbc82d634 | 361 | // Get the current C rate measurement of the MAX17048 |
greenroshks | 8:667fbc82d634 | 362 | float crate() |
greenroshks | 8:667fbc82d634 | 363 | { |
greenroshks | 8:667fbc82d634 | 364 | //Read the 16-bit raw C/Rate value |
greenroshks | 8:667fbc82d634 | 365 | short value = read(REG_CRATE); |
greenroshks | 8:667fbc82d634 | 366 | |
greenroshks | 8:667fbc82d634 | 367 | //Return C/Rate in %/hr |
greenroshks | 8:667fbc82d634 | 368 | return value * 0.208; |
greenroshks | 8:667fbc82d634 | 369 | } |
greenroshks | 8:667fbc82d634 | 370 | |
greenroshks | 8:667fbc82d634 | 371 | |
greenroshks | 8:667fbc82d634 | 372 | //I2C register addresses |
greenroshks | 8:667fbc82d634 | 373 | enum Register { |
greenroshks | 8:667fbc82d634 | 374 | REG_VCELL = 0x02, |
greenroshks | 8:667fbc82d634 | 375 | REG_SOC = 0x04, |
greenroshks | 8:667fbc82d634 | 376 | REG_MODE = 0x06, |
greenroshks | 8:667fbc82d634 | 377 | REG_VERSION = 0x08, |
greenroshks | 8:667fbc82d634 | 378 | REG_HIBRT = 0x0A, |
greenroshks | 8:667fbc82d634 | 379 | REG_CONFIG = 0x0C, |
greenroshks | 8:667fbc82d634 | 380 | REG_VALRT = 0x14, |
greenroshks | 8:667fbc82d634 | 381 | REG_CRATE = 0x16, |
greenroshks | 8:667fbc82d634 | 382 | REG_VRESET_ID = 0x18, |
greenroshks | 8:667fbc82d634 | 383 | REG_STATUS = 0x1A, |
greenroshks | 8:667fbc82d634 | 384 | REG_TABLE = 0x40, |
greenroshks | 8:667fbc82d634 | 385 | REG_CMD = 0xFE |
greenroshks | 8:667fbc82d634 | 386 | }; |
greenroshks | 8:667fbc82d634 | 387 | |
greenroshks | 8:667fbc82d634 | 388 | //Member constants |
greenroshks | 8:667fbc82d634 | 389 | static const int m_ADDR=(0x36 << 1); |
greenroshks | 8:667fbc82d634 | 390 | |
greenroshks | 8:667fbc82d634 | 391 | //Member variables |
greenroshks | 8:667fbc82d634 | 392 | I2C m_I2C; |
greenroshks | 8:667fbc82d634 | 393 | |
greenroshks | 8:667fbc82d634 | 394 | //Internal functions |
greenroshks | 8:667fbc82d634 | 395 | unsigned short read(char reg) |
greenroshks | 8:667fbc82d634 | 396 | { |
greenroshks | 8:667fbc82d634 | 397 | //Create a temporary buffer |
greenroshks | 8:667fbc82d634 | 398 | char buff[2]; |
greenroshks | 8:667fbc82d634 | 399 | |
greenroshks | 8:667fbc82d634 | 400 | //Select the register |
greenroshks | 8:667fbc82d634 | 401 | m_I2C.write(m_ADDR, ®, 1, true); |
greenroshks | 8:667fbc82d634 | 402 | |
greenroshks | 8:667fbc82d634 | 403 | //Read the 16-bit register |
greenroshks | 8:667fbc82d634 | 404 | m_I2C.read(m_ADDR, buff, 2); |
greenroshks | 8:667fbc82d634 | 405 | |
greenroshks | 8:667fbc82d634 | 406 | //Return the combined 16-bit value |
greenroshks | 8:667fbc82d634 | 407 | return (buff[0] << 8) | buff[1]; |
greenroshks | 8:667fbc82d634 | 408 | } |
greenroshks | 8:667fbc82d634 | 409 | |
greenroshks | 8:667fbc82d634 | 410 | |
greenroshks | 8:667fbc82d634 | 411 | void write(char reg, unsigned short data) |
greenroshks | 8:667fbc82d634 | 412 | { |
greenroshks | 8:667fbc82d634 | 413 | //Create a temporary buffer |
greenroshks | 8:667fbc82d634 | 414 | char buff[3]; |
greenroshks | 8:667fbc82d634 | 415 | |
greenroshks | 8:667fbc82d634 | 416 | //Load the register address and 16-bit data |
greenroshks | 8:667fbc82d634 | 417 | buff[0] = reg; |
greenroshks | 8:667fbc82d634 | 418 | buff[1] = data >> 8; |
greenroshks | 8:667fbc82d634 | 419 | buff[2] = data; |
greenroshks | 8:667fbc82d634 | 420 | |
greenroshks | 8:667fbc82d634 | 421 | //Write the data |
greenroshks | 8:667fbc82d634 | 422 | m_I2C.write(m_ADDR, buff, 3); |
greenroshks | 8:667fbc82d634 | 423 | } |
greenroshks | 8:667fbc82d634 | 424 | }; |
greenroshks | 8:667fbc82d634 | 425 | |
greenroshks | 8:667fbc82d634 | 426 | |
greenroshks | 8:667fbc82d634 | 427 | void FUNC_BATTERYGAUGE_MAIN(float[]); |
greenroshks | 8:667fbc82d634 | 428 | |
greenroshks | 8:667fbc82d634 | 429 |