//main routine int main(void) { unsigned char data; //register to hold letter received and sent Initialize(); //initialize PLL, IO, SPI, set up nRF24L01 as RX //main program loop while(1) { //wait until a packet has been received while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active())); nrf24l01_read_rx_payload(&data, 1); //read the packet into data nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01 PORTC = data; // DelayUS(130); //wait for the other 24L01 to come from standby to RX _delay_us(35); _delay_us(35); _delay_us(35); _delay_us(25); nrf24l01_set_as_tx(); //change the device to a TX to send back from the other 24L01 nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF //wait until the packet has been sent while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_tx_ds_active())); nrf24l01_irq_clear_all(); //clear interrupts again nrf24l01_set_as_rx(true); //resume normal operation as an RX // ToggleLED(); //toggle the on-board LED as visual indication that the loop has completed } }
void MainLoop() { width = 1; char data[width]; //register to hold letter sent and received int i = 0; while (1) { PORTD; while (!(nrf24l01_irq_pin_active())); while (!nrf24l01_irq_rx_dr_active()); LED2 = ~LED2; nrf24l01_read_rx_payload(data, width); //get the payload into data nrf24l01_irq_clear_all(); //clear interrupts again for (i = 0; i < width; i++) { sprintf(RS232_Out_Buffer, "%c", data[i]); putsUART1(RS232_Out_Buffer); } putsUART1("\r\n"); Delayus(130); //wait for receiver to come from standby to RX LED3 = ~LED3; } }
/******** Transceiver_Receive ********************************************** // Hardware trigger on IRQ occurred, change the state for transceiver state // machine in order to read message into microcontroller // Input: none // Output: none // ------------------------------------------------------------------------*/ void Transceiver_Receive ( void ) { portBASE_TYPE xHigherPriorityTaskWoken; unsigned char rx[TRANSCEIVER_MAX_PAYLOAD]; if ( !( nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active() ) ) { // ERROR: we got interupt but the interupt is not packet received Debug_Printf ( "Transceiver_Receive: Error 0x%x\n", nrf24l01_get_status()&nrf24l01_STATUS_INTERRUPT ); goto exit; } //get the payload into data nrf24l01_read_rx_payload ( rx, TRANSCEIVER_MAX_PAYLOAD ); Debug_NetworkTransceiver_PrintPayload(rx); if ( pdTRUE != xQueueSendFromISR(Transceiver_RX_Queue, rx, &xHigherPriorityTaskWoken) ) { // ERROR: Queue is full Debug_Printf ("Transceiver_Receive: Error 0x%x\n", ERROR_QUEUE_FULL); if ( FullQueueCallBack != NULL ) xTaskCreate( Transceiver_FullQueueCallBack, ( signed portCHAR * ) "Transceiver_FullQueueCallBack", DEFAULT_STACK_SIZE, NULL, DEFAULT_PRIORITY, NULL ); goto exit; } exit: nrf24l01_irq_clear_all(); Delay_US(130); }
//main routine int main() { unsigned char data; //register to hold letter sent and received unsigned int count; //counter for for loop Initialize(); //initialize PLL, IO, UART, SPI, set up nRF24L01 as TX stdout = &my_stream; stdin = &my_stream; printf_P(PSTR(" type character to send \n")); //main program loop while(1) { //check UART status register to see if data has been received. if so, process while(usart0_rx_data_ready()) { // data = usart0_get_rx_data(); //get data from UART data = getchar(); //get data from UART PORTC = data; nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF //wait until the packet has been sent or the maximum number of retries has been active while(!(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active()))); //check to see if the maximum number of retries has been hit. if not, wait for the RX device // to send the char back. if so, assume the packet is lost and send "*" back to UART if(!nrf24l01_irq_max_rt_active()) { nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01 nrf24l01_set_as_rx(true); //change the device to an RX to get the character back from the other 24L01 //wait a while to see if we get the data back (change the loop maximum and the lower if // argument (should be loop maximum - 1) to lengthen or shorten this time frame for(count = 0; count < 25000; count++) { //check to see if the data has been received. if so, get the data and exit the loop. // if the loop is at its last count, assume the packet has been lost and set the data // to go to the UART to "?". If neither of these is true, keep looping. if((nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active())) { nrf24l01_read_rx_payload(&data, 1); //get the payload into data break; } //if loop is on its last iteration, assume packet has been lost. if(count == 24999) data = '?'; } nrf24l01_irq_clear_all(); //clear interrupts again printf("%c", data); //print the received data (or ? if none) to the screen // DelayUS(130); //wait for receiver to come from standby to RX _delay_us(35); _delay_us(35); _delay_us(35); _delay_us(25); nrf24l01_set_as_tx(); //resume normal operation as a TX } else { nrf24l01_flush_tx(); //get the unsent character out of the TX FIFO nrf24l01_irq_clear_all(); //clear all interrupts printf("*"); //print "*" to the screen to show that the receiver did not receive the packet } // ToggleLED(); //toggle the on-board LED as visual indication that the loop has completed } } }