Sakai Ritaro / MCP4922

Files at this revision

API Documentation at this revision

Comitter:
ritarosakai
Date:
Mon Aug 05 03:53:01 2019 +0000
Child:
1:6c010acaae66
Commit message:
Initial Commit

Changed in this revision

MCP4922.cpp Show annotated file Show diff for this revision Revisions of this file
MCP4922.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4922.cpp	Mon Aug 05 03:53:01 2019 +0000
@@ -0,0 +1,31 @@
+#include "MCP4922.h"
+#include "mbed.h"
+
+MCP4922::MCP4922(PinName MOSI,PinName MISO,PinName SCK,PinName CS,PinName LDAC):
+    dac_SPI(MOSI,MISO,SCK),dac_cs(CS),dac_l(LDAC)
+{
+    dac_SPI.format(16,0);
+    dac_SPI.frequency(20000000);
+    dac_cs=1;
+    dac_l=1;
+}
+
+void MCP4922::write(int ch,float value)
+{
+    if(value<0) {
+        value=0;
+    } else if(value>1) {
+        value=1;
+    }
+    dac_cs=0;
+    int data;
+    if(ch==0) {
+        data=(0b0011<<12)|(int)(value*0xFFF);
+    } else if(ch==1) {
+        data=(0b1011<<12)|(int)(value*0xFFF);
+    }
+    dac_SPI.write(data);
+    dac_cs=1;
+    dac_l=0;
+    dac_l=1;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4922.h	Mon Aug 05 03:53:01 2019 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+
+class MCP4922
+{
+public:
+    MCP4922(PinName MOSI,PinName MISO,PinName SCK,PinName CS,PinName LDAC);
+    void write(int ch,float value);
+private:
+    SPI dac_SPI;
+    DigitalOut dac_cs;
+    DigitalOut dac_l;
+};