You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.5 KiB
137 lines
4.5 KiB
//mega 48 на погодной станции
|
|
#include <nrf24l01p.h>
|
|
#include <SPI.h>
|
|
|
|
enum
|
|
{
|
|
SPI_SS_PIN = 10,
|
|
NRF_CE_PIN = 8,//9
|
|
|
|
NRF_CHANNEL = 5,
|
|
NRF_POWER_UP_DELAY = 2,
|
|
NRF_PAYLOAD_LENGTH = 16
|
|
};
|
|
boolean mode;
|
|
char payload1[NRF_PAYLOAD_LENGTH];
|
|
char test_data[17] ;
|
|
void setup()
|
|
{
|
|
SPI.begin();
|
|
|
|
pinMode(SPI_SS_PIN, OUTPUT);
|
|
digitalWrite(SPI_SS_PIN, HIGH);
|
|
|
|
pinMode(NRF_CE_PIN, OUTPUT);
|
|
digitalWrite(NRF_CE_PIN, HIGH);
|
|
|
|
uint8_t address[5] = { 0xE2, 0xE4, 0x23, 0xE4, 0x02 };
|
|
nrf_initp(address);
|
|
pinMode(A5, OUTPUT);
|
|
pinMode(9, INPUT);
|
|
digitalWrite(9, HIGH);
|
|
Serial.begin(9600);
|
|
Serial.println("Init");
|
|
}
|
|
//*****************************************************************************
|
|
void loop()
|
|
{digitalWrite(A5, LOW);
|
|
//Serial.println("Waiting for a packet...");
|
|
mode = digitalRead(9);//ожидаем приема с эфира для передачи по сериалу
|
|
if (mode==LOW)
|
|
{
|
|
do {} while (!(nrf24l01p_get_irq_flags() & (1 << NRF24L01P_IRQ_RX_DR)));
|
|
nrf24l01p_clear_irq_flag(NRF24L01P_IRQ_RX_DR);
|
|
|
|
static char payload[NRF_PAYLOAD_LENGTH];
|
|
nrf24l01p_read_rx_payload((uint8_t*)payload);//, sizeof(payload));
|
|
digitalWrite(A5, HIGH);
|
|
// Serial.print(">>");//передачи по сериалу
|
|
Serial.println(payload);
|
|
delay (200);
|
|
}
|
|
|
|
|
|
//*******************************
|
|
//***********************Передача в эфир при поступлении данных через сериал*************************************
|
|
if (Serial.available() > 0) {
|
|
for (int i=0;i<16;i++)
|
|
{// считываем входящий байт:1d24.8h50.0p7630 2u+07.10h63.8s92
|
|
test_data[i]= Serial.read();
|
|
}
|
|
// показываем, что именно мы получили:
|
|
// Serial.print("I received: ");
|
|
// Serial.println(test_data);
|
|
// delay(5000);
|
|
while(Serial.available()){Serial.read();}
|
|
digitalWrite(A5, HIGH);
|
|
delay (100);
|
|
digitalWrite(A5, LOW);
|
|
delay (100);
|
|
digitalWrite(NRF_CE_PIN, LOW);
|
|
uint8_t address[5] = { 0xE2, 0xE4, 0x23, 0xE4, 0x02 };
|
|
nrf_initr(address);
|
|
static uint8_t payload[NRF_PAYLOAD_LENGTH];
|
|
strcpy((char*)payload, test_data);
|
|
nrf24l01p_write_tx_payload(payload, sizeof(payload));
|
|
|
|
digitalWrite(NRF_CE_PIN, HIGH);
|
|
delay(1);
|
|
digitalWrite(NRF_CE_PIN, LOW);
|
|
|
|
do {} while (!(nrf24l01p_get_irq_flags() & (1 << NRF24L01P_IRQ_TX_DS)));
|
|
nrf24l01p_clear_irq_flag(NRF24L01P_IRQ_TX_DS);
|
|
Serial.println(" done");
|
|
|
|
nrf_initp(address);
|
|
digitalWrite(NRF_CE_PIN, HIGH);
|
|
digitalWrite(A5, HIGH);
|
|
}
|
|
delay (300);
|
|
//********************************************
|
|
}
|
|
//******************************************************************
|
|
void nrf_initp(uint8_t *address)//priem
|
|
{
|
|
delay(100);
|
|
|
|
nrf24l01p_get_clear_irq_flags();
|
|
nrf24l01p_close_pipe(NRF24L01P_ALL);
|
|
nrf24l01p_open_pipe(NRF24L01P_PIPE0, false);
|
|
nrf24l01p_set_crc_mode(NRF24L01P_CRC_16BIT);
|
|
nrf24l01p_set_address_width(NRF24L01P_AW_5BYTES);
|
|
nrf24l01p_set_address(NRF24L01P_PIPE0, address);
|
|
nrf24l01p_set_operation_mode(NRF24L01P_PRX);
|
|
nrf24l01p_set_rx_payload_width(NRF24L01P_PIPE0, NRF_PAYLOAD_LENGTH);
|
|
nrf24l01p_set_rf_channel(100);
|
|
|
|
nrf24l01p_set_power_mode(NRF24L01P_PWR_UP);
|
|
delay(NRF_POWER_UP_DELAY);
|
|
}
|
|
|
|
void nrf24l01p_spi_ss(nrf24l01p_spi_ss_level_t level)
|
|
{
|
|
digitalWrite(SPI_SS_PIN, (level == NRF24L01P_SPI_SS_LOW ? LOW : HIGH));
|
|
}
|
|
|
|
uint8_t nrf24l01p_spi_rw(uint8_t value)
|
|
{
|
|
return SPI.transfer(value);
|
|
}
|
|
|
|
void nrf_initr(uint8_t *address)//peredat
|
|
{
|
|
delay(200);
|
|
|
|
nrf24l01p_get_clear_irq_flags();
|
|
nrf24l01p_close_pipe(NRF24L01P_ALL);
|
|
nrf24l01p_open_pipe(NRF24L01P_TX, false);
|
|
nrf24l01p_set_rx_payload_width(NRF24L01P_PIPE0, NRF_PAYLOAD_LENGTH);
|
|
nrf24l01p_set_crc_mode(NRF24L01P_CRC_16BIT);
|
|
nrf24l01p_set_address_width(NRF24L01P_AW_5BYTES);
|
|
nrf24l01p_set_address(NRF24L01P_TX, address);
|
|
nrf24l01p_set_operation_mode(NRF24L01P_PTX);
|
|
nrf24l01p_set_rf_channel(5);
|
|
|
|
nrf24l01p_set_power_mode(NRF24L01P_PWR_UP);
|
|
delay(NRF_POWER_UP_DELAY);
|
|
}
|
|
|