![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
UD-GS01治具の試作プログラムです
Dependencies: mbed nRF24L01P SDFileSystem
Goto_UD-GS01.cpp@9:086fae0bd5f8, 2020-12-10 (annotated)
- Committer:
- rgoto
- Date:
- Thu Dec 10 04:49:42 2020 +0000
- Revision:
- 9:086fae0bd5f8
- Parent:
- 8:3369eef8f9e1
- Child:
- 10:6e3bdd6f11c3
ver.7;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgoto | 0:53b18a9251ab | 1 | |
rgoto | 0:53b18a9251ab | 2 | //受信用と送信用を一つにする!ボタンで切り替え(元のプログラムは44で、44-2を貼り付ける) |
rgoto | 0:53b18a9251ab | 3 | |
rgoto | 0:53b18a9251ab | 4 | #include "mbed.h" |
rgoto | 0:53b18a9251ab | 5 | #include "nRF24L01P.h" |
rgoto | 9:086fae0bd5f8 | 6 | |
rgoto | 9:086fae0bd5f8 | 7 | //#include "SDFileSystem.h" |
rgoto | 8:3369eef8f9e1 | 8 | Serial pc(PA_2, PA_3); //ここはpcとケーブルでつないだときにシリアル通信させてたところだから、ここから牛のUARTにつなぐだけで大丈夫なはず。→ここは元々ST基板のPC部分(パキッと折れそうな方)との通信用に使われてるから他のシリアルは無理らしい。 |
rgoto | 8:3369eef8f9e1 | 9 | Serial UDGS01(PA_0, PA_1); // UD-GS01とのシリアル通信用にUARTピンを新たに定義。 |
rgoto | 8:3369eef8f9e1 | 10 | |
rgoto | 0:53b18a9251ab | 11 | Ticker interrput; |
rgoto | 0:53b18a9251ab | 12 | |
rgoto | 0:53b18a9251ab | 13 | #define TRANSFER_SIZE 32 |
rgoto | 0:53b18a9251ab | 14 | int out_flg = 0; |
rgoto | 0:53b18a9251ab | 15 | int rcv_flg = 0; |
rgoto | 0:53b18a9251ab | 16 | int snd_flg = 0; |
rgoto | 3:fe94916d0f12 | 17 | char txData1[TRANSFER_SIZE]; |
rgoto | 3:fe94916d0f12 | 18 | char txData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/ |
rgoto | 0:53b18a9251ab | 19 | int txDataIdx = 0; |
rgoto | 0:53b18a9251ab | 20 | int txDataCnt=0; |
rgoto | 4:de94aef84acc | 21 | char rxData1[TRANSFER_SIZE]; |
rgoto | 4:de94aef84acc | 22 | char rxData2[TRANSFER_SIZE]; /*char型で一文字ずつ配列に入れていき、表示する。char型は一つ1バイトなので、32個ずつ溜まったら送るようにする。*/ |
rgoto | 4:de94aef84acc | 23 | |
rgoto | 0:53b18a9251ab | 24 | int rxDataIdx = 0; |
rgoto | 0:53b18a9251ab | 25 | int rxDataCnt=0; |
rgoto | 0:53b18a9251ab | 26 | int i=0; |
rgoto | 9:086fae0bd5f8 | 27 | int open_flg=0; |
rgoto | 2:53829a66c0d9 | 28 | int bufferidx=0; |
rgoto | 0:53b18a9251ab | 29 | |
rgoto | 0:53b18a9251ab | 30 | nRF24L01P my_nrf24l01p(D11, D12, D13, D10, D9,D8); // mosi, miso, sck, csn, ce, irq |
rgoto | 8:3369eef8f9e1 | 31 | |
rgoto | 0:53b18a9251ab | 32 | |
rgoto | 0:53b18a9251ab | 33 | void timer(){ /*タイマー割り込みによるSPEAKER out*/ |
rgoto | 0:53b18a9251ab | 34 | uint16_t ain; |
rgoto | 0:53b18a9251ab | 35 | |
rgoto | 0:53b18a9251ab | 36 | |
rgoto | 4:de94aef84acc | 37 | if(rcv_flg == 1) { |
rgoto | 2:53829a66c0d9 | 38 | |
rgoto | 4:de94aef84acc | 39 | pc.printf("%s", &rxData2[rxDataIdx]); |
rgoto | 0:53b18a9251ab | 40 | rxDataIdx++; |
rgoto | 4:de94aef84acc | 41 | if (rxDataIdx >= TRANSFER_SIZE) |
rgoto | 2:53829a66c0d9 | 42 | rxDataIdx=0; |
rgoto | 4:de94aef84acc | 43 | rcv_flg = 0; |
rgoto | 9:086fae0bd5f8 | 44 | i++; |
rgoto | 9:086fae0bd5f8 | 45 | } |
rgoto | 0:53b18a9251ab | 46 | } |
rgoto | 0:53b18a9251ab | 47 | |
rgoto | 0:53b18a9251ab | 48 | void recieve(){ |
rgoto | 0:53b18a9251ab | 49 | if(UDGS01.readable()){ |
rgoto | 0:53b18a9251ab | 50 | |
rgoto | 0:53b18a9251ab | 51 | // ...read the data into the receive buffer |
rgoto | 3:fe94916d0f12 | 52 | txData1[txDataIdx] = UDGS01.getc(); //getcharだと、PCからのキーボード入力という標準コンソール?のやつだからだめらしい |
rgoto | 9:086fae0bd5f8 | 53 | |
rgoto | 0:53b18a9251ab | 54 | txDataIdx++; |
rgoto | 3:fe94916d0f12 | 55 | memcpy(txData2, txData1, TRANSFER_SIZE); |
rgoto | 3:fe94916d0f12 | 56 | |
rgoto | 3:fe94916d0f12 | 57 | txDataIdx=0; |
rgoto | 0:53b18a9251ab | 58 | snd_flg = 1; |
rgoto | 1:960cd07d2ae7 | 59 | |
rgoto | 3:fe94916d0f12 | 60 | } |
rgoto | 3:fe94916d0f12 | 61 | |
rgoto | 4:de94aef84acc | 62 | } |
rgoto | 4:de94aef84acc | 63 | |
rgoto | 8:3369eef8f9e1 | 64 | |
rgoto | 0:53b18a9251ab | 65 | int main() { |
rgoto | 8:3369eef8f9e1 | 66 | pc.baud(115200); |
rgoto | 8:3369eef8f9e1 | 67 | UDGS01.baud(115200); |
rgoto | 0:53b18a9251ab | 68 | my_nrf24l01p.powerUp(); |
rgoto | 0:53b18a9251ab | 69 | my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);//2400-2525 |
rgoto | 0:53b18a9251ab | 70 | my_nrf24l01p.setRfOutputPower(NRF24L01P_TX_PWR_MINUS_12_DB);//mAX 0 -6 -12 -18 |
rgoto | 0:53b18a9251ab | 71 | my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS);//250k,1000,2000K |
rgoto | 0:53b18a9251ab | 72 | // Display the (default) setup of the nRF24L01+ chip |
rgoto | 0:53b18a9251ab | 73 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
rgoto | 0:53b18a9251ab | 74 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
rgoto | 0:53b18a9251ab | 75 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
rgoto | 0:53b18a9251ab | 76 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
rgoto | 0:53b18a9251ab | 77 | // pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
rgoto | 0:53b18a9251ab | 78 | |
rgoto | 0:53b18a9251ab | 79 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
rgoto | 0:53b18a9251ab | 80 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE );//mAX 32 |
rgoto | 0:53b18a9251ab | 81 | my_nrf24l01p.setReceiveMode(); |
rgoto | 0:53b18a9251ab | 82 | my_nrf24l01p.enable(); |
rgoto | 9:086fae0bd5f8 | 83 | // SDFileSystem *sd = new SDFileSystem(PB_15, PB_14, PB_13, PB_11, "sd", NC, SDFileSystem::SWITCH_NONE, 20000000); //SDカードのやつ mosi, miso, sclk, cs, name, card detect, sw type, freq |
rgoto | 9:086fae0bd5f8 | 84 | // FILE *fp; |
rgoto | 0:53b18a9251ab | 85 | wait_ms(100); |
rgoto | 9:086fae0bd5f8 | 86 | |
rgoto | 9:086fae0bd5f8 | 87 | interrput.attach(&timer, 1);//100 usec 10Khz |
rgoto | 0:53b18a9251ab | 88 | UDGS01.attach(recieve,Serial::RxIrq);//牛からのデータ受信したら割り込み発生してrecieveを呼び出す |
rgoto | 0:53b18a9251ab | 89 | |
rgoto | 0:53b18a9251ab | 90 | // my_nrf24l01p.flush_rx_fifo(); |
rgoto | 0:53b18a9251ab | 91 | while(1){ |
rgoto | 0:53b18a9251ab | 92 | |
rgoto | 4:de94aef84acc | 93 | if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { //受信可能であれば |
rgoto | 8:3369eef8f9e1 | 94 | |
rgoto | 9:086fae0bd5f8 | 95 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*)(rxData1),TRANSFER_SIZE ); |
rgoto | 4:de94aef84acc | 96 | |
rgoto | 4:de94aef84acc | 97 | memcpy(rxData2, rxData1, TRANSFER_SIZE); |
rgoto | 9:086fae0bd5f8 | 98 | |
rgoto | 8:3369eef8f9e1 | 99 | |
rgoto | 9:086fae0bd5f8 | 100 | // if(open_flg == 0){ |
rgoto | 9:086fae0bd5f8 | 101 | // fp = fopen("/sd/log/log(UD-GS01).txt", "a"); |
rgoto | 9:086fae0bd5f8 | 102 | // open_flg = 1; |
rgoto | 9:086fae0bd5f8 | 103 | |
rgoto | 9:086fae0bd5f8 | 104 | // if(open_flg == 1){ |
rgoto | 9:086fae0bd5f8 | 105 | // fputs(rxData2, fp); |
rgoto | 9:086fae0bd5f8 | 106 | // } |
rgoto | 9:086fae0bd5f8 | 107 | |
rgoto | 9:086fae0bd5f8 | 108 | // if(i>10){ |
rgoto | 9:086fae0bd5f8 | 109 | // i=0; |
rgoto | 9:086fae0bd5f8 | 110 | // wait(1); |
rgoto | 9:086fae0bd5f8 | 111 | // fclose(fp); |
rgoto | 9:086fae0bd5f8 | 112 | // open_flg = 0; |
rgoto | 9:086fae0bd5f8 | 113 | // } |
rgoto | 4:de94aef84acc | 114 | rcv_flg = 1; |
rgoto | 4:de94aef84acc | 115 | |
rgoto | 9:086fae0bd5f8 | 116 | } |
rgoto | 2:53829a66c0d9 | 117 | |
rgoto | 0:53b18a9251ab | 118 | if (snd_flg==1) {//最初のバッファ |
rgoto | 0:53b18a9251ab | 119 | snd_flg=0; |
rgoto | 2:53829a66c0d9 | 120 | |
rgoto | 3:fe94916d0f12 | 121 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char *)txData2 , TRANSFER_SIZE ); |
rgoto | 9:086fae0bd5f8 | 122 | |
rgoto | 8:3369eef8f9e1 | 123 | pc.putc('1'); |
rgoto | 8:3369eef8f9e1 | 124 | wait(1); |
rgoto | 8:3369eef8f9e1 | 125 | }else if (snd_flg==2) {//後半のバッファ |
rgoto | 8:3369eef8f9e1 | 126 | snd_flg=0; |
rgoto | 8:3369eef8f9e1 | 127 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char*)txData2 , TRANSFER_SIZE ); |
rgoto | 8:3369eef8f9e1 | 128 | pc.putc('2'); |
rgoto | 6:bf0572ed7c6b | 129 | wait(1); |
rgoto | 0:53b18a9251ab | 130 | } |
rgoto | 0:53b18a9251ab | 131 | |
rgoto | 9:086fae0bd5f8 | 132 | |
rgoto | 9:086fae0bd5f8 | 133 | } //一番最初のwhileの} |
rgoto | 9:086fae0bd5f8 | 134 | // } |
rgoto | 9:086fae0bd5f8 | 135 | } |