|
|

楼主 |
发表于 2013-10-22 11:40:20
|
显示全部楼层
以下是接收程序:
#include "NRF24L01.h"
#define TX_ADR_WIDTH 5
#define TX_PLOAD_WIDTH 32
unsigned char TX_ADDRESS[TX_ADR_WIDTH] =
{
0x34,0x43,0x10,0x10,0x01
};
unsigned char rx_buf[TX_PLOAD_WIDTH];
unsigned char tx_buf[TX_PLOAD_WIDTH];
#define CE 3
#define CSN 2
#define SCK 5
#define MOSI 4
#define MISO 6
#define IRQ 7
//*********************************************
void setup()
{
pinMode(CE, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(CSN, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(IRQ, INPUT);
Serial.begin(9600);
NRF24L01_init();
//SPI_Read_Buf(RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH);//接收数据
}
void loop()
{
SetRX_Mode();
int i=SPI_Read(CD);//读取载波检测
if(i)//如果接收到载波,
{
// SPI_RW_Reg(FLUSH_RX,0);
Serial.println("get");
Serial.println(i);
delay(1000);
}
else
{
Serial.println("lost");
Serial.println(i);
delay(10);
}
}
void NRF24L01_init(void)
{
digitalWrite(CE,0);
digitalWrite(CSN,1);
digitalWrite(SCK,0);
digitalWrite(IRQ,1);
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
SPI_RW_Reg(WRITE_REG + EN_AA, 0x00); // Enable Auto.Ack ipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // 设置接收数据长度,本次设置为32字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
//SPI_Read_Buf(RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH);//接收数据
}
//**********************接收模式*****************************
void SetRX_Mode(void)
{
digitalWrite(CE,0);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);// IRQ收发完成中断响应,16位CRC,接收模式
digitalWrite(CE,1);
delay(1);
}
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
unsigned char revale=0;
unsigned char status=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况
if(status&RX_DR) // 判断是否接收到数据(RX_DR接收数据中断)
{
digitalWrite(CE,0); //SPI使能
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);//读取数据
revale =1; //读取数据完成标志
}
SPI_RW_Reg(WRITE_REG+STATUS,0xff); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
return revale;
}
/**************************************************
* Function: SPI_RW();
*
* Description:
* Writes one unsigned char to nRF24L01, and return the unsigned char read
* from nRF24L01 during write, according to SPI protocol
**************************************************/
unsigned char SPI_RW(unsigned char Byte)
{
unsigned char i;
for(i=0;i<8;i++) // output 8-bit
{
if(Byte&0x80)
{
digitalWrite(MOSI, 1);
}
else
{
digitalWrite(MOSI, 0);
}
digitalWrite(SCK, 1);
Byte <<= 1; // shift next bit into MSB..
if(digitalRead(MISO) == 1)
{
Byte |= 1; // capture current MISO bit
}
digitalWrite(SCK, 0);
}
return(Byte); // return read unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_RW_Reg();
*
* Description:
* Writes value 'value' to register 'reg'
/**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned char status;
digitalWrite(CSN, 0); // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
digitalWrite(CSN, 1); // CSN high again
return(status); // return nRF24L01 status unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_Read();
*
* Description:
* Read one unsigned char from nRF24L01 register, 'reg'
/**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
unsigned char reg_val;
digitalWrite(CSN, 0); // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read register value
digitalWrite(CSN, 1); // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/**************************************************/
/**************************************************
* Function: SPI_Read_Buf();
*
* Description:
* Reads 'unsigned chars' #of unsigned chars from register 'reg'
* Typically used to read RX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
unsigned char status,i;
digitalWrite(CSN, 0); // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status unsigned char
for(i=0;i<bytes;i++)
{
pBuf[i] = SPI_RW(0); // Perform SPI_RW to read unsigned char from nRF24L01
}
digitalWrite(CSN, 1); // Set CSN high again
return(status); // return nRF24L01 status unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_Write_Buf();
*
* Description:
* Writes contents of buffer '*pBuf' to nRF24L01
* Typically used to write TX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
unsigned char status,i;
digitalWrite(CSN, 0); // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status unsigned char
for(i=0;i<bytes; i++) // then write all unsigned char in buffer(*pBuf)
{
SPI_RW(*pBuf++);
}
digitalWrite(CSN, 1); // Set CSN high again
return(status); // return nRF24L01 status unsigned char
}
|
|