thunchanok phutthaphaiboon
/
M89_test_uart
version1
Diff: main.cpp
- Revision:
- 0:ac7bd75171ef
- Child:
- 1:8e615c17b2b3
diff -r 000000000000 -r ac7bd75171ef main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Mar 19 15:50:36 2019 +0000 @@ -0,0 +1,60 @@ +#include "mbed.h" + +Serial device(D1, D0); +DigitalOut led1(LED1); +DigitalIn button0(USER_BUTTON); + +void Rx_interrupt(); +void SetSerial(int c); + +int data_size = 5; +char data[5] = {}; +char package = 0; +char num_data = 0; + +int main() +{ + device.attach(&Rx_interrupt); + + led1 = 0; + while(true) + { + if (package == 1) + { + package = 0; + led1 = 1; + wait(1); + led1 = 0; + wait(0.5); + } + } +} + +void Rx_interrupt() +{ + char c = device.getc(); + int i = (int)c; + SetSerial(i); +} +void SetSerial(int c) +{ + if (num_data < 2){ + if (c == 255){ + data[num_data] = c; + num_data++; + }else{ + data[num_data] = c; + num_data = 0; + } + } + else if (num_data < data_size){ + data[num_data] = c; + num_data++; + if (num_data >= data_size){ + if (data[data_size-1]==255){ + num_data = 0; + package = 1; + } + } + } +} \ No newline at end of file