library for MAX9611 /9612 Current-Sense Amplifiers
max9611.cpp@7:012f5b39405e, 2015-01-28 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
igbt6 | 5:6fec24c37e2a | 1 | #include "max9611.h" |
igbt6 | 2:d12dffd027a8 | 2 | |
igbt6 | 2:d12dffd027a8 | 3 | |
igbt6 | 1:131a836c6b79 | 4 | |
igbt6 | 1:131a836c6b79 | 5 | |
igbt6 | 6:32b5eb1df932 | 6 | MAX9611::MAX9611(PinName sda, PinName scl, int i2cFrequencyHz, int address):mI2c(sda,scl), mI2cAddr(address) |
igbt6 | 5:6fec24c37e2a | 7 | { |
igbt6 | 6:32b5eb1df932 | 8 | mI2c.frequency(i2cFrequencyHz); |
igbt6 | 7:012f5b39405e | 9 | if(!initMax9611()); //while(1){ //TODO handle error} |
igbt6 | 7:012f5b39405e | 10 | mTemperature=0; |
igbt6 | 7:012f5b39405e | 11 | mCsaCurrentValueOffset=0x0a; //NOTE! set this parameter on your own, it depends on your used sensor |
igbt6 | 1:131a836c6b79 | 12 | } |
igbt6 | 1:131a836c6b79 | 13 | |
igbt6 | 1:131a836c6b79 | 14 | |
igbt6 | 1:131a836c6b79 | 15 | |
igbt6 | 5:6fec24c37e2a | 16 | //write data to the sensor |
igbt6 | 5:6fec24c37e2a | 17 | bool MAX9611::write(uint8_t regAddress, uint8_t* data,int dataLength) |
igbt6 | 5:6fec24c37e2a | 18 | { |
igbt6 | 5:6fec24c37e2a | 19 | uint8_t tempBuf[dataLength+1]; |
igbt6 | 5:6fec24c37e2a | 20 | tempBuf[0]=regAddress; |
igbt6 | 6:32b5eb1df932 | 21 | memcpy(&(tempBuf[1]),data,dataLength); |
igbt6 | 6:32b5eb1df932 | 22 | return mI2c.write(mI2cAddr,(char*)tempBuf,dataLength+1)==0; |
igbt6 | 6:32b5eb1df932 | 23 | |
igbt6 | 5:6fec24c37e2a | 24 | } |
igbt6 | 5:6fec24c37e2a | 25 | |
igbt6 | 3:41839eb9229f | 26 | //read data from the sensor |
igbt6 | 5:6fec24c37e2a | 27 | bool MAX9611::read(uint8_t regAddress, uint8_t *data,int dataLength) |
igbt6 | 5:6fec24c37e2a | 28 | { |
igbt6 | 6:32b5eb1df932 | 29 | mI2c.write(mI2cAddr,(char*)®Address,1,true); |
igbt6 | 6:32b5eb1df932 | 30 | return (mI2c.read(mI2cAddr,(char*)data,dataLength)==0); |
igbt6 | 2:d12dffd027a8 | 31 | } |
igbt6 | 2:d12dffd027a8 | 32 | |
igbt6 | 7:012f5b39405e | 33 | //configuration of MAX9611 |
igbt6 | 7:012f5b39405e | 34 | bool MAX9611::initMax9611(eCtrlReg1MUX mux, |
igbt6 | 7:012f5b39405e | 35 | eCtrlReg1SHDN shdn, |
igbt6 | 7:012f5b39405e | 36 | eCtrlReg1LR lr, |
igbt6 | 7:012f5b39405e | 37 | eCtrlReg1MODE mode, |
igbt6 | 7:012f5b39405e | 38 | eCtrlReg2DTIM watchdogDelay, |
igbt6 | 7:012f5b39405e | 39 | eCtrlReg2RTIM watchdogRetryDelay) |
igbt6 | 5:6fec24c37e2a | 40 | { |
igbt6 | 5:6fec24c37e2a | 41 | uint8_t retVal=0; |
igbt6 | 5:6fec24c37e2a | 42 | uint8_t controlReg1=0; |
igbt6 | 5:6fec24c37e2a | 43 | uint8_t controlReg2=0; |
igbt6 | 2:d12dffd027a8 | 44 | |
igbt6 | 5:6fec24c37e2a | 45 | controlReg1=(mode<<5|lr<<4|shdn<<3|mux); |
igbt6 | 5:6fec24c37e2a | 46 | controlReg2=(watchdogDelay<<3|watchdogRetryDelay<<2); |
igbt6 | 5:6fec24c37e2a | 47 | retVal+= write(CONTROL_REGISTER_1_ADRR,&controlReg1,1); |
igbt6 | 5:6fec24c37e2a | 48 | retVal+= write(CONTROL_REGISTER_2_ADRR,&controlReg2,1); |
igbt6 | 5:6fec24c37e2a | 49 | if(retVal!=2) return false; |
igbt6 | 5:6fec24c37e2a | 50 | return true; |
igbt6 | 2:d12dffd027a8 | 51 | } |
igbt6 | 2:d12dffd027a8 | 52 | |
igbt6 | 2:d12dffd027a8 | 53 | |
igbt6 | 5:6fec24c37e2a | 54 | bool MAX9611::readTemp(void) |
igbt6 | 7:012f5b39405e | 55 | { |
igbt6 | 7:012f5b39405e | 56 | uint8_t rawData[2]; |
igbt6 | 7:012f5b39405e | 57 | uint16_t rawTemp=0; |
igbt6 | 7:012f5b39405e | 58 | if(!read(TEMP_DATA_BYTE_MSB_ADRR, rawData,2)) return false; |
igbt6 | 7:012f5b39405e | 59 | rawTemp= get9BitData(rawData[0],rawData[1]); |
igbt6 | 7:012f5b39405e | 60 | //mRawInt =rawTemp; |
igbt6 | 7:012f5b39405e | 61 | if ( rawTemp & 0x100) { |
igbt6 | 7:012f5b39405e | 62 | mTemperature = (float) (rawTemp- 256)*0.48; |
igbt6 | 7:012f5b39405e | 63 | } else { |
igbt6 | 7:012f5b39405e | 64 | mTemperature = (float)(rawTemp) *0.48; |
igbt6 | 7:012f5b39405e | 65 | } |
igbt6 | 7:012f5b39405e | 66 | |
igbt6 | 6:32b5eb1df932 | 67 | return true; |
igbt6 | 6:32b5eb1df932 | 68 | } |
igbt6 | 6:32b5eb1df932 | 69 | |
igbt6 | 6:32b5eb1df932 | 70 | |
igbt6 | 6:32b5eb1df932 | 71 | bool MAX9611::readCSAOutputValue(void) |
igbt6 | 6:32b5eb1df932 | 72 | { |
igbt6 | 6:32b5eb1df932 | 73 | uint8_t rawData[2]; |
igbt6 | 7:012f5b39405e | 74 | uint16_t rawCSAVal=0; |
igbt6 | 6:32b5eb1df932 | 75 | if(!read(CSA_DATA_BYTE_MSB_ADRR, rawData,2)) return false; |
igbt6 | 6:32b5eb1df932 | 76 | rawCSAVal= get12BitData(rawData[0],rawData[1]); |
igbt6 | 6:32b5eb1df932 | 77 | mRawInt = rawCSAVal; |
igbt6 | 7:012f5b39405e | 78 | if(rawCSAVal<=mCsaCurrentValueOffset) |
igbt6 | 7:012f5b39405e | 79 | mCurrentSenseAmplifierOutput=0; |
igbt6 | 6:32b5eb1df932 | 80 | else mCurrentSenseAmplifierOutput= (float)(rawCSAVal)*(0.269); |
igbt6 | 7:012f5b39405e | 81 | |
igbt6 | 5:6fec24c37e2a | 82 | return true; |
igbt6 | 5:6fec24c37e2a | 83 | } |
igbt6 | 2:d12dffd027a8 | 84 | |
igbt6 | 7:012f5b39405e | 85 | // useful debug methods |
igbt6 | 7:012f5b39405e | 86 | |
igbt6 | 7:012f5b39405e | 87 | uint16_t MAX9611::readRawControl(void) |
igbt6 | 7:012f5b39405e | 88 | { |
igbt6 | 7:012f5b39405e | 89 | uint8_t rawData[2]; |
igbt6 | 7:012f5b39405e | 90 | uint16_t rawCtrl=0; |
igbt6 | 7:012f5b39405e | 91 | read(CONTROL_REGISTER_1_ADRR, rawData,2) ; |
igbt6 | 7:012f5b39405e | 92 | rawCtrl= (rawData[0]<<8)|rawData[1]; |
igbt6 | 7:012f5b39405e | 93 | return rawCtrl; |
igbt6 | 7:012f5b39405e | 94 | } |
igbt6 | 7:012f5b39405e | 95 | |
igbt6 | 2:d12dffd027a8 | 96 | |
igbt6 | 7:012f5b39405e | 97 | uint16_t MAX9611::readRawRsValue(void) |
igbt6 | 7:012f5b39405e | 98 | { |
igbt6 | 7:012f5b39405e | 99 | uint8_t rawData[2]; |
igbt6 | 7:012f5b39405e | 100 | uint16_t rawRsVal=0; |
igbt6 | 7:012f5b39405e | 101 | read(RS_DATA_BYTE_MSB_ADRR, rawData,2) ; |
igbt6 | 7:012f5b39405e | 102 | rawRsVal= get12BitData(rawData[0],rawData[1]); |
igbt6 | 7:012f5b39405e | 103 | return rawRsVal; |
igbt6 | 6:32b5eb1df932 | 104 | } |
igbt6 | 6:32b5eb1df932 | 105 | |
igbt6 | 6:32b5eb1df932 | 106 | |
igbt6 | 7:012f5b39405e | 107 | uint16_t MAX9611::readRawCSAOutValue(void) |
igbt6 | 7:012f5b39405e | 108 | { |
igbt6 | 7:012f5b39405e | 109 | uint8_t rawData[2]; |
igbt6 | 7:012f5b39405e | 110 | uint16_t rawCSAOut=0; |
igbt6 | 7:012f5b39405e | 111 | read(CSA_DATA_BYTE_MSB_ADRR, rawData,2) ; |
igbt6 | 7:012f5b39405e | 112 | rawCSAOut= get12BitData(rawData[0],rawData[1]); |
igbt6 | 7:012f5b39405e | 113 | return rawCSAOut; |
igbt6 | 6:32b5eb1df932 | 114 | } |
igbt6 | 6:32b5eb1df932 | 115 | |
igbt6 | 6:32b5eb1df932 | 116 | |
igbt6 | 7:012f5b39405e | 117 | uint16_t MAX9611::readRawOutValue(void) |
igbt6 | 7:012f5b39405e | 118 | { |
igbt6 | 7:012f5b39405e | 119 | uint8_t rawData[2]; |
igbt6 | 7:012f5b39405e | 120 | uint16_t rawOut=0; |
igbt6 | 7:012f5b39405e | 121 | read(OUT_DATA_BYTE_MSB_ADRR, rawData,2) ; |
igbt6 | 7:012f5b39405e | 122 | rawOut= get12BitData(rawData[0],rawData[1]); |
igbt6 | 7:012f5b39405e | 123 | return rawOut; |
igbt6 | 6:32b5eb1df932 | 124 | } |