Library for Akizuki MCP3425 ADC module
Fork of MCP3425 by
Library for MCP3425 ADC module from Akizuki-denshi.
Diff: MCP3425.cpp
- Revision:
- 3:378672292488
- Parent:
- 2:7375e645e806
--- a/MCP3425.cpp Wed Apr 13 14:45:11 2016 +0000 +++ b/MCP3425.cpp Fri Apr 15 13:41:32 2016 +0000 @@ -1,10 +1,7 @@ //********************** // MCP3425.cpp for mbed // -// MCP3425 mcp3425(P0_5,P0_4); -// or -// I2C i2c(P0_5,P0_4); -// MCP3425 mcp3425(i2c); +// (C)Copyright 2016 Satoshi Nihonyanagi, All Rights Reserved. // // (C)Copyright 2014 All rights reserved by Y.Onodera // http://einstlab.web.fc2.com @@ -16,7 +13,6 @@ const int MCP3425::ADDR = 0xD0; const float MCP3425::VREF = 2.048; - MCP3425::MCP3425(I2C& i2c) : _i2c(i2c), _addr(ADDR) { @@ -54,20 +50,7 @@ float MCP3425::read() { - short code = get(); - - int gain = 1; - if (_cfg.bit.G == 0) gain = 1; - else if (_cfg.bit.G == 1) gain = 2; - else if (_cfg.bit.G == 2) gain = 4; - else if (_cfg.bit.G == 3) gain = 8; - - int maxcode = 2047; - if (_cfg.bit.S == 0 ) maxcode = 2047; - else if (_cfg.bit.S == 1 ) maxcode = 8191; - else if (_cfg.bit.S == 2 ) maxcode = 32767; - - return code * VREF / gain / ( maxcode + 1 ); + return _c2v(_get_code()); } void MCP3425::_init() @@ -113,3 +96,19 @@ return _val.S; } + +float MCP3425::_c2v(short code) +{ + int gain = 1; + if (_cfg.bit.G == 0) gain = 1; + else if (_cfg.bit.G == 1) gain = 2; + else if (_cfg.bit.G == 2) gain = 4; + else if (_cfg.bit.G == 3) gain = 8; + + int maxcode = 2047; + if (_cfg.bit.S == 0 ) maxcode = 2047; + else if (_cfg.bit.S == 1 ) maxcode = 8191; + else if (_cfg.bit.S == 2 ) maxcode = 32767; + + return code * VREF / gain / ( maxcode + 1 ); +}