Files at this revision

API Documentation at this revision

Comitter:
nguyenmanhthao996tn
Date:
Sat Sep 30 09:13:06 2017 +0000
Commit message:
Worked version

Changed in this revision

software_SPI.cpp Show annotated file Show diff for this revision Revisions of this file
software_SPI.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/software_SPI.cpp	Sat Sep 30 09:13:06 2017 +0000
@@ -0,0 +1,31 @@
+#include "software_SPI.h"
+
+software_SPI::software_SPI(PinName mosi, PinName sck)
+{
+    this->mosi = new DigitalOut(mosi);
+    *(this->mosi) = 1;
+
+    this->sck = new DigitalOut(sck);
+    *(this->sck) = 0;
+}
+
+void software_SPI::sendData(char data)
+{
+    char counter = 0;
+    for(; counter < 8; counter++) {
+        if (data & 0x80) {
+            *(this->mosi) = 1;
+        } else {
+            *(this->mosi) = 0;
+        }
+
+        data <<= 1;
+
+        wait_us(SOFTWARE_SPI_SCK_LOW_TIME_US);
+        *(this->sck) = 1;
+        
+        wait_us(SOFTWARE_SPI_SCK_HIGH_TIME_US);
+        *(this->sck) = 0;
+    }
+    *(this->mosi) = 1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/software_SPI.h	Sat Sep 30 09:13:06 2017 +0000
@@ -0,0 +1,19 @@
+#ifndef __SOFTWARE_SPI__
+#define __SOFTWARE_SPI__
+
+#include "mbed.h"
+
+#define SOFTWARE_SPI_SCK_LOW_TIME_US  15
+#define SOFTWARE_SPI_SCK_HIGH_TIME_US 15
+
+class software_SPI
+{
+private:
+    DigitalOut *mosi, *sck;
+
+public:
+    software_SPI(PinName mosi, PinName sck);
+    void sendData(char data);
+};
+
+#endif /* __SOFTWARE_SPI__ */
\ No newline at end of file