您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 能源与动力工程 > nrf2401通信协议
NRF24L01通信协议#ifndef_RF_H_#define_RF_H_//****************************************************************////defineport#definePORT_DIR_CSNDDRB#definePORT_DIR_SCKDDRB#definePORT_DIR_MOSIDDRB#definePORT_DIR_CEDDRD#definePORT_DIR_MISODDRB#definePORT_DIR_IRQDDRD#definePORT_OUT_CSNPORTB#definePORT_OUT_SCKPORTB#definePORT_OUT_MOSIPORTB#definePORT_OUT_CEPORTD#definePORT_OUT_MISOPORTB#definePORT_OUT_IRQPORTD#definePORT_IN_MISOPINB#definePORT_IN_IRQPIND#defineBIT_nRF24L01_CSNBIT2#defineBIT_nRF24L01_SCKBIT7#defineBIT_nRF24L01_MOSIBIT5#defineBIT_nRF24L01_CEBIT4#defineBIT_nRF24L01_MISOBIT6#defineBIT_nRF24L01_IRQBIT2#defineRF_CSN_IO_OUTSETBIT(PORT_DIR_CSN,BIT_nRF24L01_CSN)#defineRF_SCK_IO_OUTSETBIT(PORT_DIR_SCK,BIT_nRF24L01_SCK)#defineRF_MOSI_IO_OUTSETBIT(PORT_DIR_MOSI,BIT_nRF24L01_MOSI)#defineRF_CE_IO_OUTSETBIT(PORT_DIR_CE,BIT_nRF24L01_CE)#defineRF_MISO_IO_INCLEARBIT(PORT_DIR_MISO,BIT_nRF24L01_MISO)#defineRF_IRQ_IO_INCLEARBIT(PORT_DIR_IRQ,BIT_nRF24L01_IRQ)#definenRF24L01_CSNHSETBIT(PORT_OUT_CSN,BIT_nRF24L01_CSN)#definenRF24L01_CSNLCLEARBIT(PORT_OUT_CSN,BIT_nRF24L01_CSN)#definenRF24L01_SCKHSETBIT(PORT_OUT_SCK,BIT_nRF24L01_SCK)#definenRF24L01_SCKLCLEARBIT(PORT_OUT_SCK,BIT_nRF24L01_SCK)#definenRF24L01_MOSIHSETBIT(PORT_OUT_MOSI,BIT_nRF24L01_MOSI)#definenRF24L01_MOSILCLEARBIT(PORT_OUT_MOSI,BIT_nRF24L01_MOSI)#definenRF24L01_CEHSETBIT(PORT_OUT_CE,BIT_nRF24L01_CE)#definenRF24L01_CELCLEARBIT(PORT_OUT_CE,BIT_nRF24L01_CE)#definenRF24L01_MISOHSETBIT(PORT_OUT_MISO,BIT_nRF24L01_MISO)#definenRF24L01_MISOLCLEARBIT(PORT_OUT_MISO,BIT_nRF24L01_MISO)#definenRF24L01_IRQHSETBIT(PORT_OUT_IRQ,BIT_nRF24L01_IRQ)#definenRF24L01_IRQLCLEARBIT(PORT_OUT_IRQ,BIT_nRF24L01_IRQ)#definenRF24L01_MISOCHECKBIT(PORT_IN_MISO,BIT_nRF24L01_MISO)#definenRF24L01_IRQCHECKBIT(PORT_IN_IRQ,BIT_nRF24L01_IRQ)//****************************************************************////dataandaddwidth#defineTX_ADR_WIDTH5//5bytesTX(RX)addresswidth#defineTX_PLOAD_WIDTH20//16bytesTXpayload#defineRX_ADR_WIDTH5//5bytesTX(RX)addresswidth#defineRX_PLOAD_WIDTH20//16bytesTXpayloadunsignedchartxBuf[TX_PLOAD_WIDTH];unsignedcharrxBuf[RX_PLOAD_WIDTH];unsignedcharTX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01};//DefineastaticTXaddressunsignedcharRX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01};//DefineastaticRXaddress//****************************************************************////SPI(nRF24L01)commands#defineREAD_REG0x00//Definereadcommandtoregister#defineWRITE_REG0x20//Definewritecommandtoregister#defineRD_RX_PLOAD0x61//DefineRXpayloadregisteraddress#defineWR_TX_PLOAD0xA0//DefineTXpayloadregisteraddress#defineFLUSH_TX0xE1//DefineflushTXregistercommand#defineFLUSH_RX0xE2//DefineflushRXregistercommand#defineREUSE_TX_PL0xE3//DefinereuseTXpayloadregistercommand//#defineNOP0xFF//DefineNoOperation,mightbeusedtoreadstatusregister//***************************************************////SPI(nRF24L01)registers(addresses)#defineCONFIG0x00//'Config'registeraddress#defineEN_AA0x01//'EnableAutoAcknowledgment'registeraddress#defineEN_RXADDR0x02//'EnabledRXaddresses'registeraddress#defineSETUP_AW0x03//'Setupaddresswidth'registeraddress#defineSETUP_RETR0x04//'SetupAuto.Retrans'registeraddress#defineRF_CH0x05//'RFchannel'registeraddress#defineRF_SETUP0x06//'RFsetup'registeraddress#defineSTATUS0x07//'Status'registeraddress#defineOBSERVE_TX0x08//'ObserveTX'registeraddress#defineCD0x09//'CarrierDetect'registeraddress#defineRX_ADDR_P00x0A//'RXaddresspipe0'registeraddress#defineRX_ADDR_P10x0B//'RXaddresspipe1'registeraddress#defineRX_ADDR_P20x0C//'RXaddresspipe2'registeraddress#defineRX_ADDR_P30x0D//'RXaddresspipe3'registeraddress#defineRX_ADDR_P40x0E//'RXaddresspipe4'registeraddress#defineRX_ADDR_P50x0F//'RXaddresspipe5'registeraddress#defineTX_ADDR0x10//'TXaddress'registeraddress#defineRX_PW_P00x11//'RXpayloadwidth,pipe0'registeraddress#defineRX_PW_P10x12//'RXpayloadwidth,pipe1'registeraddress#defineRX_PW_P20x13//'RXpayloadwidth,pipe2'registeraddress#defineRX_PW_P30x14//'RXpayloadwidth,pipe3'registeraddress#defineRX_PW_P40x15//'RXpayloadwidth,pipe4'registeraddress#defineRX_PW_P50x16//'RXpayloadwidth,pipe5'registeraddress#defineFIFO_STATUS0x17//'FIFOStatusRegister'registeraddress#defineMAX_RT0x10//Max#ofTXretransinterrupt#defineTX_DS0x20//TXdatasentinterrupt#defineRX_DR0x40//RXdatareceived//-----------------------------------------------------------------------------#defineSW_MODE1#defineHW_MODE2#defineRF_MODESW_MODEvoiddelay(void)//{NOP();NOP();NOP();NOP();NOP();NOP();}#if(SW_MODE==RF_MODE)voidRf24L01IoInit(void){RF_CSN_IO_OUT;RF_SCK_IO_OUT;RF_MOSI_IO_OUT;RF_CE_IO_OUT;RF_MISO_IO_IN;RF_IRQ_IO_IN;}#elsevoidRf24L01IoInit(void){RF_CSN_IO_OUT;RF_SCK_IO_OUT;RF_MOSI_IO_OUT;RF_CE_IO_OUT;RF_MISO_IO_IN;RF_IRQ_IO_IN;SPCR=BIT(SPE)|BIT(MSTR);//enableSPI,master.}#endifvoidRf24L01StatusInit(void){nRF24L01_CEL;delay();nRF24L01_CSNH;//Spidisabledelay();nRF24L01_SCKL;delay();}//清零buffer,指定字节个数voidclear_buf(unsignedchar*ptr,unsignedcharnumber){for(;number0;number--)*ptr++=0;}//-----------------------------------------
本文标题:nrf2401通信协议
链接地址:https://www.777doc.com/doc-5137747 .html