This is library for mcp320x. But operation check is not finished only "mcp3204".

Dependents:   Nucleo_mcp3204test

Committer:
SK_ROBO_
Date:
Fri Jan 29 02:30:11 2016 +0000
Revision:
0:775667bb8069
Child:
1:66fdf46dc4de
first commit.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SK_ROBO_ 0:775667bb8069 1 /* mbed simplified access to Microchip MCP320x 12 bits ADC devices (SPI)
SK_ROBO_ 0:775667bb8069 2 * Copyright (c) 2013-2013 ygarcia, MIT License
SK_ROBO_ 0:775667bb8069 3 *
SK_ROBO_ 0:775667bb8069 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
SK_ROBO_ 0:775667bb8069 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
SK_ROBO_ 0:775667bb8069 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
SK_ROBO_ 0:775667bb8069 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
SK_ROBO_ 0:775667bb8069 8 * furnished to do so, subject to the following conditions:
SK_ROBO_ 0:775667bb8069 9 *
SK_ROBO_ 0:775667bb8069 10 * The above copyright notice and this permission notice shall be included in all copies or
SK_ROBO_ 0:775667bb8069 11 * substantial portions of the Software.
SK_ROBO_ 0:775667bb8069 12 *
SK_ROBO_ 0:775667bb8069 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
SK_ROBO_ 0:775667bb8069 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
SK_ROBO_ 0:775667bb8069 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
SK_ROBO_ 0:775667bb8069 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SK_ROBO_ 0:775667bb8069 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SK_ROBO_ 0:775667bb8069 18 */
SK_ROBO_ 0:775667bb8069 19 #if !defined(__MCP320x_H__)
SK_ROBO_ 0:775667bb8069 20 #define __MCP320x_H__
SK_ROBO_ 0:775667bb8069 21
SK_ROBO_ 0:775667bb8069 22 #include <string>
SK_ROBO_ 0:775667bb8069 23 #include <vector>
SK_ROBO_ 0:775667bb8069 24
SK_ROBO_ 0:775667bb8069 25 #include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary
SK_ROBO_ 0:775667bb8069 26
SK_ROBO_ 0:775667bb8069 27 namespace MCP320x_SPI {
SK_ROBO_ 0:775667bb8069 28
SK_ROBO_ 0:775667bb8069 29 /** This class provides simplified SPI access to a Microchip MCP320x 12-Bit A/D Converter with SPI Serial Interface device. V0.0.0.1
SK_ROBO_ 0:775667bb8069 30 *
SK_ROBO_ 0:775667bb8069 31 * Microchip MCP42xxx/MCP41xxx Serial EEPROM device reference: DS11195C
SK_ROBO_ 0:775667bb8069 32 *
SK_ROBO_ 0:775667bb8069 33 * Note that MCP3201 has no SI pin, only a SO output pin
SK_ROBO_ 0:775667bb8069 34 * Note that for SPI details, please visit http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
SK_ROBO_ 0:775667bb8069 35 *
SK_ROBO_ 0:775667bb8069 36 * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V
SK_ROBO_ 0:775667bb8069 37 * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com)
SK_ROBO_ 0:775667bb8069 38 */
SK_ROBO_ 0:775667bb8069 39 class CMCP320x_SPI {
SK_ROBO_ 0:775667bb8069 40 /** Reference counter used to guarentee unicity of the instance of SPI class
SK_ROBO_ 0:775667bb8069 41 */
SK_ROBO_ 0:775667bb8069 42 static unsigned char SPIModuleRefCounter;
SK_ROBO_ 0:775667bb8069 43
SK_ROBO_ 0:775667bb8069 44 /** ChipSelect (pin 1) see DS21290F-page 15 Clause 3.3 Chip Select/Shutdown (CS/SHDN)
SK_ROBO_ 0:775667bb8069 45 */
SK_ROBO_ 0:775667bb8069 46 DigitalOut *_cs;
SK_ROBO_ 0:775667bb8069 47
SK_ROBO_ 0:775667bb8069 48 /** An unique instance of SPI class
SK_ROBO_ 0:775667bb8069 49 */
SK_ROBO_ 0:775667bb8069 50 SPI *_spiInstance;
SK_ROBO_ 0:775667bb8069 51
SK_ROBO_ 0:775667bb8069 52 /** ADC sample structure
SK_ROBO_ 0:775667bb8069 53 */
SK_ROBO_ 0:775667bb8069 54 typedef union {
SK_ROBO_ 0:775667bb8069 55 unsigned int value;
SK_ROBO_ 0:775667bb8069 56 struct {
SK_ROBO_ 0:775667bb8069 57 unsigned char bytes[2];
SK_ROBO_ 0:775667bb8069 58 };
SK_ROBO_ 0:775667bb8069 59 } ADCValue;
SK_ROBO_ 0:775667bb8069 60 ADCValue _sample;
SK_ROBO_ 0:775667bb8069 61 /** Number of channels according to the IC type
SK_ROBO_ 0:775667bb8069 62 */
SK_ROBO_ 0:775667bb8069 63 unsigned char _channelsNum;
SK_ROBO_ 0:775667bb8069 64 /** Set to true for single-ended inputs configuration, false for pseudo-differential inputs
SK_ROBO_ 0:775667bb8069 65 * @see DS21298E-page 19 Clause 5.0 SERIAL COMMUNICATIONS
SK_ROBO_ 0:775667bb8069 66 */
SK_ROBO_ 0:775667bb8069 67 unsigned char _settings;
SK_ROBO_ 0:775667bb8069 68 public:
SK_ROBO_ 0:775667bb8069 69 /** MCP320x familly
SK_ROBO_ 0:775667bb8069 70 */
SK_ROBO_ 0:775667bb8069 71 enum Mcp320xFamilly {
SK_ROBO_ 0:775667bb8069 72 _3201 = 0x00, /** See DS21290F */
SK_ROBO_ 0:775667bb8069 73 _3204 = 0x01, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 74 _3208 = 0x03 /** See DS21298E */
SK_ROBO_ 0:775667bb8069 75 };
SK_ROBO_ 0:775667bb8069 76 Mcp320xFamilly _familly;
SK_ROBO_ 0:775667bb8069 77 /** MCP320x channels to read
SK_ROBO_ 0:775667bb8069 78 */
SK_ROBO_ 0:775667bb8069 79 enum Mcp320xChannels {
SK_ROBO_ 0:775667bb8069 80 CH0 = 0x00, /** See DS21290F/DS21290F */
SK_ROBO_ 0:775667bb8069 81 CH1 = 0x01, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 82 CH2 = 0x02, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 83 CH3 = 0x03, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 84 CH4 = 0x04, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 85 CH5 = 0x05, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 86 CH6 = 0x06, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 87 CH7 = 0x07 /** See DS21298E */
SK_ROBO_ 0:775667bb8069 88 };
SK_ROBO_ 0:775667bb8069 89 public:
SK_ROBO_ 0:775667bb8069 90 /** Constructor with Write Protect command pin wired.
SK_ROBO_ 0:775667bb8069 91 *
SK_ROBO_ 0:775667bb8069 92 * @param p_mosi: MBed pin for SDI
SK_ROBO_ 0:775667bb8069 93 * @param p_miso: MBed pin for SDO
SK_ROBO_ 0:775667bb8069 94 * @param p_sclk: MBed pin for CLK
SK_ROBO_ 0:775667bb8069 95 * @param p_cs : MBed pin for Chip Select. If NC, assumes that application manage /CS, default value is NC, not connected
SK_ROBO_ 0:775667bb8069 96 * @param p_familly: MCP320x familly. Default: _3201
SK_ROBO_ 0:775667bb8069 97 * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
SK_ROBO_ 0:775667bb8069 98 */
SK_ROBO_ 0:775667bb8069 99 CMCP320x_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const Mcp320xFamilly p_familly = _3201, const unsigned int p_frequency = 1000000);
SK_ROBO_ 0:775667bb8069 100
SK_ROBO_ 0:775667bb8069 101 /** Destructor
SK_ROBO_ 0:775667bb8069 102 * If managed, the /CS pin is set to 1 before to release it
SK_ROBO_ 0:775667bb8069 103 */
SK_ROBO_ 0:775667bb8069 104 virtual ~CMCP320x_SPI();
SK_ROBO_ 0:775667bb8069 105
SK_ROBO_ 0:775667bb8069 106 /** Used to return the unique instance of SPI instance
SK_ROBO_ 0:775667bb8069 107 */
SK_ROBO_ 0:775667bb8069 108 inline const SPI * operator * () { return (const SPI *)_spiInstance; };
SK_ROBO_ 0:775667bb8069 109
SK_ROBO_ 0:775667bb8069 110 /**
SK_ROBO_ 0:775667bb8069 111 * @desc Launch an analog to digital conversion on the specified channel
SK_ROBO_ 0:775667bb8069 112 * @param p_channel The channel to convert
SK_ROBO_ 0:775667bb8069 113 * @return The converted value
SK_ROBO_ 0:775667bb8069 114 */
SK_ROBO_ 0:775667bb8069 115 float Read(const Mcp320xChannels p_channels = CH1);
SK_ROBO_ 0:775667bb8069 116
SK_ROBO_ 0:775667bb8069 117 /**
SK_ROBO_ 0:775667bb8069 118 * @desc Change current configuration (only for MCP3204/8)
SK_ROBO_ 0:775667bb8069 119 * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
SK_ROBO_ 0:775667bb8069 120 * @see DS21298E-page 17 Clause 4.1 Analog Inputs
SK_ROBO_ 0:775667bb8069 121 */
SK_ROBO_ 0:775667bb8069 122 void SetConfig(const bool p_settings);
SK_ROBO_ 0:775667bb8069 123
SK_ROBO_ 0:775667bb8069 124 /** Shutdown the device
SK_ROBO_ 0:775667bb8069 125 */
SK_ROBO_ 0:775667bb8069 126 bool Shutdown(const bool p_shutdown);
SK_ROBO_ 0:775667bb8069 127
SK_ROBO_ 0:775667bb8069 128 private:
SK_ROBO_ 0:775667bb8069 129 /** Internal reference identifier
SK_ROBO_ 0:775667bb8069 130 */
SK_ROBO_ 0:775667bb8069 131 std::string _internalId;
SK_ROBO_ 0:775667bb8069 132
SK_ROBO_ 0:775667bb8069 133 private:
SK_ROBO_ 0:775667bb8069 134
SK_ROBO_ 0:775667bb8069 135 /**
SK_ROBO_ 0:775667bb8069 136 * @desc Launch an analog to digital conversion on the specified channel for MCP3201
SK_ROBO_ 0:775667bb8069 137 * @see DS21290F-page 17 Clause 4.1 Analog Inputs
SK_ROBO_ 0:775667bb8069 138 */
SK_ROBO_ 0:775667bb8069 139 void Read_3201();
SK_ROBO_ 0:775667bb8069 140
SK_ROBO_ 0:775667bb8069 141 /**
SK_ROBO_ 0:775667bb8069 142 * @desc Launch an analog to digital conversion on the specified channel for MCP3204/8
SK_ROBO_ 0:775667bb8069 143 * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
SK_ROBO_ 0:775667bb8069 144 * @see DS21298E-page 17 Clause 4.1 Analog Inputs
SK_ROBO_ 0:775667bb8069 145 */
SK_ROBO_ 0:775667bb8069 146 void Read_320x(const Mcp320xChannels p_channels);
SK_ROBO_ 0:775667bb8069 147
SK_ROBO_ 0:775667bb8069 148 }; // End of class CMCP320x_SPI
SK_ROBO_ 0:775667bb8069 149
SK_ROBO_ 0:775667bb8069 150 } // End of namespace MCP320x_SPI
SK_ROBO_ 0:775667bb8069 151
SK_ROBO_ 0:775667bb8069 152 using namespace MCP320x_SPI;
SK_ROBO_ 0:775667bb8069 153
SK_ROBO_ 0:775667bb8069 154 #endif // __MCP320x_SPI_H__