xxxxxx

Dependencies:   mbed

Fork of FINAL by Robotics ^___^

Revision:
0:d41917b28387
Child:
1:6228de50cbf4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Mar 25 18:06:10 2017 +0000
@@ -0,0 +1,80 @@
+#include "mbed.h"
+
+Ticker timer1;
+Serial bt(D10, D2);  // TXpin, RXpin
+
+//RX
+int readcount = 0;
+int RX_flag1 = 0;
+int RX_flag2 = 0;
+char getData[6] = {0,0,0,0,0,0};
+short data_received[3] = {0,0,0};
+short data_received_old[3] = {0,0,0};
+
+//函式宣告
+void init_TIMER();
+void timer1_ITR();
+void init_UART();
+void RX_ITR();
+
+int main()
+{
+    init_TIMER();
+    init_UART();
+    while(1) {
+    }
+}
+
+void init_TIMER()
+{
+    timer1.attach_us(&timer1_ITR, 10000.0); // the address of the function to be attached (timer1_ITR) and the interval (1000 micro-seconds)
+}
+
+void init_UART()
+{
+    bt.baud(115200);  // baud rate設為115200
+    bt.attach(&RX_ITR, Serial::RxIrq);  // Attach a function(RX_ITR) to call whenever a serial interrupt is generated.
+}
+
+void timer1_ITR()
+{
+    // 避免收到錯誤資料,若超出設定範圍則用上次的資料
+    if(data_received[0]>300 || data_received[0]<-300 || data_received[1]>300 || data_received[1]<-300 || data_received[2]>90 || data_received[0]<-90) {
+        data_received[0] = data_received_old[0];
+        data_received[1] = data_received_old[1];
+        data_received[2] = data_received_old[2];
+    } else {
+        data_received_old[0] = data_received[0];
+        data_received_old[1] = data_received[1];
+        data_received_old[2] = data_received[2];
+    }
+}
+
+void RX_ITR()
+{
+    while(bt.readable()) {
+        static char uart_read;
+        uart_read = bt.getc();
+        if(uart_read == 127 && RX_flag1 == 1) {
+            RX_flag2 = 1;
+        } else {
+            RX_flag1 = 0;
+        }
+
+        if(RX_flag2 == 1) {
+            getData[readcount] = uart_read;
+            readcount++;
+            if(readcount >= 3) {
+                readcount = 0;
+                RX_flag2 = 0;
+                ///code for decoding///
+                data_received[0] = (getData[2] << 8) | getData[1];
+
+
+                ///////////////////////
+            }
+        } else if(uart_read == 254 && RX_flag1 == 0) {
+            RX_flag1 = 1;
+        }
+    }
+}
\ No newline at end of file