thunchanok phutthaphaiboon
/
M89_test_uart
version1
main.cpp@0:ac7bd75171ef, 2019-03-19 (annotated)
- Committer:
- moove1334
- Date:
- Tue Mar 19 15:50:36 2019 +0000
- Revision:
- 0:ac7bd75171ef
- Child:
- 1:8e615c17b2b3
test serial interrupt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
moove1334 | 0:ac7bd75171ef | 1 | #include "mbed.h" |
moove1334 | 0:ac7bd75171ef | 2 | |
moove1334 | 0:ac7bd75171ef | 3 | Serial device(D1, D0); |
moove1334 | 0:ac7bd75171ef | 4 | DigitalOut led1(LED1); |
moove1334 | 0:ac7bd75171ef | 5 | DigitalIn button0(USER_BUTTON); |
moove1334 | 0:ac7bd75171ef | 6 | |
moove1334 | 0:ac7bd75171ef | 7 | void Rx_interrupt(); |
moove1334 | 0:ac7bd75171ef | 8 | void SetSerial(int c); |
moove1334 | 0:ac7bd75171ef | 9 | |
moove1334 | 0:ac7bd75171ef | 10 | int data_size = 5; |
moove1334 | 0:ac7bd75171ef | 11 | char data[5] = {}; |
moove1334 | 0:ac7bd75171ef | 12 | char package = 0; |
moove1334 | 0:ac7bd75171ef | 13 | char num_data = 0; |
moove1334 | 0:ac7bd75171ef | 14 | |
moove1334 | 0:ac7bd75171ef | 15 | int main() |
moove1334 | 0:ac7bd75171ef | 16 | { |
moove1334 | 0:ac7bd75171ef | 17 | device.attach(&Rx_interrupt); |
moove1334 | 0:ac7bd75171ef | 18 | |
moove1334 | 0:ac7bd75171ef | 19 | led1 = 0; |
moove1334 | 0:ac7bd75171ef | 20 | while(true) |
moove1334 | 0:ac7bd75171ef | 21 | { |
moove1334 | 0:ac7bd75171ef | 22 | if (package == 1) |
moove1334 | 0:ac7bd75171ef | 23 | { |
moove1334 | 0:ac7bd75171ef | 24 | package = 0; |
moove1334 | 0:ac7bd75171ef | 25 | led1 = 1; |
moove1334 | 0:ac7bd75171ef | 26 | wait(1); |
moove1334 | 0:ac7bd75171ef | 27 | led1 = 0; |
moove1334 | 0:ac7bd75171ef | 28 | wait(0.5); |
moove1334 | 0:ac7bd75171ef | 29 | } |
moove1334 | 0:ac7bd75171ef | 30 | } |
moove1334 | 0:ac7bd75171ef | 31 | } |
moove1334 | 0:ac7bd75171ef | 32 | |
moove1334 | 0:ac7bd75171ef | 33 | void Rx_interrupt() |
moove1334 | 0:ac7bd75171ef | 34 | { |
moove1334 | 0:ac7bd75171ef | 35 | char c = device.getc(); |
moove1334 | 0:ac7bd75171ef | 36 | int i = (int)c; |
moove1334 | 0:ac7bd75171ef | 37 | SetSerial(i); |
moove1334 | 0:ac7bd75171ef | 38 | } |
moove1334 | 0:ac7bd75171ef | 39 | void SetSerial(int c) |
moove1334 | 0:ac7bd75171ef | 40 | { |
moove1334 | 0:ac7bd75171ef | 41 | if (num_data < 2){ |
moove1334 | 0:ac7bd75171ef | 42 | if (c == 255){ |
moove1334 | 0:ac7bd75171ef | 43 | data[num_data] = c; |
moove1334 | 0:ac7bd75171ef | 44 | num_data++; |
moove1334 | 0:ac7bd75171ef | 45 | }else{ |
moove1334 | 0:ac7bd75171ef | 46 | data[num_data] = c; |
moove1334 | 0:ac7bd75171ef | 47 | num_data = 0; |
moove1334 | 0:ac7bd75171ef | 48 | } |
moove1334 | 0:ac7bd75171ef | 49 | } |
moove1334 | 0:ac7bd75171ef | 50 | else if (num_data < data_size){ |
moove1334 | 0:ac7bd75171ef | 51 | data[num_data] = c; |
moove1334 | 0:ac7bd75171ef | 52 | num_data++; |
moove1334 | 0:ac7bd75171ef | 53 | if (num_data >= data_size){ |
moove1334 | 0:ac7bd75171ef | 54 | if (data[data_size-1]==255){ |
moove1334 | 0:ac7bd75171ef | 55 | num_data = 0; |
moove1334 | 0:ac7bd75171ef | 56 | package = 1; |
moove1334 | 0:ac7bd75171ef | 57 | } |
moove1334 | 0:ac7bd75171ef | 58 | } |
moove1334 | 0:ac7bd75171ef | 59 | } |
moove1334 | 0:ac7bd75171ef | 60 | } |