Library for Akizuki MCP3425 ADC module
Fork of MCP3425 by
Library for MCP3425 ADC module from Akizuki-denshi.
Diff: MCP3425.cpp
- Revision:
- 1:5ac344aa0aac
- Parent:
- 0:b7bc51be525f
- Child:
- 2:7375e645e806
--- a/MCP3425.cpp Wed Oct 15 14:37:59 2014 +0000 +++ b/MCP3425.cpp Tue Apr 12 14:56:07 2016 +0000 @@ -13,6 +13,8 @@ #include "mbed.h" #include "MCP3425.h" +const float MCP3425::VREF = 2.048; + MCP3425::MCP3425 (PinName sda, PinName scl) : _i2c(sda, scl) { init(); } @@ -23,14 +25,29 @@ short MCP3425::get() { - _i2c.read( MCP3425_ADDR, buf, 3); ad.byte.HB=buf[0]; ad.byte.LB=buf[1]; config.UC=buf[2]; return ad.S; +} + +float MCP3425::read() +{ + int gain = 1; + if (config.bit.G == 0) gain = 1; + else if (config.bit.G == 1) gain = 2; + else if (config.bit.G == 2) gain = 4; + else if (config.bit.G == 3) gain = 8; + + int maxcode = 2047; + if (config.bit.S == 0 ) maxcode = 2047; + else if (config.bit.S == 1 ) maxcode = 8191; + else if (config.bit.S == 2 ) maxcode = 32767; + short code = get(); + return code * VREF / gain / ( maxcode + 1 ); }