a

Committer:
tosihisa
Date:
Sat Nov 24 18:15:16 2012 +0000
Revision:
0:3a05c5755625
Child:
1:e0ebc6af5e04
1st

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tosihisa 0:3a05c5755625 1 /*
tosihisa 0:3a05c5755625 2 * Copyright (c) 2011 Toshihisa T
tosihisa 0:3a05c5755625 3 * Released under the MIT License: http://mbed.org/license/mit
tosihisa 0:3a05c5755625 4 */
tosihisa 0:3a05c5755625 5
tosihisa 0:3a05c5755625 6 #include "mbed.h"
tosihisa 0:3a05c5755625 7 #include "INA226.hpp"
tosihisa 0:3a05c5755625 8
tosihisa 0:3a05c5755625 9 INA226::INA226(I2C &i2c_,int addr_,int freq_) : i2c(i2c_),i2c_addr(addr_),freq(freq_)
tosihisa 0:3a05c5755625 10 {
tosihisa 0:3a05c5755625 11 i2c.frequency(freq);
tosihisa 0:3a05c5755625 12 }
tosihisa 0:3a05c5755625 13
tosihisa 0:3a05c5755625 14 int INA226::isExist(void)
tosihisa 0:3a05c5755625 15 {
tosihisa 0:3a05c5755625 16 char p_addr = 0; //Select Configuration Register.
tosihisa 0:3a05c5755625 17 i2c.frequency(freq);
tosihisa 0:3a05c5755625 18 if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){
tosihisa 0:3a05c5755625 19 return 1;
tosihisa 0:3a05c5755625 20 }
tosihisa 0:3a05c5755625 21 return 0;
tosihisa 0:3a05c5755625 22 }
tosihisa 0:3a05c5755625 23
tosihisa 0:3a05c5755625 24 int INA226::rawWrite(char pointer_addr,unsigned short val_)
tosihisa 0:3a05c5755625 25 {
tosihisa 0:3a05c5755625 26 char val[3];
tosihisa 0:3a05c5755625 27 val[0] = pointer_addr;
tosihisa 0:3a05c5755625 28 val[1] = static_cast<char>((val_ >> 8) & 0x00ff);
tosihisa 0:3a05c5755625 29 val[2] = static_cast<char>(val_ & 0x00ff);
tosihisa 0:3a05c5755625 30 i2c.frequency(freq);
tosihisa 0:3a05c5755625 31 if(i2c.write(i2c_addr | 0,val,sizeof(val)) == 0){
tosihisa 0:3a05c5755625 32 return 0;
tosihisa 0:3a05c5755625 33 }
tosihisa 0:3a05c5755625 34 return 1;
tosihisa 0:3a05c5755625 35 }
tosihisa 0:3a05c5755625 36
tosihisa 0:3a05c5755625 37 int INA226::rawRead(char pointer_addr,unsigned short *val_)
tosihisa 0:3a05c5755625 38 {
tosihisa 0:3a05c5755625 39 char p_addr = pointer_addr;
tosihisa 0:3a05c5755625 40 char val[2];
tosihisa 0:3a05c5755625 41 i2c.frequency(freq);
tosihisa 0:3a05c5755625 42 if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){
tosihisa 0:3a05c5755625 43 if(i2c.read(i2c_addr | 0x01,val,sizeof(val)) == 0){
tosihisa 0:3a05c5755625 44 *val_ = static_cast<unsigned short>(val[0]);
tosihisa 0:3a05c5755625 45 *val_ = (*val_ << 8) | static_cast<unsigned short>(val[1]);
tosihisa 0:3a05c5755625 46 return 0;
tosihisa 0:3a05c5755625 47 }
tosihisa 0:3a05c5755625 48 }
tosihisa 0:3a05c5755625 49 return 1;
tosihisa 0:3a05c5755625 50 }
tosihisa 0:3a05c5755625 51
tosihisa 0:3a05c5755625 52 int INA226::getVoltage(double *V_)
tosihisa 0:3a05c5755625 53 {
tosihisa 0:3a05c5755625 54 unsigned short val;
tosihisa 0:3a05c5755625 55 if(rawRead(0x02,&val) == 0){
tosihisa 0:3a05c5755625 56 *V_ = static_cast<double>(val) * 1.25;
tosihisa 0:3a05c5755625 57 return 0;
tosihisa 0:3a05c5755625 58 }
tosihisa 0:3a05c5755625 59 return 1;
tosihisa 0:3a05c5755625 60 }
tosihisa 0:3a05c5755625 61
tosihisa 0:3a05c5755625 62 int INA226::getCurrent(double *I_)
tosihisa 0:3a05c5755625 63 {
tosihisa 0:3a05c5755625 64 unsigned short val;
tosihisa 0:3a05c5755625 65 if(rawRead(0x04,&val) == 0){
tosihisa 0:3a05c5755625 66 char *s_p = reinterpret_cast<char *>(&val);
tosihisa 0:3a05c5755625 67 short d_s;
tosihisa 0:3a05c5755625 68 char *d_p = reinterpret_cast<char *>(&d_s);
tosihisa 0:3a05c5755625 69 *(d_p + 0) = *(s_p + 0);
tosihisa 0:3a05c5755625 70 *(d_p + 1) = *(s_p + 1);
tosihisa 0:3a05c5755625 71 *I_ = static_cast<double>(d_s) /* * 1.25 */;
tosihisa 0:3a05c5755625 72 return 0;
tosihisa 0:3a05c5755625 73 }
tosihisa 0:3a05c5755625 74 return 1;
tosihisa 0:3a05c5755625 75 }
tosihisa 0:3a05c5755625 76
tosihisa 0:3a05c5755625 77 int INA226::setCurrentCalibration(unsigned short val)
tosihisa 0:3a05c5755625 78 {
tosihisa 0:3a05c5755625 79 return rawWrite(0x05,val);
tosihisa 0:3a05c5755625 80 }