i2c working with old hk

Dependencies:   mbed-rtos mbed

Fork of BAE_vr2_1_1 by green rosh

Committer:
greenroshks
Date:
Wed Dec 17 05:25:04 2014 +0000
Revision:
13:1b37d98840d3
Parent:
12:ba2556c6b990
Child:
14:ef6be8ac6569
The one with the new HK

Who changed what in which revision?

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