Library for Akizuki MCP3425 ADC module
Fork of MCP3425 by
Library for MCP3425 ADC module from Akizuki-denshi.
MCP3425.h@2:7375e645e806, 2016-04-13 (annotated)
- Committer:
- sakurahilljp
- Date:
- Wed Apr 13 14:45:11 2016 +0000
- Revision:
- 2:7375e645e806
- Parent:
- 1:5ac344aa0aac
- Child:
- 3:378672292488
Added to methods to specify gain, resolution and conversion operation mode.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yasuyuki | 0:b7bc51be525f | 1 | //********************** |
yasuyuki | 0:b7bc51be525f | 2 | // MCP3425.h for mbed |
yasuyuki | 0:b7bc51be525f | 3 | // |
yasuyuki | 0:b7bc51be525f | 4 | // (C)Copyright 2014 All rights reserved by Y.Onodera |
yasuyuki | 0:b7bc51be525f | 5 | // http://einstlab.web.fc2.com |
yasuyuki | 0:b7bc51be525f | 6 | //********************** |
yasuyuki | 0:b7bc51be525f | 7 | |
yasuyuki | 0:b7bc51be525f | 8 | #ifndef MCP3425_H_ |
yasuyuki | 0:b7bc51be525f | 9 | #define MCP3425_H_ |
yasuyuki | 0:b7bc51be525f | 10 | |
sakurahilljp | 2:7375e645e806 | 11 | #include "mbed.h" |
yasuyuki | 0:b7bc51be525f | 12 | |
sakurahilljp | 2:7375e645e806 | 13 | class MCP3425 |
yasuyuki | 0:b7bc51be525f | 14 | { |
sakurahilljp | 2:7375e645e806 | 15 | public: |
yasuyuki | 0:b7bc51be525f | 16 | |
sakurahilljp | 2:7375e645e806 | 17 | static const int ADDR; |
sakurahilljp | 2:7375e645e806 | 18 | static const float VREF; |
sakurahilljp | 2:7375e645e806 | 19 | |
sakurahilljp | 2:7375e645e806 | 20 | enum Gain { G1 = 0, G2 = 1, G4 = 2, G8 = 3 }; |
sakurahilljp | 2:7375e645e806 | 21 | enum Resolution {W12 = 0, W14 = 1, W16 = 2}; |
sakurahilljp | 2:7375e645e806 | 22 | enum Conversion {ONESHOT = 0, CONTINUOUS = 1}; |
yasuyuki | 0:b7bc51be525f | 23 | |
sakurahilljp | 2:7375e645e806 | 24 | MCP3425(I2C& i2c); |
sakurahilljp | 2:7375e645e806 | 25 | MCP3425(I2C& i2c, int addr); |
sakurahilljp | 2:7375e645e806 | 26 | |
sakurahilljp | 2:7375e645e806 | 27 | void set(Gain gain); |
sakurahilljp | 2:7375e645e806 | 28 | void set(Resolution resolution); |
sakurahilljp | 2:7375e645e806 | 29 | void set(Conversion conversion); |
sakurahilljp | 2:7375e645e806 | 30 | |
sakurahilljp | 1:5ac344aa0aac | 31 | short get(); // Returns AD code |
sakurahilljp | 1:5ac344aa0aac | 32 | float read(); // Returns voltage |
yasuyuki | 0:b7bc51be525f | 33 | |
sakurahilljp | 2:7375e645e806 | 34 | private: |
sakurahilljp | 2:7375e645e806 | 35 | typedef union |
sakurahilljp | 2:7375e645e806 | 36 | { |
sakurahilljp | 2:7375e645e806 | 37 | unsigned short int Val; |
sakurahilljp | 2:7375e645e806 | 38 | unsigned char v[2]; |
sakurahilljp | 2:7375e645e806 | 39 | short S; |
sakurahilljp | 2:7375e645e806 | 40 | struct |
sakurahilljp | 2:7375e645e806 | 41 | { |
sakurahilljp | 2:7375e645e806 | 42 | unsigned char LB; |
sakurahilljp | 2:7375e645e806 | 43 | unsigned char HB; |
sakurahilljp | 2:7375e645e806 | 44 | } byte; |
sakurahilljp | 2:7375e645e806 | 45 | } WORD_VAL; |
yasuyuki | 0:b7bc51be525f | 46 | |
sakurahilljp | 2:7375e645e806 | 47 | typedef union { |
sakurahilljp | 2:7375e645e806 | 48 | unsigned char UC; |
sakurahilljp | 2:7375e645e806 | 49 | struct { |
sakurahilljp | 2:7375e645e806 | 50 | unsigned char G:2; // 00=1, 01=2, 10=4, 11=8 Gain |
sakurahilljp | 2:7375e645e806 | 51 | unsigned char S:2; // 00=12bits, 01=14bits, 10=16bits |
sakurahilljp | 2:7375e645e806 | 52 | unsigned char OC:1; // 0=One-shot, 1=Continuous |
sakurahilljp | 2:7375e645e806 | 53 | unsigned char C:2; // NA |
sakurahilljp | 2:7375e645e806 | 54 | unsigned char RDY:1; // wrinting 1=Initiate, reading 0=Ready |
sakurahilljp | 2:7375e645e806 | 55 | } bit; |
sakurahilljp | 2:7375e645e806 | 56 | } CONFIG; |
yasuyuki | 0:b7bc51be525f | 57 | |
sakurahilljp | 2:7375e645e806 | 58 | I2C _i2c; |
sakurahilljp | 2:7375e645e806 | 59 | int _addr; |
sakurahilljp | 2:7375e645e806 | 60 | |
sakurahilljp | 2:7375e645e806 | 61 | CONFIG _cfg; |
sakurahilljp | 2:7375e645e806 | 62 | WORD_VAL _val; |
sakurahilljp | 2:7375e645e806 | 63 | |
sakurahilljp | 2:7375e645e806 | 64 | char _buf[3]; |
sakurahilljp | 2:7375e645e806 | 65 | |
sakurahilljp | 2:7375e645e806 | 66 | void _init(); |
sakurahilljp | 2:7375e645e806 | 67 | void _write_cfg(); |
sakurahilljp | 2:7375e645e806 | 68 | short _get_code(); |
yasuyuki | 0:b7bc51be525f | 69 | }; |
yasuyuki | 0:b7bc51be525f | 70 | |
yasuyuki | 0:b7bc51be525f | 71 | #endif /* MCP3425_H_ */ |
yasuyuki | 0:b7bc51be525f | 72 | |
yasuyuki | 0:b7bc51be525f | 73 | |
yasuyuki | 0:b7bc51be525f | 74 |