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

Dependents:   Nucleo_mcp3204test

Committer:
SK_ROBO_
Date:
Fri Jan 29 02:36:06 2016 +0000
Revision:
1:66fdf46dc4de
Parent:
0:775667bb8069
Child:
2:d2c51376ee7c
deleted comment.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SK_ROBO_ 0:775667bb8069 1 #if !defined(__MCP320x_H__)
SK_ROBO_ 0:775667bb8069 2 #define __MCP320x_H__
SK_ROBO_ 0:775667bb8069 3
SK_ROBO_ 0:775667bb8069 4 #include <string>
SK_ROBO_ 0:775667bb8069 5 #include <vector>
SK_ROBO_ 0:775667bb8069 6
SK_ROBO_ 0:775667bb8069 7 #include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary
SK_ROBO_ 0:775667bb8069 8
SK_ROBO_ 0:775667bb8069 9 namespace MCP320x_SPI {
SK_ROBO_ 0:775667bb8069 10 class CMCP320x_SPI {
SK_ROBO_ 0:775667bb8069 11 /** Reference counter used to guarentee unicity of the instance of SPI class
SK_ROBO_ 0:775667bb8069 12 */
SK_ROBO_ 0:775667bb8069 13 static unsigned char SPIModuleRefCounter;
SK_ROBO_ 0:775667bb8069 14
SK_ROBO_ 0:775667bb8069 15 /** ChipSelect (pin 1) see DS21290F-page 15 Clause 3.3 Chip Select/Shutdown (CS/SHDN)
SK_ROBO_ 0:775667bb8069 16 */
SK_ROBO_ 0:775667bb8069 17 DigitalOut *_cs;
SK_ROBO_ 0:775667bb8069 18
SK_ROBO_ 0:775667bb8069 19 /** An unique instance of SPI class
SK_ROBO_ 0:775667bb8069 20 */
SK_ROBO_ 0:775667bb8069 21 SPI *_spiInstance;
SK_ROBO_ 0:775667bb8069 22
SK_ROBO_ 0:775667bb8069 23 /** ADC sample structure
SK_ROBO_ 0:775667bb8069 24 */
SK_ROBO_ 0:775667bb8069 25 typedef union {
SK_ROBO_ 0:775667bb8069 26 unsigned int value;
SK_ROBO_ 0:775667bb8069 27 struct {
SK_ROBO_ 0:775667bb8069 28 unsigned char bytes[2];
SK_ROBO_ 0:775667bb8069 29 };
SK_ROBO_ 0:775667bb8069 30 } ADCValue;
SK_ROBO_ 0:775667bb8069 31 ADCValue _sample;
SK_ROBO_ 0:775667bb8069 32 /** Number of channels according to the IC type
SK_ROBO_ 0:775667bb8069 33 */
SK_ROBO_ 0:775667bb8069 34 unsigned char _channelsNum;
SK_ROBO_ 0:775667bb8069 35 /** Set to true for single-ended inputs configuration, false for pseudo-differential inputs
SK_ROBO_ 0:775667bb8069 36 * @see DS21298E-page 19 Clause 5.0 SERIAL COMMUNICATIONS
SK_ROBO_ 0:775667bb8069 37 */
SK_ROBO_ 0:775667bb8069 38 unsigned char _settings;
SK_ROBO_ 0:775667bb8069 39 public:
SK_ROBO_ 0:775667bb8069 40 /** MCP320x familly
SK_ROBO_ 0:775667bb8069 41 */
SK_ROBO_ 0:775667bb8069 42 enum Mcp320xFamilly {
SK_ROBO_ 0:775667bb8069 43 _3201 = 0x00, /** See DS21290F */
SK_ROBO_ 0:775667bb8069 44 _3204 = 0x01, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 45 _3208 = 0x03 /** See DS21298E */
SK_ROBO_ 0:775667bb8069 46 };
SK_ROBO_ 0:775667bb8069 47 Mcp320xFamilly _familly;
SK_ROBO_ 0:775667bb8069 48 /** MCP320x channels to read
SK_ROBO_ 0:775667bb8069 49 */
SK_ROBO_ 0:775667bb8069 50 enum Mcp320xChannels {
SK_ROBO_ 0:775667bb8069 51 CH0 = 0x00, /** See DS21290F/DS21290F */
SK_ROBO_ 0:775667bb8069 52 CH1 = 0x01, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 53 CH2 = 0x02, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 54 CH3 = 0x03, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 55 CH4 = 0x04, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 56 CH5 = 0x05, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 57 CH6 = 0x06, /** See DS21298E */
SK_ROBO_ 0:775667bb8069 58 CH7 = 0x07 /** See DS21298E */
SK_ROBO_ 0:775667bb8069 59 };
SK_ROBO_ 0:775667bb8069 60 public:
SK_ROBO_ 0:775667bb8069 61 /** Constructor with Write Protect command pin wired.
SK_ROBO_ 0:775667bb8069 62 *
SK_ROBO_ 0:775667bb8069 63 * @param p_mosi: MBed pin for SDI
SK_ROBO_ 0:775667bb8069 64 * @param p_miso: MBed pin for SDO
SK_ROBO_ 0:775667bb8069 65 * @param p_sclk: MBed pin for CLK
SK_ROBO_ 0:775667bb8069 66 * @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 67 * @param p_familly: MCP320x familly. Default: _3201
SK_ROBO_ 0:775667bb8069 68 * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
SK_ROBO_ 0:775667bb8069 69 */
SK_ROBO_ 0:775667bb8069 70 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 71
SK_ROBO_ 0:775667bb8069 72 /** Destructor
SK_ROBO_ 0:775667bb8069 73 * If managed, the /CS pin is set to 1 before to release it
SK_ROBO_ 0:775667bb8069 74 */
SK_ROBO_ 0:775667bb8069 75 virtual ~CMCP320x_SPI();
SK_ROBO_ 0:775667bb8069 76
SK_ROBO_ 0:775667bb8069 77 /** Used to return the unique instance of SPI instance
SK_ROBO_ 0:775667bb8069 78 */
SK_ROBO_ 0:775667bb8069 79 inline const SPI * operator * () { return (const SPI *)_spiInstance; };
SK_ROBO_ 0:775667bb8069 80
SK_ROBO_ 0:775667bb8069 81 /**
SK_ROBO_ 0:775667bb8069 82 * @desc Launch an analog to digital conversion on the specified channel
SK_ROBO_ 0:775667bb8069 83 * @param p_channel The channel to convert
SK_ROBO_ 0:775667bb8069 84 * @return The converted value
SK_ROBO_ 0:775667bb8069 85 */
SK_ROBO_ 0:775667bb8069 86 float Read(const Mcp320xChannels p_channels = CH1);
SK_ROBO_ 0:775667bb8069 87
SK_ROBO_ 0:775667bb8069 88 /**
SK_ROBO_ 0:775667bb8069 89 * @desc Change current configuration (only for MCP3204/8)
SK_ROBO_ 0:775667bb8069 90 * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
SK_ROBO_ 0:775667bb8069 91 * @see DS21298E-page 17 Clause 4.1 Analog Inputs
SK_ROBO_ 0:775667bb8069 92 */
SK_ROBO_ 0:775667bb8069 93 void SetConfig(const bool p_settings);
SK_ROBO_ 0:775667bb8069 94
SK_ROBO_ 0:775667bb8069 95 /** Shutdown the device
SK_ROBO_ 0:775667bb8069 96 */
SK_ROBO_ 0:775667bb8069 97 bool Shutdown(const bool p_shutdown);
SK_ROBO_ 0:775667bb8069 98
SK_ROBO_ 0:775667bb8069 99 private:
SK_ROBO_ 0:775667bb8069 100 /** Internal reference identifier
SK_ROBO_ 0:775667bb8069 101 */
SK_ROBO_ 0:775667bb8069 102 std::string _internalId;
SK_ROBO_ 0:775667bb8069 103
SK_ROBO_ 0:775667bb8069 104 private:
SK_ROBO_ 0:775667bb8069 105
SK_ROBO_ 0:775667bb8069 106 /**
SK_ROBO_ 0:775667bb8069 107 * @desc Launch an analog to digital conversion on the specified channel for MCP3201
SK_ROBO_ 0:775667bb8069 108 * @see DS21290F-page 17 Clause 4.1 Analog Inputs
SK_ROBO_ 0:775667bb8069 109 */
SK_ROBO_ 0:775667bb8069 110 void Read_3201();
SK_ROBO_ 0:775667bb8069 111
SK_ROBO_ 0:775667bb8069 112 /**
SK_ROBO_ 0:775667bb8069 113 * @desc Launch an analog to digital conversion on the specified channel for MCP3204/8
SK_ROBO_ 0:775667bb8069 114 * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
SK_ROBO_ 0:775667bb8069 115 * @see DS21298E-page 17 Clause 4.1 Analog Inputs
SK_ROBO_ 0:775667bb8069 116 */
SK_ROBO_ 0:775667bb8069 117 void Read_320x(const Mcp320xChannels p_channels);
SK_ROBO_ 0:775667bb8069 118
SK_ROBO_ 0:775667bb8069 119 }; // End of class CMCP320x_SPI
SK_ROBO_ 0:775667bb8069 120
SK_ROBO_ 0:775667bb8069 121 } // End of namespace MCP320x_SPI
SK_ROBO_ 0:775667bb8069 122
SK_ROBO_ 0:775667bb8069 123 using namespace MCP320x_SPI;
SK_ROBO_ 0:775667bb8069 124
SK_ROBO_ 0:775667bb8069 125 #endif // __MCP320x_SPI_H__