ストロベリー・リナックスで扱ってるINA226PRC用のライブラリです。 https://strawberry-linux.com/catalog/items?code=12226
INA226.cpp@0:c81a0493eb46, 2015-12-09 (annotated)
- Committer:
- leibun
- Date:
- Wed Dec 09 16:02:25 2015 +0000
- Revision:
- 0:c81a0493eb46
first commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leibun | 0:c81a0493eb46 | 1 | /** |
leibun | 0:c81a0493eb46 | 2 | * @section LICENSE |
leibun | 0:c81a0493eb46 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
leibun | 0:c81a0493eb46 | 4 | * Copyright (C) 2012 tosihisa |
leibun | 0:c81a0493eb46 | 5 | * |
leibun | 0:c81a0493eb46 | 6 | * @section DESCRIPTION |
leibun | 0:c81a0493eb46 | 7 | * INA226 - Bi-Directional CURRENT/POWER MONITOR with I2C |
leibun | 0:c81a0493eb46 | 8 | * http://strawberry-linux.com/catalog/items?code=12031 |
leibun | 0:c81a0493eb46 | 9 | * |
leibun | 0:c81a0493eb46 | 10 | */ |
leibun | 0:c81a0493eb46 | 11 | #include "mbed.h" |
leibun | 0:c81a0493eb46 | 12 | #include "INA226.hpp" |
leibun | 0:c81a0493eb46 | 13 | |
leibun | 0:c81a0493eb46 | 14 | INA226::INA226(I2C &i2c_,int addr_,int freq_) : i2c(i2c_),i2c_addr(addr_),freq(freq_) |
leibun | 0:c81a0493eb46 | 15 | { |
leibun | 0:c81a0493eb46 | 16 | i2c.frequency(freq); |
leibun | 0:c81a0493eb46 | 17 | } |
leibun | 0:c81a0493eb46 | 18 | |
leibun | 0:c81a0493eb46 | 19 | int INA226::isExist(void) |
leibun | 0:c81a0493eb46 | 20 | { |
leibun | 0:c81a0493eb46 | 21 | char p_addr = 0; //Select Configuration Register. |
leibun | 0:c81a0493eb46 | 22 | i2c.frequency(freq); |
leibun | 0:c81a0493eb46 | 23 | if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){ |
leibun | 0:c81a0493eb46 | 24 | return 1; |
leibun | 0:c81a0493eb46 | 25 | } |
leibun | 0:c81a0493eb46 | 26 | return 0; |
leibun | 0:c81a0493eb46 | 27 | } |
leibun | 0:c81a0493eb46 | 28 | |
leibun | 0:c81a0493eb46 | 29 | int INA226::rawWrite(char pointer_addr,unsigned short val_) |
leibun | 0:c81a0493eb46 | 30 | { |
leibun | 0:c81a0493eb46 | 31 | char val[3]; |
leibun | 0:c81a0493eb46 | 32 | val[0] = pointer_addr; |
leibun | 0:c81a0493eb46 | 33 | val[1] = static_cast<char>((val_ >> 8) & 0x00ff); |
leibun | 0:c81a0493eb46 | 34 | val[2] = static_cast<char>(val_ & 0x00ff); |
leibun | 0:c81a0493eb46 | 35 | i2c.frequency(freq); |
leibun | 0:c81a0493eb46 | 36 | if(i2c.write(i2c_addr | 0,val,sizeof(val)) == 0){ |
leibun | 0:c81a0493eb46 | 37 | return 0; |
leibun | 0:c81a0493eb46 | 38 | } |
leibun | 0:c81a0493eb46 | 39 | return 1; |
leibun | 0:c81a0493eb46 | 40 | } |
leibun | 0:c81a0493eb46 | 41 | |
leibun | 0:c81a0493eb46 | 42 | int INA226::rawRead(char pointer_addr,unsigned short *val_) |
leibun | 0:c81a0493eb46 | 43 | { |
leibun | 0:c81a0493eb46 | 44 | char p_addr = pointer_addr; |
leibun | 0:c81a0493eb46 | 45 | char val[2]; |
leibun | 0:c81a0493eb46 | 46 | i2c.frequency(freq); |
leibun | 0:c81a0493eb46 | 47 | if(i2c.write(i2c_addr | 0,&p_addr,sizeof(p_addr)) == 0){ |
leibun | 0:c81a0493eb46 | 48 | if(i2c.read(i2c_addr | 0x01,val,sizeof(val)) == 0){ |
leibun | 0:c81a0493eb46 | 49 | *val_ = static_cast<unsigned short>(val[0]); |
leibun | 0:c81a0493eb46 | 50 | *val_ = (*val_ << 8) | static_cast<unsigned short>(val[1]); |
leibun | 0:c81a0493eb46 | 51 | return 0; |
leibun | 0:c81a0493eb46 | 52 | } |
leibun | 0:c81a0493eb46 | 53 | } |
leibun | 0:c81a0493eb46 | 54 | return 1; |
leibun | 0:c81a0493eb46 | 55 | } |
leibun | 0:c81a0493eb46 | 56 | |
leibun | 0:c81a0493eb46 | 57 | int INA226::getVoltage(double *V_) |
leibun | 0:c81a0493eb46 | 58 | { |
leibun | 0:c81a0493eb46 | 59 | unsigned short val; |
leibun | 0:c81a0493eb46 | 60 | if(rawRead(0x02,&val) == 0){ |
leibun | 0:c81a0493eb46 | 61 | *V_ = static_cast<double>(val) * 1.25; |
leibun | 0:c81a0493eb46 | 62 | return 0; |
leibun | 0:c81a0493eb46 | 63 | } |
leibun | 0:c81a0493eb46 | 64 | return 1; |
leibun | 0:c81a0493eb46 | 65 | } |
leibun | 0:c81a0493eb46 | 66 | |
leibun | 0:c81a0493eb46 | 67 | int INA226::getCurrent(double *I_) |
leibun | 0:c81a0493eb46 | 68 | { |
leibun | 0:c81a0493eb46 | 69 | unsigned short val; |
leibun | 0:c81a0493eb46 | 70 | if(rawRead(0x04,&val) == 0){ |
leibun | 0:c81a0493eb46 | 71 | char *s_p = reinterpret_cast<char *>(&val); |
leibun | 0:c81a0493eb46 | 72 | short d_s; |
leibun | 0:c81a0493eb46 | 73 | char *d_p = reinterpret_cast<char *>(&d_s); |
leibun | 0:c81a0493eb46 | 74 | *(d_p + 0) = *(s_p + 0); |
leibun | 0:c81a0493eb46 | 75 | *(d_p + 1) = *(s_p + 1); |
leibun | 0:c81a0493eb46 | 76 | *I_ = (static_cast<double>(d_s)) / 10.0; // [mA] |
leibun | 0:c81a0493eb46 | 77 | return 0; |
leibun | 0:c81a0493eb46 | 78 | } |
leibun | 0:c81a0493eb46 | 79 | return 1; |
leibun | 0:c81a0493eb46 | 80 | } |
leibun | 0:c81a0493eb46 | 81 | |
leibun | 0:c81a0493eb46 | 82 | int INA226::setCurrentCalibration(unsigned short val) |
leibun | 0:c81a0493eb46 | 83 | { |
leibun | 0:c81a0493eb46 | 84 | return rawWrite(0x05,val); |
leibun | 0:c81a0493eb46 | 85 | } |