Takashi Matsuoka / MjAD520x

Dependents:   MjAD520x_Hello

Files at this revision

API Documentation at this revision

Comitter:
matsujirushi
Date:
Sun Oct 18 05:12:59 2015 +0000
Commit message:
created.

Changed in this revision

MjAD520x.cpp Show annotated file Show diff for this revision Revisions of this file
MjAD520x.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MjAD520x.cpp	Sun Oct 18 05:12:59 2015 +0000
@@ -0,0 +1,27 @@
+#include "MjAD520x.h"
+
+MjAD520x::MjAD520x(PinName mosi, PinName miso, PinName sclk, PinName cs_n, int terminalResistance)
+    : HwSpi(mosi, miso, sclk), HwCs_n(cs_n, 1)
+{
+    TerminalResistance = terminalResistance;
+
+    HwSpi.format(11, 0);
+    HwSpi.frequency(1000000);
+}
+
+void MjAD520x::write(int channel, int resistance)
+{
+    write_u8(channel, resistance <= TerminalResistance ? resistance * 255 / TerminalResistance : 255);
+}
+
+void MjAD520x::write_u8(int channel, uint8_t value)
+{
+    HwCs_n.write(0);
+    wait_us(1);
+
+    HwSpi.write(channel << 8 | value);
+
+    HwCs_n.write(1);
+    wait_us(1);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MjAD520x.h	Sun Oct 18 05:12:59 2015 +0000
@@ -0,0 +1,20 @@
+#ifndef MJ_AD520X_H
+#define MJ_AD520X_H
+
+#include "mbed.h"
+
+class MjAD520x
+{
+public:
+    MjAD520x(PinName mosi, PinName miso, PinName sclk, PinName cs_n, int terminalResistance);
+    void write(int channel, int resistance);
+    void write_u8(int channel, uint8_t value);
+
+private:
+    SPI HwSpi;
+    DigitalOut HwCs_n;
+    int TerminalResistance;
+
+};
+
+#endif  // MJ_AD520X_H
\ No newline at end of file