A compilation of some hardware sensors and their shared programming interfaces.
Embed:
(wiki syntax)
Show/hide line numbers
INA219.h
00001 /* INA219.h 00002 * Tested with mbed board: FRDM-KL46Z 00003 * Author: Mark Gottscho 00004 * mgottscho@ucla.edu 00005 */ 00006 00007 #ifndef INA219_H 00008 #define INA219_H 00009 00010 #include "mbed.h" 00011 #include "I2CSensor.h" 00012 #include "PeriodicSensor.h" 00013 00014 /** 00015 * This class allows for easy control over a INA219 current/power sensing IC. 00016 */ 00017 class INA219 : public I2CSensor, public PeriodicSensor { 00018 public: 00019 /** 00020 * @param sda the pin identifier for SDA I2C signal 00021 * @param scl the pin identifier for SCL I2C signal 00022 * @param i2c_addr the 8-bit I2C address for this device. Note that LSB is a don't care. 00023 */ 00024 INA219 (PinName sda, PinName scl, int i2c_addr); 00025 00026 /** 00027 * Destroys this object 00028 */ 00029 ~INA219(); 00030 00031 /** 00032 * Initializes the device to some nice state. 00033 */ 00034 void selfInit(); 00035 00036 /** 00037 * Performs a software reset of the device. 00038 */ 00039 void reset(); 00040 00041 /** 00042 * Sets the bus voltage range to either 16V or 32V. 00043 * @param enable if true, sets the bus voltage range to 32V. Else, sets it to 16V. 00044 */ 00045 void setBusVoltageRange32V(bool enable); 00046 00047 /** 00048 * @returns true if the bus voltage range is set to 32V. if false, bus voltage range is 16V. 00049 */ 00050 bool isBusVoltageRange32V (); 00051 00052 /** 00053 * Sets the PGA amplifier gain for shunt voltage measurement. 00054 * @param gain the gain level to set. Allowed values: 1, 2, 4, 8. Default is 8. Any other values will have no effect. 00055 */ 00056 void setShuntAmpGain(unsigned int gain); 00057 00058 /** 00059 * Gets the PGA amplifier gain for shunt voltage measurement. 00060 * @returns the gain level 00061 */ 00062 unsigned int getShuntAmpGain(); 00063 00064 /** 00065 * Sets the resolution and sample averaging of the bus or shunt ADC. 00066 * If resolution is set, sample averaging is disabled. 00067 * If sample averaging is set, resolution setting is disabled. 00068 * @param shunt if true, sets for shunt ADC. if false, sets for bus ADC. 00069 * @param resolution if true, sets ADC resolution using the third parameter. if false, sets the ADC sample averaging 00070 * using the third parameter. 00071 * @param value if resolution is true, then this is the resolution in bits to use. Allowed values: 00072 * 9, 10, 11, or 12 bits. 00073 * If resolution is false, then this is the number of samples to average in each data sample. Allowed values: 00074 * 2, 4, 8, 16, 32, 64, or 128 samples. 00075 * Any other values will cause this method to have no effect. Default is 12-bit resolution mode. 00076 */ 00077 void setADCResolutionAndAveraging(bool shunt, bool resolution, unsigned int value); 00078 00079 /** 00080 * Gets the resolution or number of samples in an average of the bus or shunt ADC. 00081 * @param shunt if true, gets for shunt ADC. if false, gets for bus ADC. 00082 * @param resolution sets this to true if the return value represents resolution in bits. else, sets it to false if return 00083 * value represents sample averaging in # samples. 00084 */ 00085 unsigned int getADCResolutionAndAveraging(bool shunt, bool &resolution); 00086 00087 /** 00088 * Sets the operating mode of the device. If all parameters are false, the device powers down. 00089 * If shuntVoltage and busVoltage are false but continuous is true, the device remains on but 00090 * the ADC is powered down. 00091 * @param shuntVoltage if true, samples the shunt voltage. 00092 * @param busVoltage if true, samples the bus voltage. 00093 * @param continuous if true, samples continuously, otherwise only on triggered reads. 00094 */ 00095 void setMode(bool shuntVoltage, bool busVoltage, bool continuous); 00096 00097 /** 00098 * Gets the operating mode of the device. If all parameters are false, the device is powered down. 00099 * If shuntVoltage and busVoltage are false but continuous is true, the device is on but 00100 * the ADC is powered down. 00101 * @param shuntVoltage sets true if shunt voltage is being measured. 00102 * @param busVoltage sets true if bus voltage is being measured. 00103 * @param continuous sets true if continuous readings are being done. 00104 */ 00105 void getMode(bool &shuntVoltage, bool &busVoltage, bool &continuous); 00106 00107 /** 00108 * Gets the shunt voltage across the current sensing resistor. 00109 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00110 * The latter is preferred if this object is set up to sample using interrupts. 00111 * @returns 16-bit integer representing the shunt voltage. 00112 */ 00113 int16_t getShuntVoltage(bool sampleNow); 00114 00115 /** 00116 * Gets the shunt voltage across the current sensing resistor. 00117 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00118 * The latter is preferred if this object is set up to sample using interrupts. 00119 * @returns the shunt voltage in Volts 00120 */ 00121 float getShuntVoltageFloat(bool sampleNow); 00122 00123 00124 /** 00125 * Gets the bus voltage at the negative terminal of the current sensing resistor (V-). 00126 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00127 * The latter is preferred if this object is set up to sample using interrupts. 00128 * @returns 16-bit integer representing the bus voltage. 00129 */ 00130 int16_t getBusVoltage(bool sampleNow); 00131 00132 /** 00133 * Gets the bus voltage across the current sensing resistor. 00134 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00135 * The latter is preferred if this object is set up to sample using interrupts. 00136 * @returns the bus voltage in Volts 00137 */ 00138 float getBusVoltageFloat(bool sampleNow); 00139 00140 /** 00141 * Gets the power consumed by the load. Note that calling this method with sampleNow == true 00142 * will also implicitly sample current as well, which can be retrieved with a call to 00143 * getCurrent(false) after calling getPower(true). 00144 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00145 * The latter is preferred if this object is set up to sample using interrupts. 00146 * @returns 16-bit integer representing power consumption of the load. 00147 */ 00148 int16_t getPower(bool sampleNow); 00149 00150 /** 00151 * Gets the power consumed by the load. Note that calling this method with sampleNow == true 00152 * will also implicitly sample current as well, which can be retrieved with a call to 00153 * getCurrentFloat(false) after calling getPowerFloat(true). 00154 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00155 * The latter is preferred if this object is set up to sample using interrupts. 00156 * @returns the load power in Watts 00157 */ 00158 float getPowerFloat(bool sampleNow); 00159 00160 /** 00161 * Gets the current sunk by the load. 00162 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00163 * The latter is preferred if this object is set up to sample using interrupts. 00164 * @returns 16-bit integer representing current sunk by the load. 00165 */ 00166 int16_t getCurrent(bool sampleNow); 00167 00168 /** 00169 * Gets the current delivered to the load 00170 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value. 00171 * The latter is preferred if this object is set up to sample using interrupts. 00172 * @returns the load current in Amperes 00173 */ 00174 float getCurrentFloat(bool sampleNow); 00175 00176 /** 00177 * Gets the calibration configuration setting for current range. 00178 * @returns 16-bit unsigned integer representing calibration register value 00179 */ 00180 uint16_t getCalibration(); 00181 00182 private: 00183 /** 00184 * Interrupt service routine for fetching power data from the device. 00185 */ 00186 virtual void __sample_data_ISR(); 00187 00188 /** 00189 * Sets the calibration register of the device so that current and power measurements can be made. NOTE: This is a custom method for my project. 00190 */ 00191 void __calibrate(); 00192 00193 /////////////////////// VARIABLES ////////////////////////// 00194 bool __bus_voltage_range_32V; 00195 unsigned int __shunt_amp_gain; 00196 unsigned int __shunt_resolution; 00197 unsigned int __shunt_num_samples_in_average; 00198 unsigned int __bus_resolution; 00199 unsigned int __bus_num_samples_in_average; 00200 bool __active; 00201 bool __measure_shunt_voltage; 00202 bool __measure_bus_voltage; 00203 bool __continuous_measurements; 00204 00205 volatile int16_t __shunt_voltage; 00206 volatile int16_t __bus_voltage; 00207 volatile int16_t __power; 00208 volatile int16_t __shunt_current; 00209 volatile uint16_t __calibration; 00210 00211 float __current_div; //Amps/level 00212 float __power_div; //Watts/level 00213 00214 00215 00216 /////////////////////// CONSTANTS ////////////////////////// 00217 00218 //Device register addresses 00219 const static uint8_t CONFIG = 0x00; 00220 const static uint8_t SHUNT_VOLTAGE = 0x01; 00221 const static uint8_t BUS_VOLTAGE = 0x02; 00222 const static uint8_t POWER = 0x03; 00223 const static uint8_t CURRENT = 0x04; 00224 const static uint8_t CALIBRATION = 0x05; 00225 00226 //Register masks 00227 const static uint16_t CONFIG_RST_MASK = 0x8000; //b1000 0000 0000 0000 00228 const static uint16_t CONFIG_BRNG_MASK = 0x2000; //b0010 0000 0000 0000 00229 const static uint16_t CONFIG_PG_MASK = 0x1800; //b0001 1000 0000 0000 00230 const static uint16_t CONFIG_BADC_MASK = 0x0780; //b0000 0111 1000 0000 00231 const static uint16_t CONFIG_SADC_MASK = 0x0078; //b0000 0000 0111 1000 00232 const static uint16_t CONFIG_MODE_MASK = 0x0007; //b0000 0000 0000 0111 00233 00234 //No masks needed for SHUNT_VOLTAGE register 00235 00236 const static uint16_t BUS_VOLTAGE_BD_MASK = 0xFFF8; //b1111 1111 1111 1000 00237 const static uint16_t BUS_VOLTAGE_CNVR_MASK = 0x0002; //b0000 0000 0000 0010 00238 const static uint16_t BUS_VOLTAGE_OVF_MASK = 0x0001; //b0000 0000 0000 0001 00239 00240 //No masks needed for POWER register 00241 00242 const static uint16_t CURRENT_CSIGN_MASK = 0x8000; //b1000 0000 0000 0000 00243 const static uint16_t CURRENT_CD_MASK = 0x7FFF; //b0111 1111 1111 1111 00244 00245 const static uint16_t CALIBRATION_FS_MASK = 0xFFFE; //b1111 1111 1111 1110 00246 00247 const static float SHUNT_VOLTAGE_DIV = 0.00001; //10uV / level 00248 const static float BUS_VOLTAGE_DIV = 0.004; //4mV / level 00249 }; 00250 00251 #endif
Generated on Wed Jul 13 2022 19:24:41 by 1.7.2