⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maintutorial3remote.c

📁 介绍NRF24L01的多通道(Multiple Pipes),其中文件nrf24l01.c实现此射频芯片在多通道下的通信
💻 C
字号:
/*****************************************************************************
*
* File: maintutorial3remote.c
* 
* Copyright S. Brennen Ball, 2007
* 
* The author provides no guarantees, warantees, or promises, implied or
*	otherwise.  By using this software you agree to indemnify the author
* 	of any damages incurred by using it.
* 
*****************************************************************************/

//This code depends on a clock frequency of 40 MHz (I use a 10 MHz crystal and
//  the PLL.  If you are using a slower clock, then you must change the SPI and
//  UART rates, as well as the delay lengths in delays.c

#include <p18f452.h> 
#include <stdio.h> 
#include <usart.h> 
#include <spi.h> 
#include "delays.h" 
#include "nrf24l01.h" 

void Initialize(void); 
void InitializeIO(void); 

void ToggleLED(void); //toggle the current state of the on-board LED

//main routine
void 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 
       
      DelayUS(130); //wait for the other 24L01 to come from standby to RX 
       
      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 
   } 
} 

//initialize routine
void Initialize(void) 
{ 
   InitializeIO(); //set up IO (directions and functions)
   OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1 
   nrf24l01_initialize(nrf24l01_CONFIG_DEFAULT_VAL | nrf24l01_CONFIG_PWR_UP | nrf24l01_CONFIG_PRIM_RX, //1 byte CRC, powered up, RX
					   true,								//enable CE
					   nrf24l01_EN_AA_ENAA_NONE, 			//disable auto-ack on all pipes
					   nrf24l01_EN_RXADDR_ERX_ALL, 			//enable receive on all pipes
					   nrf24l01_SETUP_AW_DEFAULT_VAL, 		//5-byte addressing
					   nrf24l01_SETUP_RETR_DEFAULT_VAL, 	//not using auto-ack, so use default
					   nrf24l01_RF_CH_DEFAULT_VAL, 			//RF channel 3
					   nrf24l01_RF_SETUP_DEFAULT_VAL,  		//2 Mbps, 0 dBm
					   NULL, 								//default receive addresses on all 6 pipes
					   NULL, 								//""
					   nrf24l01_RX_ADDR_P2_DEFAULT_VAL, 	//""
					   nrf24l01_RX_ADDR_P3_DEFAULT_VAL, 	//""
					   nrf24l01_RX_ADDR_P4_DEFAULT_VAL, 	//""
					   nrf24l01_RX_ADDR_P5_DEFAULT_VAL, 	//""
					   NULL, 								//default TX address
					   1, 									//1 byte paylaod width on all 6 pipes
					   1, 									//""
					   1, 									//"" 
					   1,  									//""
					   1,  									//""
					   1); 									//""
} 

//initialize IO pins
void InitializeIO(void) 
{ 
   ADCON1 = 0x7; //disable AD converter functionality on PORTA
   TRISAbits.TRISA0 = 0; //make PORTA.0 an output to control LED
   PORTAbits.RA0 = 1; //turn on LED
    
   TRISBbits.TRISB0 = 1; //make sure that PORTB.0 is input since it is IRQ pin
    
   TRISC = 0xD1; //make CSN, CE, SCK, and MOSI (SDO) outputs
   PORTC = 0x04; //set CSN bit
}    

//toggles on-board LED
void ToggleLED(void) 
{ 
   PORTAbits.RA0 = ~PORTAbits.RA0; 
}    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -