library for MAX9611 /9612 Current-Sense Amplifiers

Committer:
igbt6
Date:
Wed Jan 28 12:00:25 2015 +0000
Revision:
7:012f5b39405e
Parent:
6:32b5eb1df932
Child:
8:1e392bc95666
quick code cleanup done, "how to use"  code snippet added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igbt6 1:131a836c6b79 1 /*
igbt6 5:6fec24c37e2a 2 @file MAX9611.h
igbt6 1:131a836c6b79 3
igbt6 5:6fec24c37e2a 4 @brief MAX9611 High-Side, Current-Sense Amplifiers with
igbt6 5:6fec24c37e2a 5 12-Bit ADC and Op Amp/Comparator- Breakout I2C Library
igbt6 1:131a836c6b79 6
igbt6 1:131a836c6b79 7 @Author lukasz uszko(luszko@op.pl)
igbt6 1:131a836c6b79 8
igbt6 5:6fec24c37e2a 9 Tested on FRDM-KL46Z and FRDM-KL25Z
igbt6 1:131a836c6b79 10
igbt6 1:131a836c6b79 11 Copyright (c) 2014 luszko
igbt6 1:131a836c6b79 12 Released under the MIT License (see http://mbed.org/license/mit)
igbt6 1:131a836c6b79 13
igbt6 5:6fec24c37e2a 14 Documentation regarding the MAX9611 might be found here:
igbt6 5:6fec24c37e2a 15 http://datasheets.maximintegrated.com/en/ds/MAX9611-MAX9612.pdf
igbt6 1:131a836c6b79 16 */
igbt6 1:131a836c6b79 17
igbt6 7:012f5b39405e 18 //DEMO - HOW TO USE:
igbt6 7:012f5b39405e 19 // NOTE before first use, do not forget to set mCsaCurrentValueOffset in constructor, it is a value that you can obtain from method: uint16_t readRawCSAOutValue(void),
igbt6 7:012f5b39405e 20 // when a current value flowing through shunt resistor equals 0.
igbt6 7:012f5b39405e 21 /*
igbt6 7:012f5b39405e 22 ---------------------------------------- DEMO: 1 version ----------------------------------------
igbt6 7:012f5b39405e 23 #include "mbed.h"
igbt6 7:012f5b39405e 24 #include "max9611.h"
igbt6 7:012f5b39405e 25
igbt6 7:012f5b39405e 26 #define MAX9611_PIN_SDA PTC9 // I2C0 //for example
igbt6 7:012f5b39405e 27 #define MAX9611_PIN_SCL PTC8
igbt6 7:012f5b39405e 28 int main()
igbt6 7:012f5b39405e 29 {
igbt6 7:012f5b39405e 30 MAX9611 max9611(MAX9611_PIN_SDA, MAX9611_PIN_SCL);
igbt6 7:012f5b39405e 31 Serial debug(USBTX, USBRX);
igbt6 7:012f5b39405e 32 debug.baud(115200);
igbt6 7:012f5b39405e 33 while(1) {
igbt6 7:012f5b39405e 34 if(!max9611.readCSAOutputValue()){
igbt6 7:012f5b39405e 35 debug.printf("MAX9611_CSA_Reading ERROR!- check all connections\r\n");
igbt6 7:012f5b39405e 36 }
igbt6 7:012f5b39405e 37 else{
igbt6 7:012f5b39405e 38 debug.printf("MAX9611_CSA %5.2f [mA]\r\n", max9611.getCSAOutput());
igbt6 7:012f5b39405e 39 }
igbt6 7:012f5b39405e 40
igbt6 7:012f5b39405e 41 if(!max9611.readTemp()){
igbt6 7:012f5b39405e 42 debug.printf("MAX9611_TEMP_Reading ERROR!- check all connections\r\n");
igbt6 7:012f5b39405e 43 }
igbt6 7:012f5b39405e 44 else{
igbt6 7:012f5b39405e 45 debug.printf("MAX9611_TEMP: %5.2f [C]\r\n", max9611.getTemp());
igbt6 7:012f5b39405e 46 }
igbt6 7:012f5b39405e 47
igbt6 7:012f5b39405e 48
igbt6 7:012f5b39405e 49 }
igbt6 7:012f5b39405e 50 return 0;
igbt6 7:012f5b39405e 51 }
igbt6 7:012f5b39405e 52 */
igbt6 7:012f5b39405e 53
igbt6 7:012f5b39405e 54
igbt6 7:012f5b39405e 55
igbt6 1:131a836c6b79 56
igbt6 1:131a836c6b79 57
igbt6 5:6fec24c37e2a 58 #ifndef MAX9611_H
igbt6 5:6fec24c37e2a 59 #define MAX9611_H
igbt6 1:131a836c6b79 60
igbt6 1:131a836c6b79 61 #include "mbed.h"
igbt6 1:131a836c6b79 62
igbt6 5:6fec24c37e2a 63 #define MAX9611_I2C_ADDRESS 0xE1 //A0 and A1 PIN are conected to GND , Write address 0xE0, Read Address 0xE1
igbt6 1:131a836c6b79 64
igbt6 5:6fec24c37e2a 65
igbt6 5:6fec24c37e2a 66
igbt6 2:d12dffd027a8 67
igbt6 1:131a836c6b79 68
igbt6 1:131a836c6b79 69
igbt6 5:6fec24c37e2a 70 class MAX9611{
igbt6 5:6fec24c37e2a 71
igbt6 5:6fec24c37e2a 72
igbt6 6:32b5eb1df932 73 /**********private members and methods********************************/
igbt6 6:32b5eb1df932 74 private:
igbt6 6:32b5eb1df932 75
igbt6 5:6fec24c37e2a 76 typedef enum {
igbt6 6:32b5eb1df932 77 CHANNEL_A_0=0, /*Read current-sense amplifier output from ADC, gain = 1x*/
igbt6 6:32b5eb1df932 78 CHANNEL_A_1, /*Read current-sense amplifier output from ADC, gain = 4x*/
igbt6 6:32b5eb1df932 79 CHANNEL_A_2, /*Read current-sense amplifier output from ADC, gain = 8x*/
igbt6 6:32b5eb1df932 80 CHANNEL_B, /*Read average voltage of RS+ (input common-mode voltage) from ADC*/
igbt6 6:32b5eb1df932 81 CHANNEL_C, /*Read voltage of OUT from ADC*/
igbt6 6:32b5eb1df932 82 CHANNEL_D, /*Read voltage of SET from ADC*/
igbt6 6:32b5eb1df932 83 CHANNEL_E, /*Read internal die temperature from ADC*/
igbt6 6:32b5eb1df932 84 ALL_CHANNELS /*Read all channels in fast-read mode, sequentially every 2ms. Uses last gain setting.*/
igbt6 5:6fec24c37e2a 85 }eCtrlReg1MUX;
igbt6 5:6fec24c37e2a 86
igbt6 5:6fec24c37e2a 87 typedef enum {
igbt6 6:32b5eb1df932 88 NORMAL_OPERATION_SHDN=0,
igbt6 5:6fec24c37e2a 89 SHUTDOWN_MODE
igbt6 5:6fec24c37e2a 90 }eCtrlReg1SHDN;
igbt6 5:6fec24c37e2a 91
igbt6 5:6fec24c37e2a 92 typedef enum {
igbt6 5:6fec24c37e2a 93 NORMAL_OPERATION_LR=0,
igbt6 5:6fec24c37e2a 94 RESET
igbt6 5:6fec24c37e2a 95 }eCtrlReg1LR;
igbt6 5:6fec24c37e2a 96
igbt6 5:6fec24c37e2a 97 typedef enum {
igbt6 5:6fec24c37e2a 98 NORMAL_OPERATION_MODE=0,
igbt6 5:6fec24c37e2a 99 COMPARATOR_MODE=7,
igbt6 5:6fec24c37e2a 100 OPAMP_MODE=3
igbt6 5:6fec24c37e2a 101 }eCtrlReg1MODE;
igbt6 5:6fec24c37e2a 102
igbt6 5:6fec24c37e2a 103 //watchdog delay time
igbt6 5:6fec24c37e2a 104 typedef enum {
igbt6 5:6fec24c37e2a 105 _1MS=0,
igbt6 5:6fec24c37e2a 106 _100US=1
igbt6 5:6fec24c37e2a 107 }eCtrlReg2DTIM;
igbt6 5:6fec24c37e2a 108
igbt6 5:6fec24c37e2a 109 //watchdog retry delay time
igbt6 5:6fec24c37e2a 110 typedef enum {
igbt6 5:6fec24c37e2a 111 _50MS=0,
igbt6 5:6fec24c37e2a 112 _10MS=1
igbt6 5:6fec24c37e2a 113 }eCtrlReg2RTIM;
igbt6 5:6fec24c37e2a 114
igbt6 5:6fec24c37e2a 115 //watchdog retry delay time
igbt6 5:6fec24c37e2a 116 typedef enum {
igbt6 5:6fec24c37e2a 117 CSA_DATA_BYTE_MSB_ADRR= 0x00,
igbt6 5:6fec24c37e2a 118 CSA_DATA_BYTE_LSB_ADRR= 0x01,
igbt6 5:6fec24c37e2a 119 RS_DATA_BYTE_MSB_ADRR= 0x02,
igbt6 5:6fec24c37e2a 120 RS_DATA_BYTE_LSB_ADRR= 0x03,
igbt6 5:6fec24c37e2a 121 OUT_DATA_BYTE_MSB_ADRR= 0x04,
igbt6 5:6fec24c37e2a 122 OUT_DATA_BYTE_LSB_ADRR= 0x05,
igbt6 5:6fec24c37e2a 123 SET_DATA_BYTE_MSB_ADRR= 0x06,
igbt6 5:6fec24c37e2a 124 SET_DATA_BYTE_LSB_ADRR= 0x07,
igbt6 5:6fec24c37e2a 125 TEMP_DATA_BYTE_MSB_ADRR= 0x08,
igbt6 5:6fec24c37e2a 126 TEMP_DATA_BYTE_LSB_ADRR= 0x09,
igbt6 5:6fec24c37e2a 127 CONTROL_REGISTER_1_ADRR= 0x0A,
igbt6 5:6fec24c37e2a 128 CONTROL_REGISTER_2_ADRR= 0x0B
igbt6 5:6fec24c37e2a 129 }eRegAddresses;
igbt6 6:32b5eb1df932 130
igbt6 6:32b5eb1df932 131
igbt6 5:6fec24c37e2a 132
igbt6 1:131a836c6b79 133 /** Write data to the given register
igbt6 1:131a836c6b79 134 *
igbt6 1:131a836c6b79 135 * @returns
igbt6 1:131a836c6b79 136 * 1 on success,
igbt6 1:131a836c6b79 137 * 0 on error
igbt6 1:131a836c6b79 138 */
igbt6 5:6fec24c37e2a 139 bool write(uint8_t regAddress, uint8_t* data,int dataLength);
igbt6 5:6fec24c37e2a 140
igbt6 2:d12dffd027a8 141 /** Write data to the given register
igbt6 2:d12dffd027a8 142 * @param register Address
igbt6 2:d12dffd027a8 143 * @param data to read
igbt6 2:d12dffd027a8 144 * @param length of data to read
igbt6 2:d12dffd027a8 145 * @returns
igbt6 2:d12dffd027a8 146 * 1 on success,
igbt6 2:d12dffd027a8 147 * 0 on error
igbt6 2:d12dffd027a8 148 */
igbt6 5:6fec24c37e2a 149 bool read(uint8_t regAddress, uint8_t* data,int length);
igbt6 5:6fec24c37e2a 150
igbt6 5:6fec24c37e2a 151
igbt6 5:6fec24c37e2a 152 /** Make 12 bit data from 2 bytes received from thr device data read from Data regiters of Max9611/9612 are laid in the following way :
igbt6 5:6fec24c37e2a 153 * Byte 1: bit7-MSB12........bit0-MSB05 ; Byte 2: bit7-LSB04.... bit4-LSB00
igbt6 5:6fec24c37e2a 154 * @param MSB byte
igbt6 5:6fec24c37e2a 155 * @param 4 bits of LSB bytes
igbt6 5:6fec24c37e2a 156 * @returns 1 2bit data
igbt6 5:6fec24c37e2a 157 *
igbt6 5:6fec24c37e2a 158 */
igbt6 5:6fec24c37e2a 159 inline uint16_t get12BitData(uint8_t msbByte,uint8_t lsbByte){
igbt6 5:6fec24c37e2a 160 uint16_t data12Bit= (msbByte<<4)|((lsbByte>>4)&0x0F);
igbt6 5:6fec24c37e2a 161 return data12Bit;
igbt6 5:6fec24c37e2a 162 }
igbt6 5:6fec24c37e2a 163
igbt6 5:6fec24c37e2a 164
igbt6 5:6fec24c37e2a 165 inline uint16_t get9BitData(uint8_t msbByte,uint8_t lsbByte){
igbt6 5:6fec24c37e2a 166 uint16_t data9Bit= (msbByte<<1)|((lsbByte>>6)&0x01);
igbt6 5:6fec24c37e2a 167 return data9Bit;
igbt6 6:32b5eb1df932 168
igbt6 6:32b5eb1df932 169
igbt6 6:32b5eb1df932 170
igbt6 5:6fec24c37e2a 171 }
igbt6 5:6fec24c37e2a 172
igbt6 6:32b5eb1df932 173 /**********protected methods********************************/
igbt6 6:32b5eb1df932 174 protected:
igbt6 6:32b5eb1df932 175
igbt6 6:32b5eb1df932 176 I2C mI2c;
igbt6 6:32b5eb1df932 177 int mI2cAddr;
igbt6 6:32b5eb1df932 178 float mTemperature;
igbt6 6:32b5eb1df932 179 float mCurrentSenseAmplifierOutput;
igbt6 7:012f5b39405e 180 uint16_t mCsaCurrentValueOffset; //this parameter depends on your sensor
igbt6 6:32b5eb1df932 181
igbt6 6:32b5eb1df932 182
igbt6 6:32b5eb1df932 183 /**********public methods********************************/
igbt6 6:32b5eb1df932 184 public:
igbt6 6:32b5eb1df932 185
igbt6 6:32b5eb1df932 186 /** Create an MAX9611 instance
igbt6 6:32b5eb1df932 187 * @param sda pin
igbt6 6:32b5eb1df932 188 * @param scl pin
igbt6 6:32b5eb1df932 189 * @param address: I2C slave address
igbt6 6:32b5eb1df932 190 */
igbt6 6:32b5eb1df932 191 MAX9611(PinName sda, PinName scl,int i2cFrequency=100000,int address = MAX9611_I2C_ADDRESS);
igbt6 6:32b5eb1df932 192
igbt6 6:32b5eb1df932 193
igbt6 6:32b5eb1df932 194 /** Create a MAX9611 instance
igbt6 6:32b5eb1df932 195 * @param i2c object
igbt6 6:32b5eb1df932 196 * @param address: I2C slave address
igbt6 6:32b5eb1df932 197 */
igbt6 6:32b5eb1df932 198 MAX9611(I2C& i2c, int address = MAX9611_I2C_ADDRESS);
igbt6 6:32b5eb1df932 199
igbt6 6:32b5eb1df932 200
igbt6 6:32b5eb1df932 201 /** Initialization: set member values and configuration registers, ought to be invoked in the body of constructor
igbt6 6:32b5eb1df932 202 * @returns
igbt6 6:32b5eb1df932 203 * true on success,
igbt6 6:32b5eb1df932 204 * false on error
igbt6 6:32b5eb1df932 205 */
igbt6 6:32b5eb1df932 206 bool initMax9611(eCtrlReg1MUX mux= CHANNEL_A_1,
igbt6 6:32b5eb1df932 207 eCtrlReg1SHDN shdn= NORMAL_OPERATION_SHDN,
igbt6 6:32b5eb1df932 208 eCtrlReg1LR lr=NORMAL_OPERATION_LR,
igbt6 6:32b5eb1df932 209 eCtrlReg1MODE mode= NORMAL_OPERATION_MODE,
igbt6 6:32b5eb1df932 210 eCtrlReg2DTIM watchdogDelay= _1MS,
igbt6 6:32b5eb1df932 211 eCtrlReg2RTIM watchdogRetryDelay=_50MS);
igbt6 6:32b5eb1df932 212
igbt6 6:32b5eb1df932 213
igbt6 6:32b5eb1df932 214 /** Read temperature from the MAX9611.
igbt6 6:32b5eb1df932 215 * @param none
igbt6 6:32b5eb1df932 216 * @returns
igbt6 6:32b5eb1df932 217 * 1 on success,
igbt6 6:32b5eb1df932 218 * 0 on error
igbt6 6:32b5eb1df932 219 */
igbt6 6:32b5eb1df932 220 bool readTemp(void);
igbt6 5:6fec24c37e2a 221
igbt6 1:131a836c6b79 222
igbt6 6:32b5eb1df932 223 /** Get temperature from the last measurement
igbt6 6:32b5eb1df932 224 *
igbt6 6:32b5eb1df932 225 * @returns
igbt6 6:32b5eb1df932 226 * temperature (C)
igbt6 6:32b5eb1df932 227 */
igbt6 6:32b5eb1df932 228 inline float getTemp(void) {return mTemperature;}
igbt6 6:32b5eb1df932 229
igbt6 6:32b5eb1df932 230
igbt6 6:32b5eb1df932 231 /** Read CSA output value from the MAX9611.
igbt6 6:32b5eb1df932 232 * @param none
igbt6 6:32b5eb1df932 233 * @returns
igbt6 6:32b5eb1df932 234 * 1 on success,
igbt6 6:32b5eb1df932 235 * 0 on error
igbt6 6:32b5eb1df932 236 */
igbt6 6:32b5eb1df932 237 bool readCSAOutputValue(void);
igbt6 6:32b5eb1df932 238
igbt6 6:32b5eb1df932 239
igbt6 6:32b5eb1df932 240 /** Get value of CSA output from the last measurement
igbt6 6:32b5eb1df932 241 *
igbt6 6:32b5eb1df932 242 * @returns
igbt6 7:012f5b39405e 243 * Current Value [mA]
igbt6 6:32b5eb1df932 244 */
igbt6 6:32b5eb1df932 245 inline float getCSAOutput(void) {return mCurrentSenseAmplifierOutput;}
igbt6 6:32b5eb1df932 246
igbt6 6:32b5eb1df932 247 //DEBUG
igbt6 7:012f5b39405e 248 uint16_t mRawInt;
igbt6 7:012f5b39405e 249 uint16_t readRawControl(void);
igbt6 7:012f5b39405e 250 uint16_t readRawCSAOutValue(void);
igbt6 7:012f5b39405e 251 uint16_t readRawRsValue(void);
igbt6 7:012f5b39405e 252 uint16_t readRawOutValue(void);
igbt6 6:32b5eb1df932 253
igbt6 6:32b5eb1df932 254
igbt6 1:131a836c6b79 255
igbt6 1:131a836c6b79 256 };
igbt6 1:131a836c6b79 257
igbt6 1:131a836c6b79 258 #endif