I-O DATA DEV2 / Mbed 2 deprecated commu4

Dependencies:   mbed nRF24L01P

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main4.cpp Source File

main4.cpp

00001 
00002 //受信用プログラム
00003 
00004 #include "mbed.h"
00005 #include "nRF24L01P.h"
00006 RawSerial  pc(PA_2, PA_3,115200);
00007 AnalogIn   mic_in(A0);
00008 AnalogOut  sp_out(A2);
00009 Ticker     interrput;
00010 #define TRANSFER_SIZE   16
00011 int      out_flg = 0;
00012 int      rcv_flg = 0;
00013 uint16_t rxData[TRANSFER_SIZE*2];
00014 int      rxDataIdx = 0;
00015 int      rxDataCnt = 0;
00016  
00017 nRF24L01P my_nrf24l01p(D11, D12, D13, D10, D9,D8);    // mosi, miso, sck, csn, ce, irq
00018 //nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10);    // mosi, miso, sck, csn, ce, irq
00019 
00020 
00021 void timer(){     //タイマー割り込みによるSPEAKER out
00022       if(rcv_flg) {
00023          out_flg=1;     
00024       } 
00025       if(out_flg){    
00026         sp_out.write_u16(rxData[rxDataIdx]);
00027         rxDataIdx++;
00028         if (rxDataIdx >= TRANSFER_SIZE*2) rxDataIdx=0;
00029       }    
00030 }
00031 
00032 int main() {
00033 
00034   int bufferidx=0;
00035    int i=0;
00036 
00037     my_nrf24l01p.powerUp();
00038     my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);//2400-2525
00039     my_nrf24l01p.setRfOutputPower(NRF24L01P_TX_PWR_MINUS_12_DB);//mAX 0  -6 -12 -18
00040     my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS);//250k,1000,2000K
00041     // Display the (default) setup of the nRF24L01+ chip
00042     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00043     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00044     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00045     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00046     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00047 
00048     pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
00049     my_nrf24l01p.setTransferSize( TRANSFER_SIZE * sizeof( uint16_t ) );//mAX 32
00050     my_nrf24l01p.setReceiveMode();
00051     my_nrf24l01p.enable();
00052     
00053     wait_ms(100);
00054    // my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char*)rxData,1);//dummy
00055     interrput.attach_us(&timer, 100);//100 usec 10Khz
00056 
00057  //   my_nrf24l01p.flush_rx_fifo();
00058     while (1) {
00059         // If we've received anything in the nRF24L01+...
00060         if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) {    //受信?
00061             // ...read the data into the receive buffer
00062             if (rxDataIdx < 16) bufferidx = TRANSFER_SIZE;
00063             else bufferidx = 0; 
00064             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*)(rxData+bufferidx),TRANSFER_SIZE * sizeof( uint16_t ) );
00065             //myled1 = !myled1;
00066             //if (rcv_flg==0) 
00067             if ((i++ & 0xf) ==0) pc.putc('.');
00068             rcv_flg = 1;
00069         }
00070     }
00071 }