Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices
Dependents: MCP41xxxApp MCP320xApp MCP41xxxApp
Diff: MCP4xxxx_SPI.h
- Revision:
- 6:ded0d8a6729c
- Parent:
- 5:4f6133144e7e
- Child:
- 7:12a0d89aa72f
--- a/MCP4xxxx_SPI.h Fri Apr 05 13:36:29 2013 +0000 +++ b/MCP4xxxx_SPI.h Thu Jun 06 07:59:16 2013 +0000 @@ -26,13 +26,19 @@ namespace MCP4xxxx_SPI { - /** This class provides simplified SPI access to a Microchip MCP42xxx/MCP41xxx Digital Potentiometer device. V0.0.0.1 + /** This class provides simplified SPI access to a Microchip MCP42xxx/MCP41xxx Digital Potentiometer device. V0.0.0.2 + * The SPI static settings are: 16bits, mode (0,0) + * This class manages ONLY ONE /CS pin. If more than one SPI devices are used, ALL /CS pins shall be done by the application itself * * Microchip MCP42xxx/MCP41xxx Serial EEPROM device reference: DS11195C + * Microchip MCP41xx/MCP42xx Serial EEPROM device reference: DS122060B * + * Note that MCP41xxx has no SO pin, only a SOI input pin + * Note that MCP42xxx has a SO pin reserved for daisy-chain configuration + * Note that MCP41xx/MCP42xx increment/decrement operations are not supported due to the 16 bits SPI setting * Note that for SPI details, please visit http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus * - * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V + * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V and in mixte power mode 3.3V for mbed and 5V for the Microchip 24LCxx Serial EEPROM device * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com) */ class CMCP4xxxx_SPI { @@ -55,17 +61,37 @@ /** An unique instance of SPI class */ SPI *_spiInstance; + /** Increment step value used for inc/dec methods, when inc/dec commands are not supported (such as _4xxxx famillies) + */ + unsigned char _incStep; + public: + /** MCP32xx familly + */ + enum Mcp4xxxFamilly { + _41xxx = 0x00, //<! 41xxx familly such as MCP41010 - See DS11195C + _42xxx = 0x01, //<! 42xxx familly such as MCP42010 - See DS11195C + _41xx = 0x02, //<! 41xx familly such as MCP4251 - Single Potentiometer/Rheostat + _42xx = 0x03 //<! 42x2 familly such as MCP42x2 - Dual Potentiometer/Rheostat + }; + Mcp4xxxFamilly _familly; public: /** Authorized commands * See DS11195C-page 18 */ enum Commands { - WriteToPot1, //<! Write to digital potentiometer #1 - WriteToPot2, //<! Write to digital potentiometer #2 - WriteToBoth, //<! Write to both digital potentiometers - ShutdownPot1, //<! Shutdown digital potentiometer #1 - ShutdownPot2, //<! Shutdown digital potentiometer #2 - ShutdownBoth, //<! Shutdown both digital potentiometers + WriteToDigiPot1, //<! Write to digital potentiometer #1 + WriteToDigiPot2, //<! Write to digital potentiometer #2 + WriteToBoth, //<! Write to both digital potentiometers + ShutdownDigiPot1, //<! Shutdown digital potentiometer #1 + ShutdownDigiPot2, //<! Shutdown digital potentiometer #2 + ShutdownBoth, //<! Shutdown both digital potentiometers + }; + /** Register adresses + * See DS22060B-page 33 Clause 4.0 FUNCTIONAL OVERVIEW + */ + enum Addresses { + TCon = 0x04, + Status = 0x05 }; public: /** Constructor with Write Protect command pin wired. @@ -78,10 +104,9 @@ * @param p_shdn: MBed pin to manage /SHDN input. If NC, /SHDN is not managed, default value is NC, not connected * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz */ - CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const PinName p_reset = NC, const PinName p_shdn = NC, const unsigned int p_frequency = 1000000); + CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const PinName p_reset = NC, const PinName p_shdn = NC, const Mcp4xxxFamilly familly = _41xxx, const unsigned int p_frequency = 1000000); /** Destructor - * /CS pin is set to 1 before to release it */ virtual ~CMCP4xxxx_SPI(); @@ -89,9 +114,34 @@ */ inline const SPI * operator * () { return (const SPI *)_spiInstance; }; + /** Write a value to the specified register. Only for _41xx/_42xx famillies + * @param p_address The register to be written + * @param p_value The value to write + * @return 0x0000 on success, any value otherwise + */ + unsigned short WriteRegister(const Addresses p_address, const unsigned char p_value); + + /** Read the content of the specified register. Only for _41xx/_42xx famillies + * @param p_address The register to be written + * @return The register value + */ + unsigned short ReadRegister(const Addresses p_address); + + /** Increment the specified digital potentiometer + * @param p_command The digital potentiometer to increment. Default: WriteToDigiPot1 + * @return 0x0000 on success, any value otherwise + */ + unsigned short Incrememt(const Commands p_command = WriteToDigiPot1); + + /** Decrement the specified digital potentiometer + * @param p_command The digital potentiometer to decrement. Default: WriteToDigiPot1 + * @return 0x0000 on success, any value otherwise + */ + unsigned short Decrement(const Commands p_command = WriteToDigiPot1); + /** Send a write a command (WriteToPot1, WriteToPot2 or WriteBoth) * - * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18) + * @param p_command The command to execute (See DS11195C-page 18) * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation) * @return 0x0000 on success, 0Xffff otherwise * Exemple: @@ -108,42 +158,48 @@ /** Send a shutdown a command (ShutdownPot1, ShutdownPot2 or ShutdownBoth) * - * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18) - * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation) + * @param p_command The command to execute (See DS11195C-page 18) + * @param p_set Set to true to shutdown, false to set up. Only used for _41xx/_42xx famillies (See DS22060B-page 36 REGISTER 4-2: TCON BITS) * @return 0x0000 on success, 0Xffff otherwise * Exemple: * @code * ... * g_chipSelect.write(0); - * g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1); + * g_digitalPot.Shutdown(CMCP4xxxx_SPI::ShutdownPot1); * g_chipSelect.write(1); * ... * @endcode */ - unsigned short Write(const Commands p_command); + unsigned short Shutdown(const Commands p_command, const bool p_set = true); /** Write a NOP command - */ + */ unsigned short Write(); + /** Change the current familly + * @param familly The new type of ICs + * @return the previous type of ICs + */ + Mcp4xxxFamilly SetFamilly(const Mcp4xxxFamilly familly); + /** Reset the device - * @code - * unsigned char potLevel; - * ... - * g_chipSelect.write(0); - * g_digitalPot.Reset(); - * g_chipSelect.write(1); - * ... - * @endcode - */ - bool Reset(); + * @code + * unsigned char potLevel; + * ... + * g_digitalPot.Reset(); + * ... + * @endcode + */ + void Reset(); /** Shutdown the device */ - bool Shutdown(const bool p_set); + void Shutdown(const bool p_set); private: - /** Internal reference identifier + /** Internal reference identifier. Only used when /RS in is available + * + * @param p_set Set to true to shutdown, false to set up */ std::string _internalId;