motacon

Dependents:   HelloWorld_MotorKaisei

Committer:
tosihisa
Date:
Sat Nov 24 18:29:15 2012 +0000
Revision:
1:e0ebc6af5e04
Parent:
0:3a05c5755625
wrote doxygen comment.;

Who changed what in which revision?

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