Library for MCP4822 SPI 12-bit DAC, both outputs supported, nLDAC connected to GND.

Dependents:   MCP4822_2aLerche

Revision:
0:014a10a4fed1
Child:
1:5c1e0d492b59
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4822.cpp	Fri Jan 21 10:55:58 2011 +0000
@@ -0,0 +1,66 @@
+/* MCP4822 library 
+ * Copyright (c) 2008-2010, Lerche
+ *
+ * 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, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "mbed.h"
+#include "MCP4822.h"
+
+using namespace mbed;
+
+MCP4822::MCP4822(PinName mosi, PinName sclk, PinName ncs) : _spi(mosi, NC, sclk), _ncs(ncs) {
+    _init();
+}
+
+void MCP4822::_init() {
+    _spi.format(16, 0);  // Setup the SPI
+    return;
+}
+
+void MCP4822::writeA(int ValueA) {
+    _ncs = 0;       // Chipselect the device.
+    ValueA = ValueA &= 0x0FFF;
+    ValueA = ValueA |= 0x1000;      // Write to A register  
+    _spi.write(ValueA);
+    _ncs = 1;
+    return;
+}
+
+void MCP4822::writeB(int ValueB) {
+    _ncs = 0;       // Chipselect the device.
+    ValueB = ValueB &= 0x0FFF;
+    ValueB = ValueB |= 0x9000;      // Write to B register
+    _spi.write(ValueB);
+    _ncs = 1;
+    return;
+}
+
+void MCP4822::writeAB(int ValueAB) {
+    _ncs = 0;
+    ValueAB = ValueAB &= 0x0FFF;
+    ValueAB = ValueAB |= 0x1000;
+    _spi.write(ValueAB);
+    _ncs = 1; 
+    wait_us(500);
+    _ncs = 0;
+    ValueAB = ValueAB |= 0x9000;
+    _spi.write(ValueAB);
+    _ncs = 1; 
+}
\ No newline at end of file