Yoichi Nagashima
/
CQ_nagasm_03_timer_test
NucleoF401RE timer interrupt test with XBee serial communication.
main.cpp@0:36f30c7bb3a2, 2014-12-06 (annotated)
- Committer:
- nagasm
- Date:
- Sat Dec 06 06:26:16 2014 +0000
- Revision:
- 0:36f30c7bb3a2
NucleoF401RE timer interrupt test with XBee serial communication.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nagasm | 0:36f30c7bb3a2 | 1 | #include "mbed.h" |
nagasm | 0:36f30c7bb3a2 | 2 | |
nagasm | 0:36f30c7bb3a2 | 3 | unsigned char rxFIFO[256], txFIFO[256], raw_data[6]; |
nagasm | 0:36f30c7bb3a2 | 4 | unsigned char rx_top, rx_end, tx_top, tx_end, phase; |
nagasm | 0:36f30c7bb3a2 | 5 | |
nagasm | 0:36f30c7bb3a2 | 6 | RawSerial xbee(PA_2, PA_3); |
nagasm | 0:36f30c7bb3a2 | 7 | DigitalOut myled(LED1); |
nagasm | 0:36f30c7bb3a2 | 8 | |
nagasm | 0:36f30c7bb3a2 | 9 | void tx_fifo_check(){ |
nagasm | 0:36f30c7bb3a2 | 10 | if(xbee.writeable() == 1){ |
nagasm | 0:36f30c7bb3a2 | 11 | if(tx_top != tx_end){ |
nagasm | 0:36f30c7bb3a2 | 12 | xbee.putc(txFIFO[tx_end]); |
nagasm | 0:36f30c7bb3a2 | 13 | ++tx_end &= 255; |
nagasm | 0:36f30c7bb3a2 | 14 | } |
nagasm | 0:36f30c7bb3a2 | 15 | } |
nagasm | 0:36f30c7bb3a2 | 16 | } |
nagasm | 0:36f30c7bb3a2 | 17 | |
nagasm | 0:36f30c7bb3a2 | 18 | int rx_fifo_check(){ |
nagasm | 0:36f30c7bb3a2 | 19 | unsigned char data; |
nagasm | 0:36f30c7bb3a2 | 20 | if(rx_top != rx_end){ |
nagasm | 0:36f30c7bb3a2 | 21 | data = rxFIFO[rx_end]; |
nagasm | 0:36f30c7bb3a2 | 22 | ++rx_end &= 255; |
nagasm | 0:36f30c7bb3a2 | 23 | if (data < 33){ |
nagasm | 0:36f30c7bb3a2 | 24 | phase = 0; |
nagasm | 0:36f30c7bb3a2 | 25 | return(1); |
nagasm | 0:36f30c7bb3a2 | 26 | } |
nagasm | 0:36f30c7bb3a2 | 27 | raw_data[phase] = data; |
nagasm | 0:36f30c7bb3a2 | 28 | if(++phase > 5) phase = 0; |
nagasm | 0:36f30c7bb3a2 | 29 | return(0); |
nagasm | 0:36f30c7bb3a2 | 30 | } |
nagasm | 0:36f30c7bb3a2 | 31 | return(0); |
nagasm | 0:36f30c7bb3a2 | 32 | } |
nagasm | 0:36f30c7bb3a2 | 33 | |
nagasm | 0:36f30c7bb3a2 | 34 | void rx_fifoset(void){ |
nagasm | 0:36f30c7bb3a2 | 35 | rxFIFO[rx_top] = xbee.getc(); |
nagasm | 0:36f30c7bb3a2 | 36 | ++rx_top &= 255; |
nagasm | 0:36f30c7bb3a2 | 37 | } |
nagasm | 0:36f30c7bb3a2 | 38 | |
nagasm | 0:36f30c7bb3a2 | 39 | void tx_fifoset(unsigned char data){ |
nagasm | 0:36f30c7bb3a2 | 40 | txFIFO[tx_top] = data; |
nagasm | 0:36f30c7bb3a2 | 41 | ++tx_top &= 255; |
nagasm | 0:36f30c7bb3a2 | 42 | } |
nagasm | 0:36f30c7bb3a2 | 43 | |
nagasm | 0:36f30c7bb3a2 | 44 | unsigned char hex_conv(unsigned char data){ |
nagasm | 0:36f30c7bb3a2 | 45 | data &= 15; |
nagasm | 0:36f30c7bb3a2 | 46 | if(data < 10) return(data+48); |
nagasm | 0:36f30c7bb3a2 | 47 | else return(data+55); |
nagasm | 0:36f30c7bb3a2 | 48 | } |
nagasm | 0:36f30c7bb3a2 | 49 | |
nagasm | 0:36f30c7bb3a2 | 50 | unsigned char conv_hex(unsigned char data){ |
nagasm | 0:36f30c7bb3a2 | 51 | if((data > 47) && (data < 58)) return(data-48); |
nagasm | 0:36f30c7bb3a2 | 52 | else if((data > 64) && (data < 71)) return(data-55); |
nagasm | 0:36f30c7bb3a2 | 53 | return(0); |
nagasm | 0:36f30c7bb3a2 | 54 | } |
nagasm | 0:36f30c7bb3a2 | 55 | |
nagasm | 0:36f30c7bb3a2 | 56 | void tx_message(int data){ |
nagasm | 0:36f30c7bb3a2 | 57 | int i; |
nagasm | 0:36f30c7bb3a2 | 58 | for (i=0; i<6; i++) tx_fifoset(hex_conv((data>>(4*(5-i))) & 15)); |
nagasm | 0:36f30c7bb3a2 | 59 | tx_fifoset(13); |
nagasm | 0:36f30c7bb3a2 | 60 | } |
nagasm | 0:36f30c7bb3a2 | 61 | |
nagasm | 0:36f30c7bb3a2 | 62 | int timer_value[5]; |
nagasm | 0:36f30c7bb3a2 | 63 | Ticker timer_setup; |
nagasm | 0:36f30c7bb3a2 | 64 | |
nagasm | 0:36f30c7bb3a2 | 65 | void timer_interrupt(){ |
nagasm | 0:36f30c7bb3a2 | 66 | int i; |
nagasm | 0:36f30c7bb3a2 | 67 | for (i=0; i<5; i++) ++timer_value[i] &= 65535; |
nagasm | 0:36f30c7bb3a2 | 68 | } |
nagasm | 0:36f30c7bb3a2 | 69 | |
nagasm | 0:36f30c7bb3a2 | 70 | int main(){ |
nagasm | 0:36f30c7bb3a2 | 71 | int i, j, sum; |
nagasm | 0:36f30c7bb3a2 | 72 | i = j = 0; |
nagasm | 0:36f30c7bb3a2 | 73 | rx_top = rx_end = tx_top = tx_end = phase = 0; |
nagasm | 0:36f30c7bb3a2 | 74 | xbee.baud(38400); |
nagasm | 0:36f30c7bb3a2 | 75 | xbee.attach(&rx_fifoset, xbee.RxIrq); |
nagasm | 0:36f30c7bb3a2 | 76 | for (i=0; i<5; i++) timer_value[i] = 0; |
nagasm | 0:36f30c7bb3a2 | 77 | timer_setup.attach_us(&timer_interrupt, 100); |
nagasm | 0:36f30c7bb3a2 | 78 | while(1){ |
nagasm | 0:36f30c7bb3a2 | 79 | tx_fifo_check(); |
nagasm | 0:36f30c7bb3a2 | 80 | if(rx_fifo_check() == 1){ |
nagasm | 0:36f30c7bb3a2 | 81 | sum = 0; |
nagasm | 0:36f30c7bb3a2 | 82 | for (i=0; i<6; i++) sum += conv_hex(raw_data[i])<<(4*(5-i)); |
nagasm | 0:36f30c7bb3a2 | 83 | tx_message(sum); /* Echo Back */ |
nagasm | 0:36f30c7bb3a2 | 84 | } |
nagasm | 0:36f30c7bb3a2 | 85 | if(timer_value[0] > 4999){ |
nagasm | 0:36f30c7bb3a2 | 86 | timer_value[0] = 0; |
nagasm | 0:36f30c7bb3a2 | 87 | myled = !myled; |
nagasm | 0:36f30c7bb3a2 | 88 | } |
nagasm | 0:36f30c7bb3a2 | 89 | if(timer_value[1] > 9999){ |
nagasm | 0:36f30c7bb3a2 | 90 | timer_value[1] = 0; |
nagasm | 0:36f30c7bb3a2 | 91 | j &= 8388607; |
nagasm | 0:36f30c7bb3a2 | 92 | tx_message(j++); |
nagasm | 0:36f30c7bb3a2 | 93 | } |
nagasm | 0:36f30c7bb3a2 | 94 | } |
nagasm | 0:36f30c7bb3a2 | 95 | } |