#include "DataProc.h"
#include <string.h>
#include "printf.h"
#include "flash.h"
#include "LCD.h"
//SPIType	SPI_Using = SPI_PORT1;	//选择一个SPI接口

extern void IWDG_Feed(void);

u8 CheckData_1356[10] = {0xD1,0xD1,0xE0,0xD0,0x00,0x00,0x00,0x00,0xD2,0xD2}; //13.56M接收数据
u8 GetNum_1356[5] = {0x00,0x00,0x00,0x00,0x00}; //13.56M缓存
static u8 ResNum_1356[5];  //13.56M缓存，用于回复A2
static u8 oldResNum_1356[5];

u8 uartSendData[100]; //最大包100个字节
u8 uartRecData[100]; //最大包100个字节

u8 uartSendRdNumData[100];
u8 uartSendWrNumOkData[100];
u8 uartSendConfigKaoQinData[100];
volatile u16 count;
u16 count_num;
u8 flag_1ms;
u8 first_clear_flag;
u8 HW_Open;
u16 HW_OpenCount;

u8 Flag_update;
u8 WriteOKFlag=0;
u8 protocol_config_flg;

struct Protocol_send_data   //通讯协议 发送
{
    u8 HeadFix;
    u8 Control;
    u8 Magic[4];
    u8 SequenceID[4];
    u8 Command;
    u8 Tag;
    u8 Length;
    u8 Value[6];            //最大值是6
    u8 FootCRC[2];          //CRC 暂时固定为00 00
    u8 FootFix;             //固定为0xFB
};

struct Protocol_rec_data   //通讯协议 接收
{
    u8 HeadFix;
    u8 Control;
    u8 Magic[4];
    u8 SequenceID[4];
    u8 Command;
    u8 Tag;
    u8 Length;
    u8 Value[62];           //最大值是62
    u8 FootCRC[2];          //CRC 暂时固定为00 00
    u8 FootFix;             //固定为0xFB
};

//struct ReadNum_send_data   //读号回复指令，长度 30Byte
//{
//    u8 HeadFix;
//    u8 Control;
//    u8 Magic[4];
//    u8 SequenceID[4];
//    u8 Command;
//    u8 Tag1;
//    u8 Length1;
//    u8 Value1[6];            //最大值是6
//    u8 Tag2;
//    u8 Length2;
//    u8 Value2[6];            //最大值是6
//    u8 FootCRC[2];          //CRC 暂时固定为00 00
//    u8 FootFix;             //固定为0xFB
//};

struct ReadNum_send_data   //读号回复指令，长度 22Byte
{
    u8 HeadFix;
    u8 Control;
    u8 Magic[4];
    u8 SequenceID[4];
    u8 Command;
    u8 Tag1;
    u8 Length1;
    u8 Value1[6];            //最大值是6
    u8 FootCRC[2];          //CRC 暂时固定为00 00
    u8 FootFix;             //固定为0xFB
};

struct WrNumOk_send_data   //写卡成功回复指令
{
    u8 HeadFix;
    u8 Control;
    u8 Magic[4];
    u8 SequenceID[4];
    u8 Command;
    u8 FootCRC[2];          //CRC 暂时固定为00 00
    u8 FootFix;             //固定为0xFB
};

struct ConfigKaoQin_send_data
{
	u8 HeadFix;           //固定为0xFA
	u8 Control;           // 1表示成功
	u8 Magic[4];
	u8 SequenceID[4];
	u8 Command;            //  A8
	u8 FootCRC[2];			//CRC 暂时固定为00 00
	u8 FootFix; 			//固定为0xFB
};


#define ReceiveBuf ((struct Protocol_rec_data *)&uartRecData[0])
#define SendBuf ((struct Protocol_send_data *)&uartSendData[0])
#define ConfigKaoQinBuf ((struct ConfigKaoQin_send_data *)&uartSendConfigKaoQinData[0])
#define WrNumOkBuf ((struct WrNumOk_send_data *)&uartSendWrNumOkData[0])
#define ReadNumBuf ((struct ReadNum_send_data *)&uartSendRdNumData[0])

#define	CndRspBufferSize  100

unsigned char CmdRspBuffer[CndRspBufferSize];
unsigned char CmdRspBufferWr_Rx = 0;

u8 idBuf[5];        //2.4G ID缓存空间


u16 count_A34;
u8 idResA3Buf[5];     //2.4G ID缓存，用于回复A3
u8 ReadAttenID[5];  //2.4G 考勤卡ID

u8 newIdFlag;   //写2.4G ID标志
u8 recFlag;
u8 rspFlag_A3;
u8 read1356flag;

unsigned char txBuf[32]="123456789123456789";
unsigned char txA3Buf[32]="123456789123456789";
unsigned char rspFlag = 0;  //是否处理答题卡上电2.4G信息 标志
unsigned char wrNumFlag = 0;//写号是否完成标志

/****************************************************
功能: 校验和

方法: sum = ~(data0+...+datan)+1;
*****************************************************/
u8 CheckSum(u8 *buf,u8 len)
{
	u8 sum = 0;
	u8 i;
	for(i = 0;i < len; i++)
	{
		sum += buf[i];
	}
	sum = ~sum;
	sum += 1;
	return sum;
}

//void AT_AddNewRxByte(u8 c)
//{
//	if (c != 0x0D)
//	{
//		if (c < 32) return;											// ignore control charcters			
//		if (CmdRspBufferWr_Rx >= (CndRspBufferSize - 1)) return;	       // buffer overflow - ignore character
//		CmdRspBuffer[CmdRspBufferWr_Rx++] = c;
//		CmdRspBuffer[CmdRspBufferWr_Rx] = 0;	
//		return;
//	}
//	//----------------------------------------------------
//	// ID=XXXXX
//	if((CmdRspBuffer[0] == 'I')&&(CmdRspBuffer[1] == 'D')&&(CmdRspBuffer[2] == '='))	
//	{
//	   memcpy(idBuf,&CmdRspBuffer[3],5);
//	   SendConsoleStr("id=");
//	   SendConsoleData(idBuf,5);
//	   SendConsoleStr("\n");
//	   newIdFlag = 1;
//	}
//	else if((CmdRspBuffer[0] == 'I')&&(CmdRspBuffer[1] == 'D')&&(CmdRspBuffer[2] == '?'))
//	{
//	  	SendConsoleStr("id=");
//	   	SendConsoleData(idBuf,5);
//		SendConsoleStr("\n");  		
//	}
//	memset(CmdRspBuffer,0,sizeof(CmdRspBuffer));
//	CmdRspBufferWr_Rx = 0;
//}


void Data_Init(void)    //包数据初始化
{
    SendBuf->HeadFix = 0xFA;        //发送缓冲区初始化
    SendBuf->Control = 0x10;
    SendBuf->Magic[0]= 0;
    SendBuf->Magic[1]= 0;
    SendBuf->Magic[2]= 0;
    SendBuf->Magic[3]= 0;
    SendBuf->SequenceID[0] = 0;
    SendBuf->SequenceID[1] = 0;
    SendBuf->SequenceID[2] = 0;
    SendBuf->SequenceID[3] = 0;
    SendBuf->FootCRC[0] = 0;
    SendBuf->FootCRC[1] = 0;
    SendBuf->FootFix = 0xFB;
    
    ReceiveBuf->HeadFix = 0xFA;         //接收缓冲区初始化
    ReceiveBuf->Control = 0x10;
    ReceiveBuf->Magic[0]= 0;
    ReceiveBuf->Magic[1]= 0;
    ReceiveBuf->Magic[2]= 0;
    ReceiveBuf->Magic[3]= 0;
    ReceiveBuf->SequenceID[0] = 0;
    ReceiveBuf->SequenceID[1] = 0;
    ReceiveBuf->SequenceID[2] = 0;
    ReceiveBuf->SequenceID[3] = 0;
    ReceiveBuf->FootCRC[0] = 0;
    ReceiveBuf->FootCRC[1] = 0;
    ReceiveBuf->FootFix = 0xFB;

	ConfigKaoQinBuf->HeadFix = 0xFA;         //接收缓冲区初始化
    ConfigKaoQinBuf->Control = 0x10;
    ConfigKaoQinBuf->Magic[0]= 0;
    ConfigKaoQinBuf->Magic[1]= 0;
    ConfigKaoQinBuf->Magic[2]= 0;
    ConfigKaoQinBuf->Magic[3]= 0;
    ConfigKaoQinBuf->SequenceID[0] = 0;
    ConfigKaoQinBuf->SequenceID[1] = 0;
    ConfigKaoQinBuf->SequenceID[2] = 0;
    ConfigKaoQinBuf->SequenceID[3] = 0;
    ConfigKaoQinBuf->FootCRC[0] = 0;
    ConfigKaoQinBuf->FootCRC[1] = 0;
    ConfigKaoQinBuf->FootFix = 0xFB;

	
    ReadNumBuf->HeadFix = 0xFA;         //接收缓冲区初始化
    ReadNumBuf->Control = 0x10;
    ReadNumBuf->Magic[0]= 0;
    ReadNumBuf->Magic[1]= 0;
    ReadNumBuf->Magic[2]= 0;
    ReadNumBuf->Magic[3]= 0;
    ReadNumBuf->SequenceID[0] = 0;
    ReadNumBuf->SequenceID[1] = 0;
    ReadNumBuf->SequenceID[2] = 0;
    ReadNumBuf->SequenceID[3] = 0;
    ReadNumBuf->FootCRC[0] = 0;
    ReadNumBuf->FootCRC[1] = 0;
    ReadNumBuf->FootFix = 0xFB;
    
    WrNumOkBuf->HeadFix = 0xFA;         //接收缓冲区初始化
    WrNumOkBuf->Control = 0x10;
    WrNumOkBuf->Magic[0]= 0;
    WrNumOkBuf->Magic[1]= 0;
    WrNumOkBuf->Magic[2]= 0;
    WrNumOkBuf->Magic[3]= 0;
    WrNumOkBuf->SequenceID[0] = 0;
    WrNumOkBuf->SequenceID[1] = 0;
    WrNumOkBuf->SequenceID[2] = 0;
    WrNumOkBuf->SequenceID[3] = 0;
    WrNumOkBuf->FootCRC[0] = 0;
    WrNumOkBuf->FootCRC[1] = 0;
    WrNumOkBuf->FootFix = 0xFB;
}

//void Usart1_Process(void)
//{
//    
//    if(USART1_RX_CNT==10 && USART1_RX_BUF[0]==0xd1 && USART1_RX_BUF[9]==0xd2)
//    {
//        USART1_REC_Status |= 0x8000;    //收到ID设置命令，十进制10位ID
//    }
//    
//    
//    if(USART1_REC_Status & 0x8000)
//    {
////        memcpy(idBuf,&USART1_RX_BUF[3],5);  //把5位十进制ID号写到idBuf
//        idBuf[0]=0;
//        idBuf[1]=USART1_RX_BUF[4];
//        idBuf[2]=USART1_RX_BUF[5];
//        idBuf[3]=USART1_RX_BUF[6];
//        idBuf[4]=USART1_RX_BUF[7];  //串口收到的ID号写入到idBuf缓冲区
//        
//        SendConsoleStr("id=");
//        SendConsoleData(idBuf,5);
//        SendConsoleStr("\n");
//        newIdFlag = 1;
//        
//        USART1_RX_CNT = 0;
//        USART1_REC_Status = 0;
//    }
//}


void DataProc_1356(void)
{    
	u32 dat1,dat2,dat3,dat4;
	u8 IdBuf[10]={0};
	IWDG_Feed();
	if(USART3_RX_CNT==10)
	{
		if(USART3_RX_BUF[0]==CheckData_1356[0] && USART3_RX_BUF[1]==CheckData_1356[1]
		&& USART3_RX_BUF[8]==CheckData_1356[8] && USART3_RX_BUF[9]==CheckData_1356[9])
		{
		    USART3_REC_Status |= 0x8000;    //串口3完成接收一帧数据
		}
		else
		{
			memset(USART3_RX_BUF,0,20);
		}
	}
    
	if(USART3_REC_Status & 0x8000)
	{        
        //写号清零防止对下一次写号造成影响
        //delay_ms(1);
#if 0
        idBuf[0]=0;
        idBuf[1]=0;
        idBuf[2]=0;
        idBuf[3]=0;
        idBuf[4]=0;

        GetNum_1356[0]=0;
        GetNum_1356[1]=0;
        GetNum_1356[2]=0;
        GetNum_1356[3]=0;
        GetNum_1356[4]=0;
        
        idResA3Buf[0]=0;
        idResA3Buf[1]=0;
        idResA3Buf[2]=0;
        idResA3Buf[3]=0;
        idResA3Buf[4]=0;
        
        ReadAttenID[0]=0;
        ReadAttenID[1]=0;
        ReadAttenID[2]=0;
        ReadAttenID[3]=0;
        ReadAttenID[4]=0;
 #else
		memset(idBuf,0,5);
		memset(GetNum_1356,0,5);
		memset(idResA3Buf,0,5);
		memset(ReadAttenID,0,5);
 #endif
//        SendConsoleData(USART3_RX_BUF,10);
//        DegugPrintf(USART3_RX_BUF,10);
//        SendConsoleStr("\n");
       
        //IWDG_Feed();
        GetNum_1356[0]=0;
        GetNum_1356[1]=USART3_RX_BUF[4];
        GetNum_1356[2]=USART3_RX_BUF[5];
        GetNum_1356[3]=USART3_RX_BUF[6];
        GetNum_1356[4]=USART3_RX_BUF[7];  //串口收到的ID号写入到idBuf缓冲区
        
		ResNum_1356[0]=0;
		ResNum_1356[1]=GetNum_1356[1];
		ResNum_1356[2]=GetNum_1356[2];
		ResNum_1356[3]=GetNum_1356[3];
		ResNum_1356[4]=GetNum_1356[4];

       // NRF24L01_RX_Mode(SPI_SEND);     //
        dat1 = (GetNum_1356[1]<<24);
        dat2 = (GetNum_1356[2]<<16);
        dat3 = (GetNum_1356[3]<<8);
        dat4 = dat1 + dat2 + dat3 + GetNum_1356[4];
#if LCD
		HexToDec(dat4,IdBuf,sizeof(IdBuf));
		vDispClearLine(2);
		vDispStr(2,1,"13.56:",6);
		vDispStr(2,7,(char*)IdBuf,10);
#endif
		read1356flag = 1;
//        printf("1356_0=%x\r\n",GetNum_1356[0]);
//        printf("1356_1=%x\r\n",GetNum_1356[1]);
//        printf("1356_2=%x\r\n",GetNum_1356[2]);
//        printf("1356_3=%x\r\n",GetNum_1356[3]);
//        printf("1356_4=%x\r\n",GetNum_1356[4]);
//        printf("\r\n读到13.56M卡号：%u\r\n",dat4);       
        IWDG_Feed();
//        idBuf[0]=GetNum_1356[0];
//        idBuf[1]=GetNum_1356[1];
//        idBuf[2]=GetNum_1356[2];
//        idBuf[3]=GetNum_1356[3];
//        idBuf[4]=GetNum_1356[4];
//        delay_ms(100);delay_ms(100);
        
        //HW_Open = 1;                     //开关红外扫描枪
        
//        newIdFlag = 1;  //2.4G开始写号
//        count = 0;

        //IWDG_Feed();
        USART3_RX_CNT = 0;		   //
		USART3_REC_Status = 0;
		memset(USART3_RX_BUF,0,20);
    }
}
#if 0
void DataProc_RecNrf24L01(void)
{
    u8 buffer[7];
    u8 temp;
    u32 dat1,dat2,dat3,dat4;
    IWDG_Feed();
//    if(WriteOKFlag==1)
//    {
        
            if(SPI2_IRQ == 0)
            {
                IWDG_Feed();
                temp = NRF24L01_RxPacket(SPI_SEND,buffer);
                
        //        printf("temp=%x\r\n",temp);
        //            SendConsoleData(buffer,RX_PLOAD_WIDTH);
                
                idBuf[0] = 0;
                idBuf[1] = buffer[1];
                idBuf[2] = buffer[2];
                idBuf[3] = buffer[3];
                idBuf[4] = buffer[4];
                
//                if(idBuf[0]==GetNum_1356[0] && idBuf[1]==GetNum_1356[1] && idBuf[2]==GetNum_1356[2] && idBuf[3]==GetNum_1356[3] && idBuf[4]==GetNum_1356[4])
//                {
//                    recFlag = 2;    //已写2.4G卡号
                    dat1 = idBuf[1]<<24;
                    dat2 = idBuf[2]<<16;
                    dat3 = idBuf[3]<<8;
                    dat4 = dat1 + dat2 + dat3 + idBuf[4];
//                    if(WriteOKFlag)
//                        printf("读到考勤ID=%u\r\n",dat4);
                        delay_ms(10);
//                    else
//                        printf("已写号，读到考勤:%u\r\n",dat4);
                    
                    WriteOKFlag = 0;
                    idBuf[0]=0;
                    idBuf[1]=0;
                    idBuf[2]=0;
                    idBuf[3]=0;
                    idBuf[4]=0;
                    GetNum_1356[0]=0;
                    GetNum_1356[1]=0;
                    GetNum_1356[2]=0;
                    GetNum_1356[3]=0;
                    GetNum_1356[4]=0;
//                }
//                else
//                {
////                    printf("未写卡号！\r\n");
////                    NRF24L01_RX_Mode(SPI_SEND);
//                    IWDG_Feed();
//                    
//                    WriteOKFlag = 0;
//                    WriteFailFlag = 0;
//                    idBuf[0] = 0;
//                    idBuf[1] = 0;
//                    idBuf[2] = 0;
//                    idBuf[3] = 0;
//                    idBuf[4] = 0;
//                    GetNum_1356[0]=0;
//                    GetNum_1356[1]=0;
//                    GetNum_1356[2]=0;
//                    GetNum_1356[3]=0;
//                    GetNum_1356[4]=0;
//                    
//                    NRF24L01_TX_Mode(SPI_SEND);
//                }
            }
       

}

#else
void DataProc_RecNrf24L01(void)
{
    u8 tmp;
    u32 IdData;
    u8  IdBuf[10]={0};
    //if(read1356flag)
    {
        NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//NOACK??,?????
        count_A34 = 0;
        {
            while(1)   //
            {

                tmp = DataProc_A4_nrf();

                if(tmp)
                    break;
            }
            read1356flag = 0;
#if LCD
            IdData =(ReadAttenID[1]<<24)|(ReadAttenID[2]<<16)|(ReadAttenID[3]<<8)|(ReadAttenID[4]);
            HexToDec(IdData,IdBuf,sizeof(IdBuf));
            vDispClearLine(3);
            vDispStr(3,1,"ID:",3);
            vDispStr(3,4,(char*)IdBuf,10); 
            if((ReadAttenID[0] & 0x80) !=0)
            {
                vDispStr(3,15,"1",1); 
            }
            else
            {
                vDispStr(3,15,"0",1); 
            } 
#endif
            SendConsoleData((char*)uartSendRdNumData,22);   //回显，返回给PC端
            delay_ms(50);

            DegugPrintf(uartSendRdNumData,22);
            SendUSART2Str("\n");	
#if LCD
            if(ResNum_1356[0] ==ReadAttenID[0] &&ResNum_1356[1] ==ReadAttenID[1] && ResNum_1356[2] ==ReadAttenID[2]
                &&ResNum_1356[3] ==ReadAttenID[3] && ResNum_1356[4] ==ReadAttenID[4]) 
            {
                vDispClearLine(4);
                vDispStr(4,4,"OK",2);

            }
            else
            {
                vDispClearLine(4);
                vDispStr(4,4,"NG",2);

            }
#endif
            //写号清零防止对下一次写号造成影响
            ReadAttenID[0]=0;
            ReadAttenID[1]=0;
            ReadAttenID[2]=0;
            ReadAttenID[3]=0;
            ReadAttenID[4]=0;
            ResNum_1356[0]=0;
            ResNum_1356[1]=0;
            ResNum_1356[2]=0;
            ResNum_1356[3]=0;
            ResNum_1356[4]=0;
            GetNum_1356[0]=0;
            GetNum_1356[1]=0;
            GetNum_1356[2]=0;
            GetNum_1356[3]=0;
            GetNum_1356[4]=0;    
        }
    }

}

#endif

void DataProc_Nrf24l01_SPI2(void)
{
    u8 buffer[32];
    u8 temp;
    //u32 dat1,dat2,dat3,dat4,cardid;
    IWDG_Feed();

    if(newIdFlag==1)
    {
        newIdFlag = 0;
        txBuf[0] = 0xfc;
        txBuf[1] = 0x11;
//        if(idBuf[0]==GetNum_1356[0] && idBuf[1]==GetNum_1356[1] && idBuf[2]==GetNum_1356[2] && idBuf[3]==GetNum_1356[3] && idBuf[4]==GetNum_1356[4])
//        {
            txBuf[2] = idBuf[0];
            txBuf[3] = idBuf[1];
            txBuf[4] = idBuf[2];
            txBuf[5] = idBuf[3];
            txBuf[6] = idBuf[4];
//        }
        
        txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
		SPI2_CE =0;
        
        NRF24L01_Write_Reg(SPI_SEND,FLUSH_TX,0xff);//清除TX FIFO寄存器 
		NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF 8个字节，自动发送
		NRF24L01_RX_Mode(SPI_SEND);      //设置24L01为接收模式
        rspFlag = 1;
        count=0;
        IWDG_Feed();
        
    }
        

        
    if(rspFlag) //处理答题卡上电发送的数据
    {
        IWDG_Feed();
        if(count<=2000) //2S内未收到2.4G回复信号，则rspFlag失效
        {
            if(SPI2_IRQ == 0)   //24L01收到答题卡上电发送的数据
            {
                IWDG_Feed();
                temp = NRF24L01_RxPacket(SPI_SEND,buffer);
//                SendConsoleByte(temp);

                if((temp&0x40)==0x40)   //接收到的数据显示
                {
                    buffer[RX_PLOAD_WIDTH]='\0';
                    led0=~led0;

//                    SendConsoleData(buffer,RX_PLOAD_WIDTH); //

                    if((buffer[1]==idBuf[0])&& (buffer[2]==idBuf[1])&&(buffer[3]==idBuf[2])&&(buffer[4]==idBuf[3])&&(buffer[5]==idBuf[4]))    //收到改写后编号
                    {
//                        if((buffer[1]==GetNum_1356[0])&& (buffer[2]==GetNum_1356[1])&&(buffer[3]==GetNum_1356[2])&&(buffer[4]==GetNum_1356[3])&&(buffer[5]==GetNum_1356[4]))
//                        {
                            if(rspFlag == 1)
                            {
                                rspFlag++;
                                txBuf[1] = 0x12;             ///0xfc + cmd(0x12) +id +crc   ----cmd=0x12 确认
                                txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
                                NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF 8个字节，自动发送
                            }
                            else
                            {
                                rspFlag=0;  //如果ID已经是最新，rspFlag清零，不处理接收数据
                                count = 0;  //计数清零
                                idBuf[0]=0;
                                
                                WrNumOkBuf->Control = 0x10; //写号完成
                                SendConsoleData((char*)uartSendWrNumOkData,14);   //回显，返回给PC端
                                
                                DegugPrintf(uartSendWrNumOkData,14);
                                SendUSART2Str("\n");
                                
                                //dat1 = (idBuf[1]<<24);
                                //dat2 = (idBuf[2]<<16);
                                //dat3 = (idBuf[3]<<8);
                                //dat4 = idBuf[4];
                                //cardid = dat1 + dat2 + dat3 + dat4;
//                                printf("写号完成！卡号：%u\r\n",cardid);
                                
                                //NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
                            #if APP
                            NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//???
                            #else
                            NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
                            #endif
                                WriteOKFlag = 1;
                            }
//                        }
                        
                        
                    }
                    else
                    {
                        SPI2_CE =0;
                        txBuf[1] = 0x11;
                        txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
                        NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF  8个字节
                        SPI2_CE =1;
                    }
                }
            }
            
        }
        else    //2s超时回复
        {
            IWDG_Feed();
            rspFlag=0;  //如果ID已经是最新，rspFlag清零，不处理接收数据
            count = 0;  //计数清零
            
            idBuf[0]=0;
            idBuf[1]=0;
            idBuf[2]=0;
            idBuf[3]=0;
            idBuf[4]=0;
            GetNum_1356[0]=0;
            GetNum_1356[1]=0;
            GetNum_1356[2]=0;
            GetNum_1356[3]=0;
            GetNum_1356[4]=0;
            
            
            WrNumOkBuf->Control = 0x30; //错误
            SendConsoleData((char*)uartSendWrNumOkData,14);   //回显，返回给PC端
            
            DegugPrintf(uartSendWrNumOkData,14);
            SendUSART2Str("\n");
//            printf("写号失败:超时！\r\n");
//            NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
        }
        
    }

}


u8 DataProc_A0_WriteNum_SPI2(void)
{
    u8 temp;
    u8 buffer[32];
    
    if(newIdFlag==1)
    {
        newIdFlag = 0;
        txBuf[0] = 0xfc;
        txBuf[1] = 0x11;
        txBuf[2] = idBuf[0];
        txBuf[3] = idBuf[1];
        txBuf[4] = idBuf[2];
        txBuf[5] = idBuf[3];
        txBuf[6] = idBuf[4];
        
        txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
		SPI2_CE =0;
        NRF24L01_Write_Reg(SPI_SEND,FLUSH_TX,0xff);//清除TX FIFO寄存器 
		NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF 8个字节，自动发送
		NRF24L01_RX_Mode(SPI_SEND);      //设置24L01为接收模式
        SPI2_CE =1;
        rspFlag = 1;
        count=0;
        IWDG_Feed();
        
    }
    
    if(count<=3000)
    {
        IWDG_Feed();
        if(SPI2_IRQ == 0)   //24L01收到答题卡上电发送的数据
        {
            IWDG_Feed();
            temp = NRF24L01_RxPacket(SPI_SEND,buffer);
            SendUSART2Byte(temp);
            SendUSART2Str("\n");

            if((temp&0x40)==0x40)   //接收到的数据显示
            {
                buffer[RX_PLOAD_WIDTH]='\0';
                
                DegugPrintf(buffer,7);
                SendUSART2Str("\n");

                if((buffer[1]==idBuf[0])&& (buffer[2]==idBuf[1])&&(buffer[3]==idBuf[2])&&(buffer[4]==idBuf[3])&&(buffer[5]==idBuf[4]))    //收到改写后编号
                {
                        if(rspFlag == 1)
                        {
                            rspFlag++;
                            txBuf[1] = 0x12;             ///0xfc + cmd(0x12) +id +crc   ----cmd=0x12 确认
                            txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
                            SPI2_CE =0;
                            NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF 8个字节，自动发送
                            SPI2_CE =1;
                        }
                        else
                        {
                            rspFlag=0;  //如果ID已经是最新，rspFlag清零，不处理接收数据
                        
                            DegugPrintf(uartSendWrNumOkData,14);
                            SendUSART2Str("\n");
                            
                            //NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
                            #if  APP
                            NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//???
                            #else
                             NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
                            #endif
                            WrNumOkBuf->Control = 0x10; //写号完成
                            SendConsoleData((char*)uartSendWrNumOkData,14);   //回显，返回给PC端

                            SendUSART2Str("OK\n");
                            return 1;
                        }                    
                    
                }
                else
                {
                    SPI2_CE =0;
                    txBuf[1] = 0x11;
                    txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
                    NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF  8个字节
                    SPI2_CE =1;
                    
                }
            }
            
        }
    }
    else
    {
        idBuf[0] = 0;
        idBuf[1] = 0x00;
        idBuf[2] = 0x00;
        idBuf[3] = 0x00;
        idBuf[4] = 0x00;
        ReadAttenID[0]=0;
        ReadAttenID[1]=0;
        ReadAttenID[2]=0;
        ReadAttenID[3]=0;
        ReadAttenID[4]=0;
        ResNum_1356[0]=0;
        ResNum_1356[1]=0;
        ResNum_1356[2]=0;
        ResNum_1356[3]=0;
        ResNum_1356[4]=0;
        GetNum_1356[0]=0;
        GetNum_1356[1]=0;
        GetNum_1356[2]=0;
        GetNum_1356[3]=0;
        GetNum_1356[4]=0;
        WrNumOkBuf->Control = 0x30; //写号失败
        SendConsoleData((char*)uartSendWrNumOkData,14);   //回显，返回给PC端
//        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);	//开始串口1接收中断
        SendUSART2Str("3000\n");
        return 1;
    }
    IWDG_Feed();
    return 0;
}


u8 DataProc_A3_nrf(void)
{
    u8 temp;
    u8 buffer[32];
    
    if(rspFlag_A3)
    {
        rspFlag_A3=0;
        SPI2_CE = 0;
        NRF24L01_Write_Reg(SPI_SEND,FLUSH_TX,0xff);//清除TX FIFO寄存器 
        NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txA3Buf,8);   //写数据到TX BUF 8个字节，自动发送
        NRF24L01_RX_Mode(SPI_SEND);      //设置24L01为接收模式
        SPI2_CE = 1;    
    }
    if(count_A34<1500)
    {
        if(SPI2_IRQ == 0)   //24L01收到答题卡上电发送的数据
        {
            IWDG_Feed();
            temp = NRF24L01_RxPacket(SPI_SEND,buffer);


            
            if((temp&0x40)==0x40)   //接收到的数据显示
            {
                buffer[RX_PLOAD_WIDTH]='\0';

                DegugPrintf(buffer,7);
                SendUSART2Str("\n");

                if((buffer[1]==0)&& (buffer[2]==0)&&(buffer[3]==0)&&(buffer[4]==0)&&(buffer[5]==0))    //未写卡号
                {
                    idResA3Buf[0] = 0;
                    idResA3Buf[1] = 0;
                    idResA3Buf[2] = 0;
                    idResA3Buf[3] = 0;
                    idResA3Buf[4] = 0;
//                        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);	//开始串口1接收中断
                    return 1;
                }
                else
                {
                    idResA3Buf[0] = buffer[1];
                    idResA3Buf[1] = buffer[2];
                    idResA3Buf[2] = buffer[3];
                    idResA3Buf[3] = buffer[4];
                    idResA3Buf[4] = buffer[5];
//                        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);	//开始串口1接收中断
                    return 1;
                }

            }
        }
        IWDG_Feed();
    }
    else
    {
        idResA3Buf[0] = 0;
        idResA3Buf[1] = 0x00;
        idResA3Buf[2] = 0x00;
        idResA3Buf[3] = 0x00;
        idResA3Buf[4] = 0x00;
//        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);	//开始串口1接收中断
        return 1;
    }
    return 0;
}

u8 DataProc_A4_nrf(void)
{
    u8 temp;
    u8 buffer[32];
	if(count_A34<3000)
	{
		if(SPI2_IRQ == 0)   //24L01收到答题卡上电发送的数据
		{
			IWDG_Feed();
			temp = NRF24L01_RxPacket(SPI_SEND,buffer);

			if((temp&0x40)==0x40)   //接收到的数据显示
			{
				buffer[RX_PLOAD_WIDTH]='\0';

				//DegugPrintf(buffer,7);
				//SendUSART2Str("\n");
				//SendConsoleData(buffer,32);   //回显，返回给PC端
				if((buffer[1]==0)&& (buffer[2]==0)&&(buffer[3]==0))    //未写考勤                    
				{
					ReadAttenID[0] = 0;
					ReadAttenID[1] = 0;
					ReadAttenID[2] = 0;
					ReadAttenID[3] = 0;
					ReadAttenID[4] = 0;
					return 1;
				}
				else
				{
					if((protocol_config_flg == 7)||(protocol_config_flg == 13))
					{
					    ReadAttenID[0] = 0;
					    ReadAttenID[1] = buffer[0];
					    ReadAttenID[2] = buffer[1];
					    ReadAttenID[3] = buffer[2];
					    ReadAttenID[4] = buffer[3];
					}
					else if((protocol_config_flg == 8))
					{
					    ReadAttenID[0] = buffer[1];
					    ReadAttenID[1] = buffer[2];
					    ReadAttenID[2] = buffer[3];
					    ReadAttenID[3] = buffer[4];
					    ReadAttenID[4] = buffer[5];
					}
					else if((protocol_config_flg == 9)||(protocol_config_flg == 14))
					{
					    ReadAttenID[0] = 0;
					    ReadAttenID[1] = (buffer[1]^0x53);
					    ReadAttenID[2] = (buffer[2]^0x43);
					    ReadAttenID[3] = (buffer[3]^0x44);
					    ReadAttenID[4] = (buffer[4]^0x58);

					}
					else if(protocol_config_flg == 20)
					{
						ReadAttenID[0] = 0;
						ReadAttenID[1] = buffer[4];
						ReadAttenID[2] = buffer[5];
						ReadAttenID[3] = buffer[6];
						ReadAttenID[4] = buffer[7];
					}
					else
					{															
						ReadAttenID[0] = 0;
						ReadAttenID[1] = buffer[1];
						ReadAttenID[2] = buffer[2];
						ReadAttenID[3] = buffer[3];
						ReadAttenID[4] = buffer[4];
					}
				    return 1;
				}

			}
		}
		IWDG_Feed();
	}
	else
	{
		ReadAttenID[0] = 0;
		ReadAttenID[1] = 0;
		ReadAttenID[2] = 0;
		ReadAttenID[3] = 0;
		ReadAttenID[4] = 0;
		return 1;
	}
	return 0;
}



void DataProc_USART1(void)
{
//    u8 i;
//    u16 reclen;
//	u32 IdData;
//	u8  IdBuf[10]={0};
    IWDG_Feed();

    if(USART1_RX_CNT==8 && USART1_RX_BUF[0]=='u' && USART1_RX_BUF[1]=='p' && USART1_RX_BUF[2]=='d' && USART1_RX_BUF[3]=='a' && USART1_RX_BUF[4]=='t' && USART1_RX_BUF[5]=='e' )
    {
        USART1_RX_CNT =0;
        NVIC_SystemReset(); //系统复位
    }    

    if(USART1_RX_BUF[0]!='u' && USART1_RX_BUF[0]!=0xFA)
    {
        USART1_RX_CNT =0;
        return;
    }
    
    if(USART1_RX_CNT < 14)
    {
        return;
    }
    if(USART1_RX_BUF[USART1_RX_CNT-1] != 0xFB)
    {
        return;
    }
    if(USART1_RX_BUF[10]==0xA0 && USART1_RX_CNT != 21)
    {
        return;
    }
    
    if(USART1_RX_CNT == 14)       //接收完一条指令标志
    {
        
        if(USART1_RX_BUF[0]==0xFA && USART1_RX_BUF[13]==0xFB)//包头包尾检测
        {
            USART1_REC_Status |= 0x8000;
            USART1_RX_CNT = 0;
//            SendUartData(USART1_RX_BUF,14,USART2);
            DegugPrintf(USART1_RX_BUF,14);
            SendUSART2Str("\n");
        }
        else
        {
            USART1_RX_CNT = 0;
            USART1_REC_Status = 0;  //接收长度正确但包头包尾检测没通过
        }
    }
    else if(USART1_RX_CNT == 21)
    {

        if(USART1_RX_BUF[0]==0xFA && USART1_RX_BUF[20]==0xFB)//包头包尾检测
        {
            USART1_REC_Status |= 0x8000;
            USART1_RX_CNT = 0;
//            SendUartData(USART1_RX_BUF,21,USART2);
            DegugPrintf(USART1_RX_BUF,21);
            SendUSART2Str("\n");
        }
        else
        {
            USART1_RX_CNT = 0;
            USART1_REC_Status = 0;  //接收长度正确但包头包尾检测没通过
        }
    }
    else if(USART1_RX_CNT == 17)
    {
        if(USART1_RX_BUF[0]==0xFA && USART1_RX_BUF[16]==0xFB)//包头包尾检测
        {
            USART1_REC_Status |= 0x8000;
            USART1_RX_CNT = 0;
//            SendUartData(USART1_RX_BUF,21,USART2);
            DegugPrintf(USART1_RX_BUF,17);
            SendUSART2Str("\n");
        }
        else
        {
            USART1_RX_CNT = 0;
            USART1_REC_Status = 0;  //接收长度正确但包头包尾检测没通过
        }  
    }



    if(USART1_REC_Status & 0x8000)                  //接收一帧数据完成
    {
//        DegugPrintf(USART1_RX_BUF,strlen(USART1_RX_BUF));
//        printf("len=%d\n",USART1_RX_CNT);
		

        IWDG_Feed();
//        printf("Rec = %d\r\n",USART1_RX_CNT);
        ReceiveBuf->SequenceID[0] = USART1_RX_BUF[6];
        ReceiveBuf->SequenceID[1] = USART1_RX_BUF[7];
        ReceiveBuf->SequenceID[2] = USART1_RX_BUF[8];
        ReceiveBuf->SequenceID[3] = USART1_RX_BUF[9];
        
        ReceiveBuf->Command = USART1_RX_BUF[10];    //命令
//        ReceiveBuf->Tag     = USART1_RX_BUF[11];    //Tag 命令类型
//        ReceiveBuf->Length  = USART1_RX_BUF[12];    //数据长度        
        
        SendBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
        SendBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
        SendBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
        SendBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
        
        //USART1_REC_Status = 0;
        //USART1_RX_CNT =0;
        
        
        switch(ReceiveBuf->Command)
        {
            case Reset:                     //复位
//                printf("Reset\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;
                
            case GetFirmversion:            //获取固件版本
//                printf("GetFirmversion\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;
                
            case SetReaderID:            //设置阅读器ID
//                printf("SetReaderID\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;
                
            case GetReaderID:            //获取阅读器ID
//                printf("GetReaderID\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case DelWhite:            //删除白名单
//                printf("DelWhite\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case OpenWhite:            //打开白名单
//                printf("OpenWhite\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case CloseWhite:            //关闭白名单
//                printf("CloseWhite\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case AddWhite:            //添加白名单
//                printf("AddWhite\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case GetWhite:            //获取白名单
//                printf("GetWhite\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case Heart:            //心跳
//                printf("Heart\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case GetWhiteState:            //获取白名单状态
//                printf("GetWhiteState\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case WriteNum:            //写号 A0
                //FA 00 00 00 00 00 00 00 00 00 A0 01 05 00 01 02 03 04 00 00 FB --21byte
//                printf("写 2.4G 卡号\n");                
//                DegugPrintf(USART1_RX_BUF,21);
//                SendUSART2Str("\n");
            {   
                u8 tmp;
                //数据处理
                USART1_RX_CNT = 0;
                WrNumOkBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
                WrNumOkBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
                WrNumOkBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
                WrNumOkBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
                
                WrNumOkBuf->Command = ReceiveBuf->Command;
                
                ReceiveBuf->Value[0] = USART1_RX_BUF[13];
                ReceiveBuf->Value[1] = USART1_RX_BUF[14];
                ReceiveBuf->Value[2] = USART1_RX_BUF[15];
                ReceiveBuf->Value[3] = USART1_RX_BUF[16];
                ReceiveBuf->Value[4] = USART1_RX_BUF[17];

                idBuf[0] = USART1_RX_BUF[13];
                idBuf[1] = USART1_RX_BUF[14];
                idBuf[2] = USART1_RX_BUF[15];
                idBuf[3] = USART1_RX_BUF[16];
                idBuf[4] = USART1_RX_BUF[17];
    
                newIdFlag = 1;  //2.4G写号
                count = 0;      //计时器计数清零
                NRF24L01_RX_Mode(SPI_SEND);
                
                SendUSART2Str("0000\n");
                while(1)   //
                {
                    
                    tmp = DataProc_A0_WriteNum_SPI2();
                    
                    if(tmp)
                        break;
                }
                SendUSART2Str("0000\n");
                
                ReadAttenID[0]=0;
                ReadAttenID[1]=0;
                ReadAttenID[2]=0;
                ReadAttenID[3]=0;
                ReadAttenID[4]=0;
                ResNum_1356[0]=0;
                ResNum_1356[1]=0;
                ResNum_1356[2]=0;
                ResNum_1356[3]=0;
                ResNum_1356[4]=0;
                GetNum_1356[0]=0;
                GetNum_1356[1]=0;
                GetNum_1356[2]=0;
                GetNum_1356[3]=0;
                GetNum_1356[4]=0;
                    
//                USART1_REC_Status = 0;
//                USART1_RX_CNT =0;
//                for(i=0;i<30;i++)
//                {
//                    USART3_RX_BUF[i] = 0;
//                }
                ReceiveBuf->Command = 0;    //清零
            }
            
                
                break;

            case WriteNumState:             //写号结果上报

                ReceiveBuf->Command = 0;    //清零
                break;

            case Read_1345Num:            //读13.45M卡号 A2
                    //FA 00 00 00 00 00 00 00 00 00 A2 00 00 FB
					if(first_clear_flag == 0)
					{
						memset(ResNum_1356,0,5);
						//memset(oldResNum_1356,0,5);
						first_clear_flag = 1;
					}
                    if((ResNum_1356[1] == 0) && (ResNum_1356[2] == 0)&& (ResNum_1356[3] == 0)&&(ResNum_1356[4] ==0))
                    {
						           break;
                    }
					          //ledL;
                    USART1_RX_CNT = 0;
                    ReadNumBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
                    ReadNumBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
                    ReadNumBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
                    ReadNumBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
                    
                    ReadNumBuf->Command  = ReceiveBuf->Command;
                    ReadNumBuf->Tag1      = 1;
                    ReadNumBuf->Length1   = 6;
                    ReadNumBuf->Value1[0] = 0;      //len=0 无效
                    ReadNumBuf->Value1[1] = ResNum_1356[0];
                    ReadNumBuf->Value1[2] = ResNum_1356[1];
                    ReadNumBuf->Value1[3] = ResNum_1356[2];
                    ReadNumBuf->Value1[4] = ResNum_1356[3];
                    ReadNumBuf->Value1[5] = ResNum_1356[4];

//                    SendUartData(uartSendRdNumData,22,USART2);
                    
                    SendConsoleData((char*)uartSendRdNumData,22);   //回显，返回给PC端
                    delay_ms(50);

                    DegugPrintf(uartSendRdNumData,22);
                    SendUSART2Str("\n");
										//ledH;                    
                    ResNum_1356[0]=0;
                    ResNum_1356[1]=0;
                    ResNum_1356[2]=0;
                    ResNum_1356[3]=0;
                    ResNum_1356[4]=0;
//                    USART1_REC_Status = 0;
//                    USART1_RX_CNT =0;
//                    for(i=0;i<30;i++)
//                    {
//                        USART3_RX_BUF[i] = 0;
//                    }
										first_clear_flag = 0;

                    ReceiveBuf->Command = 0;    //清零
                   // ledH;
                break;
                
            case Read_24Num:  //A3 读一次2.4G卡号
                //FA 00 00 00 00 00 00 00 00 00 A3 00 00 FB
/*                        USART1_RX_CNT = 0;
                        NRF24L01_RX_Mode(SPI_SEND);
                        count_A34 = 0;
                        rspFlag_A3 = 1;

                    {
                        u8 tmp;
                        SendUSART2Str("3333\n");
                        while(1)   //
                        {
                            
                            tmp = DataProc_A3_nrf();
                            
                            if(tmp)
                                break;
                        }
                        SendUSART2Str("3333\n");
                        ReadNumBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
                        ReadNumBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
                        ReadNumBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
                        ReadNumBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
                        
                        ReadNumBuf->Command  = ReceiveBuf->Command;
                        ReadNumBuf->Tag1      = 1;
                        ReadNumBuf->Length1   = 6;
                        ReadNumBuf->Value1[0] = 0;      //len=0 无效
                        ReadNumBuf->Value1[1] = idResA3Buf[0];
                        ReadNumBuf->Value1[2] = idResA3Buf[1];
                        ReadNumBuf->Value1[3] = idResA3Buf[2];
                        ReadNumBuf->Value1[4] = idResA3Buf[3];
                        ReadNumBuf->Value1[5] = idResA3Buf[4];
                        
                            
                        DegugPrintf(uartSendRdNumData,22);
                        SendUSART2Str("\n");
                        
                        SendConsoleData(uartSendRdNumData,22);   //回显，返回给PC端
                        delay_ms(50);

                            
                        //写号清零防止对下一次写号造成影响
                        ReadAttenID[0]=0;
                        ReadAttenID[1]=0;
                        ReadAttenID[2]=0;
                        ReadAttenID[3]=0;
                        ReadAttenID[4]=0;
                        ResNum_1356[0]=0;
                        ResNum_1356[1]=0;
                        ResNum_1356[2]=0;
                        ResNum_1356[3]=0;
                        ResNum_1356[4]=0;
                        GetNum_1356[0]=0;
                        GetNum_1356[1]=0;
                        GetNum_1356[2]=0;
                        GetNum_1356[3]=0;
                        GetNum_1356[4]=0;  

//                        USART1_REC_Status = 0;
//                        USART1_RX_CNT =0;
//                        for(i=0;i<30;i++)
//                        {
//                            USART3_RX_BUF[i] = 0;
//                        }
                        ReceiveBuf->Command = 0;    //清零
                        
                    }
                
  */              break;
                
            case ReadCardNum:   //A4 读考勤
                    USART1_RX_CNT = 0;
                    //SendConsoleByte(protocol_config_flg);   //回显，返回给PC端
                    //NRF24L01_RX_Mode_NOACK(SPI_SEND);//NOACK模式，读江西考勤
                    #if APP
                    NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//NOACK??,?????
                    #else
                    NRF24L01_RX_Mode_NOACK(SPI_SEND);////NOACK模式，读江西考勤
                    #endif
                        count_A34 = 0;
                    {
                        u8 tmp;
                        SendUSART2Str("4444\n");
                        while(1)   //
                        {
                            
                            tmp = DataProc_A4_nrf();
                            
                            if(tmp)
                                break;
                        }
																						
                        SendUSART2Str("4444\n");
                        ReadNumBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
                        ReadNumBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
                        ReadNumBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
                        ReadNumBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID
                        
                        ReadNumBuf->Command  = ReceiveBuf->Command;
                        ReadNumBuf->Tag1      = 1;
                        ReadNumBuf->Length1   = 6;
                        ReadNumBuf->Value1[0] = 0;      //
                        ReadNumBuf->Value1[1] = ReadAttenID[0];
                        ReadNumBuf->Value1[2] = ReadAttenID[1];
                        ReadNumBuf->Value1[3] = ReadAttenID[2];
                        ReadNumBuf->Value1[4] = ReadAttenID[3];
                        ReadNumBuf->Value1[5] = ReadAttenID[4];
 
                            
                        SendConsoleData((char*)uartSendRdNumData,22);   //回显，返回给PC端
                        delay_ms(50);

                        DegugPrintf(uartSendRdNumData,22);
                        SendUSART2Str("\n");
                            
                        //写号清零防止对下一次写号造成影响
                        ReadAttenID[0]=0;
                        ReadAttenID[1]=0;
                        ReadAttenID[2]=0;
                        ReadAttenID[3]=0;
                        ReadAttenID[4]=0;
                        ResNum_1356[0]=0;
                        ResNum_1356[1]=0;
                        ResNum_1356[2]=0;
                        ResNum_1356[3]=0;
                        ResNum_1356[4]=0;
                        GetNum_1356[0]=0;
                        GetNum_1356[1]=0;
                        GetNum_1356[2]=0;
                        GetNum_1356[3]=0;
                        GetNum_1356[4]=0;
                        //memset(ResNum_1356,0,5);
                        
                        ReceiveBuf->Command = 0;    //
                        
                    }
                break;
#if APP1
			case Config24Agreement:
                
				protocol_config_flg = USART1_RX_BUF[13];
                ConfigKaoQinBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
                ConfigKaoQinBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
                ConfigKaoQinBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
                ConfigKaoQinBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID
                ConfigKaoQinBuf->Command = ReceiveBuf->Command;
				ConfigKaoQinBuf->Control =0x01;
                SendConsoleData((char*)uartSendConfigKaoQinData,14);   //回显，返回给PC端
                delay_ms(50);
#if 1
                StoreBuffer[0] = protocol_config_flg;//修改协议版本
                SavePara_to_flash();    //保持数据到Flash
                //Readpara_from_flash();  //读出数据并打印出来
                //protocol_config_flg = Save_Buffer[2*0];
#endif 
                //SendConsoleByte(protocol_config_flg);   //回显，返回给PC端
                //SendConsoleByte(0xff);
                //SendConsoleByte(protocol_config_flg);
                //写号清零防止对下一次写号造成影响
                ReadAttenID[0]=0;
                ReadAttenID[1]=0;
                ReadAttenID[2]=0;
                ReadAttenID[3]=0;
                ReadAttenID[4]=0;
                ResNum_1356[0]=0;
                ResNum_1356[1]=0;
                ResNum_1356[2]=0;
                ResNum_1356[3]=0;
                ResNum_1356[4]=0;
                GetNum_1356[0]=0;
                GetNum_1356[1]=0;
                GetNum_1356[2]=0;
                GetNum_1356[3]=0;
                GetNum_1356[4]=0;
                ReceiveBuf->Command = 0;    //??
            
				break;
#endif
            case UpdateFirmversion:            //升级固件
//                printf("UpdateFirmversion\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case UpdateFirm:            //升级固件开关
//                printf("UpdateFirm\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            case UpdateFirmState:            //升级固件结果上报
//                printf("UpdateFirmState\n");
                //数据处理
                
                ReceiveBuf->Command = 0;    //清零
                break;

            default:
                break;
        }


        
        USART1_REC_Status = 0;                      //参数清零
        //USART1_RX_CNT = 0;

    }
    
}

void DataProc_Nrf24l01(void)
{
    u8 buffer[32];
    u8 temp;
    IWDG_Feed();
    
    if(newIdFlag==1)
    {
        newIdFlag = 0;
        recFlag = 0;
        txBuf[0] = 0xfc;
        txBuf[1] = 0x11;
        if(idBuf[0]==GetNum_1356[0] && idBuf[1]==GetNum_1356[1] && idBuf[2]==GetNum_1356[2] && idBuf[3]==GetNum_1356[3] && idBuf[4]==GetNum_1356[4])
        {
            txBuf[2] = idBuf[0];
            txBuf[3] = idBuf[1];
            txBuf[4] = idBuf[2];
            txBuf[5] = idBuf[3];
            txBuf[6] = idBuf[4];
        }
        
        txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
		SPI1_CE =0;
        NRF24L01_Write_Reg(SPI_REV,FLUSH_TX,0xff);//清除TX FIFO寄存器 
		NRF24L01_Write_Buf(SPI_REV,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF 8个字节，自动发送
		NRF24L01_RX_Mode(SPI_REV);      //设置24L01为接收模式
        rspFlag = 1;
        count=0;
        IWDG_Feed();
        
    }
    

    
    if(rspFlag) //处理答题卡上电发送的数据
    {
        IWDG_Feed();
        if(count<=2000) //1.5S内未收到2.4G回复信号，则rspFlag失效
        {
            if(SPI1_IRQ == 0)   //24L01收到答题卡上电发送的数据
            {
                IWDG_Feed();
                temp = NRF24L01_RxPacket(SPI_REV,buffer);
//                SendConsoleByte(temp);

                if((temp&0x40)==0x40)   //接收到的数据显示
                {
                    buffer[RX_PLOAD_WIDTH]='\0';
                    led0=~led0;

//                    SendConsoleData(buffer,RX_PLOAD_WIDTH); //

                    if((buffer[1]==idBuf[0])&& (buffer[2]==idBuf[1])&&(buffer[3]==idBuf[2])&&(buffer[4]==idBuf[3])&&(buffer[5]==idBuf[4]))    //收到改写后编号
                    {
                        if((buffer[1]==GetNum_1356[0])&& (buffer[2]==GetNum_1356[1])&&(buffer[3]==GetNum_1356[2])&&(buffer[4]==GetNum_1356[3])&&(buffer[5]==GetNum_1356[4]))
                        {
                            if(rspFlag == 1)
                            {
                                rspFlag++;
                                txBuf[1] = 0x12;             ///0xfc + cmd(0x12) +id +crc   ----cmd=0x12 确认
                                txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
                                NRF24L01_Write_Buf(SPI_REV,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF 8个字节，自动发送
                            }
                            else
                            {
                                rspFlag=0;  //如果ID已经是最新，rspFlag清零，不处理接收数据
                                count = 0;  //计数清零
                                idBuf[0]=0;
                                WrNumOkBuf->Control = 0x10; //写号完成
                                SendConsoleData((char*)uartSendWrNumOkData,14);   //回显，返回给PC端
                                
                            }
                        }
                        
                        
                    }
                    else
                    {
                        SPI1_CE =0;
                        txBuf[1] = 0x11;
                        txBuf[7] = CheckSum(txBuf,7);   //写号到txBuf
                        NRF24L01_Write_Buf(SPI_REV,W_ACK_PAYLOAD,txBuf,8);   //写数据到TX BUF  8个字节
                        SPI1_CE =1;
                    }
                }
            }
            
        }
        else    //2s超时回复
        {
            IWDG_Feed();
            rspFlag=0;  //如果ID已经是最新，rspFlag清零，不处理接收数据
            count = 0;  //计数清零
            
            idBuf[0]=0;
            idBuf[1]=0;
            idBuf[2]=0;
            idBuf[3]=0;
            idBuf[4]=0;
            GetNum_1356[0]=0;
            GetNum_1356[1]=0;
            GetNum_1356[2]=0;
            GetNum_1356[3]=0;
            GetNum_1356[4]=0;
            
            WrNumOkBuf->Control = 0x30; //错误
            SendConsoleData((char*)uartSendWrNumOkData,14);   //回显，返回给PC端
        }
        
    }
}

 //------------------------------------------------------------------
//函数名：void TIM3_IRQHandler(void)
//输入参数：null
//返回参数：null
//说明：定时器3中断服务
//------------------------------------------------------------------
void TIM3_IRQHandler(void)
{ 
  if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)        //判断是否为定时器3溢出中断
  {
	 TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清中断标记
	 count ++;
     count_A34 ++;
     count_num ++;
     if(HW_Open)
     {
        GPIO_SetBits(GPIOC,GPIO_Pin_9);   //打开红外扫描枪
        HW_OpenCount ++;
        if(HW_OpenCount>=400)
        {
            GPIO_ResetBits(GPIOC,GPIO_Pin_9);   //关闭红外扫描枪
            HW_OpenCount = 0;
            HW_Open = 0;
        }

     }
     if(count_num==5000)
     {
        count_num = 0;
//        printf("1356_0=%x\r\n",GetNum_1356[0]);
//        printf("1356_1=%x\r\n",GetNum_1356[1]);
//        printf("1356_2=%x\r\n",GetNum_1356[2]);
//        printf("1356_3=%x\r\n",GetNum_1356[3]);
//        printf("1356_4=%x\r\n",GetNum_1356[4]);
//        
//        printf("2.4G_0=%x\r\n",idBuf[0]);
//        printf("2.4G_1=%x\r\n",idBuf[1]);
//        printf("2.4G_2=%x\r\n",idBuf[2]);
//        printf("2.4G_3=%x\r\n",idBuf[3]);
//        printf("2.4G_4=%x\r\n",idBuf[4]);
     }
	 flag_1ms = 1;
  }
}
