Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MCP3425 by
Revision 3:378672292488, committed 2016-04-15
- Comitter:
- sakurahilljp
- Date:
- Fri Apr 15 13:41:32 2016 +0000
- Parent:
- 2:7375e645e806
- Commit message:
- Added documentation.
Changed in this revision
MCP3425.cpp | Show annotated file Show diff for this revision Revisions of this file |
MCP3425.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7375e645e806 -r 378672292488 MCP3425.cpp --- 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 ); +}
diff -r 7375e645e806 -r 378672292488 MCP3425.h --- a/MCP3425.h Wed Apr 13 14:45:11 2016 +0000 +++ b/MCP3425.h Fri Apr 15 13:41:32 2016 +0000 @@ -1,6 +1,8 @@ //********************** // MCP3425.h for mbed // +// (C)Copyright 2016 Satoshi Nihonyanagi, All Rights Reserved. +// // (C)Copyright 2014 All rights reserved by Y.Onodera // http://einstlab.web.fc2.com //********************** @@ -9,29 +11,64 @@ #define MCP3425_H_ #include "mbed.h" +/** MCP3425 class. + * Used for MCP3425 ADC module from Akizuki-denshi. + * + * Example: + * @code + * #include "mbed.h" + * #include "MCP3425.h" + * + * I2C i2c(dp13, dp15); + * MCP3425 adc(i2c); + * + * int main() + * { + * adc.set(MCP3425::G1); + * adc.set(MCP3425::W12); + * adc.set(MCP3425::CONTINUOUS); + * + * short c = adc.code(); + * float v = adc.read(); + * } + * @endcode + */ class MCP3425 { -public: - - static const int ADDR; - static const float VREF; - +public: enum Gain { G1 = 0, G2 = 1, G4 = 2, G8 = 3 }; enum Resolution {W12 = 0, W14 = 1, W16 = 2}; enum Conversion {ONESHOT = 0, CONTINUOUS = 1}; + /** Construct a new instance.*/ MCP3425(I2C& i2c); + + /** Construct a new instance with the specified I2C slave address. + ( Address of MCP3425 from akizuki-denshi is fixed to 0x0D.) + */ MCP3425(I2C& i2c, int addr); + /** Specify gain. */ void set(Gain gain); + + /** Specify resolution (sample bits). */ void set(Resolution resolution); + + /** Specify conversion operation mode. */ void set(Conversion conversion); - short get(); // Returns AD code - float read(); // Returns voltage + /** Return AD code. */ + short get(); + + /** Return voltage. */ + float read(); private: + + static const int ADDR; + static const float VREF; + typedef union { unsigned short int Val; @@ -66,6 +103,7 @@ void _init(); void _write_cfg(); short _get_code(); + float _c2v(short code); }; #endif /* MCP3425_H_ */