Library for Akizuki MCP3425 ADC module
Fork of MCP3425 by
Library for MCP3425 ADC module from Akizuki-denshi.
Diff: MCP3425.h
- Revision:
- 2:7375e645e806
- Parent:
- 1:5ac344aa0aac
- Child:
- 3:378672292488
diff -r 5ac344aa0aac -r 7375e645e806 MCP3425.h --- a/MCP3425.h Tue Apr 12 14:56:07 2016 +0000 +++ b/MCP3425.h Wed Apr 13 14:45:11 2016 +0000 @@ -8,44 +8,64 @@ #ifndef MCP3425_H_ #define MCP3425_H_ -#define MCP3425_ADDR (0xD0) +#include "mbed.h" -#include "mbed.h" -#include "typedef.h" - - -typedef union +class MCP3425 { - unsigned char UC; - struct - { - unsigned char G:2; // 00=1, 01=2, 10=4, 11=8 Gain - unsigned char S:2; // 00=12bits, 01=14bits, 10=16bits - unsigned char OC:1; // 0=One-shot, 1=Continuous - unsigned char C:2; // NA - unsigned char RDY:1; // wrinting 1=Initiate, reading 0=Ready - } bit; -} CONFIG; +public: - + static const int ADDR; + static const float VREF; + + enum Gain { G1 = 0, G2 = 1, G4 = 2, G8 = 3 }; + enum Resolution {W12 = 0, W14 = 1, W16 = 2}; + enum Conversion {ONESHOT = 0, CONTINUOUS = 1}; -class MCP3425{ -public: - MCP3425 (PinName sda, PinName scl); - MCP3425 (I2C& p_i2c); - void init(); + MCP3425(I2C& i2c); + MCP3425(I2C& i2c, int addr); + + void set(Gain gain); + void set(Resolution resolution); + void set(Conversion conversion); + short get(); // Returns AD code float read(); // Returns voltage -protected: - static const float VREF; - - I2C _i2c; +private: + typedef union + { + unsigned short int Val; + unsigned char v[2]; + short S; + struct + { + unsigned char LB; + unsigned char HB; + } byte; + } WORD_VAL; - CONFIG config; - WORD_VAL ad; - char buf[3]; + typedef union { + unsigned char UC; + struct { + unsigned char G:2; // 00=1, 01=2, 10=4, 11=8 Gain + unsigned char S:2; // 00=12bits, 01=14bits, 10=16bits + unsigned char OC:1; // 0=One-shot, 1=Continuous + unsigned char C:2; // NA + unsigned char RDY:1; // wrinting 1=Initiate, reading 0=Ready + } bit; + } CONFIG; + I2C _i2c; + int _addr; + + CONFIG _cfg; + WORD_VAL _val; + + char _buf[3]; + + void _init(); + void _write_cfg(); + short _get_code(); }; #endif /* MCP3425_H_ */