#include
//
//只需要把自己单片机的I/O 口接上对应
sbit MISO = P1^3;
sbit MOSI = P1^4;
sbit CK = P1^5;
sbit CE = P1^6;
sbit CSN = P3^7;
sbit IRQ = P1^2;
sbit LED2 = P3^5;
sbit LED1 = P3^4;
sbit KEY1 = P3^0;
sbit KEY2 = P3^1;
//SPI(nRF24L01)commands
#define EAD_REG 0x00 //Definereadcommandtoregister
#define ITE_REG 0x20 //Definewritecommandtoregister
#define D_RX_PLOAD 0x61 //DefineRXpayloadregisteraddress
#define LOAD 0xA0 //DefineTXpayloadregisteraddress
#define FLUSH_TX 0xE1 //DefineflushTXregistercommand
#define FLUSH_RX 0xE2 //DefineflushRXregistercommand
#define EUSE_TX_PL 0xE3 //DefinereuseTXpayloadregistercommand
#define Nop0xFF //DefineNoOperation,mightbeusedtoreadstatusregister
//***************************************************//
//SPI(nRF24L01)registers(addresses)
#define CONFIG 0x00 //'Config' registeraddress
#define EN_AA 0x01 //'Enable AutoAcknowledgment'registeraddress
#define EN_RXADDR 0x02 //'Enabled RXaddresses'registeraddress
#define ETUP_AW 0x03 //'Setup addresswidth'registeraddress
#define ETUP_RETR 0x04 //'Setup Auto.Retrans'registeraddress
#define F_CH 0x05 //'RF channel'registeraddress
#define F_SETUP 0x06 //'RF setup'registeraddress
#define ATUS 0x07 //'Status' registeraddress
#define BSERVE_TX 0x08 //'Observe TX'registeraddress
#define CD 0x09 //'Carrier Detect'registeraddress
#define ADDR_P0 0x0A //'RX addresspipe0'registeraddress
#define ADDR_P1 0x0B //'RX addresspipe1'registeraddress
#define ADDR_P2 0x0C //'RX addresspipe2'registeraddress
#define ADDR_P3 0x0D //'RX addresspipe3'registeraddress
#define ADDR_P4 0x0E //'RX addresspipe4'registeraddress
#define ADDR_P5 0x0F //'RX addresspipe5'registeraddress
#define ADDR 0x10 //'TX address'registeraddress
#define 0x11 //'RX payloadwidth,pipe0'registeraddress
#define 1 0x12 //'RX payloadwidth,pipe1'registeraddress
#define 2 0x13 //'RX payloadwidth,pipe2'registeraddress
#define 3 0x14 //'RX payloadwidth,pipe3'registeraddress
#define 4 0x15 //'RX payloadwidth,pipe4'registeraddress
#define 5 0x16 //'RX payloadwidth,pipe5'registeraddress
#define FIFO_STATUS 0x17 //'FIFO StatusRegister'registeraddress
//------------------------------------------------------------// 写一个字节到24L01,同时读出一个字节
uchar SPI_RW(ucharbyte)
{
uchar ;
for (bit_ctr=0;bit_ctr<8;bit_ctr ) //output8-bit
{
MOSI = (byte&0x80);//output'byte', MSBtoMOSI
byte = (byte<<1);//shiftnextbitintoMSB..
SCK = 1;//SetSCKhigh..
byte |= MISO;//capturecurrentMISObit
SCK = 0;//..then setSCKlowagain
}
return(byte);//returnreadbyte
}
// 向寄存器reg写一个字节,同时返回状态字节
uchar SPI_RW_Reg(BYTEreg,BYTEvalue)
{
uchar status;
CSN = 0;//CSNlow, initSPItransaction
status = SPI_RW(reg);//selectregister
SPI_RW(value);//..and writevaluetoit..
CSN = 1;//CSNhighagain
return(status);//returnnRF24L01statusbyte
}
// 读出bytes字节的数据
uchar SPI_Read_Buf(BYTEreg,BYTE*pBuf,BYTEbytes)
{
uchar status,byte_ctr;
CSN = 0;//SetCSNlow,initSPItranaction
status = SPI_RW(reg);//Selectregistertowritetoandreadstatusbyte
for (byte_ctr=0;byte_ctr pBuf[byte_ctr] = SPI_RW(0);//
CSN = 1;
return(status);//returnnRF24L01statusbyte
}
// 写入bytes字节的数据
uchar SPI_Write_Buf(BYTEreg,BYTE*pBuf,BYTEbytes)
{
uchar status,byte_ctr;
CSN = 0;
status = SPI_RW(reg);
for (byte_ctr=0;byte_ctr SPI_RW(*pBuf );
CSN = 1;//SetCSNhighagain
return(status);//
}
// 接收函数,返回1表示有数据收到,否则没有数据接受到
unsigned char nRF24L01_RxPacket(unsignedchar*rx_buf)
{
unsigned char revale=0;
//setinRXmode
SPI_RW_Reg(WRITE_REG CONFIG,0x0f);//SetPWR_UPbit,enableCRC(2bytes)&
Prim:RX.RX_DRenabled..
CE = 1;//SetCEpinhightoenableRXdevice
dalay130us();
sta = SPI_Read(STATUS);//readregisterSTATUS'svalue
if (RX_DR)//ifreceivedataready(RX_DR)interrupt
{
CE = 0;//standbymode
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);//readreceivepayloadfrom
RX_FIFObuffer
revale = 1;
}
SPI_RW_Reg(WRITE_REG STATUS,sta);//clearRX_DRorTX_DSorMAX_RTinterrupt flag
return revale;
}
// 发送函数
voidn RF24L01_TxPacket(unsignedchar*tx_buf)
{
CE = 0;
//SPI_Write_Buf(WRITE_REG TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);//Writes
TX_AddresstonRF24L01
//SPI_Write_Buf(WRITE_REG RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);//
RX_Addr0sameasTX_AdrforAuto.Ack
SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);//WritesdatatoTXpayload
SPI_RW_Reg(WRITE_REG CONFIG,0x0e);//SetPWR_UPbit,enableCRC(2bytes)&
Prim:TX.MAX_RT&TX_DSenabled..
CE = 1;
dalay10us();
CE = 0;
}
// 配置函数
void nRF24L01_Config(void)
{
//initialio
CE = 0;//chipenable
CSN = 1;//Spidisable
SCK = 0;//Spiclockline inithigh
CE = 0;
SPI_RW_Reg(WRITE_REG CONFIG,0x0f);//SetPWR_UPbit,enableCRC(2bytes)&
Prim:RX.RX_DRenabled..
SPI_RW_Reg(WRITE_REG EN_AA,0x01);
SPI_RW_Reg(WRITE_REG EN_RXADDR,0x01);//EnablePipe0
SPI_RW_Reg(WRITE_REG SETUP_AW,0x02);//Setupaddresswidth=5bytes
SPI_RW_Reg(WRITE_REG SETUP_RETR,0x1a);//500us 86us,10retrans...
SPI_RW_Reg(WRITE_REG RF_CH,0);
SPI_RW_Reg(WRITE_REG RF_SETUP,0x07);//TX_PWR:0dBm,Datarate:1Mbps,
LNA:HCURR
SPI_RW_Reg(WRITE_REG RX_PW_P0,RX_PLOAD_WIDTH);
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);CE=1;//
}
//////////////////第三维编辑/////////////////////////////////////////////////////