Dual DAC library

Dependents:   elixys

Files at this revision

API Documentation at this revision

Comitter:
henryeherman
Date:
Sun Dec 15 02:42:11 2013 +0000
Parent:
0:b58572ce83b6
Commit message:
Working dual DAC library

Changed in this revision

MCP482X.cpp Show diff for this revision Revisions of this file
MCP482X.h Show diff for this revision Revisions of this file
mcp482x.cpp Show annotated file Show diff for this revision Revisions of this file
mcp482x.h Show annotated file Show diff for this revision Revisions of this file
diff -r b58572ce83b6 -r 277e5be610bc MCP482X.cpp
--- a/MCP482X.cpp	Wed Aug 14 00:21:42 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-#include "mbed.h"
-#include "MCP482X.h"
-
-using namespace mbed;
-
-MCP482X::MCP482X(SPI &spi, DigitalOut &cspin): _spi(spi), _cspin(cspin) {
-    gainA = 1;
-    gainB = 1;
-    valA = 0;
-    valB = 0;     
-    enable();
-    _cspin.write(1);
-}
-
-MCP482X::~MCP482X() {
-}
-
-int MCP482X::writeA(int value){
-    int tempval = 0;
-    valA = (value & 0x0FFF);
-    tempval = SELECTDACA(valA);
-    
-    if(gainA == 1) {
-        tempval = SELECT1XGAIN(tempval);           
-    } else {
-        tempval = SELECT2XGAIN(tempval);        
-    }
-    
-    if(bshutdown==true) {
-        tempval = SELECTPWROFF(tempval);
-    } else {
-        tempval = SELECTPWRON(tempval);
-    }
-    
-    sendValue(tempval);
-    return tempval;
-}
-
-int MCP482X::writeB(int value){
-
-    int tempval = 0;
-    valB = (value & 0x0FFF);
-    tempval = SELECTDACB(valB);
-    
-    if(gainB == 1) {
-        tempval = SELECT1XGAIN(tempval);           
-    } else {
-        tempval = SELECT2XGAIN(tempval);        
-    }
-    
-    if(bshutdown==true) {
-        tempval = SELECTPWROFF(tempval);
-    } else {
-        tempval = SELECTPWRON(tempval);
-    }
-    
-    sendValue(tempval);
-    return tempval;
-}
-
-void MCP482X::setGainA(int value){
-    if(value==1 || value == 2)
-        gainA = value;
-    else
-        gainA = 1;
-}
-
-void MCP482X::setGainB(int value){
-    if(value==1 || value == 2)
-        gainB = value;
-    else
-        gainB = 1;
-}
-
-void MCP482X::disable() {
-    bshutdown = true;
-    writeA(valA);
-    writeB(valB);        
-}
-
-void MCP482X::enable() {
-    bshutdown = false;
-    writeA(valA);
-    writeB(valB);
-}
-
-void MCP482X::configspi() {
-    _spi.format(16, 0);
-    _spi.frequency(50000);
-}
-
-void MCP482X::sendValue(int value) {
-    configspi();
-    _cspin.write(0);
-    _spi.write(value);    
-    _cspin.write(1);
-}
\ No newline at end of file
diff -r b58572ce83b6 -r 277e5be610bc MCP482X.h
--- a/MCP482X.h	Wed Aug 14 00:21:42 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,82 +0,0 @@
-#include "mbed.h"
-#include "bit.h"
-#ifndef MCP482X_H
-#define MCP482X_H
-
-#define MCP482X_VREF 2048
-
-#define DACSELBIT       BIT(15)
-#define DACUNDEF        BIT(14)
-#define GAINBIT         BIT(13)
-#define SHDNBIT         BIT(12)
-
-#define SELECTDACA(VALUE)        CLEARBITS(VALUE,DACSELBIT)
-#define SELECTDACB(VALUE)        SETBITS(VALUE,DACSELBIT)
-#define SELECT1XGAIN(VALUE)      SETBITS(VALUE,GAINBIT)
-#define SELECT2XGAIN(VALUE)      CLEARBITS(VALUE,GAINBIT)
-#define SELECTPWRON(VALUE)       SETBITS(VALUE, SHDNBIT)
-#define SELECTPWROFF(VALUE)      CLEARBITS(VALUE, SHDNBIT)
-
-// BITS 0-11 set the output voltage!!!
-
-class MCP482X {
-public:
-
-/*
-* Constructor
-*/
-MCP482X(SPI &spi, DigitalOut &cspin);
-
-/*
-* Destructor
-*/
-~MCP482X();
-
-/*
-* Write to DAC A
-*/
-int writeA(int value);
-
-
-/*
-* Write to DAC B
-*/
-int writeB(int value);
-
-
-/*
-* Set Gain for DAC A 1 or 2
-*/
-void setGainA(int value);
-
-
-/*
-* Set Gain for DAC B 1 or 2
-*/
-void setGainB(int value);
-
-/*
-* Shutdown DAC
-*/
-void disable();
-
-/*
-* Power On DAC
-*/
-void enable();
-
-private:
-
-int gainA;
-int gainB;
-int valA;
-int valB;    
-bool bshutdown;
-SPI &_spi;
-DigitalOut &_cspin;
-
-void configspi();
-void sendValue(int value);
-}; // end class MCP482X
-
-#endif //MCP482X_H
\ No newline at end of file
diff -r b58572ce83b6 -r 277e5be610bc mcp482x.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcp482x.cpp	Sun Dec 15 02:42:11 2013 +0000
@@ -0,0 +1,108 @@
+#include "mbed.h"
+#include "mcp482x.h"
+
+using namespace mbed;
+
+MCP482X::MCP482X(SPI &spi, void(*sel)(void), void(*usel)(void)): _spi(spi) {
+    gainA = 1;
+    gainB = 1;
+    valA = 0;
+    valB = 0;
+    selectfxn = sel;
+    unselectfxn = usel;     
+    enable();    
+}
+
+MCP482X::~MCP482X() {
+}
+
+int MCP482X::writeA(int value){
+    int tempval = 0;
+    valA = (value & 0x0FFF);
+    tempval = SELECTDACA(valA);
+    
+    if(gainA == 1) {
+        tempval = SELECT1XGAIN(tempval);           
+    } else {
+        tempval = SELECT2XGAIN(tempval);        
+    }
+    
+    if(bshutdown==true) {
+        tempval = SELECTPWROFF(tempval);
+    } else {
+        tempval = SELECTPWRON(tempval);
+    }
+    
+    sendValue(tempval);
+    return tempval;
+}
+
+int MCP482X::writeB(int value){
+
+    int tempval = 0;
+    valB = (value & 0x0FFF);
+    tempval = SELECTDACB(valB);
+    
+    if(gainB == 1) {
+        tempval = SELECT1XGAIN(tempval);           
+    } else {
+        tempval = SELECT2XGAIN(tempval);        
+    }
+    
+    if(bshutdown==true) {
+        tempval = SELECTPWROFF(tempval);
+    } else {
+        tempval = SELECTPWRON(tempval);
+    }
+    
+    sendValue(tempval);
+    return tempval;
+}
+
+void MCP482X::setGainA(int value){
+    if(value==1 || value == 2)
+        gainA = value;
+    else
+        gainA = 1;
+}
+
+void MCP482X::setGainB(int value){
+    if(value==1 || value == 2)
+        gainB = value;
+    else
+        gainB = 1;
+}
+
+void MCP482X::disable() {
+    bshutdown = true;
+    writeA(valA);
+    writeB(valB);        
+}
+
+void MCP482X::enable() {
+    bshutdown = false;
+    writeA(valA);
+    writeB(valB);
+}
+
+void MCP482X::configspi() {
+    _spi.format(16, 0);
+    //_spi.frequency();
+}
+
+void MCP482X::sendValue(int value) {    
+    select();
+    _spi.write(value);    
+    deselect();
+}
+
+void MCP482X::select() {
+    //Set CS low to start transmission (interrupts conversion)   
+    configspi(); 
+    selectfxn();
+}
+
+void MCP482X::deselect() {
+    //Set CS high to stop transmission (restarts conversion)    
+    unselectfxn();    
+}
\ No newline at end of file
diff -r b58572ce83b6 -r 277e5be610bc mcp482x.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcp482x.h	Sun Dec 15 02:42:11 2013 +0000
@@ -0,0 +1,87 @@
+#include "mbed.h"
+#include "bit.h"
+#ifndef MCP482X_H
+#define MCP482X_H
+
+#define MCP482X_VREF 2048
+
+#define DACSELBIT       BIT(15)
+#define DACUNDEF        BIT(14)
+#define GAINBIT         BIT(13)
+#define SHDNBIT         BIT(12)
+
+#define SELECTDACA(VALUE)        CLEARBITS(VALUE,DACSELBIT)
+#define SELECTDACB(VALUE)        SETBITS(VALUE,DACSELBIT)
+#define SELECT1XGAIN(VALUE)      SETBITS(VALUE,GAINBIT)
+#define SELECT2XGAIN(VALUE)      CLEARBITS(VALUE,GAINBIT)
+#define SELECTPWRON(VALUE)       SETBITS(VALUE, SHDNBIT)
+#define SELECTPWROFF(VALUE)      CLEARBITS(VALUE, SHDNBIT)
+
+// BITS 0-11 set the output voltage!!!
+
+class MCP482X {
+public:
+
+/*
+* Constructor
+*/
+MCP482X(SPI &spi, void(*sel)(void), void(*usel)(void));
+
+/*
+* Destructor
+*/
+~MCP482X();
+
+/*
+* Write to DAC A
+*/
+int writeA(int value);
+
+
+/*
+* Write to DAC B
+*/
+int writeB(int value);
+
+
+/*
+* Set Gain for DAC A 1 or 2
+*/
+void setGainA(int value);
+
+
+/*
+* Set Gain for DAC B 1 or 2
+*/
+void setGainB(int value);
+
+/*
+* Shutdown DAC
+*/
+void disable();
+
+/*
+* Power On DAC
+*/
+void enable();
+
+void select();
+
+void deselect();
+
+private:
+
+void(*selectfxn)(void);
+void(*unselectfxn)(void);
+int gainA;
+int gainB;
+int valA;
+int valB;    
+bool bshutdown;
+SPI &_spi;
+
+void configspi();
+void sendValue(int value);
+}; // end class MCP482X
+
+#endif //MCP482X_H
\ No newline at end of file