library for MAX9611 /9612 Current-Sense Amplifiers
max9611.h@5:6fec24c37e2a, 2014-11-08 (annotated)
- Committer:
- igbt6
- Date:
- Sat Nov 08 20:45:21 2014 +0000
- Revision:
- 5:6fec24c37e2a
- Parent:
- adt7410.h@4:89644cedc8c8
- Child:
- 6:32b5eb1df932
windmill tests 1st commit
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:131a836c6b79 | 18 | |
igbt6 | 1:131a836c6b79 | 19 | |
igbt6 | 5:6fec24c37e2a | 20 | #ifndef MAX9611_H |
igbt6 | 5:6fec24c37e2a | 21 | #define MAX9611_H |
igbt6 | 1:131a836c6b79 | 22 | |
igbt6 | 1:131a836c6b79 | 23 | #include "mbed.h" |
igbt6 | 1:131a836c6b79 | 24 | |
igbt6 | 5:6fec24c37e2a | 25 | #define MAX9611_I2C_ADDRESS 0xE1 //A0 and A1 PIN are conected to GND , Write address 0xE0, Read Address 0xE1 |
igbt6 | 1:131a836c6b79 | 26 | |
igbt6 | 5:6fec24c37e2a | 27 | |
igbt6 | 5:6fec24c37e2a | 28 | |
igbt6 | 2:d12dffd027a8 | 29 | |
igbt6 | 1:131a836c6b79 | 30 | |
igbt6 | 1:131a836c6b79 | 31 | |
igbt6 | 5:6fec24c37e2a | 32 | class MAX9611{ |
igbt6 | 5:6fec24c37e2a | 33 | |
igbt6 | 5:6fec24c37e2a | 34 | private: |
igbt6 | 5:6fec24c37e2a | 35 | |
igbt6 | 5:6fec24c37e2a | 36 | typedef struct{ |
igbt6 | 5:6fec24c37e2a | 37 | |
igbt6 | 5:6fec24c37e2a | 38 | |
igbt6 | 5:6fec24c37e2a | 39 | |
igbt6 | 5:6fec24c37e2a | 40 | }ControlReg1; |
igbt6 | 5:6fec24c37e2a | 41 | |
igbt6 | 5:6fec24c37e2a | 42 | typedef enum { |
igbt6 | 5:6fec24c37e2a | 43 | CHANNEL_A_0=0, |
igbt6 | 5:6fec24c37e2a | 44 | CHANNEL_A_1, |
igbt6 | 5:6fec24c37e2a | 45 | CHANNEL_A_2, |
igbt6 | 5:6fec24c37e2a | 46 | CHANNEL_B, |
igbt6 | 5:6fec24c37e2a | 47 | CHANNEL_C, |
igbt6 | 5:6fec24c37e2a | 48 | CHANNEL_D, |
igbt6 | 5:6fec24c37e2a | 49 | CHANNEL_E, |
igbt6 | 5:6fec24c37e2a | 50 | ALL_CHANNELS |
igbt6 | 5:6fec24c37e2a | 51 | }eCtrlReg1MUX; |
igbt6 | 5:6fec24c37e2a | 52 | |
igbt6 | 5:6fec24c37e2a | 53 | typedef enum { |
igbt6 | 5:6fec24c37e2a | 54 | NORMAL_OPERATION_SHDN=0, |
igbt6 | 5:6fec24c37e2a | 55 | SHUTDOWN_MODE |
igbt6 | 5:6fec24c37e2a | 56 | }eCtrlReg1SHDN; |
igbt6 | 5:6fec24c37e2a | 57 | |
igbt6 | 5:6fec24c37e2a | 58 | typedef enum { |
igbt6 | 5:6fec24c37e2a | 59 | NORMAL_OPERATION_LR=0, |
igbt6 | 5:6fec24c37e2a | 60 | RESET |
igbt6 | 5:6fec24c37e2a | 61 | }eCtrlReg1LR; |
igbt6 | 5:6fec24c37e2a | 62 | |
igbt6 | 5:6fec24c37e2a | 63 | typedef enum { |
igbt6 | 5:6fec24c37e2a | 64 | NORMAL_OPERATION_MODE=0, |
igbt6 | 5:6fec24c37e2a | 65 | COMPARATOR_MODE=7, |
igbt6 | 5:6fec24c37e2a | 66 | OPAMP_MODE=3 |
igbt6 | 5:6fec24c37e2a | 67 | }eCtrlReg1MODE; |
igbt6 | 5:6fec24c37e2a | 68 | |
igbt6 | 5:6fec24c37e2a | 69 | //watchdog delay time |
igbt6 | 5:6fec24c37e2a | 70 | typedef enum { |
igbt6 | 5:6fec24c37e2a | 71 | _1MS=0, |
igbt6 | 5:6fec24c37e2a | 72 | _100US=1 |
igbt6 | 5:6fec24c37e2a | 73 | }eCtrlReg2DTIM; |
igbt6 | 5:6fec24c37e2a | 74 | |
igbt6 | 5:6fec24c37e2a | 75 | //watchdog retry delay time |
igbt6 | 5:6fec24c37e2a | 76 | typedef enum { |
igbt6 | 5:6fec24c37e2a | 77 | _50MS=0, |
igbt6 | 5:6fec24c37e2a | 78 | _10MS=1 |
igbt6 | 5:6fec24c37e2a | 79 | }eCtrlReg2RTIM; |
igbt6 | 5:6fec24c37e2a | 80 | |
igbt6 | 5:6fec24c37e2a | 81 | //watchdog retry delay time |
igbt6 | 5:6fec24c37e2a | 82 | typedef enum { |
igbt6 | 5:6fec24c37e2a | 83 | CSA_DATA_BYTE_MSB_ADRR= 0x00, |
igbt6 | 5:6fec24c37e2a | 84 | CSA_DATA_BYTE_LSB_ADRR= 0x01, |
igbt6 | 5:6fec24c37e2a | 85 | RS_DATA_BYTE_MSB_ADRR= 0x02, |
igbt6 | 5:6fec24c37e2a | 86 | RS_DATA_BYTE_LSB_ADRR= 0x03, |
igbt6 | 5:6fec24c37e2a | 87 | OUT_DATA_BYTE_MSB_ADRR= 0x04, |
igbt6 | 5:6fec24c37e2a | 88 | OUT_DATA_BYTE_LSB_ADRR= 0x05, |
igbt6 | 5:6fec24c37e2a | 89 | SET_DATA_BYTE_MSB_ADRR= 0x06, |
igbt6 | 5:6fec24c37e2a | 90 | SET_DATA_BYTE_LSB_ADRR= 0x07, |
igbt6 | 5:6fec24c37e2a | 91 | TEMP_DATA_BYTE_MSB_ADRR= 0x08, |
igbt6 | 5:6fec24c37e2a | 92 | TEMP_DATA_BYTE_LSB_ADRR= 0x09, |
igbt6 | 5:6fec24c37e2a | 93 | CONTROL_REGISTER_1_ADRR= 0x0A, |
igbt6 | 5:6fec24c37e2a | 94 | CONTROL_REGISTER_2_ADRR= 0x0B |
igbt6 | 5:6fec24c37e2a | 95 | }eRegAddresses; |
igbt6 | 5:6fec24c37e2a | 96 | |
igbt6 | 5:6fec24c37e2a | 97 | |
igbt6 | 1:131a836c6b79 | 98 | public: |
igbt6 | 1:131a836c6b79 | 99 | |
igbt6 | 5:6fec24c37e2a | 100 | /** Create an MAX9611 instance |
igbt6 | 1:131a836c6b79 | 101 | * @param sda pin |
igbt6 | 1:131a836c6b79 | 102 | * @param scl pin |
igbt6 | 1:131a836c6b79 | 103 | * @param address: I2C slave address |
igbt6 | 1:131a836c6b79 | 104 | */ |
igbt6 | 5:6fec24c37e2a | 105 | MAX9611(PinName sda, PinName scl,int i2cFrequency=100000,int address = MAX9611_I2C_ADDRESS); |
igbt6 | 1:131a836c6b79 | 106 | |
igbt6 | 5:6fec24c37e2a | 107 | /** Create a MAX9611 instance |
igbt6 | 1:131a836c6b79 | 108 | * @param i2c object |
igbt6 | 1:131a836c6b79 | 109 | * @param address: I2C slave address |
igbt6 | 1:131a836c6b79 | 110 | */ |
igbt6 | 5:6fec24c37e2a | 111 | MAX9611(I2C& i2c, int address = MAX9611_I2C_ADDRESS); |
igbt6 | 1:131a836c6b79 | 112 | |
igbt6 | 2:d12dffd027a8 | 113 | /** Initialization: set member values and |
igbt6 | 1:131a836c6b79 | 114 | * @returns |
igbt6 | 5:6fec24c37e2a | 115 | * true on success, |
igbt6 | 5:6fec24c37e2a | 116 | * false on error |
igbt6 | 1:131a836c6b79 | 117 | */ |
igbt6 | 5:6fec24c37e2a | 118 | bool initMax9611(eCtrlReg1MUX mux= ALL_CHANNELS, |
igbt6 | 5:6fec24c37e2a | 119 | eCtrlReg1SHDN shdn= NORMAL_OPERATION_SHDN, |
igbt6 | 5:6fec24c37e2a | 120 | eCtrlReg1LR lr=NORMAL_OPERATION_LR, |
igbt6 | 5:6fec24c37e2a | 121 | eCtrlReg1MODE mode=NORMAL_OPERATION_MODE, |
igbt6 | 5:6fec24c37e2a | 122 | eCtrlReg2DTIM watchdogDelay= _1MS, |
igbt6 | 5:6fec24c37e2a | 123 | eCtrlReg2RTIM watchdogRetryDelay=_50MS); |
igbt6 | 1:131a836c6b79 | 124 | |
igbt6 | 5:6fec24c37e2a | 125 | /** Read temperature from the MAX9611. |
igbt6 | 1:131a836c6b79 | 126 | * @param temperature (C) |
igbt6 | 1:131a836c6b79 | 127 | * @returns |
igbt6 | 1:131a836c6b79 | 128 | * 1 on success, |
igbt6 | 1:131a836c6b79 | 129 | * 0 on error |
igbt6 | 1:131a836c6b79 | 130 | */ |
igbt6 | 5:6fec24c37e2a | 131 | bool readTemp(); |
igbt6 | 3:41839eb9229f | 132 | |
igbt6 | 5:6fec24c37e2a | 133 | /** Get temperature from the last measurement |
igbt6 | 1:131a836c6b79 | 134 | * |
igbt6 | 1:131a836c6b79 | 135 | * @returns |
igbt6 | 1:131a836c6b79 | 136 | * temperature (C) |
igbt6 | 1:131a836c6b79 | 137 | */ |
igbt6 | 5:6fec24c37e2a | 138 | inline float getTemp(void) {return mTemperature;} |
igbt6 | 1:131a836c6b79 | 139 | |
igbt6 | 1:131a836c6b79 | 140 | protected: |
igbt6 | 5:6fec24c37e2a | 141 | |
igbt6 | 2:d12dffd027a8 | 142 | I2C i2c; |
igbt6 | 2:d12dffd027a8 | 143 | int i2cAddr; |
igbt6 | 2:d12dffd027a8 | 144 | |
igbt6 | 1:131a836c6b79 | 145 | private: |
igbt6 | 5:6fec24c37e2a | 146 | |
igbt6 | 5:6fec24c37e2a | 147 | float mTemperature; |
igbt6 | 5:6fec24c37e2a | 148 | |
igbt6 | 1:131a836c6b79 | 149 | /** Write data to the given register |
igbt6 | 1:131a836c6b79 | 150 | * |
igbt6 | 1:131a836c6b79 | 151 | * @returns |
igbt6 | 1:131a836c6b79 | 152 | * 1 on success, |
igbt6 | 1:131a836c6b79 | 153 | * 0 on error |
igbt6 | 1:131a836c6b79 | 154 | */ |
igbt6 | 5:6fec24c37e2a | 155 | bool write(uint8_t regAddress, uint8_t* data,int dataLength); |
igbt6 | 5:6fec24c37e2a | 156 | |
igbt6 | 2:d12dffd027a8 | 157 | /** Write data to the given register |
igbt6 | 2:d12dffd027a8 | 158 | * @param register Address |
igbt6 | 2:d12dffd027a8 | 159 | * @param data to read |
igbt6 | 2:d12dffd027a8 | 160 | * @param length of data to read |
igbt6 | 2:d12dffd027a8 | 161 | * @returns |
igbt6 | 2:d12dffd027a8 | 162 | * 1 on success, |
igbt6 | 2:d12dffd027a8 | 163 | * 0 on error |
igbt6 | 2:d12dffd027a8 | 164 | */ |
igbt6 | 5:6fec24c37e2a | 165 | bool read(uint8_t regAddress, uint8_t* data,int length); |
igbt6 | 5:6fec24c37e2a | 166 | |
igbt6 | 5:6fec24c37e2a | 167 | |
igbt6 | 5:6fec24c37e2a | 168 | /** 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 | 169 | * Byte 1: bit7-MSB12........bit0-MSB05 ; Byte 2: bit7-LSB04.... bit4-LSB00 |
igbt6 | 5:6fec24c37e2a | 170 | * @param MSB byte |
igbt6 | 5:6fec24c37e2a | 171 | * @param 4 bits of LSB bytes |
igbt6 | 5:6fec24c37e2a | 172 | * @returns 1 2bit data |
igbt6 | 5:6fec24c37e2a | 173 | * |
igbt6 | 5:6fec24c37e2a | 174 | */ |
igbt6 | 5:6fec24c37e2a | 175 | inline uint16_t get12BitData(uint8_t msbByte,uint8_t lsbByte){ |
igbt6 | 5:6fec24c37e2a | 176 | uint16_t data12Bit= (msbByte<<4)|((lsbByte>>4)&0x0F); |
igbt6 | 5:6fec24c37e2a | 177 | return data12Bit; |
igbt6 | 5:6fec24c37e2a | 178 | } |
igbt6 | 5:6fec24c37e2a | 179 | |
igbt6 | 5:6fec24c37e2a | 180 | |
igbt6 | 5:6fec24c37e2a | 181 | inline uint16_t get9BitData(uint8_t msbByte,uint8_t lsbByte){ |
igbt6 | 5:6fec24c37e2a | 182 | uint16_t data9Bit= (msbByte<<1)|((lsbByte>>6)&0x01); |
igbt6 | 5:6fec24c37e2a | 183 | return data9Bit; |
igbt6 | 5:6fec24c37e2a | 184 | } |
igbt6 | 5:6fec24c37e2a | 185 | |
igbt6 | 5:6fec24c37e2a | 186 | |
igbt6 | 1:131a836c6b79 | 187 | |
igbt6 | 1:131a836c6b79 | 188 | |
igbt6 | 1:131a836c6b79 | 189 | |
igbt6 | 1:131a836c6b79 | 190 | |
igbt6 | 1:131a836c6b79 | 191 | }; |
igbt6 | 1:131a836c6b79 | 192 | |
igbt6 | 1:131a836c6b79 | 193 | #endif |