2.7V 4-Channel 12-Bit A/D Converters with SPI™ Serial Interface
Fork of MCP3204 by
MCP3204.cpp@1:cf7328552994, 2016-01-20 (annotated)
- Committer:
- stjo2809
- Date:
- Wed Jan 20 10:53:06 2016 +0000
- Revision:
- 1:cf7328552994
- Parent:
- 0:b5950cd5e330
Tested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stjo2809 | 1:cf7328552994 | 1 | /* mbed MCP3204 Library, for driving the 2.7V 4-Channel 12-Bit A/D Converters with SPI™ Serial Interface |
stjo2809 | 0:b5950cd5e330 | 2 | * Copyright (c) 2015, Created by Steen Joergensen (stjo2809) |
stjo2809 | 0:b5950cd5e330 | 3 | * |
stjo2809 | 0:b5950cd5e330 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
stjo2809 | 0:b5950cd5e330 | 5 | * of this software and associated documentation files (the "Software"), to deal |
stjo2809 | 0:b5950cd5e330 | 6 | * in the Software without restriction, including without limitation the rights |
stjo2809 | 0:b5950cd5e330 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
stjo2809 | 0:b5950cd5e330 | 8 | * copies of the Software, and to permit persons to whom the Software is |
stjo2809 | 0:b5950cd5e330 | 9 | * furnished to do so, subject to the following conditions: |
stjo2809 | 0:b5950cd5e330 | 10 | * |
stjo2809 | 0:b5950cd5e330 | 11 | * The above copyright notice and this permission notice shall be included in |
stjo2809 | 0:b5950cd5e330 | 12 | * all copies or substantial portions of the Software. |
stjo2809 | 0:b5950cd5e330 | 13 | * |
stjo2809 | 0:b5950cd5e330 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
stjo2809 | 0:b5950cd5e330 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
stjo2809 | 0:b5950cd5e330 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
stjo2809 | 0:b5950cd5e330 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
stjo2809 | 0:b5950cd5e330 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
stjo2809 | 0:b5950cd5e330 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
stjo2809 | 0:b5950cd5e330 | 20 | * THE SOFTWARE. |
stjo2809 | 0:b5950cd5e330 | 21 | */ |
stjo2809 | 0:b5950cd5e330 | 22 | |
stjo2809 | 0:b5950cd5e330 | 23 | #include "mbed.h" |
stjo2809 | 0:b5950cd5e330 | 24 | #include "MCP3204.h" |
stjo2809 | 0:b5950cd5e330 | 25 | |
stjo2809 | 0:b5950cd5e330 | 26 | //============================================================================= |
stjo2809 | 0:b5950cd5e330 | 27 | // Public functions |
stjo2809 | 0:b5950cd5e330 | 28 | //============================================================================= |
stjo2809 | 0:b5950cd5e330 | 29 | |
stjo2809 | 0:b5950cd5e330 | 30 | MCP3204::MCP3204(SPI& spi, PinName nCs) : _spi(spi), _nCs(nCs) |
stjo2809 | 0:b5950cd5e330 | 31 | { |
stjo2809 | 1:cf7328552994 | 32 | _nCs = 1; _nCs = 0; _nCs = 1; |
stjo2809 | 0:b5950cd5e330 | 33 | } |
stjo2809 | 0:b5950cd5e330 | 34 | |
stjo2809 | 0:b5950cd5e330 | 35 | int MCP3204::sgl(char channel) |
stjo2809 | 0:b5950cd5e330 | 36 | { |
stjo2809 | 0:b5950cd5e330 | 37 | return _read(SINGLE, channel); |
stjo2809 | 0:b5950cd5e330 | 38 | } |
stjo2809 | 0:b5950cd5e330 | 39 | |
stjo2809 | 0:b5950cd5e330 | 40 | int MCP3204::diff(char channel) |
stjo2809 | 0:b5950cd5e330 | 41 | { |
stjo2809 | 0:b5950cd5e330 | 42 | return _read(DIFFERENTIAL, channel); |
stjo2809 | 0:b5950cd5e330 | 43 | } |
stjo2809 | 0:b5950cd5e330 | 44 | |
stjo2809 | 0:b5950cd5e330 | 45 | //============================================================================= |
stjo2809 | 0:b5950cd5e330 | 46 | // Private functions |
stjo2809 | 0:b5950cd5e330 | 47 | //============================================================================= |
stjo2809 | 0:b5950cd5e330 | 48 | |
stjo2809 | 0:b5950cd5e330 | 49 | int MCP3204::_make_value_from_responce(int _msb, int _lsb) |
stjo2809 | 0:b5950cd5e330 | 50 | { |
stjo2809 | 1:cf7328552994 | 51 | int responce = _msb << 8; |
stjo2809 | 1:cf7328552994 | 52 | responce = responce | _lsb; |
stjo2809 | 1:cf7328552994 | 53 | responce = responce & 0xfff; |
stjo2809 | 1:cf7328552994 | 54 | |
stjo2809 | 1:cf7328552994 | 55 | return responce; |
stjo2809 | 0:b5950cd5e330 | 56 | } |
stjo2809 | 0:b5950cd5e330 | 57 | |
stjo2809 | 0:b5950cd5e330 | 58 | int MCP3204::_read(char sgl_or_diff, char channel) |
stjo2809 | 0:b5950cd5e330 | 59 | { |
stjo2809 | 0:b5950cd5e330 | 60 | _nCs = 0; |
stjo2809 | 0:b5950cd5e330 | 61 | _spi.write(sgl_or_diff); |
stjo2809 | 0:b5950cd5e330 | 62 | int responce_msb = _spi.write(channel); |
stjo2809 | 0:b5950cd5e330 | 63 | int responce_lsb = _spi.write(0xff); // don't care bits |
stjo2809 | 0:b5950cd5e330 | 64 | _nCs = 1; |
stjo2809 | 0:b5950cd5e330 | 65 | |
stjo2809 | 0:b5950cd5e330 | 66 | int responce = _make_value_from_responce(responce_msb, responce_lsb); |
stjo2809 | 0:b5950cd5e330 | 67 | |
stjo2809 | 0:b5950cd5e330 | 68 | return responce; |
stjo2809 | 1:cf7328552994 | 69 | } |