Chen Wei Ting
/
UART_send
UAER ver1
main.cpp@0:d89da59efc0d, 2018-07-22 (annotated)
- Committer:
- JJting
- Date:
- Sun Jul 22 06:55:25 2018 +0000
- Revision:
- 0:d89da59efc0d
UART send ver1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JJting | 0:d89da59efc0d | 1 | #include "mbed.h" |
JJting | 0:d89da59efc0d | 2 | #include <math.h> |
JJting | 0:d89da59efc0d | 3 | |
JJting | 0:d89da59efc0d | 4 | // timer 1 |
JJting | 0:d89da59efc0d | 5 | #define LOOPTIME 1000 // 1 ms |
JJting | 0:d89da59efc0d | 6 | //unsigned long lastMilli = 1000; |
JJting | 0:d89da59efc0d | 7 | unsigned long lastMilli; |
JJting | 0:d89da59efc0d | 8 | // sampling time |
JJting | 0:d89da59efc0d | 9 | //float T = 0.001; |
JJting | 0:d89da59efc0d | 10 | Timer t; |
JJting | 0:d89da59efc0d | 11 | // BT |
JJting | 0:d89da59efc0d | 12 | Serial uart_1(SERIAL_TX, SERIAL_RX); |
JJting | 0:d89da59efc0d | 13 | //Serial uart_1(D10,D2); // TX : D10 RX : D2 // serial 1 |
JJting | 0:d89da59efc0d | 14 | void init_uart(void); |
JJting | 0:d89da59efc0d | 15 | void separate(void); |
JJting | 0:d89da59efc0d | 16 | void uart_send(void); |
JJting | 0:d89da59efc0d | 17 | |
JJting | 0:d89da59efc0d | 18 | // uart send data |
JJting | 0:d89da59efc0d | 19 | int display[6] = {0,0,0,0,0,0}; |
JJting | 0:d89da59efc0d | 20 | char num[14] = {255,255,0,0,0,0,0,0,0,0,0,0,0,0}; // char num[0] , num[1] : 2 start byte |
JJting | 0:d89da59efc0d | 21 | unsigned long time_flag; |
JJting | 0:d89da59efc0d | 22 | unsigned long uart_flag; |
JJting | 0:d89da59efc0d | 23 | unsigned long Tstart; |
JJting | 0:d89da59efc0d | 24 | unsigned long Tperiod; |
JJting | 0:d89da59efc0d | 25 | |
JJting | 0:d89da59efc0d | 26 | // FSR |
JJting | 0:d89da59efc0d | 27 | AnalogIn FSR(A0); |
JJting | 0:d89da59efc0d | 28 | Ticker timer1; |
JJting | 0:d89da59efc0d | 29 | float Voltage_FSR; |
JJting | 0:d89da59efc0d | 30 | void init_TIMER(void); |
JJting | 0:d89da59efc0d | 31 | void timer1_interrupt(void); |
JJting | 0:d89da59efc0d | 32 | |
JJting | 0:d89da59efc0d | 33 | // EMG |
JJting | 0:d89da59efc0d | 34 | AnalogIn EMG(A1); |
JJting | 0:d89da59efc0d | 35 | float Voltage_EMG; |
JJting | 0:d89da59efc0d | 36 | |
JJting | 0:d89da59efc0d | 37 | |
JJting | 0:d89da59efc0d | 38 | int main() |
JJting | 0:d89da59efc0d | 39 | { |
JJting | 0:d89da59efc0d | 40 | t.start(); |
JJting | 0:d89da59efc0d | 41 | lastMilli = t.read_ms(); |
JJting | 0:d89da59efc0d | 42 | uart_flag = 0; |
JJting | 0:d89da59efc0d | 43 | Tperiod = 0; |
JJting | 0:d89da59efc0d | 44 | init_uart(); |
JJting | 0:d89da59efc0d | 45 | |
JJting | 0:d89da59efc0d | 46 | // FSR |
JJting | 0:d89da59efc0d | 47 | init_TIMER(); |
JJting | 0:d89da59efc0d | 48 | |
JJting | 0:d89da59efc0d | 49 | while(1) { |
JJting | 0:d89da59efc0d | 50 | // timer 1 |
JJting | 0:d89da59efc0d | 51 | //flag_time = 0; |
JJting | 0:d89da59efc0d | 52 | // if((t.read_ms() - lastMilli) >= LOOPTIME) // 1 ms |
JJting | 0:d89da59efc0d | 53 | // { |
JJting | 0:d89da59efc0d | 54 | //// flag_time = 1; |
JJting | 0:d89da59efc0d | 55 | // lastMilli = t.read_us(); |
JJting | 0:d89da59efc0d | 56 | // uart_send(); |
JJting | 0:d89da59efc0d | 57 | // uart_flag = t.read_us() - lastMilli; |
JJting | 0:d89da59efc0d | 58 | // } |
JJting | 0:d89da59efc0d | 59 | Tstart = t.read_us(); |
JJting | 0:d89da59efc0d | 60 | uart_send(); |
JJting | 0:d89da59efc0d | 61 | Tperiod = t.read_us()-Tstart; |
JJting | 0:d89da59efc0d | 62 | Tperiod = Tperiod/10; |
JJting | 0:d89da59efc0d | 63 | } // while(1) end |
JJting | 0:d89da59efc0d | 64 | } |
JJting | 0:d89da59efc0d | 65 | |
JJting | 0:d89da59efc0d | 66 | int i = 0; |
JJting | 0:d89da59efc0d | 67 | void uart_send(void) |
JJting | 0:d89da59efc0d | 68 | { |
JJting | 0:d89da59efc0d | 69 | // uart send data |
JJting | 0:d89da59efc0d | 70 | // display[0] = Gyro_axis_data[0]; |
JJting | 0:d89da59efc0d | 71 | display[0] = Voltage_FSR; |
JJting | 0:d89da59efc0d | 72 | display[1] = Voltage_EMG; |
JJting | 0:d89da59efc0d | 73 | display[2] = Tperiod; |
JJting | 0:d89da59efc0d | 74 | display[3] = 3; |
JJting | 0:d89da59efc0d | 75 | display[4] = 4; |
JJting | 0:d89da59efc0d | 76 | display[5] = 5; |
JJting | 0:d89da59efc0d | 77 | |
JJting | 0:d89da59efc0d | 78 | // wait(0.2); |
JJting | 0:d89da59efc0d | 79 | separate(); |
JJting | 0:d89da59efc0d | 80 | |
JJting | 0:d89da59efc0d | 81 | int j = 0; |
JJting | 0:d89da59efc0d | 82 | int k = 1; |
JJting | 0:d89da59efc0d | 83 | while(k) { |
JJting | 0:d89da59efc0d | 84 | if(uart_1.writeable()) { |
JJting | 0:d89da59efc0d | 85 | uart_1.putc(num[i]); |
JJting | 0:d89da59efc0d | 86 | i++; |
JJting | 0:d89da59efc0d | 87 | j++; |
JJting | 0:d89da59efc0d | 88 | } |
JJting | 0:d89da59efc0d | 89 | |
JJting | 0:d89da59efc0d | 90 | if(j>1) { // send 2 bytes |
JJting | 0:d89da59efc0d | 91 | k = 0; |
JJting | 0:d89da59efc0d | 92 | j = 0; |
JJting | 0:d89da59efc0d | 93 | } |
JJting | 0:d89da59efc0d | 94 | } |
JJting | 0:d89da59efc0d | 95 | |
JJting | 0:d89da59efc0d | 96 | if(i>13) |
JJting | 0:d89da59efc0d | 97 | i = 0; |
JJting | 0:d89da59efc0d | 98 | } |
JJting | 0:d89da59efc0d | 99 | |
JJting | 0:d89da59efc0d | 100 | void init_uart() |
JJting | 0:d89da59efc0d | 101 | { |
JJting | 0:d89da59efc0d | 102 | uart_1.baud(115200); // 設定baudrate |
JJting | 0:d89da59efc0d | 103 | } |
JJting | 0:d89da59efc0d | 104 | |
JJting | 0:d89da59efc0d | 105 | void separate(void) |
JJting | 0:d89da59efc0d | 106 | { |
JJting | 0:d89da59efc0d | 107 | num[2] = display[0] & 0x00ff; // LSB(8bit)資料存入陣列 |
JJting | 0:d89da59efc0d | 108 | num[3] = display[0] >> 8; // HSB(8bit)資料存入陣列 |
JJting | 0:d89da59efc0d | 109 | num[4] = display[1] & 0x00ff;; |
JJting | 0:d89da59efc0d | 110 | num[5] = display[1] >> 8; |
JJting | 0:d89da59efc0d | 111 | num[6] = display[2] & 0x00ff;; |
JJting | 0:d89da59efc0d | 112 | num[7] = display[2] >> 8; |
JJting | 0:d89da59efc0d | 113 | num[8] = display[3] & 0x00ff;; |
JJting | 0:d89da59efc0d | 114 | num[9] = display[3] >> 8; |
JJting | 0:d89da59efc0d | 115 | num[10] = display[4] & 0x00ff;; |
JJting | 0:d89da59efc0d | 116 | num[11] = display[4] >> 8; |
JJting | 0:d89da59efc0d | 117 | num[12] = display[5] & 0x00ff;; |
JJting | 0:d89da59efc0d | 118 | num[13] = display[5] >> 8; |
JJting | 0:d89da59efc0d | 119 | |
JJting | 0:d89da59efc0d | 120 | |
JJting | 0:d89da59efc0d | 121 | } |
JJting | 0:d89da59efc0d | 122 | /********************************************************************/ |
JJting | 0:d89da59efc0d | 123 | // init_TIMER |
JJting | 0:d89da59efc0d | 124 | /********************************************************************/ |
JJting | 0:d89da59efc0d | 125 | void init_TIMER(void) |
JJting | 0:d89da59efc0d | 126 | { |
JJting | 0:d89da59efc0d | 127 | timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz) |
JJting | 0:d89da59efc0d | 128 | // timer2.attach(&timer2_interrupt, 1);//1s interrupt period (1 Hz) |
JJting | 0:d89da59efc0d | 129 | } |
JJting | 0:d89da59efc0d | 130 | |
JJting | 0:d89da59efc0d | 131 | void timer1_interrupt(void) |
JJting | 0:d89da59efc0d | 132 | { |
JJting | 0:d89da59efc0d | 133 | Voltage_FSR = FSR.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
JJting | 0:d89da59efc0d | 134 | Voltage_EMG = EMG.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
JJting | 0:d89da59efc0d | 135 | Voltage_FSR = Voltage_FSR * 33; // Change the value to be in the 0 to 3300 range |
JJting | 0:d89da59efc0d | 136 | Voltage_EMG = Voltage_EMG * 33; // Change the value to be in the 0 to 3300 range |
JJting | 0:d89da59efc0d | 137 | time_flag = t.read_ms(); |
JJting | 0:d89da59efc0d | 138 | } |