Beispiel #1
0
void EXTI9_5_IRQHandler(void)
{
	uint8_t state = 0;
	if(EXTI_GetITStatus(EXTI_Line5) != RESET){
		EXTI_ClearITPendingBit(EXTI_Line5);
		switch(NRF24L01_Get_State(&state)){
			case TX_OK:
				NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,state); //清除TX_DS或MAX_RT中断标志
				NRF24L01_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器 
				nrf_state = TX_OK;
				break;
			case MAX_TX:
				NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,state); //清除TX_DS或MAX_RT中断标志
				NRF24L01_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器 
				nrf_state = MAX_TX;
				break;
			case RX_OK:
				NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS, state);				//清除TX_DS或MAX_RT中断标志
				NRF24L01_Read_Buf(RD_RX_PLOAD, nrf_rx_buffer, RX_PLOAD_WIDTH);	//读取数据
				NRF24L01_Write_Reg(FLUSH_TX, 0xff);								//清除TX FIFO寄存器 
				nrf_state = RX_OK;
				break;
		}
	}
}
Beispiel #2
0
//检测24L01是否存在
//返回值:0,成功;1,失败	
u8 NRF24L01_Check(void)
{
	u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
	u8 i;
	SPI2_SetSpeed(SPI_BaudRatePrescaler_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)   	 
	NRF24L01_Write_Buf(WRITE_REG_NRF+TX_ADDR,buf,5);//写入5个字节的地址.	
	NRF24L01_Read_Buf(TX_ADDR,buf,5); //读出写入的地址  
	for(i=0;i<5;i++)if(buf[i]!=0XA5)break;	 							   
	if(i!=5)return 1;//检测24L01错误	
	return 0;		 //检测到24L01
}	 	 
Beispiel #3
0
//上电检测NRF24L01是否在位
//写5个数据然后再读回来进行比较,
//相同时返回值:0,表示在位;否则返回1,表示不在位	
u8 NRF24L01_Check(void)
{
	u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
	u8 buf1[5];
	u8 i;   	 
	NRF24L01_Write_Buf(SPI_WRITE_REG+TX_ADDR,buf,5);//写入5个字节的地址.	
	NRF24L01_Read_Buf(TX_ADDR,buf1,5);              //读出写入的地址  	
	for(i=0;i<5;i++)if(buf1[i]!=0XA5)break;
	if(i!=5)return 1;                               //NRF24L01不在位	
	return 0;		                                //NRF24L01在位
}	 	 
/*******************************************************************************
* Function Name  : NRF24L01_Check
* Description    : 上电检测NRF24L01是否在位
* Input          : None
* Output         : None
* Return         : RT_EOK:表示在位;RT_ERROR,表示不在位
*******************************************************************************/
static rt_err_t NRF24L01_Check(void)
{
  rt_uint8_t buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
  rt_uint8_t buf1[5];
  rt_uint8_t i;   

  NRF24L01_Write_Buf(SPI_WRITE_REG+TX_ADDR,buf,5);//写入5个字节的地址.	
  NRF24L01_Read_Buf(TX_ADDR,buf1,5);              //读出写入的地址  	
  for(i=0;i<5;i++)if(buf1[i]!=0XA5)break;					   
  if(i!=5)return RT_ERROR;                               //NRF24L01不在位	
  return RT_EOK;		                                //NRF24L01在位
}	 	 
Beispiel #5
0
u8 NRF24L01_Check(void)
{
	u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
	u8 i;
//	SPIx_SetSpeed(SPI_SPEED_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)    
	NRF24L01_Write_Buf(WRITE_REG+TX_ADDR,buf,5);//写入5个字节的地址.
	NRF24L01_Read_Buf(TX_ADDR,buf,5); //读出写入的地址  
	for(i=0;i<5;i++)if(buf[i]!=0XA5)break;
	printf("i=%d\n",i);  
	if(i!=5)return 1;//检测24L01错误
	return 0; //检测到24L01
}  
Beispiel #6
0
// Receive data packet
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
	u8 sta;		    							   
	sta=NRF24L01_Read_Reg(NRF_STATUS);
	NRF24L01_Write_Reg(NRF_STATUS,sta);
	if(sta & NRF_RX_OK )
	{
		NRF24L01_Read_Buf(rxbuf,NRF_PLOAD_WIDTH);
		NRF24L01_Flush_RX();
		return NRF_RX_OK;
	}	   
	return 0x00;
}
/*******************************************************************************
* Function Name  : NRF24L01_RxPacket
* Description    : 启动NRF24L01接收一次数据
* Input          : rxbuf:接收数据首地址
* Output         : None
* Return         :  接收完成状况
*******************************************************************************/
static rt_err_t NRF24L01_RxPacket(rt_uint8_t *rxbuf)
{
  rt_uint8_t state;		    							      
  state=NRF24L01_Read_Reg(STATUS);                //读取状态寄存器的值    	 
  NRF24L01_Write_Reg(SPI_WRITE_REG+STATUS,state); //清除TX_DS或MAX_RT中断标志
  if(state&RX_OK)                                 //接收到数据
  {
          NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
          NRF24L01_Write_Reg(FLUSH_RX,0xff);          //清除RX FIFO寄存器 
          return RT_EOK; 
  }	   
  return RT_ERROR;                                      //没收到任何数据
}
Beispiel #8
0
//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
//返回值:0,接收完成;其他,错误代码
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
	u8 state;		    							      
	state=NRF24L01_Read_Reg(STATUS);                //读取状态寄存器的值    	 
	NRF24L01_Write_Reg(SPI_WRITE_REG+STATUS,state); //清除TX_DS或MAX_RT中断标志
	if(state&RX_OK)                                 //接收到数据
	{
		NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
		NRF24L01_Write_Reg(FLUSH_RX,0xff);          //清除RX FIFO寄存器 
		return 0; 
	}	   
	return 1;                                      //没收到任何数据
}
Beispiel #9
0
//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
//返回值:0,接收完成;其他,错误代码
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
	u8 sta;		    							   
	SPI2_SetSpeed(SPI_BaudRatePrescaler_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)   
	sta=NRF24L01_Read_Reg(STATUS);  //读取状态寄存器的值    	 
	NRF24L01_Write_Reg(WRITE_REG_NRF+STATUS,sta); //清除TX_DS或MAX_RT中断标志
	if(sta&RX_OK)//接收到数据
	{
		NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
		NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器 
		return 0; 
	}	   
	return 1;//没收到任何数据
}					    
Beispiel #10
0
//检测24L01是否存在
//返回值:0,成功;1,失败	
uint8_t NRF24L01_Check(void)
{
	uint8_t buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
	uint8_t i;
	spi_set_speed(SPI_BaudRatePrescaler_8);	 			//spi速度为10.5Mhz(24L01的最大SPI时钟为10Mhz)   	 
	NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);	//写入5个字节的地址.	
	NRF24L01_Read_Buf(TX_ADDR,buf,5); 					//读出写入的地址  
	for(i=0;i<5;i++){
		if(buf[i]!=0XA5){
			return 1;//检测24L01错误	
		}
	}		
	return 0;		 //检测到24L01
}
Beispiel #11
0
unsigned char NRF24L01_RxPacket(unsigned char *rxbuf)
{
	unsigned char sta;		    							   
	//SPIx_SetSpeed(SPI_SPEED_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)   
	sta=nrf_read(STATUS);  //读取状态寄存器的值    	 
	nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志
	if(sta&0x40)//接收到数据
	{
		NRF24L01_Read_Buf(0x61,rxbuf,TX_DATA_WITDH);//读取数据
		nrf_write(FLUSH_RX,0xff);//清除RX FIFO寄存器 
		return 0; 
	}	   
	return 1;//没收到任何数据
}
Beispiel #12
0
////////////////////
///
///注意:执行完后会进入待机模式,如需进行下一次接收,必须使能CE,或者重新使用NRF24L01_RX_Mode();进入接收模式
//////////////////////
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
	u8 sta;
	NRF24L01_CE_L();	//进入待机模式
	SPI2_SetSpeed(SPI_SPEED_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)   
	sta=NRF24L01_Read_Reg(NRF24L01_STATUS);  //读取状态寄存器的值      
	NRF24L01_Write_Reg(NRF24L01_WRITE_REG+NRF24L01_STATUS,sta); //清除TX_DS或MAX_RT中断标志	
	if(sta&NRF24L01_RX_OK)//接收到数据
	{
		NRF24L01_Read_Buf(NRF24L01_RD_RX_PLOAD,rxbuf,NRF24L01_RX_PLOAD_WIDTH);//读取数据
		NRF24L01_Write_Reg(NRF24L01_FLUSH_RX,0xff);//清除RX FIFO寄存器 
		return NRF24L01_RX_OK; 
	}
	
	return 0;//没收到任何数据
}    
Beispiel #13
0
u8 NRF24L01_Check(void)
{
	u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
	u8 i;
	SPI2_SetSpeed(SPI_SPEED_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)    
	NRF24L01_Write_Buf(NRF24L01_WRITE_REG+NRF24L01_TX_ADDR,buf,5);//写入5个字节的地址.
	NRF24L01_Read_Buf(NRF24L01_TX_ADDR,buf,5); //读出写入的地址  
	for(i=0;i<5;i++)
	{
		if(buf[i]!=0XA5)
			break; 
	}		
	if(i==5)
		return 1;//检测到24L01
	return 0; //检测24L01错误
}  
Beispiel #14
0
void NRF24L01_Check_Event(void)
{
	u8 sta;
	sta = NRF24L01_Read_Reg(NRF_READ_REG + STATUS);  //读取状态寄存器的值 
	//接收到数据
	if(sta & RX_OK){
		u8 rx_len = NRF24L01_Read_Reg(R_RX_PL_WID);
		if(rx_len<33)
		{
			NRF24L01_Read_Buf(RD_RX_PLOAD,NRF24L01_RXDATA,rx_len);// read receive payload from RX_FIFO buffer
			RemoteData_Handle(NRF24L01_RXDATA,rx_len);
		}
		else 
		{
			NRF24L01_Write_Reg(FLUSH_RX,0xff);//清空缓冲区
		}
	}
	NRF24L01_Write_Reg(NRF_WRITE_REG + STATUS, sta);
}
void rf1read(void)
{ 
  rt_uint8_t data;
  rt_uint8_t buf[32];

  NRF24L01_Read_Buf( CONFIG      , &data , 1);       
  rt_kprintf("\r\n CONFIG = 0x%02x", data);
  NRF24L01_Read_Buf( EN_AA       , &data , 1);       
  rt_kprintf("\r\n EN_AA = 0x%02x", data);
  NRF24L01_Read_Buf( EN_RXADDR   , &data , 1);      
  rt_kprintf("\r\n EN_RXADDR = 0x%02x", data);
  NRF24L01_Read_Buf( SETUP_AW    , &data , 1);       
  rt_kprintf("\r\n SETUP_AW = 0x%02x", data);
  NRF24L01_Read_Buf( SETUP_RETR  , &data , 1);      
  rt_kprintf("\r\n SETUP_RETR = 0x%02x",data);
  NRF24L01_Read_Buf( RF_CH       , &data , 1);       
  rt_kprintf("\r\n RF_CH = 0x%02x",data);
  NRF24L01_Read_Buf( RF_SETUP    , &data , 1);      
  rt_kprintf("\r\n RF_SETUP = 0x%02x",data);
  NRF24L01_Read_Buf( STATUS      , &data , 1);       
  rt_kprintf("\r\n STATUS = 0x%02x",data);
  NRF24L01_Read_Buf( OBSERVE_TX  , &data , 1);       
  rt_kprintf("\r\n OBSERVE_TX = 0x%02x",data);
  NRF24L01_Read_Buf( CD          , &data , 1);      
  rt_kprintf("\r\n CD = 0x%02x",data);
  NRF24L01_Read_Buf( RX_ADDR_P0  , buf , 5);       
  rt_kprintf("\r\n RX_ADDR_P0 = 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", buf[0],buf[1],buf[2],buf[3],buf[4]);
  NRF24L01_Read_Buf( RX_ADDR_P1  , buf , 5);      
  rt_kprintf("\r\n RX_ADDR_P1 = 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", buf[0],buf[1],buf[2],buf[3],buf[4]);
  NRF24L01_Read_Buf( RX_ADDR_P2  , &data , 1);       
  rt_kprintf("\r\n RX_ADDR_P2 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_ADDR_P3  , &data , 1);      
  rt_kprintf("\r\n RX_ADDR_P3 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_ADDR_P4  , &data , 1);       
  rt_kprintf("\r\n RX_ADDR_P4 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_ADDR_P5  , &data , 1);      
  rt_kprintf("\r\n RX_ADDR_P5 = 0x%02x",data);
  NRF24L01_Read_Buf( TX_ADDR     , buf , 5);       
  rt_kprintf("\r\n TX_ADDR = 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", buf[0],buf[1],buf[2],buf[3],buf[4]);
  NRF24L01_Read_Buf( RX_PW_P0    , &data , 1);      
  rt_kprintf("\r\n RX_PW_P0 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_PW_P1    , &data , 1);       
  rt_kprintf("\r\n RX_PW_P1 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_PW_P2    , &data , 1);      
  rt_kprintf("\r\n RX_PW_P2 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_PW_P3    , &data , 1);       
  rt_kprintf("\r\n RX_PW_P3 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_PW_P4    , &data , 1);      
  rt_kprintf("\r\n RX_PW_P4 = 0x%02x",data);
  NRF24L01_Read_Buf( RX_PW_P5    , &data , 1);       
  rt_kprintf("\r\n RX_PW_P5 = 0x%02x",data);
  NRF24L01_Read_Buf( FIFO_STATUS , &data , 1);      
  rt_kprintf("\r\n FIFO_STATUS = 0x%02x",data);

}