![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
四轴
Dependencies: BufferedSerial SoftPWM mbed send
Fork of send_copy by
main.cpp@2:1199904d820e, 2017-05-06 (annotated)
- Committer:
- zhangyx
- Date:
- Sat May 06 04:46:18 2017 +0000
- Revision:
- 2:1199904d820e
- Parent:
- 1:5be2682710c6
- Child:
- 3:61afd8d17063
test version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 2 | #include "nRF24L01P.h" |
Owen | 0:a51a6e7da590 | 3 | |
zhangyx | 2:1199904d820e | 4 | Serial pc(USBTX, USBRX, 115200); // tx, rx |
Owen | 0:a51a6e7da590 | 5 | |
zhangyx | 2:1199904d820e | 6 | nRF24L01P my_nrf24l01p(D4, D5, D3, D7, D8, D6); // mosi, miso, sck, csn, ce, irq |
Owen | 0:a51a6e7da590 | 7 | |
Owen | 0:a51a6e7da590 | 8 | DigitalOut myled1(LED1); |
Owen | 0:a51a6e7da590 | 9 | DigitalOut myled2(LED2); |
Owen | 0:a51a6e7da590 | 10 | |
Owen | 0:a51a6e7da590 | 11 | int main() { |
zhangyx | 2:1199904d820e | 12 | |
zhangyx | 2:1199904d820e | 13 | pc.printf("init\r\n"); |
Owen | 0:a51a6e7da590 | 14 | |
Owen | 0:a51a6e7da590 | 15 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 16 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 17 | // only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 18 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 19 | |
Owen | 0:a51a6e7da590 | 20 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 21 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 22 | int rxDataCnt = 0; |
Owen | 0:a51a6e7da590 | 23 | |
Owen | 0:a51a6e7da590 | 24 | my_nrf24l01p.powerUp(); |
Owen | 0:a51a6e7da590 | 25 | |
Owen | 0:a51a6e7da590 | 26 | // Display the (default) setup of the nRF24L01+ chip |
Owen | 0:a51a6e7da590 | 27 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 28 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
Owen | 0:a51a6e7da590 | 29 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
Owen | 0:a51a6e7da590 | 30 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
Owen | 0:a51a6e7da590 | 31 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
Owen | 0:a51a6e7da590 | 32 | |
Owen | 0:a51a6e7da590 | 33 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 34 | |
Owen | 0:a51a6e7da590 | 35 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 36 | |
Owen | 0:a51a6e7da590 | 37 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 38 | my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 39 | |
Owen | 0:a51a6e7da590 | 40 | while (1) { |
Owen | 0:a51a6e7da590 | 41 | |
Owen | 0:a51a6e7da590 | 42 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 43 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 44 | |
Owen | 0:a51a6e7da590 | 45 | // ...add it to the transmit buffer |
Owen | 0:a51a6e7da590 | 46 | txData[txDataCnt++] = pc.getc(); |
Owen | 0:a51a6e7da590 | 47 | |
Owen | 0:a51a6e7da590 | 48 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 49 | if ( txDataCnt >= sizeof( txData ) ) { |
Owen | 0:a51a6e7da590 | 50 | |
Owen | 0:a51a6e7da590 | 51 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 52 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 53 | |
Owen | 0:a51a6e7da590 | 54 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 55 | } |
Owen | 0:a51a6e7da590 | 56 | |
Owen | 0:a51a6e7da590 | 57 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 58 | myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 59 | } |
Owen | 0:a51a6e7da590 | 60 | |
Owen | 0:a51a6e7da590 | 61 | // If we've received anything in the nRF24L01+... |
Owen | 0:a51a6e7da590 | 62 | if ( my_nrf24l01p.readable() ) { |
Owen | 0:a51a6e7da590 | 63 | |
Owen | 0:a51a6e7da590 | 64 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 65 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 66 | |
Owen | 0:a51a6e7da590 | 67 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 68 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 69 | |
Owen | 0:a51a6e7da590 | 70 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 71 | } |
Owen | 0:a51a6e7da590 | 72 | |
Owen | 0:a51a6e7da590 | 73 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
Owen | 0:a51a6e7da590 | 74 | myled2 = !myled2; |
Owen | 0:a51a6e7da590 | 75 | } |
Owen | 0:a51a6e7da590 | 76 | } |
Owen | 0:a51a6e7da590 | 77 | } |