Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of INA226 by
INA226.cpp
00001 /** 00002 * @section LICENSE 00003 * Released under the MIT License: http://mbed.org/license/mit 00004 * Copyright (C) 2012 tosihisa 00005 * 00006 * @section DESCRIPTION 00007 * INA226 - Bi-Directional CURRENT/POWER MONITOR with I2C 00008 * http://strawberry-linux.com/catalog/items?code=12031 00009 * 00010 */ 00011 #include "mbed.h" 00012 #include "INA226.hpp" 00013 00014 INA226::INA226(I2C &i2c_,int addr_,int freq_) : i2c(i2c_),i2c_addr(addr_),freq(freq_) 00015 { 00016 i2c.frequency(freq); 00017 } 00018 00019 int INA226::isExist(void) 00020 { 00021 char p_addr = 0; //Select Configuration Register. 00022 i2c.frequency(freq); 00023 if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){ 00024 return 1; 00025 } 00026 return 0; 00027 } 00028 00029 int INA226::rawWrite(char pointer_addr,unsigned short val_) 00030 { 00031 char val[3]; 00032 val[0] = pointer_addr; 00033 val[1] = static_cast<char>((val_ >> 8) & 0x00ff); 00034 val[2] = static_cast<char>(val_ & 0x00ff); 00035 i2c.frequency(freq); 00036 if(i2c.write(i2c_addr | 0,val,sizeof(val)) == 0){ 00037 return 0; 00038 } 00039 return 1; 00040 } 00041 00042 int INA226::rawRead(char pointer_addr,unsigned short *val_) 00043 { 00044 char p_addr = pointer_addr; 00045 char val[2]; 00046 i2c.frequency(freq); 00047 if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){ 00048 if(i2c.read(i2c_addr | 0x01,val,sizeof(val)) == 0){ 00049 *val_ = static_cast<unsigned short>(val[0]); 00050 *val_ = (*val_ << 8) | static_cast<unsigned short>(val[1]); 00051 return 0; 00052 } 00053 } 00054 return 1; 00055 } 00056 00057 int INA226::getVoltage(double *V_) 00058 { 00059 unsigned short val; 00060 if(rawRead(0x02,&val) == 0){ 00061 *V_ = static_cast<double>(val) * 1.25; 00062 return 0; 00063 } 00064 return 1; 00065 } 00066 00067 int INA226::getCurrent(double *I_) 00068 { 00069 unsigned short val; 00070 if(rawRead(0x04,&val) == 0){ 00071 char *s_p = reinterpret_cast<char *>(&val); 00072 short d_s; 00073 char *d_p = reinterpret_cast<char *>(&d_s); 00074 *(d_p + 0) = *(s_p + 0); 00075 *(d_p + 1) = *(s_p + 1); 00076 *I_ = static_cast<double>(d_s) /* * 1.25 */; 00077 return 0; 00078 } 00079 return 1; 00080 } 00081 00082 int INA226::setCurrentCalibration(unsigned short val) 00083 { 00084 return rawWrite(0x05,val); 00085 }
Generated on Sun Jul 17 2022 01:28:42 by
