12-Bit DAC with internal Vref and SPI interface
Fork of MCP4822 by
Revision 1:d97fd6c6e2e4, committed 2016-01-20
- Comitter:
- stjo2809
- Date:
- Wed Jan 20 10:55:01 2016 +0000
- Parent:
- 0:929babff65b1
- Commit message:
- tested
Changed in this revision
MCP4822.cpp | Show annotated file Show diff for this revision Revisions of this file |
MCP4822.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MCP4822.cpp Mon Mar 23 07:53:55 2015 +0000 +++ b/MCP4822.cpp Wed Jan 20 10:55:01 2016 +0000 @@ -27,15 +27,9 @@ // Public functions //============================================================================= - MCP4822::MCP4822(SPI& spi, PinName nLDAC, PinName nCs) : _spi(spi), _nLDAC(nLDAC), _nCs(nCs) + MCP4822::MCP4822(SPI& spi, PinName nCs, PinName nLDAC) : _spi(spi), _nCs(nCs), _nLDAC(nLDAC) { - - } - - - MCP4822::MCP4822(PinName nLDAC, PinName mosi, PinName miso,PinName sck, PinName nCs) : _nLDAC(nLDAC), _mosi(mosi), _miso(miso), _sck(sck), _nCs(nCs) - { - SPI _spi(_mosi,_miso,_sck); + _nCs = 1; } void MCP4822::a(bool gain, int data) @@ -53,9 +47,20 @@ _nLDAC = ~act; } + bool MCP4822::ldac() + { + return _nLDAC; + } + void MCP4822::shdn(bool output, bool act) { - _write(output, 1, ~act, 0x00); + _write(output, 1, ~act, 0x00); + _SHDN_STATUS = act; + } + + bool MCP4822::shdn() + { + return _SHDN_STATUS; } //============================================================================= @@ -66,7 +71,7 @@ { char responce = 0; - if(output == '0') + if(output == 0) { responce = responce | CB_OUTPUT_A; } @@ -75,7 +80,7 @@ responce = responce | CB_OUTPUT_B; } - if(gain == '0') + if(gain == 0) { responce = responce | CB_GAIN_2X; } @@ -84,7 +89,7 @@ responce = responce | CB_GAIN_1X; } - if(shdn == '0') + if(shdn == 0) { responce = responce | CB_NSHDN; } @@ -93,7 +98,7 @@ responce = responce | CB_SHDN; } - if(data > 0xff && data < 0xfff) + if(data > 0xff && data <= 0xfff) { responce = responce | (data >> 8); } @@ -116,4 +121,4 @@ _spi.write(_upper_half); _spi.write(_lower_half); _nCs = 1; - } \ No newline at end of file + }
--- a/MCP4822.h Mon Mar 23 07:53:55 2015 +0000 +++ b/MCP4822.h Wed Jan 20 10:55:01 2016 +0000 @@ -29,12 +29,12 @@ // Declaration of variables & custom #defines //============================================================================= -#define CB_OUTPUT_A 0x00 // commad bit for output A -#define CB_OUTPUT_B 0x80 // commad bit for output A -#define CB_GAIN_1X 0x20 // commad bit for gain 1x -#define CB_GAIN_2X 0x00 // commad bit for gain 2x -#define CB_SHDN 0x10 // commad bit for Output enabled -#define CB_NSHDN 0x00 // commad bit for Output buffer disabled, output is high-impedance +#define CB_OUTPUT_A 0x00 // command bit for output A +#define CB_OUTPUT_B 0x80 // command bit for output A +#define CB_GAIN_1X 0x20 // command bit for gain 1x +#define CB_GAIN_2X 0x00 // command bit for gain 2x +#define CB_SHDN 0x10 // command bit for Output enabled +#define CB_NSHDN 0x00 // command bit for Output buffer disabled, output is high-impedance //============================================================================= // Functions Declaration @@ -56,19 +56,8 @@ * @param nLDAC The Latch DAC Synchronization pin * @param nCs The SPI chip select pin. */ - MCP4822(SPI& spi, PinName nLDAC, PinName nCs); + MCP4822(SPI& spi, PinName nCs, PinName nLDAC); - /** Create an instance of the MCP4261 connected with SPI pins. - * - * @param nLDAC The Latch DAC Synchronization 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. - */ - MCP4822(PinName nLDAC, PinName mosi, PinName miso,PinName sck, PinName nCs); - - /** Write to output A. * * @param gain the gain is 2x = '0' and 1x = '1'. @@ -89,6 +78,12 @@ */ void ldac(bool act); + /** Output Synchronization. + * + * @return The status the LDAC. + */ + bool ldac(); + /** Output Shutdown. * * @param output A = '0' and B = '1'. @@ -96,12 +91,19 @@ */ void shdn(bool output, bool act); + /** Output Shutdown. + * + * @return The status the SHDN. + */ + bool shdn(); + private: SPI& _spi; DigitalOut _nCs; DigitalOut _nLDAC; + bool _SHDN_STATUS; char _upper_half; char _lower_half;