8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
Fork of MCP4261 by
Revision 2:1e4469bdbed9, committed 2016-01-20
- Comitter:
- stjo2809
- Date:
- Wed Jan 20 10:54:05 2016 +0000
- Parent:
- 1:935321af7311
- Commit message:
- tested
Changed in this revision
MCP4261.cpp | Show annotated file Show diff for this revision Revisions of this file |
MCP4261.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 935321af7311 -r 1e4469bdbed9 MCP4261.cpp --- a/MCP4261.cpp Sun Jun 28 10:24:49 2015 +0000 +++ b/MCP4261.cpp Wed Jan 20 10:54:05 2016 +0000 @@ -29,33 +29,28 @@ MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP, PinName nSHDN): _spi(spi), _nCs(nCs), _nWP(nWP), _nSHDN(nSHDN) { - + _nCs = 1; } - MCP4261::MCP4261(PinName mosi, PinName miso, PinName sck, PinName nCs, PinName nWP, PinName nSHDN) : _spi(mosi, miso, sck), _nCs(nCs), _nWP(nWP), _nSHDN(nSHDN) + MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP): _spi(spi), _nCs(nCs), _nWP(nWP), _nSHDN(NC) { - - } - - MCP4261::MCP4261(SPI& spi, PinName nCs, PinName nWP): _spi(spi), _nCs(nCs), _nWP(nWP) - { - + _nCs = 1; } int MCP4261::read(char address) { - _read(char address); + return _read(address); } void MCP4261::write(char address, int data) { - _write(char address, int data); + _write(address, data); } void MCP4261::inc(bool number) { - if(number == '0') + if(number == 0) { _make_command_byte(CB_INCR, VW0_ADDR, 0); _nCs = 0; @@ -73,7 +68,7 @@ void MCP4261::dec(bool number) { - if(number == '0') + if(number == 0) { _make_command_byte(CB_DECR, VW0_ADDR, 0); _nCs = 0; @@ -92,70 +87,71 @@ int MCP4261::status() { - _read(STATUS_ADDR); + return _read(STATUS_ADDR); } int MCP4261::tcon() { - _read(TCON_ADDR); + return _read(TCON_ADDR); } void MCP4261::tcon(int data) { - _write(TCON_ADDR, int data); + _write(TCON_ADDR, data); } int MCP4261::wiper(bool number) { - if(number == '0') + if(number == 0) { - _read(VW0_ADDR); + return _read(VW0_ADDR); } else { - _read(VW1_ADDR); - } + return _read(VW1_ADDR); + } + } void MCP4261::wiper(bool number, int data) { - if(number == '0') + if(number == 0) { - _write(VW0_ADDR, int data); + _write(VW0_ADDR, data); } else { - _write(VW1_ADDR, int data); + _write(VW1_ADDR, data); } } int MCP4261::nvwiper(bool number) { - if(number == '0') + if(number == 0) { - _read(NVW0_ADDR); + return _read(NVW0_ADDR); } else { - _read(NVW1_ADDR); + return _read(NVW1_ADDR); } } void MCP4261::nvwiper(bool number, int data) { - if(number == '0') + if(number == 0) { - _write(NVW0_ADDR, int data); + _write(NVW0_ADDR, data); } else { - _write(NVW1_ADDR, int data); + _write(NVW1_ADDR, data); } } @@ -177,17 +173,19 @@ char MCP4261::_make_command_byte(int com, char address, int data) { - if(data > 0xff && data < 0x3FF) + if(data > 0xff && data <= 0x3FF) { - _command_byte = address << 4; // add address to _commad_byte - _command_byte = _command_byte | (data >> 8); // add data to _commad_byte - _command_byte = _command_byte | (com << 2); // add com to _commad_byte + _command_byte = address << 4; // add address to _command_byte + _command_byte = _command_byte | (data >> 8); // add data to _command_byte + _command_byte = _command_byte | (com << 2); // add com to _command_byte } else { - _command_byte = address << 4; // add address to _commad_byte - _command_byte = _command_byte | (com << 2); // add com to _commad_byte - } + _command_byte = address << 4; // add address to _command_byte + _command_byte = _command_byte | (com << 2); // add com to _command_byte + } + + return _command_byte; } int MCP4261::_read(char address) @@ -203,10 +201,8 @@ _make_command_byte(CB_READ, address, 0); _nCs = 0; - _spi.write(_command_byte); - _spi.write(0xff); // not important bit of the 16 bits - _response_msb = _spi.write(); // get response - _response_lsb = _spi.write(); // get response + _response_msb = _spi.write(_command_byte); + _response_lsb = _spi.write(0xff); // not important bit of the 16 bits _nCs = 1; _response = _response_msb << 8; @@ -229,3 +225,4 @@ +
diff -r 935321af7311 -r 1e4469bdbed9 MCP4261.h --- a/MCP4261.h Sun Jun 28 10:24:49 2015 +0000 +++ b/MCP4261.h Wed Jan 20 10:54:05 2016 +0000 @@ -31,10 +31,10 @@ #define TCON_ADDR 0x04 // Controls the state of each resistor network terminal connection. #define STATUS_ADDR 0x05 // Status (STATUS) Register, This register contains 5 status bits. WiperLock bits, Shutdown bit, Write Protect bit, EEPROM write cycle. -#define VW0_ADDR 0x02 // Volatile Wiper 0 -#define VW1_ADDR 0x03 // Volatile Wiper 1 -#define NVW0_ADDR 0x04 // Non Volatile Wiper 0 -#define NVW1_ADDR 0x05 // Non Volatile Wiper 1 +#define VW0_ADDR 0x00 // Volatile Wiper 0 +#define VW1_ADDR 0x01 // Volatile Wiper 1 +#define NVW0_ADDR 0x02 // Non Volatile Wiper 0 +#define NVW1_ADDR 0x03 // Non Volatile Wiper 1 // DATA EEPROM locations has the address from 0x06 to 0x0F @@ -72,17 +72,6 @@ */ MCP4261(SPI& spi, PinName nCs, PinName nWP, PinName nSHDN); - /** Create an instance of the MCP4261 connected with SPI pins. - * - * @param nWP The Hardware Write Protect Control pin. - * @param nSHDN The Shutdown pin. - * @param mosi The SPI Master Output, Slave Input pin. - * @param miso The SPI Master Input, Slave Output pin. - * @param sck The SPI Serial Clock pin. - * @param nCs The SPI chip select pin. - */ - MCP4261(PinName mosi, PinName miso,PinName sck, PinName nCs, PinName nWP, PinName nSHDN); - /** Create an instance of the MCP4261 connected via specfied SPI instance (use wthen SHDN is linked to Global reset). * * @param spi The mbed SPI instance (make in main routine) @@ -179,9 +168,10 @@ private: SPI& _spi; + DigitalOut _nCs; DigitalOut _nWP; DigitalOut _nSHDN; - DigitalOut _nCs; + char _command_byte;