#include "NRF24L01_inte.H"
#include <string.h>

#define RF_CH_DEF		40
#define RF_C_NUM_DEF	0		//0通道无法发送
unsigned char RF_CH = RF_CH_DEF;			//RF频道
unsigned char RF_C_NUM = RF_C_NUM_DEF;		//RF通道
unsigned char NRF24L01_FLAG = 0;			//NRF24L01当前状态标志
//DB7; 1接收模式 			0发射模式
//DB6; 1有任务   			0空闲
//DB5; 1发送成功 			0发送失败(配合DB6检测是否发送成功)
//DB4; 1NRF正常连接 	0 检测不到连接
//DB3; 1接收到数据包 	0 缓冲区无数据
//DB2; 1 ACK模式      0 NOACK模式
//DB1; 1发送完自动进入接收模式 0关闭自动切换模式
unsigned char DYNPD_LEN = 0;			//用于存放动态数据包长度
//接收地址-- 接收有6个通道
const unsigned char RX_ADD[NRF24L01_CH_NUM_MAX + 1][NRF24L01_ADR_LEN] =
{
    {0x00, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//通道0
    {0x01, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//通道1
    {0x02, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//通道2
    {0x03, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//通道3
    {0x04, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//通道4
    {0x05, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4}		//通道5
};
//本地地址-- 发送只有1个通道
const unsigned char TX_ADD[NRF24L01_CH_NUM_MAX + 1][NRF24L01_ADR_LEN] =
{
    {0x00, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//对应通道0
    {0x01, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//对应通道1
    {0x02, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//对应通道2
    {0x03, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//对应通道3
    {0x04, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4},		//对应通道4
    {0x05, TR_ADDR_B1, TR_ADDR_B2, TR_ADDR_B3, TR_ADDR_B4}		//对应通道5
};


struct init_reg_data
{
    unsigned char reg;
    unsigned char data1;
    unsigned char *data2;
    unsigned char len2;
};

const struct init_reg_data init_first[] =
{
#if 1
    //RX全部通道配置
    {NRF24L01_RX_ADDR_P0, 0, RX_ADD[0], NRF24L01_ADR_LEN},		//P0接收地址
    {NRF24L01_RX_ADDR_P1, 0, RX_ADD[1], NRF24L01_ADR_LEN}, 	//P1接收地址
    {NRF24L01_RX_ADDR_P2, 0, RX_ADD[2], 1}, 					//P2接收地址
    {NRF24L01_RX_ADDR_P3, 0, RX_ADD[3], 1}, 					//P3接收地址
    {NRF24L01_RX_ADDR_P4, 0, RX_ADD[4], 1}, 					//P4接收地址
    {NRF24L01_RX_ADDR_P5, 0, RX_ADD[5], 1}, 					//P5接收地址
    {NRF24L01_RX_PW_P0, NRF24L01_ADR_LEN, NULL, 0},				//P0接受通道有效宽度
    {NRF24L01_RX_PW_P1, NRF24L01_ADR_LEN, NULL, 0}, 			//P1接受通道有效宽度
    {NRF24L01_RX_PW_P2, NRF24L01_ADR_LEN, NULL, 0}, 			//P2接受通道有效宽度
    {NRF24L01_RX_PW_P3, NRF24L01_ADR_LEN, NULL, 0}, 			//P3接受通道有效宽度
    {NRF24L01_RX_PW_P4, NRF24L01_ADR_LEN, NULL, 0}, 			//P4接受通道有效宽度
    {NRF24L01_RX_PW_P5, NRF24L01_ADR_LEN, NULL, 0}, 			//P5接受通道有效宽度
    //其他配置
    {NRF24L01_SETUP_AW, NRF24L01_ADR_LEN - 2, NULL, 0},			//设置地址长度为 NRF24L01_ADR_LEN
    {NRF24L01_RF_SETUP, 0x0f, NULL, 0},           				//设置TX发射参数,7db增益,2Mbps,低噪声增益开启
    //RX模式配置
    {NRF24L01_FEATURE, 0x06, NULL, 0},							//使能动态数据包长度,AUTO_ACK模式
    //TX模式配置
    {NRF24L01_SETUP_RETR, 0x1a, NULL, 0},        				//设置自动重发间隔时间:500us + 86us;重发次数:10次

    //通道/频段/TX地址配置
    {NRF24L01_RF_CH, RF_CH_DEF, NULL, 0}, 								//设置RF频道为RFCH [6:0]
    {NRF24L01_EN_AA, 1 << RF_C_NUM_DEF, NULL, 0}, 						//通道自动应答	: 0~5通道
    {NRF24L01_EN_RXADDR, 1 << RF_C_NUM_DEF, NULL, 0},					//接收通道使能  : 0~5通道
    {NRF24L01_TX_ADDR, 0, TX_ADD[RF_C_NUM_DEF], NRF24L01_ADR_LEN},		//写TX节点地址
    {NRF24L01_DYNPD, 1 << RF_C_NUM_DEF, NULL, 0},        				//选择通道0动态数据包长度
#else
    {NRF24L01_RX_PW_P0, NRF24L01_ADR_LEN, NULL, 0}, 			//设置接收数据长度，本次设置为32字节
    //{NRF24L01_FLUSE_RX, 0xff, NULL, 0}, 						//清除RX FIFO寄存器
    {NRF24L01_TX_ADDR, 0, TX_ADD[0], NRF24L01_ADR_LEN}, 		//写TX节点地址
    {NRF24L01_RX_ADDR_P0, 0, RX_ADD[0], NRF24L01_ADR_LEN}, 		//设置RX节点地址,主要为了使能ACK

    {NRF24L01_EN_AA, 0x01, NULL, 0}, 							//使能通道0的自动应答
    {NRF24L01_EN_RXADDR, 0x01, NULL, 0}, 						//使能通道0的接收地址

    {NRF24L01_SETUP_RETR, 0x1a, NULL, 0}, 						//设置自动重发间隔时间:500us + 86us;最大自动重发次数:10(a)次
    {NRF24L01_RF_CH, 40, NULL, 0}, 								//设置RF通道为40 收发必须一致，0为2.4GHz + 40
    {NRF24L01_RF_SETUP, 0x0f, NULL, 0}, 						//设置TX发射参数,0db增益,2Mbps,低噪声增益开启
    {NRF24L01_CONFIG, 0x0f, NULL, 0}, 							//配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断

#endif
};

const struct init_reg_data init_recv_mod[] =
{
    {NRF24L01_CONFIG, 0x0f, NULL, 0},							//bit0接收模式
    {NRF24L01_STATUS, 0x70, NULL, 0},							//清中断
    {NRF24L01_FLUSE_RX, NRF24L01_NOP, NULL, 0},					//清理接收FIFO
};

const struct init_reg_data init_send_mod[] =
{
    {NRF24L01_CONFIG, 0x0e, NULL, 0},							//bit0发送模式
    {NRF24L01_STATUS, 0x70, NULL, 0},							//清中断
    {NRF24L01_FLUSE_TX, NRF24L01_NOP, NULL, 0},					//清理发送FIFO
};

void NRF24L01_Init_reg ( struct init_reg_data *addr, unsigned char len )
{
    unsigned char i;

    for ( i = 0; i < len; i++ )
    {
        if ( addr[i].data2 == NULL )
        {
            NRF24L01_Write_Reg ( addr[i].reg, addr[i].data1 );
        }
        else
        {
            NRF24L01_Write_Buf ( addr[i].reg, addr[i].data2, addr[i].len2 );
        }
    }
}

unsigned char  NRF24L01_check_reg ( struct init_reg_data *addr, unsigned char len )
{
    unsigned char buff[5];
    unsigned char i, j;

    for ( i = 0; i < len; i++ )
    {
        if ( addr[i].data2 == NULL )
        {
            buff[0] = NRF24L01_Read_Reg ( addr[i].reg );

            if ( buff[0] != addr[i].data1 )
            {
                return 0;
            }
        }
        else
        {
            NRF24L01_Read_Buf ( addr[i].reg, buff, addr[i].len2 );

            for ( j = 0; j < 5; j++ )
            {
                if ( buff[j] != addr[i].data2[j] )
                {
                    return 0;
                }
            }
        }
    }

    return 1;
}

//向从机发送一个字节数据并返回接收数据
/**************************************************/
#if 0
unsigned char SPI_WriteRead ( unsigned char byte )
{
    unsigned char bit_ctr;

    for ( bit_ctr = 0; bit_ctr < 8; bit_ctr++ ) // 输出8位
    {
        NRF_MOSI = ( byte & 0x80 ); 			// MSB TO MOSI
        byte = ( byte << 1 );					// shift next bit to MSB
        NRF_SCK = 1;
        byte |= NRF_MISO;	        			// capture current MISO bit
        NRF_SCK = 0;
    }

    return byte;
}
#else
unsigned char SPI_WriteRead ( unsigned char byte )
{
    unsigned char i;
    SPDR = byte;						//写寄存器

    for ( i = 0; i < 255; i++ )
    {
        if ( SPSR & SET_BIT7 )
        {
            break;   //等待传输
        }
    }

    SPSR &= ~SET_BIT7;
    byte = SPDR;						//读寄存器
    return byte;
}

#endif

//写NRF24L01寄存器，返回状态值
unsigned char NRF24L01_Write_Reg ( unsigned char reg, unsigned char value )
{
    unsigned char BackDate;
    NRF_CE = 0;		//在写寄存器之前一定要进入待机模式或掉电模式。
    NRF_CSN = 0;

    if ( reg <= 0x1F )  		//0~0x1f
    {
        reg = NRF24L01_WRITE_REG | reg;
    }

    SPI_WriteRead ( reg );
    BackDate = SPI_WriteRead ( value );
    NRF_CSN = 1;
    NRF_CE = 1;		//恢复正常模式
    return ( BackDate );
}
//读NRF24L01寄存器，返回寄存器值
unsigned char NRF24L01_Read_Reg ( unsigned char reg )
{
    unsigned char BackDate;
    NRF_CSN = 0;

    if ( reg <= 0x1F )  		//0~0x1f
    {
        reg = NRF24L01_READ_REG | reg;
    }

    SPI_WriteRead ( reg );
    BackDate = SPI_WriteRead ( NRF24L01_NOP );	//NOP，无效值，用于读取
    NRF_CSN = 1;
    return ( BackDate );
}

//向缓冲区写入数据
unsigned char NRF24L01_Write_Buf ( unsigned char reg, unsigned char *pBuf, unsigned char bytes )
{
    unsigned char status, byte_ctr;

    NRF_CE = 0;		//在写寄存器之前一定要进入待机模式或掉电模式。
    NRF_CSN = 0;

    if ( reg <= 0x1F )  		//0~0x1f
    {
        reg = NRF24L01_WRITE_REG | reg;
    }

    status = SPI_WriteRead ( reg );

    for ( byte_ctr = 0; byte_ctr < bytes; byte_ctr++ )
    {
        SPI_WriteRead ( *pBuf++ );
    }

    NRF_CSN = 1;
    NRF_CE = 1;		//恢复正常模式
    return ( status );
}

//读取缓冲区数据
void NRF24L01_Read_Buf ( unsigned char reg, unsigned char *pBuf, unsigned char bytes )
{
    unsigned char byte_ctr;
    NRF_CSN = 0;

    if ( reg <= 0x1F )  		//0~0x1f
    {
        reg = NRF24L01_READ_REG | reg;
    }

    SPI_WriteRead ( reg );

    for ( byte_ctr = 0; byte_ctr < bytes; byte_ctr++ )
    {
        pBuf[byte_ctr] = SPI_WriteRead ( NRF24L01_NOP );	//NOP，无效值，用于读取
    }

    NRF_CSN = 1;
}


/*
PIPEN 中断使能
PINEN 正反向特性
PICON 触发方式（电平或边沿检测）
PIF 中断标志
*/
void NRF24L01_Interrupt_P03 ( void )
{
    //P0M1|=SET_BIT3;P0M2&=~SET_BIT3;				//input
    PICON &= ~ ( SET_BIT0 | SET_BIT1 );
    PICON |= ( 0x00 );							//[1:0]=0.端口0
    PICON |= SET_BIT5;							//通道3=1.边沿触发
    PINEN |= SET_BIT3;							// 1 = 低电平/ 下降沿触发中断
    PIPEN &= ~SET_BIT3;							//0 = 关闭中断(高电平/ 上升沿触发中断)
    EIE |= SET_BIT1;							//管脚中断，最多8个,中断号7
    EA = 1;										// 总中断
}




uint8	NRF24L01_Wait_Param ( void );
//NRF24L01初始化函数
void NRF24L01_Init ( void )
{
    //uint8 i;
#if BK2423
    //NRF_CE = 0;		//在写寄存器之前一定要进入待机模式或掉电模式。
    NRF24L01_Write_Reg ( ACTIVATE_CMD, 0x73 );
    //NRF_CE = 1;
#endif

    NRF_CSN = 1;			// Spi disable
    NRF_SCK = 0;			// Spi clock line init high

    //NRF_CE = 0;		//在写寄存器之前一定要进入待机模式或掉电模式。
    //配置寄存器
    NRF24L01_FLAG |= NRF24L01_AUTO_ACK;		//是否开启应答模式
    NRF24L01_FLAG |= NRF24L01_AUTO_RX;		//发送完是否自动进入接收模式

#if 0

    //0-5通道接收地址,长度配置
    for ( i = 0; i <= NRF24L01_CH_NUM_MAX; i++ )
    {
        NRF24L01_Write_Buf ( NRF24L01_RX_ADDR_P0 + i, RX_ADD[i], NRF24L01_ADR_LEN );	//设置接收地址
        NRF24L01_Write_Reg ( NRF24L01_RX_PW_P0 + i, NRF24L01_ADR_LEN );				//接受通道有效宽度
    }

    NRF24L01_Write_Reg ( NRF24L01_SETUP_AW, NRF24L01_ADR_LEN - 2 );	//设置地址长度为 NRF24L01_ADR_LEN
    NRF24L01_Write_Reg ( NRF24L01_RF_CH, RF_CH );              //设置RF通道为RFCH [6:0]
    NRF24L01_Write_Reg ( NRF24L01_RF_SETUP, 0x07 );            //设置TX发射参数,7db增益,1Mbps,低噪声增益开启
    NRF24L01_Write_Reg ( NRF24L01_EN_AA, 0x01 );         					//使能通道0的自动应答
    NRF24L01_Write_Reg ( NRF24L01_EN_RXADDR, 0x01 );           //使能通道0的接收地址
    //RX模式配置
    //NRF24L01_Write_Buf ( NRF24L01_RX_ADDR_P0, RX_ADD[0], NRF24L01_ADR_LEN ); //写RX节点地址
    NRF24L01_Write_Reg ( NRF24L01_FEATURE, 0x05 );									//使能动态数据包长度,AUTO_ACK模式
    NRF24L01_Write_Reg ( NRF24L01_DYNPD, 0X01 );        						//选择通道0动态数据包长度
    NRF24L01_Write_Reg ( NRF24L01_FLUSE_RX, NRF24L01_NOP );            //清除RX FIFO寄存器,写1清除
    //TX模式配置
    NRF24L01_Write_Buf ( NRF24L01_TX_ADDR , TX_ADD[RF_C_NUM], NRF24L01_ADR_LEN );	//写TX节点地址
    NRF24L01_Write_Reg ( NRF24L01_SETUP_RETR, 0x1f );        //设置自动重发间隔时间:500us + 86us;最大自动重发次数:15次
    NRF24L01_Write_Reg ( NRF24L01_FLUSE_TX, NRF24L01_NOP );           //清除TX FIFO寄存器,写1清除
    NRF24L01_RxMode();                                                  					//默认进入接收模式
    //NRF_CE = 1;
    NRF24L01_Change_Working_Frequency ( 0x64 );
    NRF24L01_Change_Data_Channel ( 2 );
#else
    NRF24L01_Init_reg ( init_first, sizeof ( init_first ) / sizeof ( init_first[0] ) );
#endif

    NRF24L01_RxMode();
    NRF24L01_Interrupt_P03();

    NRF24L01_Wait_Param();
}

//NRF24L01进入发送模式
void NRF24L01_TxMode ( void )
{
    //NRF_CE = 0;		//在写寄存器之前一定要进入待机模式或掉电模式。
#if 0
    NRF24L01_Write_Reg ( NRF24L01_CONFIG, 0x0e );
    NRF24L01_Write_Reg ( NRF24L01_STATUS, 0x70 );		//清除中断标志
    NRF24L01_Write_Reg ( NRF24L01_FLUSE_TX, NRF24L01_NOP ); //清除TX FIFO寄存器,写1清除
#else
    NRF24L01_Init_reg ( init_send_mod, sizeof ( init_send_mod ) / sizeof ( init_send_mod[0] ) );
#endif
    //NRF_CE = 1;
    NRF24L01_FLAG &= ~NRF24L01_MODE_RX;		//修改状态标识为发射模式
    NRF24L01_FLAG &= ~NRF24L01_BUSY;	//BUSY标志置0
    NRF24L01_FLAG &= ~NRF24L01_RECIVE;//清除有数据标志
    Timer3_Delay10us ( 1 );
}
//NRF24L01进入接收模式
void NRF24L01_RxMode ( void )
{
    //NRF_CE = 0;		//在写寄存器之前一定要进入待机模式或掉电模式。
#if 0
    NRF24L01_Write_Reg ( NRF24L01_CONFIG, 0x0f ); 	//配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式
    NRF24L01_Write_Reg ( NRF24L01_STATUS, 0x70 );		//清除中断标志
    NRF24L01_Write_Reg ( NRF24L01_FLUSE_RX, NRF24L01_NOP ); //清除RX FIFO寄存器,写1清除
#else
    NRF24L01_Init_reg ( init_recv_mod, sizeof ( init_recv_mod ) / sizeof ( init_recv_mod[0] ) );
#endif
    //NRF_CE = 1;
    NRF24L01_FLAG |= NRF24L01_MODE_RX;		//修改状态标识为接收模式
    NRF24L01_FLAG &= ~NRF24L01_BUSY;	//BUSY标志置0
    DYNPD_LEN = 0;	//清0动态数据包长度
    NRF24L01_FLAG &= ~NRF24L01_RECIVE;//清除有数据标志
    Timer3_Delay10us ( 13 );
}


uint8 NRF24L01_Wait_Param ( void )
{
    uint8 res = 1;
#if 1
    uint8 i;
    uint8 TX_Buff[5] = {0}, RX_Buff[5] = {0}, FRE, CH;
    NRF24L01_Read_Buf ( NRF24L01_TX_ADDR, TX_Buff, NRF24L01_ADR_LEN );
    NRF24L01_Read_Buf ( NRF24L01_RX_ADDR_P0 + RF_C_NUM, RX_Buff, NRF24L01_ADR_LEN );
    CH = NRF24L01_Read_Reg ( NRF24L01_EN_RXADDR );
    FRE = NRF24L01_Read_Reg ( NRF24L01_RF_CH );

    //接收/发送地址检查
    for ( i = 0; i < ( RF_C_NUM < 2 ? 5 : 1 ); i++ )
    {
        if ( TX_Buff[i] != TX_ADD[RF_C_NUM][i] || RX_Buff[i] != RX_ADD[RF_C_NUM][i] )
        {
            res = 0;
            break;
        }
    }

    //通道使能检查
    if ( ! ( CH & ( 1 << RF_C_NUM ) ) )
    {
        res = 0;
    }

    //频道检查
    if ( FRE != RF_CH )
    {
        res = 0;
    }

    DBG ( "TX=%x %x %x %x %x\n"
          "RX=%x %x %x %x %x\n"
          "CH=%d\n"
          "FRE=%d\n",
          TX_Buff[0], TX_Buff[1], TX_Buff[2], TX_Buff[3], TX_Buff[4],
          RX_Buff[0], RX_Buff[1], RX_Buff[2], RX_Buff[3], RX_Buff[4],
          CH,
          FRE );
#else
    res = NRF24L01_check_reg ( init_first, sizeof ( init_first ) / sizeof ( init_first[0] ) );
#endif

    res ? ( NRF24L01_FLAG |= NRF24L01_CHECK ) : ( NRF24L01_FLAG &= ~NRF24L01_CHECK );
    return res;
}


//NRF24L01发送数据
void NRF24L01_SendFrame ( unsigned char *temp, unsigned char len )
{
    if ( NRF24L01_FLAG & NRF24L01_MODE_RX )
    {
        NRF24L01_TxMode();
    }

    NRF24L01_FLAG |= NRF24L01_BUSY;	//BUSY标志置1
    NRF24L01_FLAG &= ~NRF24L01_TX_ACCESS;//清除发送成功标志
    //NRF_CE = 0;

    if ( NRF24L01_FLAG & NRF24L01_AUTO_ACK )
    {
        NRF24L01_Write_Buf ( NRF24L01_WR_TX_PLOAD, temp, len );	//写待发数据包,需要回应
    }
    else
    {
        NRF24L01_Write_Buf ( W_TX_PAYLOAD_NOACK_CMD, temp, len );	//写待发数据包,无须回应
    }

    //NRF_CE = 1;
}

//NRF24L01接收数据
void NRF24L01_RecvFrame ( unsigned char *temp )
{
    DYNPD_LEN = NRF24L01_Read_Reg ( R_RX_PL_WID_CMD );

    if ( DYNPD_LEN > 32 )
    {
        NRF24L01_RxMode();	//重新初始化接收模式
    }
    else
    {
        NRF24L01_Read_Buf ( NRF24L01_RD_RX_PLOAD, temp, DYNPD_LEN );	//读取缓冲区数据
    }

    NRF24L01_FLAG &= ~NRF24L01_RECIVE;//清除有数据标志
}
//更改工作频率0~7f
void NRF24L01_Change_Working_Frequency ( unsigned char ch ) //0~7f
{
    if ( ch > 0x7f )
    {
        return;
    }

    //NRF_CE = 0;
    NRF24L01_Write_Reg ( NRF24L01_RF_CH, ch & 0x7F );  //设置RF频道
    //NRF_CE = 1;
    RF_CH = ch & 0x7F;					//同步设置到系统参数
}

//更改通道0~3f
void NRF24L01_Change_Data_Channel ( unsigned char ch ) //0~5
{
    if ( ch > NRF24L01_CH_NUM_MAX )
    {
        return;
    }

    RF_C_NUM = ch;

    NRF24L01_Write_Reg ( NRF24L01_EN_AA, 1 << RF_C_NUM ); //通道自动应答
    NRF24L01_Write_Reg ( NRF24L01_EN_RXADDR, 1 << RF_C_NUM ); //接收通道使能

    NRF24L01_Write_Buf ( NRF24L01_TX_ADDR , TX_ADD[RF_C_NUM], NRF24L01_ADR_LEN );	//写TX节点地址
    NRF24L01_Write_Reg ( NRF24L01_DYNPD, 1 << RF_C_NUM ); //通道TX自动长度
}

//NRF24L01中断处理:当状态寄存器中 TX_DS、 RX_DR 或 MAX_RT 为高时触发中断。
#ifdef N76E003_IAR
#pragma vector=0x3B
__interrupt void NRF24L01_Handler ( void )
#else
void NRF24L01_Handler ( void ) interrupt 7            //interrupt address is 0x001B
#endif
{
    unsigned char state, fifo_state, flag;
    clr_EPI;				//关闭管脚中断7
    flag = PIF;
    PIF = 0x00;				//清除全部标志
    state = NRF24L01_Read_Reg ( NRF24L01_STATUS );		//读NRF24L01状态寄存器
    fifo_state = NRF24L01_Read_Reg ( NRF24L01_FIFO_STATUS );		//读NRF24L01FIFO状态寄存器
    NRF24L01_Write_Reg ( NRF24L01_STATUS, state );  //清除中断,写1清除

    //发送成功中断
    if ( state & NRF24L01_STATUS_TX_DS )
    {
        NRF24L01_FLAG &= ~NRF24L01_BUSY;	//BUSY标志置0
        NRF24L01_FLAG |= NRF24L01_TX_ACCESS;//发送成功标志置1

        if ( NRF24L01_FLAG & NRF24L01_AUTO_RX )
        {
            NRF24L01_RxMode();	//重新初始化接收模式
        }
    }

    //达到最大重发次数中断
    if ( state & NRF24L01_STATUS_MAX_RT )
    {
        NRF24L01_Write_Reg ( NRF24L01_FLUSE_TX, NRF24L01_NOP );  //清除TX FIFO寄存器,写1清除
        NRF24L01_FLAG &= ~NRF24L01_BUSY;	//BUSY标志置0
        NRF24L01_FLAG &= ~NRF24L01_TX_ACCESS;//发送成功标志置0
    }

    //接收到数据中断
    if ( state & NRF24L01_STATUS_RX_DR )
    {
        NRF24L01_FLAG |= NRF24L01_RECIVE;	//接收到数据标志位置1
    }

    //TX_FIFO满中断
    if ( state & NRF24L01_STATUS_TX_FULL )
    {
        NRF24L01_TxMode();	//重新初始化发射模式
    }

    //RX_FIFO满
    if ( fifo_state & NRF24L01_STATUS_RX_FULL )
    {
        NRF24L01_RxMode();	//重新初始化接收模式
    }

    state = NRF24L01_Read_Reg ( NRF24L01_CONFIG );		//读NRF24L01状态寄存器
    set_EPI;				//开启管脚中断7
}
