A wrapper library for easy access to DACs using the SPI interface. Individual devices will receive their own libraries.

Dependents:   MAX500 MCP4922 MCP4822

Files at this revision

API Documentation at this revision

Comitter:
JimmyTheHack
Date:
Tue Jul 16 05:52:01 2013 +0000
Parent:
0:bce9b1450b19
Commit message:
attempted to merge with DAC_SPI

Changed in this revision

DAC.cpp Show diff for this revision Revisions of this file
DAC.h Show diff for this revision Revisions of this file
DAC_SPI.cpp Show annotated file Show diff for this revision Revisions of this file
DAC_SPI.h Show annotated file Show diff for this revision Revisions of this file
--- a/DAC.cpp	Wed Jun 13 23:41:08 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,16 +0,0 @@
-#include "DAC.h"
-
-/*SPI Channels */
-SPI SPI_A(p5, NC, p7);  //since we only have two SPI channels, which must be shared, define them here.
-SPI SPI_B(p11, NC, p13);
-
-/*Initialize DAC */
-DAC::DAC(int SPIchannelNum, PinName _CS, PinName _LDAC) : CS(_CS), LDAC(_LDAC){  
-    if (SPIchannelNum ==1){
-        DACspi = &SPI_B;
-    }  
-    else{
-        DACspi = &SPI_A;
-    }
-    (*DACspi).format(16,0);
-}
\ No newline at end of file
--- a/DAC.h	Wed Jun 13 23:41:08 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-#ifndef DAC_H
-#define DAC_H
-#include "mbed.h"
-
-//a basic parent library for use with DACs controlled via SPI.  Individual hardware implementations will need their own libraries.
-class DAC
-{
-    public:
-    DAC(int SPIchannelNum, PinName CS,  PinName LDAC);
-    virtual void write(int millivolts) =0; //write a value to the Digital Analog Converter    
-    
-    //DigitalOut SCK;
-    DigitalOut CS;  //serial chip select pin
-    DigitalOut LDAC; //synchronize pin to update both DACs together    
-    SPI * DACspi;
-};
-
-extern SPI SPI_A;  //channel A SPI (pins 5,7)
-extern SPI SPI_B;  //channel B SPI (pins 11,13)
-
-#endif //DAC_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DAC_SPI.cpp	Tue Jul 16 05:52:01 2013 +0000
@@ -0,0 +1,39 @@
+#ifndef DAC_SPI_CPP
+#define DAC_SPI_CPP
+
+#include "DAC_SPI.h"
+/*SPI Channels */
+                          //since we only have two SPI channels, which must be shared, define them here.  
+SPI SPI_A(p5, NC, p7);    //If using with other serial devices, can pass a pointer to the serial channel.
+SPI SPI_B(p11, NC, p13);
+
+/*Initialize DAC */
+DAC_SPI::DAC_SPI(int SPIchannelNum, PinName _CS, PinName _LDAC) : CS(_CS), LDAC(_LDAC){  
+    if (SPIchannelNum ==1){
+        DACspi = &SPI_B;
+    }  
+    else{
+        DACspi = &SPI_A;
+    }
+    messageBits(16);
+}
+
+/** Manually change the SPI frequency 
+* Must be a value supported by the mbed and DAC
+*/
+void DAC_SPI::frequency(int freq){
+    (*DACspi).frequency(freq);    
+}
+
+
+//specify the number of bits used in message packets to the DAC.
+void DAC_SPI::messageBits(int bits){
+    (*DACspi).format(bits,0);
+}
+
+//select a DACnum for use with chips with multiple onboard DACs.
+void DAC_SPI::select(char DACnum){
+    DACselect=DACnum;
+}
+
+#endif //DAC_SPI_CPP
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DAC_SPI.h	Tue Jul 16 05:52:01 2013 +0000
@@ -0,0 +1,33 @@
+#ifndef DAC_SPI_H
+#define DAC_SPI_H
+#include "mbed.h"
+
+/* a basic parent library for use with DACs controlled via SPI.  Individual hardware implementations will need their own libraries.
+This provides basic initialization of common pins and provides a serial channel.   Doesn't provide a lot of added functionality, but serves as an interface for the SPI library.
+*/
+class DAC_SPI
+{
+    public:
+    DAC_SPI(int SPIchannelNum, PinName CS,  PinName LDAC);
+    virtual void select(char DACvalue); //choose the active DAC
+    virtual void write(int DACvalue) =0; //write a value to the Digital Analog Converter    
+    virtual void write_mV(int millivolts) =0; //write a value to the Digital Analog Converter specified in mV (may require appropriate scaling factor)
+    
+    /** Manually change the SPI frequency 
+    * @param freq Desired frequency in hertz: must be a value supported by both the mbed and DAC.  The MCP4922 supports frequencies to 20MHz.
+    */
+    void frequency(int freq);
+    //set the number of bits for DAC
+    void messageBits(int bits);
+    //DigitalOut SCK;
+    /** The currently selected DAC channel.  0 for DAC A, 1 for DAC B, etc*/
+    char DACselect;
+    DigitalOut CS;  //serial chip select pin
+    DigitalOut LDAC; //synchronize pin to update both DACs together    
+    SPI * DACspi;  //SPI channel.  0 for p5-p7, 1 for p11-p13
+};
+
+extern SPI SPI_A;  //channel A SPI (pins 5,7)
+extern SPI SPI_B;  //channel B SPI (pins 11,13)
+
+#endif //DAC_H
\ No newline at end of file