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
- Committer:
- hyunsungkim
- Date:
- 2018-11-25
- Branch:
- RF24_library_test_tx
- Revision:
- 9:d85804e8d3b3
- Parent:
- 8:78294b936c70
- Child:
- 10:02d3ca034103
File content as of revision 9:d85804e8d3b3:
#include "mbed.h"
#include "nRF24L01P.h"
#include "beep.h"
#define PING 1
#define PONG 2
#define nrf_CE D2
#define nrf_CSN A3
#define spi_SCK D13
#define spi_MOSI D11
#define spi_MISO D12
#define spi_IRQ D4
#define TRANSFER_SIZE 14
nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ); // mosi, miso, sck, csn, ce, irq
//volatile int role = PING;
volatile int role = PONG;
Serial pc(USBTX, USBRX);
Serial lidar(D1, D0);
PwmOut motor_RA(D9);
PwmOut motor_RB(D10);
PwmOut motor_LA(D3);
PwmOut motor_LB(D6);
PwmOut led_B(A5);
PwmOut led_G(A2);
PwmOut led_R(A1);
PwmOut buzzer(D5);
AnalogIn batteryCheck(A0);
void beepStart();
void endBeep();
void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData)
{
*(txData+0) = id/10+'0';
*(txData+1) = id%10+'0';
*(txData+2) = count/10000+'0';
*(txData+3) = count/1000%10+'0';
*(txData+4) = count/100%10+'0';
*(txData+5) = count/10%10+'0';
*(txData+6) = count%10+'0';
*(txData+7) = lspeed/100+'0';
*(txData+8) = lspeed/10%10+'0';
*(txData+9) = lspeed%10+'0';
*(txData+10) = rspeed/100+'0';
*(txData+11) = rspeed/10%10+'0';
*(txData+12) = rspeed%10+'0';
*(txData+13) = '\0';
}
int main() {
char txData[TRANSFER_SIZE];
int txDataCnt = 0;
char rxData[TRANSFER_SIZE];
int rxDataCnt = 0;
beepStart();
pc.baud(115200);
// Print setting of radio module
if(role == PING) {
nrf.setTxAddress(0xDEADBEEF0F);
nrf.powerUp();
nrf.setTransferSize( TRANSFER_SIZE );
nrf.setTransmitMode();
nrf.enable();
} else {
nrf.setRxAddress(0xDEADBEEF0F);
nrf.powerUp();
nrf.setTransferSize( TRANSFER_SIZE );
nrf.setReceiveMode();
nrf.enable();
}
pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", nrf.getRfFrequency() );
pc.printf( "nRF24L01+ Output power : %d dBm\r\n", nrf.getRfOutputPower() );
pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", nrf.getAirDataRate() );
pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nrf.getTxAddress() );
pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nrf.getRxAddress() );
while(1) {
if(role == PING) {
int id = 1;
int lspeed=100;
int rspeed=123;
//printf("transmitting\r\n");
txDataCnt++;
getPayload(id, txDataCnt, lspeed, rspeed, txData);
pc.printf("PING:%s\r\n", txData);
//char txData_[] = "123456789ABCD";
nrf.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
wait(0.2);
}
else if(role == PONG) {
if ( nrf.readable() ) {
rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE);
printf("%s\r\n", rxData);
}
}
}
}