Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices
Dependents: MCP41xxxApp MCP320xApp MCP41xxxApp
Revision 1:cf3cee91eb87, committed 2013-01-27
- Comitter:
- Yann
- Date:
- Sun Jan 27 17:04:05 2013 +0000
- Parent:
- 0:03314ad622d6
- Child:
- 2:7c27fb9785be
- Commit message:
- Validate library with hardware (MCP4100)
Changed in this revision
| MCP4xxxx_SPI.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MCP4xxxx_SPI.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MCP4xxxx_SPI.cpp Fri Jan 25 16:10:58 2013 +0000
+++ b/MCP4xxxx_SPI.cpp Sun Jan 27 17:04:05 2013 +0000
@@ -1,5 +1,5 @@
/* mbed simplified access to Microchip 24LCxx Serial EEPROM devices (SPI)
- * Copyright (c) 2010-2012 ygarcia, MIT License
+ * Copyright (c) 2013 ygarcia, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -84,6 +84,12 @@
unsigned short CMCP4xxxx_SPI::Write(const Commands p_command, const unsigned char p_value) {
DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x - 0x%02x", (unsigned char)p_command, p_value)
+ // Sanity check
+ if ((p_command != WriteToPot1) && (p_command != WriteToPot2) && (p_command != WriteToBoth)) {
+ // Wrong parameters
+ return (unsigned short) -1;
+ }
+
unsigned short command = 0;
switch (p_command) {
case WriteToPot1:
@@ -92,9 +98,28 @@
case WriteToPot2:
command = (0x12 << 8 | p_value);
break;
- case WriteToPotBoth:
+ default:
command = (0x13 << 8 | p_value);
- break;
+ } // End of 'switch' statement
+
+ DEBUG("CMCP4xxxx_SPI: Send command: 0x%04x", command)
+ unsigned short result = _spiInstance->write(command);
+
+ DEBUG_LEAVE("CMCP4xxxx_SPI::Write: %d", result)
+ return result;
+ }
+
+ unsigned short CMCP4xxxx_SPI::Write(const Commands p_command) {
+ DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x", (unsigned char)p_command)
+
+ // Sanity check
+ if ((p_command != ShutdownPot1) && (p_command != ShutdownPot2) && (p_command != ShutdownBoth)) {
+ // Wrong parameters
+ return (unsigned short) -1;
+ }
+
+ unsigned short command = 0;
+ switch (p_command) {
case ShutdownPot1:
command = (0x21 << 8);
break;
@@ -111,7 +136,6 @@
DEBUG_LEAVE("CMCP4xxxx_SPI::Write: %d", result)
return result;
}
-
unsigned short CMCP4xxxx_SPI::Write() {
return _spiInstance->write(0);
}
--- a/MCP4xxxx_SPI.h Fri Jan 25 16:10:58 2013 +0000
+++ b/MCP4xxxx_SPI.h Sun Jan 27 17:04:05 2013 +0000
@@ -57,10 +57,10 @@
enum Commands {
WriteToPot1, //<! Write to digital potentiometer #1
WriteToPot2, //<! Write to digital potentiometer #2
- WriteToPotBoth, //<! Write to both digital potentiometers
+ WriteToBoth, //<! Write to both digital potentiometers
ShutdownPot1, //<! Shutdown digital potentiometer #1
ShutdownPot2, //<! Shutdown digital potentiometer #2
- ShutdownPotBoth, //<! Shutdown both digital potentiometers
+ ShutdownBoth, //<! Shutdown both digital potentiometers
};
public:
/** Constructor with Write Protect command pin wired.
@@ -82,26 +82,52 @@
*/
inline const SPI * operator * () { return (const SPI *)_spiInstance; };
- /** Write a command
+ /** 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_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation)
- * @return true on success, false otherwise
+ * @return 0x00 on success, 0Xffff otherwise
* Exemple:
* @code
- * unsigned char value = 0xaa;
+ * unsigned char potLevel;
* ...
- * myEEPROM.Write(memoryAddress, value);
+ * g_chipSelect.write(0);
+ * g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
+ * g_chipSelect.write(1);
* ...
* @endcode
*/
unsigned short Write(const Commands p_command, const unsigned char p_value);
+ /** 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)
+ * @return 0x00 on success, 0Xffff otherwise
+ * Exemple:
+ * @code
+ * ...
+ * g_chipSelect.write(0);
+ * g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
+ * g_chipSelect.write(1);
+ * ...
+ * @endcode
+ */
+ unsigned short Write(const Commands p_command);
+
/** Write a NOP command
*/
unsigned short Write();
/** Reset the device
+ * @code
+ * unsigned char potLevel;
+ * ...
+ * g_chipSelect.write(0);
+ * g_digitalPot.Reset();
+ * g_chipSelect.write(1);
+ * ...
+ * @endcode
*/
bool Reset();
Yann Garcia