12-Bit DAC with internal Vref and SPI interface

Files at this revision

API Documentation at this revision

Comitter:
stjo2809
Date:
Mon Mar 23 07:53:55 2015 +0000
Commit message:
revision 0.1

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4822.cpp	Mon Mar 23 07:53:55 2015 +0000
@@ -0,0 +1,119 @@
+/* mbed MCP4822 Library, for driving the 12-Bit DAC with internal Vref and SPI interface
+ * Copyright (c) 2015, Created by Steen Joergensen (stjo2809)
+ *
+ * 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"
+ 
+//=============================================================================
+// Public functions
+//=============================================================================
+
+    MCP4822::MCP4822(SPI& spi, PinName nLDAC, PinName nCs) : _spi(spi), _nLDAC(nLDAC), _nCs(nCs)
+    {
+        
+    }
+    
+    
+    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);    
+    }
+    
+    void MCP4822::a(bool gain, int data)
+    {
+        _write(0, gain, 1, data);    
+    }
+    
+    void MCP4822::b(bool gain, int data)
+    {
+        _write(1, gain, 1, data);     
+    }
+   
+    void MCP4822::ldac(bool act)
+    {
+        _nLDAC = ~act;    
+    }
+    
+    void MCP4822::shdn(bool output, bool act)
+    {
+        _write(output, 1, ~act, 0x00);    
+    }
+    
+//=============================================================================
+// Private functions
+//=============================================================================
+
+    char MCP4822::_make_upper_half(bool output, bool gain, bool shdn, int data)
+    {
+        char responce = 0;
+        
+        if(output == '0')
+        {
+            responce = responce | CB_OUTPUT_A;        
+        }
+        else
+        {
+            responce = responce | CB_OUTPUT_B;      
+        }
+        
+        if(gain == '0')
+        {
+            responce = responce | CB_GAIN_2X;        
+        }
+        else
+        {
+            responce = responce | CB_GAIN_1X;      
+        }
+        
+        if(shdn == '0')
+        {
+            responce = responce | CB_NSHDN;        
+        }
+        else
+        {
+            responce = responce | CB_SHDN;      
+        }
+        
+        if(data > 0xff && data < 0xfff)
+        {
+            responce = responce | (data >> 8);        
+        }
+        
+        return responce;     
+    }
+    
+    char MCP4822::_make_lower_half(int data)
+    {
+      return (data & 0xff);    
+    }
+    
+    void MCP4822::_write(bool output, bool gain, bool shdn, int data)  
+    {
+        
+        _upper_half = _make_upper_half(output, gain, shdn, data);
+        _lower_half = _make_lower_half(data);
+        
+        _nCs = 0;
+        _spi.write(_upper_half);
+        _spi.write(_lower_half);
+        _nCs = 1;    
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4822.h	Mon Mar 23 07:53:55 2015 +0000
@@ -0,0 +1,115 @@
+/* mbed MCP4822 Library, for driving the 12-Bit DAC with internal Vref and SPI interface
+ * Copyright (c) 2015, Created by Steen Joergensen (stjo2809)
+ *
+ * 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"
+
+#ifndef MBED_MCP4822_H
+#define MBED_MCP4822_H
+
+//=============================================================================
+// 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
+
+//=============================================================================
+// Functions Declaration
+//=============================================================================
+
+/** Interface to the 12-Bit DAC with internal Vref and SPI interface
+ *
+  *  Using the driver:
+ *   - remenber to setup SPI in main routine or use pins instance.
+ *
+ *  Defaults in this driver on start up:
+ *   - Datasheet start up
+ */
+class MCP4822 {
+public:
+    /** Create an instance of the MCP4822 connected via specfied SPI instance.
+     *
+     * @param spi The mbed SPI instance (make in main routine)
+     * @param nLDAC The Latch DAC Synchronization pin 
+     * @param nCs The SPI chip select pin.
+     */
+    MCP4822(SPI& spi, PinName nLDAC, PinName nCs);
+    
+    /** 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'.
+     * @param data The 12 bits value to write to the output.
+     */
+    void a(bool gain, int data);
+    
+    /** Write to output B.
+     *
+     * @param gain the gain is 2x = '0' and 1x = '1'.
+     * @param data The 12 bits value to write to the output.
+     */
+    void b(bool gain, int data);
+    
+    /** Output Synchronization.
+     *
+     * @param act The LDAC is Active = true and Inactive = false.
+     */
+    void ldac(bool act);
+    
+    /** Output Shutdown.
+     *
+     * @param output A = '0' and B = '1'.
+     * @param act The SHDN is Active = true and Inactive = false.
+     */
+    void shdn(bool output, bool act);
+    
+  
+
+private:
+    SPI& _spi;
+    DigitalOut _nCs;
+    DigitalOut _nLDAC;
+    
+    char _upper_half;
+    char _lower_half;
+    
+    char _make_upper_half(bool output, bool gain, bool shdn, int data);
+    char _make_lower_half(int data);
+    void _write(bool output, bool gain, bool shdn, int data);             
+
+};
+
+#endif
\ No newline at end of file