YICHUN SAVE US
Dependencies: mbed
Fork of FINALFINALNORMAL by
main.cpp@0:d41917b28387, 2017-03-25 (annotated)
- Committer:
- hoting
- Date:
- Sat Mar 25 18:06:10 2017 +0000
- Revision:
- 0:d41917b28387
- Child:
- 1:6228de50cbf4
Robotics_LAB_UART
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hoting | 0:d41917b28387 | 1 | #include "mbed.h" |
hoting | 0:d41917b28387 | 2 | |
hoting | 0:d41917b28387 | 3 | Ticker timer1; |
hoting | 0:d41917b28387 | 4 | Serial bt(D10, D2); // TXpin, RXpin |
hoting | 0:d41917b28387 | 5 | |
hoting | 0:d41917b28387 | 6 | //RX |
hoting | 0:d41917b28387 | 7 | int readcount = 0; |
hoting | 0:d41917b28387 | 8 | int RX_flag1 = 0; |
hoting | 0:d41917b28387 | 9 | int RX_flag2 = 0; |
hoting | 0:d41917b28387 | 10 | char getData[6] = {0,0,0,0,0,0}; |
hoting | 0:d41917b28387 | 11 | short data_received[3] = {0,0,0}; |
hoting | 0:d41917b28387 | 12 | short data_received_old[3] = {0,0,0}; |
hoting | 0:d41917b28387 | 13 | |
hoting | 0:d41917b28387 | 14 | //函式宣告 |
hoting | 0:d41917b28387 | 15 | void init_TIMER(); |
hoting | 0:d41917b28387 | 16 | void timer1_ITR(); |
hoting | 0:d41917b28387 | 17 | void init_UART(); |
hoting | 0:d41917b28387 | 18 | void RX_ITR(); |
hoting | 0:d41917b28387 | 19 | |
hoting | 0:d41917b28387 | 20 | int main() |
hoting | 0:d41917b28387 | 21 | { |
hoting | 0:d41917b28387 | 22 | init_TIMER(); |
hoting | 0:d41917b28387 | 23 | init_UART(); |
hoting | 0:d41917b28387 | 24 | while(1) { |
hoting | 0:d41917b28387 | 25 | } |
hoting | 0:d41917b28387 | 26 | } |
hoting | 0:d41917b28387 | 27 | |
hoting | 0:d41917b28387 | 28 | void init_TIMER() |
hoting | 0:d41917b28387 | 29 | { |
hoting | 0:d41917b28387 | 30 | timer1.attach_us(&timer1_ITR, 10000.0); // the address of the function to be attached (timer1_ITR) and the interval (1000 micro-seconds) |
hoting | 0:d41917b28387 | 31 | } |
hoting | 0:d41917b28387 | 32 | |
hoting | 0:d41917b28387 | 33 | void init_UART() |
hoting | 0:d41917b28387 | 34 | { |
hoting | 0:d41917b28387 | 35 | bt.baud(115200); // baud rate設為115200 |
hoting | 0:d41917b28387 | 36 | bt.attach(&RX_ITR, Serial::RxIrq); // Attach a function(RX_ITR) to call whenever a serial interrupt is generated. |
hoting | 0:d41917b28387 | 37 | } |
hoting | 0:d41917b28387 | 38 | |
hoting | 0:d41917b28387 | 39 | void timer1_ITR() |
hoting | 0:d41917b28387 | 40 | { |
hoting | 0:d41917b28387 | 41 | // 避免收到錯誤資料,若超出設定範圍則用上次的資料 |
hoting | 0:d41917b28387 | 42 | 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) { |
hoting | 0:d41917b28387 | 43 | data_received[0] = data_received_old[0]; |
hoting | 0:d41917b28387 | 44 | data_received[1] = data_received_old[1]; |
hoting | 0:d41917b28387 | 45 | data_received[2] = data_received_old[2]; |
hoting | 0:d41917b28387 | 46 | } else { |
hoting | 0:d41917b28387 | 47 | data_received_old[0] = data_received[0]; |
hoting | 0:d41917b28387 | 48 | data_received_old[1] = data_received[1]; |
hoting | 0:d41917b28387 | 49 | data_received_old[2] = data_received[2]; |
hoting | 0:d41917b28387 | 50 | } |
hoting | 0:d41917b28387 | 51 | } |
hoting | 0:d41917b28387 | 52 | |
hoting | 0:d41917b28387 | 53 | void RX_ITR() |
hoting | 0:d41917b28387 | 54 | { |
hoting | 0:d41917b28387 | 55 | while(bt.readable()) { |
hoting | 0:d41917b28387 | 56 | static char uart_read; |
hoting | 0:d41917b28387 | 57 | uart_read = bt.getc(); |
hoting | 0:d41917b28387 | 58 | if(uart_read == 127 && RX_flag1 == 1) { |
hoting | 0:d41917b28387 | 59 | RX_flag2 = 1; |
hoting | 0:d41917b28387 | 60 | } else { |
hoting | 0:d41917b28387 | 61 | RX_flag1 = 0; |
hoting | 0:d41917b28387 | 62 | } |
hoting | 0:d41917b28387 | 63 | |
hoting | 0:d41917b28387 | 64 | if(RX_flag2 == 1) { |
hoting | 0:d41917b28387 | 65 | getData[readcount] = uart_read; |
hoting | 0:d41917b28387 | 66 | readcount++; |
hoting | 0:d41917b28387 | 67 | if(readcount >= 3) { |
hoting | 0:d41917b28387 | 68 | readcount = 0; |
hoting | 0:d41917b28387 | 69 | RX_flag2 = 0; |
hoting | 0:d41917b28387 | 70 | ///code for decoding/// |
hoting | 0:d41917b28387 | 71 | data_received[0] = (getData[2] << 8) | getData[1]; |
hoting | 0:d41917b28387 | 72 | |
hoting | 0:d41917b28387 | 73 | |
hoting | 0:d41917b28387 | 74 | /////////////////////// |
hoting | 0:d41917b28387 | 75 | } |
hoting | 0:d41917b28387 | 76 | } else if(uart_read == 254 && RX_flag1 == 0) { |
hoting | 0:d41917b28387 | 77 | RX_flag1 = 1; |
hoting | 0:d41917b28387 | 78 | } |
hoting | 0:d41917b28387 | 79 | } |
hoting | 0:d41917b28387 | 80 | } |