2.7V 4-Channel 12-Bit A/D Converters with SPI™ Serial Interface

Fork of MCP3204 by TeamElectronics

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?

UserRevisionLine numberNew 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
stjo2809 0:b5950cd5e330 25 #ifndef MBED_MCP3204_H
stjo2809 0:b5950cd5e330 26 #define MBED_MCP3204_H
stjo2809 0:b5950cd5e330 27
stjo2809 0:b5950cd5e330 28 //=============================================================================
stjo2809 0:b5950cd5e330 29 // Declaration of variables & custom #defines
stjo2809 0:b5950cd5e330 30 //=============================================================================
stjo2809 0:b5950cd5e330 31
stjo2809 0:b5950cd5e330 32 #define CH0 0x00 // Channel 0 for single and for differential is CH0 = IN+ and CH1 = IN-
stjo2809 0:b5950cd5e330 33 #define CH1 0x40 // Channel 1 for single and for differential is CH0 = IN- and CH1 = IN+
stjo2809 0:b5950cd5e330 34 #define CH2 0x80 // Channel 2 for single and for differential is CH2 = IN+ and CH3 = IN-
stjo2809 0:b5950cd5e330 35 #define CH3 0xC0 // Channel 3 for single and for differential is CH2 = IN- and CH3 = IN+
stjo2809 0:b5950cd5e330 36 #define SINGLE 0x06 // Single command
stjo2809 0:b5950cd5e330 37 #define DIFFERENTIAL 0x04 // Differential command
stjo2809 0:b5950cd5e330 38
stjo2809 0:b5950cd5e330 39 //=============================================================================
stjo2809 0:b5950cd5e330 40 // Functions Declaration
stjo2809 0:b5950cd5e330 41 //=============================================================================
stjo2809 0:b5950cd5e330 42
stjo2809 0:b5950cd5e330 43 /** Interface to the 4-Channel 12-Bit A/D Converters with SPI interface
stjo2809 0:b5950cd5e330 44 *
stjo2809 0:b5950cd5e330 45 * Using the driver:
stjo2809 0:b5950cd5e330 46 * - remenber to setup SPI in main routine or use pins instance.
stjo2809 0:b5950cd5e330 47 *
stjo2809 0:b5950cd5e330 48 * Defaults in this driver on start up:
stjo2809 0:b5950cd5e330 49 * - Datasheet start up
stjo2809 0:b5950cd5e330 50 */
stjo2809 0:b5950cd5e330 51 class MCP3204 {
stjo2809 0:b5950cd5e330 52 public:
stjo2809 0:b5950cd5e330 53 /** Create an instance of the MCP3204 connected via specfied SPI instance.
stjo2809 0:b5950cd5e330 54 *
stjo2809 0:b5950cd5e330 55 * @param spi The mbed SPI instance (make in main routine)
stjo2809 0:b5950cd5e330 56 * @param nCs The SPI chip select pin.
stjo2809 0:b5950cd5e330 57 */
stjo2809 0:b5950cd5e330 58 MCP3204(SPI& spi, PinName nCs);
stjo2809 0:b5950cd5e330 59
stjo2809 0:b5950cd5e330 60 /** Create an instance of the MCP4261 connected with SPI pins.
stjo2809 0:b5950cd5e330 61 *
stjo2809 0:b5950cd5e330 62 * @param mosi The SPI Master Output, Slave Input pin.
stjo2809 0:b5950cd5e330 63 * @param miso The SPI Master Input, Slave Output pin.
stjo2809 0:b5950cd5e330 64 * @param sck The SPI Serial Clock pin.
stjo2809 0:b5950cd5e330 65 * @param nCs The SPI chip select pin.
stjo2809 0:b5950cd5e330 66 */
stjo2809 0:b5950cd5e330 67 MCP3204(PinName mosi, PinName miso,PinName sck, PinName nCs);
stjo2809 0:b5950cd5e330 68
stjo2809 0:b5950cd5e330 69
stjo2809 0:b5950cd5e330 70 /** Read from single channel.
stjo2809 0:b5950cd5e330 71 *
stjo2809 0:b5950cd5e330 72 * use defines to simplify use of function ( CH0, CH1, CH2, CH3)
stjo2809 0:b5950cd5e330 73 * @param channel The channel to receive the 12 bits value from.
stjo2809 0:b5950cd5e330 74 */
stjo2809 0:b5950cd5e330 75 int sgl(char channel);
stjo2809 0:b5950cd5e330 76
stjo2809 0:b5950cd5e330 77 /** Read from differential channel.
stjo2809 0:b5950cd5e330 78 *
stjo2809 0:b5950cd5e330 79 * use defines to simplify use of function ( CH0, CH1, CH2, CH3)
stjo2809 0:b5950cd5e330 80 * @param channel The channel to receive the 12 bits value from.
stjo2809 0:b5950cd5e330 81 */
stjo2809 0:b5950cd5e330 82 int diff(char channel);
stjo2809 0:b5950cd5e330 83
stjo2809 0:b5950cd5e330 84
stjo2809 0:b5950cd5e330 85 private:
stjo2809 0:b5950cd5e330 86 SPI& _spi;
stjo2809 0:b5950cd5e330 87 DigitalOut _nCs;
stjo2809 0:b5950cd5e330 88
stjo2809 0:b5950cd5e330 89 int _make_value_from_responce(int responce_msb, int responce_lsb);
stjo2809 0:b5950cd5e330 90 int _read(char sgl_or_diff, char channel);
stjo2809 0:b5950cd5e330 91
stjo2809 0:b5950cd5e330 92 };
stjo2809 0:b5950cd5e330 93
stjo2809 0:b5950cd5e330 94 #endif