ストロベリー・リナックスで扱ってるINA226PRC用のライブラリです。 https://strawberry-linux.com/catalog/items?code=12226
Revision 0:c81a0493eb46, committed 2015-12-09
- Comitter:
- leibun
- Date:
- Wed Dec 09 16:02:25 2015 +0000
- Commit message:
- first commit.
Changed in this revision
INA226.cpp | Show annotated file Show diff for this revision Revisions of this file |
INA226.hpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r c81a0493eb46 INA226.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA226.cpp Wed Dec 09 16:02:25 2015 +0000 @@ -0,0 +1,85 @@ +/** + * @section LICENSE + * Released under the MIT License: http://mbed.org/license/mit + * Copyright (C) 2012 tosihisa + * + * @section DESCRIPTION + * INA226 - Bi-Directional CURRENT/POWER MONITOR with I2C + * http://strawberry-linux.com/catalog/items?code=12031 + * + */ +#include "mbed.h" +#include "INA226.hpp" + +INA226::INA226(I2C &i2c_,int addr_,int freq_) : i2c(i2c_),i2c_addr(addr_),freq(freq_) +{ + i2c.frequency(freq); +} + +int INA226::isExist(void) +{ + char p_addr = 0; //Select Configuration Register. + i2c.frequency(freq); + if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){ + return 1; + } + return 0; +} + +int INA226::rawWrite(char pointer_addr,unsigned short val_) +{ + char val[3]; + val[0] = pointer_addr; + val[1] = static_cast<char>((val_ >> 8) & 0x00ff); + val[2] = static_cast<char>(val_ & 0x00ff); + i2c.frequency(freq); + if(i2c.write(i2c_addr | 0,val,sizeof(val)) == 0){ + return 0; + } + return 1; +} + +int INA226::rawRead(char pointer_addr,unsigned short *val_) +{ + char p_addr = pointer_addr; + char val[2]; + i2c.frequency(freq); + if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){ + if(i2c.read(i2c_addr | 0x01,val,sizeof(val)) == 0){ + *val_ = static_cast<unsigned short>(val[0]); + *val_ = (*val_ << 8) | static_cast<unsigned short>(val[1]); + return 0; + } + } + return 1; +} + +int INA226::getVoltage(double *V_) +{ + unsigned short val; + if(rawRead(0x02,&val) == 0){ + *V_ = static_cast<double>(val) * 1.25; + return 0; + } + return 1; +} + +int INA226::getCurrent(double *I_) +{ + unsigned short val; + if(rawRead(0x04,&val) == 0){ + char *s_p = reinterpret_cast<char *>(&val); + short d_s; + char *d_p = reinterpret_cast<char *>(&d_s); + *(d_p + 0) = *(s_p + 0); + *(d_p + 1) = *(s_p + 1); + *I_ = (static_cast<double>(d_s)) / 10.0; // [mA] + return 0; + } + return 1; +} + +int INA226::setCurrentCalibration(unsigned short val) +{ + return rawWrite(0x05,val); +}
diff -r 000000000000 -r c81a0493eb46 INA226.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA226.hpp Wed Dec 09 16:02:25 2015 +0000 @@ -0,0 +1,77 @@ +/** + * @section LICENSE + * Released under the MIT License: http://mbed.org/license/mit + * Copyright (C) 2012 tosihisa + * + * @section DESCRIPTION + * INA226 - Bi-Directional CURRENT/POWER MONITOR with I2C + * http://strawberry-linux.com/catalog/items?code=12031 + * + */ +#ifndef __INA226_INCLUDE // { +#define __INA226_INCLUDE +#include "mbed.h" +/** + * INA226 Class. + */ +class INA226 { + public: + /** + * Constructor. + * + * @param i2c_ instance of I2C. + * @param addr_ I2C slave address. + * @param freq_ I2C frequency. + */ + INA226(I2C &i2c_,int addr_ = 0x80,int freq_ = 100000); + /** + * Check INA226 exist. + * + * @param none + * @return 0:NOT EXIST / !0:EXIST + */ + int isExist(void); + /** + * INA226 raw level read. + * + * @param pointer_addr INA226 pointer address. + * @param val_ read value. + * @return 0:Read OK / !0:Read NG + */ + int rawRead(char pointer_addr,unsigned short *val_); + /** + * INA226 raw level write. + * + * @param pointer_addr INA226 pointer address. + * @param val_ write value. + * @return 0:Read OK / !0:Read NG + */ + int rawWrite(char pointer_addr,unsigned short val_); + /** + * Get voltage. + * + * @param V_ read value; + * @return 0:Read OK / !0:Read NG + */ + int getVoltage(double *V_); + /** + * Get current. + * + * @param I_ read value[mA]; + * @return 0:Read OK / !0:Read NG + */ + int getCurrent(double *I_); + /** + * Set current calibration. + * + * @param val write value; + * @return 0:Read OK / !0:Read NG + */ + int setCurrentCalibration(unsigned short val = 0x0800); + + private: + I2C &i2c; + int i2c_addr; + int freq; +}; +#endif // }