library for MAX9611 /9612 Current-Sense Amplifiers

Committer:
igbt6
Date:
Tue Nov 11 02:25:05 2014 +0000
Revision:
6:32b5eb1df932
Parent:
5:6fec24c37e2a
Child:
7:012f5b39405e
library for MAX9611/9612 current sense amplifier

Who changed what in which revision?

UserRevisionLine numberNew 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 6:32b5eb1df932 9 if(!initMax9611()); //while(1){ //TODO handle error
igbt6 6:32b5eb1df932 10 //}
igbt6 5:6fec24c37e2a 11 mTemperature=0;
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*)&regAddress,1,true);
igbt6 6:32b5eb1df932 30 return (mI2c.read(mI2cAddr,(char*)data,dataLength)==0);
igbt6 2:d12dffd027a8 31 }
igbt6 2:d12dffd027a8 32
igbt6 5:6fec24c37e2a 33 //configuration of MAX9611
igbt6 5:6fec24c37e2a 34 bool MAX9611::initMax9611(eCtrlReg1MUX mux,
igbt6 5:6fec24c37e2a 35 eCtrlReg1SHDN shdn,
igbt6 5:6fec24c37e2a 36 eCtrlReg1LR lr,
igbt6 5:6fec24c37e2a 37 eCtrlReg1MODE mode,
igbt6 5:6fec24c37e2a 38 eCtrlReg2DTIM watchdogDelay,
igbt6 5:6fec24c37e2a 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 5:6fec24c37e2a 55 {
igbt6 5:6fec24c37e2a 56 uint8_t rawData[2];
igbt6 5:6fec24c37e2a 57 uint16_t rawTemp=0;
igbt6 5:6fec24c37e2a 58 if(!read(TEMP_DATA_BYTE_MSB_ADRR, rawData,2)) return false;
igbt6 5:6fec24c37e2a 59 rawTemp= get9BitData(rawData[0],rawData[1]);
igbt6 6:32b5eb1df932 60 //mRawInt =rawTemp;
igbt6 5:6fec24c37e2a 61 if ( rawTemp & 0x100) {
igbt6 5:6fec24c37e2a 62 mTemperature = (float) (rawTemp- 256)*0.48;
igbt6 5:6fec24c37e2a 63 }
igbt6 5:6fec24c37e2a 64 else {
igbt6 5:6fec24c37e2a 65 mTemperature = (float)(rawTemp) *0.48;
igbt6 5:6fec24c37e2a 66 }
igbt6 6:32b5eb1df932 67
igbt6 6:32b5eb1df932 68 return true;
igbt6 6:32b5eb1df932 69 }
igbt6 6:32b5eb1df932 70
igbt6 6:32b5eb1df932 71
igbt6 6:32b5eb1df932 72 bool MAX9611::readCSAOutputValue(void)
igbt6 6:32b5eb1df932 73 {
igbt6 6:32b5eb1df932 74 uint8_t rawData[2];
igbt6 6:32b5eb1df932 75 uint16_t rawCSAVal=0;
igbt6 6:32b5eb1df932 76 if(!read(CSA_DATA_BYTE_MSB_ADRR, rawData,2)) return false;
igbt6 6:32b5eb1df932 77 rawCSAVal= get12BitData(rawData[0],rawData[1]);
igbt6 6:32b5eb1df932 78 mRawInt = rawCSAVal;
igbt6 6:32b5eb1df932 79 if(rawCSAVal<=csaCurrentValueOffset)
igbt6 6:32b5eb1df932 80 mCurrentSenseAmplifierOutput=0;// (float)(rawCSAVal-csaCurrentValueOffset)*(0.269);
igbt6 6:32b5eb1df932 81 else mCurrentSenseAmplifierOutput= (float)(rawCSAVal)*(0.269);
igbt6 6:32b5eb1df932 82
igbt6 5:6fec24c37e2a 83 return true;
igbt6 5:6fec24c37e2a 84 }
igbt6 2:d12dffd027a8 85
igbt6 2:d12dffd027a8 86
igbt6 6:32b5eb1df932 87
igbt6 6:32b5eb1df932 88 uint16_t MAX9611::readControl(void)
igbt6 6:32b5eb1df932 89 {
igbt6 6:32b5eb1df932 90 uint8_t rawData[2];
igbt6 6:32b5eb1df932 91 uint16_t rawTemp=0;
igbt6 6:32b5eb1df932 92 read(CONTROL_REGISTER_1_ADRR, rawData,2) ;
igbt6 6:32b5eb1df932 93 rawTemp= (rawData[0]<<8)|rawData[1];
igbt6 6:32b5eb1df932 94 return rawTemp;
igbt6 6:32b5eb1df932 95 }
igbt6 6:32b5eb1df932 96
igbt6 6:32b5eb1df932 97
igbt6 6:32b5eb1df932 98 uint16_t MAX9611::readRsValue(void)
igbt6 6:32b5eb1df932 99 {
igbt6 6:32b5eb1df932 100 uint8_t rawData[2];
igbt6 6:32b5eb1df932 101 uint16_t rawTemp=0;
igbt6 6:32b5eb1df932 102 read(RS_DATA_BYTE_MSB_ADRR, rawData,2) ;
igbt6 6:32b5eb1df932 103 rawTemp= get12BitData(rawData[0],rawData[1]);
igbt6 6:32b5eb1df932 104 return rawTemp;
igbt6 6:32b5eb1df932 105 }
igbt6 6:32b5eb1df932 106
igbt6 6:32b5eb1df932 107
igbt6 6:32b5eb1df932 108 uint16_t MAX9611::readOutValue(void)
igbt6 6:32b5eb1df932 109 {
igbt6 6:32b5eb1df932 110 uint8_t rawData[2];
igbt6 6:32b5eb1df932 111 uint16_t rawTemp=0;
igbt6 6:32b5eb1df932 112 read(OUT_DATA_BYTE_MSB_ADRR, rawData,2) ;
igbt6 6:32b5eb1df932 113 rawTemp= get12BitData(rawData[0],rawData[1]);
igbt6 6:32b5eb1df932 114 return rawTemp;
igbt6 6:32b5eb1df932 115 }