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