Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@9:d85804e8d3b3, 2018-11-25 (annotated)
- Committer:
- hyunsungkim
- Date:
- Sun Nov 25 10:44:40 2018 +0000
- Branch:
- RF24_library_test_tx
- Revision:
- 9:d85804e8d3b3
- Parent:
- 8:78294b936c70
- Child:
- 10:02d3ca034103
2 ?? RX ?? ??? ???? Broadcast ?? ??
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hyunsungkim | 0:2dade6db4d3a | 1 | #include "mbed.h" |
| hyunsungkim | 8:78294b936c70 | 2 | #include "nRF24L01P.h" |
| hyunsungkim | 3:690740ab3394 | 3 | #include "beep.h" |
| hyunsungkim | 3:690740ab3394 | 4 | |
| hyunsungkim | 8:78294b936c70 | 5 | #define PING 1 |
| hyunsungkim | 8:78294b936c70 | 6 | #define PONG 2 |
| hyunsungkim | 3:690740ab3394 | 7 | |
| hyunsungkim | 3:690740ab3394 | 8 | #define nrf_CE D2 |
| hyunsungkim | 3:690740ab3394 | 9 | #define nrf_CSN A3 |
| hyunsungkim | 3:690740ab3394 | 10 | #define spi_SCK D13 |
| hyunsungkim | 3:690740ab3394 | 11 | #define spi_MOSI D11 |
| hyunsungkim | 3:690740ab3394 | 12 | #define spi_MISO D12 |
| hyunsungkim | 8:78294b936c70 | 13 | #define spi_IRQ D4 |
| hyunsungkim | 8:78294b936c70 | 14 | |
| hyunsungkim | 8:78294b936c70 | 15 | #define TRANSFER_SIZE 14 |
| hyunsungkim | 8:78294b936c70 | 16 | |
| hyunsungkim | 8:78294b936c70 | 17 | nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ); // mosi, miso, sck, csn, ce, irq |
| hyunsungkim | 8:78294b936c70 | 18 | |
| hyunsungkim | 9:d85804e8d3b3 | 19 | //volatile int role = PING; |
| hyunsungkim | 9:d85804e8d3b3 | 20 | volatile int role = PONG; |
| hyunsungkim | 0:2dade6db4d3a | 21 | |
| hyunsungkim | 0:2dade6db4d3a | 22 | Serial pc(USBTX, USBRX); |
| hyunsungkim | 0:2dade6db4d3a | 23 | Serial lidar(D1, D0); |
| hyunsungkim | 8:78294b936c70 | 24 | |
| hyunsungkim | 0:2dade6db4d3a | 25 | PwmOut motor_RA(D9); |
| hyunsungkim | 0:2dade6db4d3a | 26 | PwmOut motor_RB(D10); |
| hyunsungkim | 0:2dade6db4d3a | 27 | PwmOut motor_LA(D3); |
| hyunsungkim | 0:2dade6db4d3a | 28 | PwmOut motor_LB(D6); |
| hyunsungkim | 4:ae81aeeed069 | 29 | PwmOut led_B(A5); |
| hyunsungkim | 0:2dade6db4d3a | 30 | PwmOut led_G(A2); |
| hyunsungkim | 4:ae81aeeed069 | 31 | PwmOut led_R(A1); |
| hyunsungkim | 0:2dade6db4d3a | 32 | PwmOut buzzer(D5); |
| hyunsungkim | 0:2dade6db4d3a | 33 | AnalogIn batteryCheck(A0); |
| hyunsungkim | 0:2dade6db4d3a | 34 | |
| hyunsungkim | 8:78294b936c70 | 35 | void beepStart(); |
| hyunsungkim | 8:78294b936c70 | 36 | void endBeep(); |
| hyunsungkim | 8:78294b936c70 | 37 | |
| hyunsungkim | 8:78294b936c70 | 38 | void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData) |
| hyunsungkim | 3:690740ab3394 | 39 | { |
| hyunsungkim | 8:78294b936c70 | 40 | *(txData+0) = id/10+'0'; |
| hyunsungkim | 8:78294b936c70 | 41 | *(txData+1) = id%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 42 | *(txData+2) = count/10000+'0'; |
| hyunsungkim | 8:78294b936c70 | 43 | *(txData+3) = count/1000%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 44 | *(txData+4) = count/100%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 45 | *(txData+5) = count/10%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 46 | *(txData+6) = count%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 47 | *(txData+7) = lspeed/100+'0'; |
| hyunsungkim | 8:78294b936c70 | 48 | *(txData+8) = lspeed/10%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 49 | *(txData+9) = lspeed%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 50 | *(txData+10) = rspeed/100+'0'; |
| hyunsungkim | 8:78294b936c70 | 51 | *(txData+11) = rspeed/10%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 52 | *(txData+12) = rspeed%10+'0'; |
| hyunsungkim | 8:78294b936c70 | 53 | *(txData+13) = '\0'; |
| hyunsungkim | 8:78294b936c70 | 54 | } |
| hyunsungkim | 6:42ecdff5956a | 55 | |
| hyunsungkim | 0:2dade6db4d3a | 56 | int main() { |
| hyunsungkim | 8:78294b936c70 | 57 | |
| hyunsungkim | 8:78294b936c70 | 58 | char txData[TRANSFER_SIZE]; |
| hyunsungkim | 8:78294b936c70 | 59 | int txDataCnt = 0; |
| hyunsungkim | 8:78294b936c70 | 60 | char rxData[TRANSFER_SIZE]; |
| hyunsungkim | 8:78294b936c70 | 61 | int rxDataCnt = 0; |
| hyunsungkim | 6:42ecdff5956a | 62 | |
| hyunsungkim | 8:78294b936c70 | 63 | beepStart(); |
| hyunsungkim | 0:2dade6db4d3a | 64 | pc.baud(115200); |
| hyunsungkim | 8:78294b936c70 | 65 | // Print setting of radio module |
| hyunsungkim | 0:2dade6db4d3a | 66 | |
| hyunsungkim | 8:78294b936c70 | 67 | if(role == PING) { |
| hyunsungkim | 8:78294b936c70 | 68 | nrf.setTxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 69 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 70 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 71 | nrf.setTransmitMode(); |
| hyunsungkim | 8:78294b936c70 | 72 | nrf.enable(); |
| hyunsungkim | 8:78294b936c70 | 73 | } else { |
| hyunsungkim | 8:78294b936c70 | 74 | nrf.setRxAddress(0xDEADBEEF0F); |
| hyunsungkim | 8:78294b936c70 | 75 | nrf.powerUp(); |
| hyunsungkim | 8:78294b936c70 | 76 | nrf.setTransferSize( TRANSFER_SIZE ); |
| hyunsungkim | 8:78294b936c70 | 77 | nrf.setReceiveMode(); |
| hyunsungkim | 8:78294b936c70 | 78 | nrf.enable(); |
| hyunsungkim | 0:2dade6db4d3a | 79 | } |
| hyunsungkim | 8:78294b936c70 | 80 | |
| hyunsungkim | 0:2dade6db4d3a | 81 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() ); |
| hyunsungkim | 0:2dade6db4d3a | 82 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() ); |
| hyunsungkim | 0:2dade6db4d3a | 83 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() ); |
| hyunsungkim | 0:2dade6db4d3a | 84 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() ); |
| hyunsungkim | 0:2dade6db4d3a | 85 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() ); |
| hyunsungkim | 8:78294b936c70 | 86 | |
| hyunsungkim | 8:78294b936c70 | 87 | while(1) { |
| hyunsungkim | 8:78294b936c70 | 88 | if(role == PING) { |
| hyunsungkim | 8:78294b936c70 | 89 | int id = 1; |
| hyunsungkim | 8:78294b936c70 | 90 | int lspeed=100; |
| hyunsungkim | 8:78294b936c70 | 91 | int rspeed=123; |
| hyunsungkim | 8:78294b936c70 | 92 | //printf("transmitting\r\n"); |
| hyunsungkim | 8:78294b936c70 | 93 | txDataCnt++; |
| hyunsungkim | 8:78294b936c70 | 94 | getPayload(id, txDataCnt, lspeed, rspeed, txData); |
| hyunsungkim | 9:d85804e8d3b3 | 95 | pc.printf("PING:%s\r\n", txData); |
| hyunsungkim | 9:d85804e8d3b3 | 96 | //char txData_[] = "123456789ABCD"; |
| hyunsungkim | 8:78294b936c70 | 97 | nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE); |
| hyunsungkim | 8:78294b936c70 | 98 | wait(0.2); |
| hyunsungkim | 8:78294b936c70 | 99 | } |
| hyunsungkim | 8:78294b936c70 | 100 | else if(role == PONG) { |
| hyunsungkim | 8:78294b936c70 | 101 | if ( nrf.readable() ) { |
| hyunsungkim | 8:78294b936c70 | 102 | rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE); |
| hyunsungkim | 8:78294b936c70 | 103 | printf("%s\r\n", rxData); |
| hyunsungkim | 0:2dade6db4d3a | 104 | } |
| hyunsungkim | 0:2dade6db4d3a | 105 | } |
| hyunsungkim | 0:2dade6db4d3a | 106 | } |
| hyunsungkim | 8:78294b936c70 | 107 | } |
| hyunsungkim | 8:78294b936c70 | 108 |