Library for Akizuki MCP3425 ADC module
Fork of MCP3425 by
Library for MCP3425 ADC module from Akizuki-denshi.
MCP3425.cpp@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.cpp for mbed |
yasuyuki | 0:b7bc51be525f | 3 | // |
yasuyuki | 0:b7bc51be525f | 4 | // MCP3425 mcp3425(P0_5,P0_4); |
yasuyuki | 0:b7bc51be525f | 5 | // or |
yasuyuki | 0:b7bc51be525f | 6 | // I2C i2c(P0_5,P0_4); |
yasuyuki | 0:b7bc51be525f | 7 | // MCP3425 mcp3425(i2c); |
yasuyuki | 0:b7bc51be525f | 8 | // |
yasuyuki | 0:b7bc51be525f | 9 | // (C)Copyright 2014 All rights reserved by Y.Onodera |
yasuyuki | 0:b7bc51be525f | 10 | // http://einstlab.web.fc2.com |
yasuyuki | 0:b7bc51be525f | 11 | //********************** |
yasuyuki | 0:b7bc51be525f | 12 | |
yasuyuki | 0:b7bc51be525f | 13 | #include "mbed.h" |
yasuyuki | 0:b7bc51be525f | 14 | #include "MCP3425.h" |
yasuyuki | 0:b7bc51be525f | 15 | |
sakurahilljp | 2:7375e645e806 | 16 | const int MCP3425::ADDR = 0xD0; |
sakurahilljp | 1:5ac344aa0aac | 17 | const float MCP3425::VREF = 2.048; |
sakurahilljp | 1:5ac344aa0aac | 18 | |
sakurahilljp | 2:7375e645e806 | 19 | |
sakurahilljp | 2:7375e645e806 | 20 | MCP3425::MCP3425(I2C& i2c) |
sakurahilljp | 2:7375e645e806 | 21 | : _i2c(i2c), _addr(ADDR) |
sakurahilljp | 2:7375e645e806 | 22 | { |
sakurahilljp | 2:7375e645e806 | 23 | _init(); |
yasuyuki | 0:b7bc51be525f | 24 | } |
sakurahilljp | 2:7375e645e806 | 25 | |
sakurahilljp | 2:7375e645e806 | 26 | MCP3425::MCP3425(I2C& i2c, int addr) |
sakurahilljp | 2:7375e645e806 | 27 | : _i2c(i2c), _addr(addr) |
sakurahilljp | 2:7375e645e806 | 28 | { |
sakurahilljp | 2:7375e645e806 | 29 | _init(); |
yasuyuki | 0:b7bc51be525f | 30 | } |
yasuyuki | 0:b7bc51be525f | 31 | |
sakurahilljp | 2:7375e645e806 | 32 | void MCP3425::set(MCP3425::Gain gain) |
sakurahilljp | 2:7375e645e806 | 33 | { |
sakurahilljp | 2:7375e645e806 | 34 | _cfg.bit.G = (unsigned char)gain; |
sakurahilljp | 2:7375e645e806 | 35 | _write_cfg(); |
sakurahilljp | 2:7375e645e806 | 36 | } |
sakurahilljp | 2:7375e645e806 | 37 | |
sakurahilljp | 2:7375e645e806 | 38 | void MCP3425::set(MCP3425::Resolution resolution) |
sakurahilljp | 2:7375e645e806 | 39 | { |
sakurahilljp | 2:7375e645e806 | 40 | _cfg.bit.S = (unsigned char)resolution; |
sakurahilljp | 2:7375e645e806 | 41 | _write_cfg(); |
sakurahilljp | 2:7375e645e806 | 42 | } |
sakurahilljp | 2:7375e645e806 | 43 | |
sakurahilljp | 2:7375e645e806 | 44 | void MCP3425::set(MCP3425::Conversion conversion) |
sakurahilljp | 2:7375e645e806 | 45 | { |
sakurahilljp | 2:7375e645e806 | 46 | _cfg.bit.OC = (unsigned char)conversion; |
sakurahilljp | 2:7375e645e806 | 47 | _write_cfg(); |
sakurahilljp | 2:7375e645e806 | 48 | } |
yasuyuki | 0:b7bc51be525f | 49 | |
yasuyuki | 0:b7bc51be525f | 50 | short MCP3425::get() |
yasuyuki | 0:b7bc51be525f | 51 | { |
sakurahilljp | 2:7375e645e806 | 52 | return _get_code(); |
sakurahilljp | 1:5ac344aa0aac | 53 | } |
sakurahilljp | 1:5ac344aa0aac | 54 | |
sakurahilljp | 1:5ac344aa0aac | 55 | float MCP3425::read() |
sakurahilljp | 1:5ac344aa0aac | 56 | { |
sakurahilljp | 2:7375e645e806 | 57 | short code = get(); |
sakurahilljp | 2:7375e645e806 | 58 | |
sakurahilljp | 1:5ac344aa0aac | 59 | int gain = 1; |
sakurahilljp | 2:7375e645e806 | 60 | if (_cfg.bit.G == 0) gain = 1; |
sakurahilljp | 2:7375e645e806 | 61 | else if (_cfg.bit.G == 1) gain = 2; |
sakurahilljp | 2:7375e645e806 | 62 | else if (_cfg.bit.G == 2) gain = 4; |
sakurahilljp | 2:7375e645e806 | 63 | else if (_cfg.bit.G == 3) gain = 8; |
sakurahilljp | 1:5ac344aa0aac | 64 | |
sakurahilljp | 1:5ac344aa0aac | 65 | int maxcode = 2047; |
sakurahilljp | 2:7375e645e806 | 66 | if (_cfg.bit.S == 0 ) maxcode = 2047; |
sakurahilljp | 2:7375e645e806 | 67 | else if (_cfg.bit.S == 1 ) maxcode = 8191; |
sakurahilljp | 2:7375e645e806 | 68 | else if (_cfg.bit.S == 2 ) maxcode = 32767; |
sakurahilljp | 2:7375e645e806 | 69 | |
sakurahilljp | 2:7375e645e806 | 70 | return code * VREF / gain / ( maxcode + 1 ); |
sakurahilljp | 2:7375e645e806 | 71 | } |
sakurahilljp | 2:7375e645e806 | 72 | |
sakurahilljp | 2:7375e645e806 | 73 | void MCP3425::_init() |
sakurahilljp | 2:7375e645e806 | 74 | { |
sakurahilljp | 2:7375e645e806 | 75 | _cfg.bit.G = (unsigned char)G1; |
sakurahilljp | 2:7375e645e806 | 76 | _cfg.bit.S = (unsigned char)W16; |
sakurahilljp | 2:7375e645e806 | 77 | _cfg.bit.OC = (unsigned char)ONESHOT; |
sakurahilljp | 2:7375e645e806 | 78 | _cfg.bit.C = 0; |
sakurahilljp | 2:7375e645e806 | 79 | _cfg.bit.RDY = 0; |
sakurahilljp | 2:7375e645e806 | 80 | |
sakurahilljp | 2:7375e645e806 | 81 | _write_cfg(); |
yasuyuki | 0:b7bc51be525f | 82 | } |
yasuyuki | 0:b7bc51be525f | 83 | |
sakurahilljp | 2:7375e645e806 | 84 | void MCP3425::_write_cfg() |
yasuyuki | 0:b7bc51be525f | 85 | { |
sakurahilljp | 2:7375e645e806 | 86 | _buf[0] = _cfg.UC; |
sakurahilljp | 2:7375e645e806 | 87 | _i2c.write(_addr, _buf, 1); |
yasuyuki | 0:b7bc51be525f | 88 | } |
yasuyuki | 0:b7bc51be525f | 89 | |
sakurahilljp | 2:7375e645e806 | 90 | short MCP3425::_get_code() |
sakurahilljp | 2:7375e645e806 | 91 | { |
sakurahilljp | 2:7375e645e806 | 92 | // Trigger updating value if one-shot Mode |
sakurahilljp | 2:7375e645e806 | 93 | if (_cfg.bit.OC == 0) |
sakurahilljp | 2:7375e645e806 | 94 | { |
sakurahilljp | 2:7375e645e806 | 95 | _cfg.bit.RDY = 1; |
sakurahilljp | 2:7375e645e806 | 96 | _write_cfg(); |
sakurahilljp | 2:7375e645e806 | 97 | } |
sakurahilljp | 2:7375e645e806 | 98 | |
sakurahilljp | 2:7375e645e806 | 99 | while(1) { |
sakurahilljp | 2:7375e645e806 | 100 | if( _i2c.read( ADDR, _buf, 3) != 0 ) |
sakurahilljp | 2:7375e645e806 | 101 | { |
sakurahilljp | 2:7375e645e806 | 102 | // failed to read (nack) |
sakurahilljp | 2:7375e645e806 | 103 | return 0; |
sakurahilljp | 2:7375e645e806 | 104 | } |
sakurahilljp | 2:7375e645e806 | 105 | |
sakurahilljp | 2:7375e645e806 | 106 | _val.byte.HB = _buf[0]; |
sakurahilljp | 2:7375e645e806 | 107 | _val.byte.LB = _buf[1]; |
sakurahilljp | 2:7375e645e806 | 108 | _cfg.UC = _buf[2]; |
sakurahilljp | 2:7375e645e806 | 109 | |
sakurahilljp | 2:7375e645e806 | 110 | if(_cfg.bit.RDY == 0 ) break; |
sakurahilljp | 2:7375e645e806 | 111 | wait_ms(5); // fastest rate: 240 SPS with 12-bit |
sakurahilljp | 2:7375e645e806 | 112 | } |
sakurahilljp | 2:7375e645e806 | 113 | |
sakurahilljp | 2:7375e645e806 | 114 | return _val.S; |
sakurahilljp | 2:7375e645e806 | 115 | } |