|
|
@ -8,15 +8,16 @@
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <nRF24L01.h>
|
|
|
|
#include <nRF24L01.h>
|
|
|
|
#include <RF24.h>
|
|
|
|
#include <RF24.h>
|
|
|
|
#define CE_PIN 7
|
|
|
|
//#define CE_PIN 7
|
|
|
|
#define CSN_PIN 8
|
|
|
|
#define CE_PIN 3
|
|
|
|
|
|
|
|
//#define CS_PIN 8
|
|
|
|
|
|
|
|
#define CS_PIN 2
|
|
|
|
|
|
|
|
|
|
|
|
#define LED PORTD2
|
|
|
|
#define LED PORTD2
|
|
|
|
#define PULL PORTD3
|
|
|
|
#define PULL PORTD3
|
|
|
|
#define ENABLE PORTD5
|
|
|
|
#define ENABLE PORTD5
|
|
|
|
#define DIRECTION PORTD6
|
|
|
|
#define DIRECTION PORTD6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uart_init() {
|
|
|
|
void uart_init() {
|
|
|
|
// Upper and lower bytes of the calculated prescaler value for baud.
|
|
|
|
// Upper and lower bytes of the calculated prescaler value for baud.
|
|
|
|
UBRR0H = UBRRH_VALUE;
|
|
|
|
UBRR0H = UBRRH_VALUE;
|
|
|
@ -45,38 +46,94 @@ void uart_putstr(char *data) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char thisSlaveAddress[5] = {'R','x','A','A','A'};
|
|
|
|
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
|
|
|
|
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
int radio_number = ROLE;
|
|
|
|
digitalWrite(10, 10);
|
|
|
|
int role = ROLE;
|
|
|
|
|
|
|
|
|
|
|
|
uart_init();
|
|
|
|
int main(void) {
|
|
|
|
|
|
|
|
|
|
|
|
DDRC &= ~(1<<0); // Input
|
|
|
|
uart_init();
|
|
|
|
PORTC |= (1<<0); // Enable Internal Pull Up (Setting it to HIGH)
|
|
|
|
|
|
|
|
|
|
|
|
// pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
|
|
|
|
// digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
|
|
|
|
|
|
|
|
// DDRD |= (1 << ENABLE);
|
|
|
|
RF24 radio(CE_PIN, CS_PIN);
|
|
|
|
DDRD = DDRD | 0xFC;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PORTD |= _BV(ENABLE);
|
|
|
|
|
|
|
|
PORTD |= _BV(DIRECTION);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(;;){
|
|
|
|
|
|
|
|
RF24 radio(CE_PIN, CSN_PIN);
|
|
|
|
|
|
|
|
radio.begin();
|
|
|
|
radio.begin();
|
|
|
|
radio.setDataRate(RF24_1MBPS);
|
|
|
|
|
|
|
|
|
|
|
|
if(radio_number) {
|
|
|
|
|
|
|
|
radio.openWritingPipe(pipes[1]);
|
|
|
|
// uart_putstr("hello\n");
|
|
|
|
radio.openReadingPipe(1,pipes[0]);
|
|
|
|
_delay_ms(400);
|
|
|
|
} else {
|
|
|
|
PORTD |= _BV(PULL);
|
|
|
|
radio.openWritingPipe(pipes[0]);
|
|
|
|
_delay_ms(400);
|
|
|
|
radio.openReadingPipe(1,pipes[1]);
|
|
|
|
PORTD &= ~_BV(PULL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t port_value = 2;
|
|
|
|
|
|
|
|
// port_value = PINC & (1 << 0);
|
|
|
|
|
|
|
|
uart_putchar((char)port_value);
|
|
|
|
|
|
|
|
// uart_putchar((char)'\n');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
radio.startListening();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long start_time = micros();
|
|
|
|
|
|
|
|
// ping
|
|
|
|
|
|
|
|
if (role == 1) {
|
|
|
|
|
|
|
|
radio.stopListening();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// uart_putstr("Now sending\n");
|
|
|
|
|
|
|
|
// digitalWrite(LED_BUILTIN, HIGH);
|
|
|
|
|
|
|
|
// _delay_ms(100);
|
|
|
|
|
|
|
|
// digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!radio.write( &start_time, sizeof(unsigned long))) {
|
|
|
|
|
|
|
|
// uart_putstr("failed\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
radio.startListening();
|
|
|
|
|
|
|
|
unsigned long started_waiting_at = micros();
|
|
|
|
|
|
|
|
boolean timeout = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (!radio.available()) {
|
|
|
|
|
|
|
|
if (micros() - started_waiting_at > 20000) {
|
|
|
|
|
|
|
|
timeout = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (timeout) {
|
|
|
|
|
|
|
|
// uart_putstr("Failed, reponse timed out\n");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
unsigned long got_time;
|
|
|
|
|
|
|
|
radio.read( & got_time, sizeof(unsigned long));
|
|
|
|
|
|
|
|
unsigned long end_time = micros();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// uart_putstr("Got response\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delay(500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ping bag
|
|
|
|
|
|
|
|
if (role == 0) {
|
|
|
|
|
|
|
|
unsigned long got_time;
|
|
|
|
|
|
|
|
if (radio.available()) {
|
|
|
|
|
|
|
|
while (radio.available()) {
|
|
|
|
|
|
|
|
radio.read( &got_time, sizeof(unsigned long));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
radio.stopListening();
|
|
|
|
|
|
|
|
radio.write( & got_time, sizeof(unsigned long));
|
|
|
|
|
|
|
|
radio.startListening();
|
|
|
|
|
|
|
|
// uart_putstr("Sent\n");
|
|
|
|
|
|
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
|
|
|
|
|
|
_delay_ms(100);
|
|
|
|
|
|
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|