2.7V 4-Channel 12-Bit A/D Converters with SPI™ Serial Interface
Fork of MCP3204 by
MCP3204.cpp@0:b5950cd5e330, 2015-03-23 (annotated)
- Committer:
- stjo2809
- Date:
- Mon Mar 23 07:49:58 2015 +0000
- Revision:
- 0:b5950cd5e330
- Child:
- 1:cf7328552994
revision 0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stjo2809 | 0:b5950cd5e330 | 1 | /* mbed MCP3204 Library, for driving the 2.7V 4-Channel/8-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 | 0:b5950cd5e330 | 32 | |
stjo2809 | 0:b5950cd5e330 | 33 | } |
stjo2809 | 0:b5950cd5e330 | 34 | |
stjo2809 | 0:b5950cd5e330 | 35 | |
stjo2809 | 0:b5950cd5e330 | 36 | MCP3204::MCP3204(PinName mosi, PinName miso,PinName sck, PinName nCs) : _mosi(mosi), _miso(miso), _sck(sck), _nCs(nCs) |
stjo2809 | 0:b5950cd5e330 | 37 | { |
stjo2809 | 0:b5950cd5e330 | 38 | SPI _spi(_mosi,_miso,_sck); |
stjo2809 | 0:b5950cd5e330 | 39 | } |
stjo2809 | 0:b5950cd5e330 | 40 | |
stjo2809 | 0:b5950cd5e330 | 41 | |
stjo2809 | 0:b5950cd5e330 | 42 | int MCP3204::sgl(char channel) |
stjo2809 | 0:b5950cd5e330 | 43 | { |
stjo2809 | 0:b5950cd5e330 | 44 | return _read(SINGLE, channel); |
stjo2809 | 0:b5950cd5e330 | 45 | } |
stjo2809 | 0:b5950cd5e330 | 46 | |
stjo2809 | 0:b5950cd5e330 | 47 | int MCP3204::diff(char channel) |
stjo2809 | 0:b5950cd5e330 | 48 | { |
stjo2809 | 0:b5950cd5e330 | 49 | return _read(DIFFERENTIAL, channel); |
stjo2809 | 0:b5950cd5e330 | 50 | } |
stjo2809 | 0:b5950cd5e330 | 51 | |
stjo2809 | 0:b5950cd5e330 | 52 | //============================================================================= |
stjo2809 | 0:b5950cd5e330 | 53 | // Private functions |
stjo2809 | 0:b5950cd5e330 | 54 | //============================================================================= |
stjo2809 | 0:b5950cd5e330 | 55 | |
stjo2809 | 0:b5950cd5e330 | 56 | int MCP3204::_make_value_from_responce(int _msb, int _lsb) |
stjo2809 | 0:b5950cd5e330 | 57 | { |
stjo2809 | 0:b5950cd5e330 | 58 | --- code --- |
stjo2809 | 0:b5950cd5e330 | 59 | } |
stjo2809 | 0:b5950cd5e330 | 60 | |
stjo2809 | 0:b5950cd5e330 | 61 | int MCP3204::_read(char sgl_or_diff, char channel) |
stjo2809 | 0:b5950cd5e330 | 62 | { |
stjo2809 | 0:b5950cd5e330 | 63 | _make_value_from_responce(int responce_msb, int responce_lsb); |
stjo2809 | 0:b5950cd5e330 | 64 | |
stjo2809 | 0:b5950cd5e330 | 65 | _nCs = 0; |
stjo2809 | 0:b5950cd5e330 | 66 | _spi.write(sgl_or_diff); |
stjo2809 | 0:b5950cd5e330 | 67 | int responce_msb = _spi.write(channel); |
stjo2809 | 0:b5950cd5e330 | 68 | int responce_lsb = _spi.write(0xff); // don't care bits |
stjo2809 | 0:b5950cd5e330 | 69 | _nCs = 1; |
stjo2809 | 0:b5950cd5e330 | 70 | |
stjo2809 | 0:b5950cd5e330 | 71 | int responce = _make_value_from_responce(responce_msb, responce_lsb); |
stjo2809 | 0:b5950cd5e330 | 72 | |
stjo2809 | 0:b5950cd5e330 | 73 | return responce; |
stjo2809 | 0:b5950cd5e330 | 74 | } |