Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1664 lines
52KB

  1. #include "DataProc.h"
  2. #include <string.h>
  3. #include "printf.h"
  4. #include "flash.h"
  5. #include "LCD.h"
  6. //SPIType SPI_Using = SPI_PORT1; //选择一个SPI接口
  7. extern void IWDG_Feed(void);
  8. u8 CheckData_1356[10] = {0xD1,0xD1,0xE0,0xD0,0x00,0x00,0x00,0x00,0xD2,0xD2}; //13.56M接收数据
  9. u8 GetNum_1356[5] = {0x00,0x00,0x00,0x00,0x00}; //13.56M缓存
  10. static u8 ResNum_1356[5]; //13.56M缓存,用于回复A2
  11. static u8 oldResNum_1356[5];
  12. u8 uartSendData[100]; //最大包100个字节
  13. u8 uartRecData[100]; //最大包100个字节
  14. u8 uartSendRdNumData[100];
  15. u8 uartSendWrNumOkData[100];
  16. u8 uartSendConfigKaoQinData[100];
  17. volatile u16 count;
  18. u16 count_num;
  19. u8 flag_1ms;
  20. u8 first_clear_flag;
  21. u8 HW_Open;
  22. u16 HW_OpenCount;
  23. u8 Flag_update;
  24. u8 WriteOKFlag=0;
  25. u8 protocol_config_flg;
  26. struct Protocol_send_data //通讯协议 发送
  27. {
  28. u8 HeadFix;
  29. u8 Control;
  30. u8 Magic[4];
  31. u8 SequenceID[4];
  32. u8 Command;
  33. u8 Tag;
  34. u8 Length;
  35. u8 Value[6]; //最大值是6
  36. u8 FootCRC[2]; //CRC 暂时固定为00 00
  37. u8 FootFix; //固定为0xFB
  38. };
  39. struct Protocol_rec_data //通讯协议 接收
  40. {
  41. u8 HeadFix;
  42. u8 Control;
  43. u8 Magic[4];
  44. u8 SequenceID[4];
  45. u8 Command;
  46. u8 Tag;
  47. u8 Length;
  48. u8 Value[62]; //最大值是62
  49. u8 FootCRC[2]; //CRC 暂时固定为00 00
  50. u8 FootFix; //固定为0xFB
  51. };
  52. //struct ReadNum_send_data //读号回复指令,长度 30Byte
  53. //{
  54. // u8 HeadFix;
  55. // u8 Control;
  56. // u8 Magic[4];
  57. // u8 SequenceID[4];
  58. // u8 Command;
  59. // u8 Tag1;
  60. // u8 Length1;
  61. // u8 Value1[6]; //最大值是6
  62. // u8 Tag2;
  63. // u8 Length2;
  64. // u8 Value2[6]; //最大值是6
  65. // u8 FootCRC[2]; //CRC 暂时固定为00 00
  66. // u8 FootFix; //固定为0xFB
  67. //};
  68. struct ReadNum_send_data //读号回复指令,长度 22Byte
  69. {
  70. u8 HeadFix;
  71. u8 Control;
  72. u8 Magic[4];
  73. u8 SequenceID[4];
  74. u8 Command;
  75. u8 Tag1;
  76. u8 Length1;
  77. u8 Value1[6]; //最大值是6
  78. u8 FootCRC[2]; //CRC 暂时固定为00 00
  79. u8 FootFix; //固定为0xFB
  80. };
  81. struct WrNumOk_send_data //写卡成功回复指令
  82. {
  83. u8 HeadFix;
  84. u8 Control;
  85. u8 Magic[4];
  86. u8 SequenceID[4];
  87. u8 Command;
  88. u8 FootCRC[2]; //CRC 暂时固定为00 00
  89. u8 FootFix; //固定为0xFB
  90. };
  91. struct ConfigKaoQin_send_data
  92. {
  93. u8 HeadFix; //固定为0xFA
  94. u8 Control; // 1表示成功
  95. u8 Magic[4];
  96. u8 SequenceID[4];
  97. u8 Command; // A8
  98. u8 FootCRC[2]; //CRC 暂时固定为00 00
  99. u8 FootFix; //固定为0xFB
  100. };
  101. #define ReceiveBuf ((struct Protocol_rec_data *)&uartRecData[0])
  102. #define SendBuf ((struct Protocol_send_data *)&uartSendData[0])
  103. #define ConfigKaoQinBuf ((struct ConfigKaoQin_send_data *)&uartSendConfigKaoQinData[0])
  104. #define WrNumOkBuf ((struct WrNumOk_send_data *)&uartSendWrNumOkData[0])
  105. #define ReadNumBuf ((struct ReadNum_send_data *)&uartSendRdNumData[0])
  106. #define CndRspBufferSize 100
  107. unsigned char CmdRspBuffer[CndRspBufferSize];
  108. unsigned char CmdRspBufferWr_Rx = 0;
  109. u8 idBuf[5]; //2.4G ID缓存空间
  110. u16 count_A34;
  111. u8 idResA3Buf[5]; //2.4G ID缓存,用于回复A3
  112. u8 ReadAttenID[5]; //2.4G 考勤卡ID
  113. u8 newIdFlag; //写2.4G ID标志
  114. u8 recFlag;
  115. u8 rspFlag_A3;
  116. u8 read1356flag;
  117. unsigned char txBuf[32]="123456789123456789";
  118. unsigned char txA3Buf[32]="123456789123456789";
  119. unsigned char rspFlag = 0; //是否处理答题卡上电2.4G信息 标志
  120. unsigned char wrNumFlag = 0;//写号是否完成标志
  121. /****************************************************
  122. 功能: 校验和
  123. 方法: sum = ~(data0+...+datan)+1;
  124. *****************************************************/
  125. u8 CheckSum(u8 *buf,u8 len)
  126. {
  127. u8 sum = 0;
  128. u8 i;
  129. for(i = 0;i < len; i++)
  130. {
  131. sum += buf[i];
  132. }
  133. sum = ~sum;
  134. sum += 1;
  135. return sum;
  136. }
  137. //void AT_AddNewRxByte(u8 c)
  138. //{
  139. // if (c != 0x0D)
  140. // {
  141. // if (c < 32) return; // ignore control charcters
  142. // if (CmdRspBufferWr_Rx >= (CndRspBufferSize - 1)) return; // buffer overflow - ignore character
  143. // CmdRspBuffer[CmdRspBufferWr_Rx++] = c;
  144. // CmdRspBuffer[CmdRspBufferWr_Rx] = 0;
  145. // return;
  146. // }
  147. // //----------------------------------------------------
  148. // // ID=XXXXX
  149. // if((CmdRspBuffer[0] == 'I')&&(CmdRspBuffer[1] == 'D')&&(CmdRspBuffer[2] == '='))
  150. // {
  151. // memcpy(idBuf,&CmdRspBuffer[3],5);
  152. // SendConsoleStr("id=");
  153. // SendConsoleData(idBuf,5);
  154. // SendConsoleStr("\n");
  155. // newIdFlag = 1;
  156. // }
  157. // else if((CmdRspBuffer[0] == 'I')&&(CmdRspBuffer[1] == 'D')&&(CmdRspBuffer[2] == '?'))
  158. // {
  159. // SendConsoleStr("id=");
  160. // SendConsoleData(idBuf,5);
  161. // SendConsoleStr("\n");
  162. // }
  163. // memset(CmdRspBuffer,0,sizeof(CmdRspBuffer));
  164. // CmdRspBufferWr_Rx = 0;
  165. //}
  166. void Data_Init(void) //包数据初始化
  167. {
  168. SendBuf->HeadFix = 0xFA; //发送缓冲区初始化
  169. SendBuf->Control = 0x10;
  170. SendBuf->Magic[0]= 0;
  171. SendBuf->Magic[1]= 0;
  172. SendBuf->Magic[2]= 0;
  173. SendBuf->Magic[3]= 0;
  174. SendBuf->SequenceID[0] = 0;
  175. SendBuf->SequenceID[1] = 0;
  176. SendBuf->SequenceID[2] = 0;
  177. SendBuf->SequenceID[3] = 0;
  178. SendBuf->FootCRC[0] = 0;
  179. SendBuf->FootCRC[1] = 0;
  180. SendBuf->FootFix = 0xFB;
  181. ReceiveBuf->HeadFix = 0xFA; //接收缓冲区初始化
  182. ReceiveBuf->Control = 0x10;
  183. ReceiveBuf->Magic[0]= 0;
  184. ReceiveBuf->Magic[1]= 0;
  185. ReceiveBuf->Magic[2]= 0;
  186. ReceiveBuf->Magic[3]= 0;
  187. ReceiveBuf->SequenceID[0] = 0;
  188. ReceiveBuf->SequenceID[1] = 0;
  189. ReceiveBuf->SequenceID[2] = 0;
  190. ReceiveBuf->SequenceID[3] = 0;
  191. ReceiveBuf->FootCRC[0] = 0;
  192. ReceiveBuf->FootCRC[1] = 0;
  193. ReceiveBuf->FootFix = 0xFB;
  194. ConfigKaoQinBuf->HeadFix = 0xFA; //接收缓冲区初始化
  195. ConfigKaoQinBuf->Control = 0x10;
  196. ConfigKaoQinBuf->Magic[0]= 0;
  197. ConfigKaoQinBuf->Magic[1]= 0;
  198. ConfigKaoQinBuf->Magic[2]= 0;
  199. ConfigKaoQinBuf->Magic[3]= 0;
  200. ConfigKaoQinBuf->SequenceID[0] = 0;
  201. ConfigKaoQinBuf->SequenceID[1] = 0;
  202. ConfigKaoQinBuf->SequenceID[2] = 0;
  203. ConfigKaoQinBuf->SequenceID[3] = 0;
  204. ConfigKaoQinBuf->FootCRC[0] = 0;
  205. ConfigKaoQinBuf->FootCRC[1] = 0;
  206. ConfigKaoQinBuf->FootFix = 0xFB;
  207. ReadNumBuf->HeadFix = 0xFA; //接收缓冲区初始化
  208. ReadNumBuf->Control = 0x10;
  209. ReadNumBuf->Magic[0]= 0;
  210. ReadNumBuf->Magic[1]= 0;
  211. ReadNumBuf->Magic[2]= 0;
  212. ReadNumBuf->Magic[3]= 0;
  213. ReadNumBuf->SequenceID[0] = 0;
  214. ReadNumBuf->SequenceID[1] = 0;
  215. ReadNumBuf->SequenceID[2] = 0;
  216. ReadNumBuf->SequenceID[3] = 0;
  217. ReadNumBuf->FootCRC[0] = 0;
  218. ReadNumBuf->FootCRC[1] = 0;
  219. ReadNumBuf->FootFix = 0xFB;
  220. WrNumOkBuf->HeadFix = 0xFA; //接收缓冲区初始化
  221. WrNumOkBuf->Control = 0x10;
  222. WrNumOkBuf->Magic[0]= 0;
  223. WrNumOkBuf->Magic[1]= 0;
  224. WrNumOkBuf->Magic[2]= 0;
  225. WrNumOkBuf->Magic[3]= 0;
  226. WrNumOkBuf->SequenceID[0] = 0;
  227. WrNumOkBuf->SequenceID[1] = 0;
  228. WrNumOkBuf->SequenceID[2] = 0;
  229. WrNumOkBuf->SequenceID[3] = 0;
  230. WrNumOkBuf->FootCRC[0] = 0;
  231. WrNumOkBuf->FootCRC[1] = 0;
  232. WrNumOkBuf->FootFix = 0xFB;
  233. }
  234. //void Usart1_Process(void)
  235. //{
  236. //
  237. // if(USART1_RX_CNT==10 && USART1_RX_BUF[0]==0xd1 && USART1_RX_BUF[9]==0xd2)
  238. // {
  239. // USART1_REC_Status |= 0x8000; //收到ID设置命令,十进制10位ID
  240. // }
  241. //
  242. //
  243. // if(USART1_REC_Status & 0x8000)
  244. // {
  245. //// memcpy(idBuf,&USART1_RX_BUF[3],5); //把5位十进制ID号写到idBuf
  246. // idBuf[0]=0;
  247. // idBuf[1]=USART1_RX_BUF[4];
  248. // idBuf[2]=USART1_RX_BUF[5];
  249. // idBuf[3]=USART1_RX_BUF[6];
  250. // idBuf[4]=USART1_RX_BUF[7]; //串口收到的ID号写入到idBuf缓冲区
  251. //
  252. // SendConsoleStr("id=");
  253. // SendConsoleData(idBuf,5);
  254. // SendConsoleStr("\n");
  255. // newIdFlag = 1;
  256. //
  257. // USART1_RX_CNT = 0;
  258. // USART1_REC_Status = 0;
  259. // }
  260. //}
  261. void DataProc_1356(void)
  262. {
  263. u32 dat1,dat2,dat3,dat4;
  264. u8 IdBuf[10]={0};
  265. IWDG_Feed();
  266. if(USART3_RX_CNT==10)
  267. {
  268. if(USART3_RX_BUF[0]==CheckData_1356[0] && USART3_RX_BUF[1]==CheckData_1356[1]
  269. && USART3_RX_BUF[8]==CheckData_1356[8] && USART3_RX_BUF[9]==CheckData_1356[9])
  270. {
  271. USART3_REC_Status |= 0x8000; //串口3完成接收一帧数据
  272. }
  273. else
  274. {
  275. memset(USART3_RX_BUF,0,20);
  276. }
  277. }
  278. if(USART3_REC_Status & 0x8000)
  279. {
  280. //写号清零防止对下一次写号造成影响
  281. //delay_ms(1);
  282. #if 0
  283. idBuf[0]=0;
  284. idBuf[1]=0;
  285. idBuf[2]=0;
  286. idBuf[3]=0;
  287. idBuf[4]=0;
  288. GetNum_1356[0]=0;
  289. GetNum_1356[1]=0;
  290. GetNum_1356[2]=0;
  291. GetNum_1356[3]=0;
  292. GetNum_1356[4]=0;
  293. idResA3Buf[0]=0;
  294. idResA3Buf[1]=0;
  295. idResA3Buf[2]=0;
  296. idResA3Buf[3]=0;
  297. idResA3Buf[4]=0;
  298. ReadAttenID[0]=0;
  299. ReadAttenID[1]=0;
  300. ReadAttenID[2]=0;
  301. ReadAttenID[3]=0;
  302. ReadAttenID[4]=0;
  303. #else
  304. memset(idBuf,0,5);
  305. memset(GetNum_1356,0,5);
  306. memset(idResA3Buf,0,5);
  307. memset(ReadAttenID,0,5);
  308. #endif
  309. // SendConsoleData(USART3_RX_BUF,10);
  310. // DegugPrintf(USART3_RX_BUF,10);
  311. // SendConsoleStr("\n");
  312. //IWDG_Feed();
  313. GetNum_1356[0]=0;
  314. GetNum_1356[1]=USART3_RX_BUF[4];
  315. GetNum_1356[2]=USART3_RX_BUF[5];
  316. GetNum_1356[3]=USART3_RX_BUF[6];
  317. GetNum_1356[4]=USART3_RX_BUF[7]; //串口收到的ID号写入到idBuf缓冲区
  318. ResNum_1356[0]=0;
  319. ResNum_1356[1]=GetNum_1356[1];
  320. ResNum_1356[2]=GetNum_1356[2];
  321. ResNum_1356[3]=GetNum_1356[3];
  322. ResNum_1356[4]=GetNum_1356[4];
  323. // NRF24L01_RX_Mode(SPI_SEND); //
  324. dat1 = (GetNum_1356[1]<<24);
  325. dat2 = (GetNum_1356[2]<<16);
  326. dat3 = (GetNum_1356[3]<<8);
  327. dat4 = dat1 + dat2 + dat3 + GetNum_1356[4];
  328. #if LCD
  329. HexToDec(dat4,IdBuf,sizeof(IdBuf));
  330. vDispClearLine(2);
  331. vDispStr(2,1,"13.56:",6);
  332. vDispStr(2,7,(char*)IdBuf,10);
  333. #endif
  334. read1356flag = 1;
  335. // printf("1356_0=%x\r\n",GetNum_1356[0]);
  336. // printf("1356_1=%x\r\n",GetNum_1356[1]);
  337. // printf("1356_2=%x\r\n",GetNum_1356[2]);
  338. // printf("1356_3=%x\r\n",GetNum_1356[3]);
  339. // printf("1356_4=%x\r\n",GetNum_1356[4]);
  340. // printf("\r\n读到13.56M卡号:%u\r\n",dat4);
  341. IWDG_Feed();
  342. // idBuf[0]=GetNum_1356[0];
  343. // idBuf[1]=GetNum_1356[1];
  344. // idBuf[2]=GetNum_1356[2];
  345. // idBuf[3]=GetNum_1356[3];
  346. // idBuf[4]=GetNum_1356[4];
  347. // delay_ms(100);delay_ms(100);
  348. //HW_Open = 1; //开关红外扫描枪
  349. // newIdFlag = 1; //2.4G开始写号
  350. // count = 0;
  351. //IWDG_Feed();
  352. USART3_RX_CNT = 0; //
  353. USART3_REC_Status = 0;
  354. memset(USART3_RX_BUF,0,20);
  355. }
  356. }
  357. #if 0
  358. void DataProc_RecNrf24L01(void)
  359. {
  360. u8 buffer[7];
  361. u8 temp;
  362. u32 dat1,dat2,dat3,dat4;
  363. IWDG_Feed();
  364. // if(WriteOKFlag==1)
  365. // {
  366. if(SPI2_IRQ == 0)
  367. {
  368. IWDG_Feed();
  369. temp = NRF24L01_RxPacket(SPI_SEND,buffer);
  370. // printf("temp=%x\r\n",temp);
  371. // SendConsoleData(buffer,RX_PLOAD_WIDTH);
  372. idBuf[0] = 0;
  373. idBuf[1] = buffer[1];
  374. idBuf[2] = buffer[2];
  375. idBuf[3] = buffer[3];
  376. idBuf[4] = buffer[4];
  377. // 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])
  378. // {
  379. // recFlag = 2; //已写2.4G卡号
  380. dat1 = idBuf[1]<<24;
  381. dat2 = idBuf[2]<<16;
  382. dat3 = idBuf[3]<<8;
  383. dat4 = dat1 + dat2 + dat3 + idBuf[4];
  384. // if(WriteOKFlag)
  385. // printf("读到考勤ID=%u\r\n",dat4);
  386. delay_ms(10);
  387. // else
  388. // printf("已写号,读到考勤:%u\r\n",dat4);
  389. WriteOKFlag = 0;
  390. idBuf[0]=0;
  391. idBuf[1]=0;
  392. idBuf[2]=0;
  393. idBuf[3]=0;
  394. idBuf[4]=0;
  395. GetNum_1356[0]=0;
  396. GetNum_1356[1]=0;
  397. GetNum_1356[2]=0;
  398. GetNum_1356[3]=0;
  399. GetNum_1356[4]=0;
  400. // }
  401. // else
  402. // {
  403. //// printf("未写卡号!\r\n");
  404. //// NRF24L01_RX_Mode(SPI_SEND);
  405. // IWDG_Feed();
  406. //
  407. // WriteOKFlag = 0;
  408. // WriteFailFlag = 0;
  409. // idBuf[0] = 0;
  410. // idBuf[1] = 0;
  411. // idBuf[2] = 0;
  412. // idBuf[3] = 0;
  413. // idBuf[4] = 0;
  414. // GetNum_1356[0]=0;
  415. // GetNum_1356[1]=0;
  416. // GetNum_1356[2]=0;
  417. // GetNum_1356[3]=0;
  418. // GetNum_1356[4]=0;
  419. //
  420. // NRF24L01_TX_Mode(SPI_SEND);
  421. // }
  422. }
  423. }
  424. #else
  425. void DataProc_RecNrf24L01(void)
  426. {
  427. u8 tmp;
  428. u32 IdData;
  429. u8 IdBuf[10]={0};
  430. //if(read1356flag)
  431. {
  432. NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//NOACK??,?????
  433. count_A34 = 0;
  434. {
  435. while(1) //
  436. {
  437. tmp = DataProc_A4_nrf();
  438. if(tmp)
  439. break;
  440. }
  441. read1356flag = 0;
  442. #if LCD
  443. IdData =(ReadAttenID[1]<<24)|(ReadAttenID[2]<<16)|(ReadAttenID[3]<<8)|(ReadAttenID[4]);
  444. HexToDec(IdData,IdBuf,sizeof(IdBuf));
  445. vDispClearLine(3);
  446. vDispStr(3,1,"ID:",3);
  447. vDispStr(3,4,(char*)IdBuf,10);
  448. if((ReadAttenID[0] & 0x80) !=0)
  449. {
  450. vDispStr(3,15,"1",1);
  451. }
  452. else
  453. {
  454. vDispStr(3,15,"0",1);
  455. }
  456. #endif
  457. SendConsoleData((char*)uartSendRdNumData,22); //回显,返回给PC端
  458. delay_ms(50);
  459. DegugPrintf(uartSendRdNumData,22);
  460. SendUSART2Str("\n");
  461. #if LCD
  462. if(ResNum_1356[0] ==ReadAttenID[0] &&ResNum_1356[1] ==ReadAttenID[1] && ResNum_1356[2] ==ReadAttenID[2]
  463. &&ResNum_1356[3] ==ReadAttenID[3] && ResNum_1356[4] ==ReadAttenID[4])
  464. {
  465. vDispClearLine(4);
  466. vDispStr(4,4,"OK",2);
  467. }
  468. else
  469. {
  470. vDispClearLine(4);
  471. vDispStr(4,4,"NG",2);
  472. }
  473. #endif
  474. //写号清零防止对下一次写号造成影响
  475. ReadAttenID[0]=0;
  476. ReadAttenID[1]=0;
  477. ReadAttenID[2]=0;
  478. ReadAttenID[3]=0;
  479. ReadAttenID[4]=0;
  480. ResNum_1356[0]=0;
  481. ResNum_1356[1]=0;
  482. ResNum_1356[2]=0;
  483. ResNum_1356[3]=0;
  484. ResNum_1356[4]=0;
  485. GetNum_1356[0]=0;
  486. GetNum_1356[1]=0;
  487. GetNum_1356[2]=0;
  488. GetNum_1356[3]=0;
  489. GetNum_1356[4]=0;
  490. }
  491. }
  492. }
  493. #endif
  494. void DataProc_Nrf24l01_SPI2(void)
  495. {
  496. u8 buffer[32];
  497. u8 temp;
  498. //u32 dat1,dat2,dat3,dat4,cardid;
  499. IWDG_Feed();
  500. if(newIdFlag==1)
  501. {
  502. newIdFlag = 0;
  503. txBuf[0] = 0xfc;
  504. txBuf[1] = 0x11;
  505. // 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])
  506. // {
  507. txBuf[2] = idBuf[0];
  508. txBuf[3] = idBuf[1];
  509. txBuf[4] = idBuf[2];
  510. txBuf[5] = idBuf[3];
  511. txBuf[6] = idBuf[4];
  512. // }
  513. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  514. SPI2_CE =0;
  515. NRF24L01_Write_Reg(SPI_SEND,FLUSH_TX,0xff);//清除TX FIFO寄存器
  516. NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节,自动发送
  517. NRF24L01_RX_Mode(SPI_SEND); //设置24L01为接收模式
  518. rspFlag = 1;
  519. count=0;
  520. IWDG_Feed();
  521. }
  522. if(rspFlag) //处理答题卡上电发送的数据
  523. {
  524. IWDG_Feed();
  525. if(count<=2000) //2S内未收到2.4G回复信号,则rspFlag失效
  526. {
  527. if(SPI2_IRQ == 0) //24L01收到答题卡上电发送的数据
  528. {
  529. IWDG_Feed();
  530. temp = NRF24L01_RxPacket(SPI_SEND,buffer);
  531. // SendConsoleByte(temp);
  532. if((temp&0x40)==0x40) //接收到的数据显示
  533. {
  534. buffer[RX_PLOAD_WIDTH]='\0';
  535. led0=~led0;
  536. // SendConsoleData(buffer,RX_PLOAD_WIDTH); //
  537. if((buffer[1]==idBuf[0])&& (buffer[2]==idBuf[1])&&(buffer[3]==idBuf[2])&&(buffer[4]==idBuf[3])&&(buffer[5]==idBuf[4])) //收到改写后编号
  538. {
  539. // 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]))
  540. // {
  541. if(rspFlag == 1)
  542. {
  543. rspFlag++;
  544. txBuf[1] = 0x12; ///0xfc + cmd(0x12) +id +crc ----cmd=0x12 确认
  545. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  546. NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节,自动发送
  547. }
  548. else
  549. {
  550. rspFlag=0; //如果ID已经是最新,rspFlag清零,不处理接收数据
  551. count = 0; //计数清零
  552. idBuf[0]=0;
  553. WrNumOkBuf->Control = 0x10; //写号完成
  554. SendConsoleData((char*)uartSendWrNumOkData,14); //回显,返回给PC端
  555. DegugPrintf(uartSendWrNumOkData,14);
  556. SendUSART2Str("\n");
  557. //dat1 = (idBuf[1]<<24);
  558. //dat2 = (idBuf[2]<<16);
  559. //dat3 = (idBuf[3]<<8);
  560. //dat4 = idBuf[4];
  561. //cardid = dat1 + dat2 + dat3 + dat4;
  562. // printf("写号完成!卡号:%u\r\n",cardid);
  563. //NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
  564. #if APP
  565. NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//???
  566. #else
  567. NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
  568. #endif
  569. WriteOKFlag = 1;
  570. }
  571. // }
  572. }
  573. else
  574. {
  575. SPI2_CE =0;
  576. txBuf[1] = 0x11;
  577. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  578. NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节
  579. SPI2_CE =1;
  580. }
  581. }
  582. }
  583. }
  584. else //2s超时回复
  585. {
  586. IWDG_Feed();
  587. rspFlag=0; //如果ID已经是最新,rspFlag清零,不处理接收数据
  588. count = 0; //计数清零
  589. idBuf[0]=0;
  590. idBuf[1]=0;
  591. idBuf[2]=0;
  592. idBuf[3]=0;
  593. idBuf[4]=0;
  594. GetNum_1356[0]=0;
  595. GetNum_1356[1]=0;
  596. GetNum_1356[2]=0;
  597. GetNum_1356[3]=0;
  598. GetNum_1356[4]=0;
  599. WrNumOkBuf->Control = 0x30; //错误
  600. SendConsoleData((char*)uartSendWrNumOkData,14); //回显,返回给PC端
  601. DegugPrintf(uartSendWrNumOkData,14);
  602. SendUSART2Str("\n");
  603. // printf("写号失败:超时!\r\n");
  604. // NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
  605. }
  606. }
  607. }
  608. u8 DataProc_A0_WriteNum_SPI2(void)
  609. {
  610. u8 temp;
  611. u8 buffer[32];
  612. if(newIdFlag==1)
  613. {
  614. newIdFlag = 0;
  615. txBuf[0] = 0xfc;
  616. txBuf[1] = 0x11;
  617. txBuf[2] = idBuf[0];
  618. txBuf[3] = idBuf[1];
  619. txBuf[4] = idBuf[2];
  620. txBuf[5] = idBuf[3];
  621. txBuf[6] = idBuf[4];
  622. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  623. SPI2_CE =0;
  624. NRF24L01_Write_Reg(SPI_SEND,FLUSH_TX,0xff);//清除TX FIFO寄存器
  625. NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节,自动发送
  626. NRF24L01_RX_Mode(SPI_SEND); //设置24L01为接收模式
  627. SPI2_CE =1;
  628. rspFlag = 1;
  629. count=0;
  630. IWDG_Feed();
  631. }
  632. if(count<=3000)
  633. {
  634. IWDG_Feed();
  635. if(SPI2_IRQ == 0) //24L01收到答题卡上电发送的数据
  636. {
  637. IWDG_Feed();
  638. temp = NRF24L01_RxPacket(SPI_SEND,buffer);
  639. SendUSART2Byte(temp);
  640. SendUSART2Str("\n");
  641. if((temp&0x40)==0x40) //接收到的数据显示
  642. {
  643. buffer[RX_PLOAD_WIDTH]='\0';
  644. DegugPrintf(buffer,7);
  645. SendUSART2Str("\n");
  646. if((buffer[1]==idBuf[0])&& (buffer[2]==idBuf[1])&&(buffer[3]==idBuf[2])&&(buffer[4]==idBuf[3])&&(buffer[5]==idBuf[4])) //收到改写后编号
  647. {
  648. if(rspFlag == 1)
  649. {
  650. rspFlag++;
  651. txBuf[1] = 0x12; ///0xfc + cmd(0x12) +id +crc ----cmd=0x12 确认
  652. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  653. SPI2_CE =0;
  654. NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节,自动发送
  655. SPI2_CE =1;
  656. }
  657. else
  658. {
  659. rspFlag=0; //如果ID已经是最新,rspFlag清零,不处理接收数据
  660. DegugPrintf(uartSendWrNumOkData,14);
  661. SendUSART2Str("\n");
  662. //NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
  663. #if APP
  664. NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//???
  665. #else
  666. NRF24L01_RX_Mode_NOACK(SPI_SEND);//读考勤
  667. #endif
  668. WrNumOkBuf->Control = 0x10; //写号完成
  669. SendConsoleData((char*)uartSendWrNumOkData,14); //回显,返回给PC端
  670. SendUSART2Str("OK\n");
  671. return 1;
  672. }
  673. }
  674. else
  675. {
  676. SPI2_CE =0;
  677. txBuf[1] = 0x11;
  678. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  679. NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节
  680. SPI2_CE =1;
  681. }
  682. }
  683. }
  684. }
  685. else
  686. {
  687. idBuf[0] = 0;
  688. idBuf[1] = 0x00;
  689. idBuf[2] = 0x00;
  690. idBuf[3] = 0x00;
  691. idBuf[4] = 0x00;
  692. ReadAttenID[0]=0;
  693. ReadAttenID[1]=0;
  694. ReadAttenID[2]=0;
  695. ReadAttenID[3]=0;
  696. ReadAttenID[4]=0;
  697. ResNum_1356[0]=0;
  698. ResNum_1356[1]=0;
  699. ResNum_1356[2]=0;
  700. ResNum_1356[3]=0;
  701. ResNum_1356[4]=0;
  702. GetNum_1356[0]=0;
  703. GetNum_1356[1]=0;
  704. GetNum_1356[2]=0;
  705. GetNum_1356[3]=0;
  706. GetNum_1356[4]=0;
  707. WrNumOkBuf->Control = 0x30; //写号失败
  708. SendConsoleData((char*)uartSendWrNumOkData,14); //回显,返回给PC端
  709. // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开始串口1接收中断
  710. SendUSART2Str("3000\n");
  711. return 1;
  712. }
  713. IWDG_Feed();
  714. return 0;
  715. }
  716. u8 DataProc_A3_nrf(void)
  717. {
  718. u8 temp;
  719. u8 buffer[32];
  720. if(rspFlag_A3)
  721. {
  722. rspFlag_A3=0;
  723. SPI2_CE = 0;
  724. NRF24L01_Write_Reg(SPI_SEND,FLUSH_TX,0xff);//清除TX FIFO寄存器
  725. NRF24L01_Write_Buf(SPI_SEND,W_ACK_PAYLOAD,txA3Buf,8); //写数据到TX BUF 8个字节,自动发送
  726. NRF24L01_RX_Mode(SPI_SEND); //设置24L01为接收模式
  727. SPI2_CE = 1;
  728. }
  729. if(count_A34<1500)
  730. {
  731. if(SPI2_IRQ == 0) //24L01收到答题卡上电发送的数据
  732. {
  733. IWDG_Feed();
  734. temp = NRF24L01_RxPacket(SPI_SEND,buffer);
  735. if((temp&0x40)==0x40) //接收到的数据显示
  736. {
  737. buffer[RX_PLOAD_WIDTH]='\0';
  738. DegugPrintf(buffer,7);
  739. SendUSART2Str("\n");
  740. if((buffer[1]==0)&& (buffer[2]==0)&&(buffer[3]==0)&&(buffer[4]==0)&&(buffer[5]==0)) //未写卡号
  741. {
  742. idResA3Buf[0] = 0;
  743. idResA3Buf[1] = 0;
  744. idResA3Buf[2] = 0;
  745. idResA3Buf[3] = 0;
  746. idResA3Buf[4] = 0;
  747. // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开始串口1接收中断
  748. return 1;
  749. }
  750. else
  751. {
  752. idResA3Buf[0] = buffer[1];
  753. idResA3Buf[1] = buffer[2];
  754. idResA3Buf[2] = buffer[3];
  755. idResA3Buf[3] = buffer[4];
  756. idResA3Buf[4] = buffer[5];
  757. // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开始串口1接收中断
  758. return 1;
  759. }
  760. }
  761. }
  762. IWDG_Feed();
  763. }
  764. else
  765. {
  766. idResA3Buf[0] = 0;
  767. idResA3Buf[1] = 0x00;
  768. idResA3Buf[2] = 0x00;
  769. idResA3Buf[3] = 0x00;
  770. idResA3Buf[4] = 0x00;
  771. // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开始串口1接收中断
  772. return 1;
  773. }
  774. return 0;
  775. }
  776. u8 DataProc_A4_nrf(void)
  777. {
  778. u8 temp;
  779. u8 buffer[32];
  780. if(count_A34<3000)
  781. {
  782. if(SPI2_IRQ == 0) //24L01收到答题卡上电发送的数据
  783. {
  784. IWDG_Feed();
  785. temp = NRF24L01_RxPacket(SPI_SEND,buffer);
  786. if((temp&0x40)==0x40) //接收到的数据显示
  787. {
  788. buffer[RX_PLOAD_WIDTH]='\0';
  789. //DegugPrintf(buffer,7);
  790. //SendUSART2Str("\n");
  791. //SendConsoleData(buffer,32); //回显,返回给PC端
  792. if((buffer[1]==0)&& (buffer[2]==0)&&(buffer[3]==0)) //未写考勤
  793. {
  794. ReadAttenID[0] = 0;
  795. ReadAttenID[1] = 0;
  796. ReadAttenID[2] = 0;
  797. ReadAttenID[3] = 0;
  798. ReadAttenID[4] = 0;
  799. return 1;
  800. }
  801. else
  802. {
  803. if((protocol_config_flg == 7)||(protocol_config_flg == 13))
  804. {
  805. ReadAttenID[0] = 0;
  806. ReadAttenID[1] = buffer[0];
  807. ReadAttenID[2] = buffer[1];
  808. ReadAttenID[3] = buffer[2];
  809. ReadAttenID[4] = buffer[3];
  810. }
  811. else if((protocol_config_flg == 8))
  812. {
  813. ReadAttenID[0] = buffer[1];
  814. ReadAttenID[1] = buffer[2];
  815. ReadAttenID[2] = buffer[3];
  816. ReadAttenID[3] = buffer[4];
  817. ReadAttenID[4] = buffer[5];
  818. }
  819. else if((protocol_config_flg == 9)||(protocol_config_flg == 14))
  820. {
  821. ReadAttenID[0] = 0;
  822. ReadAttenID[1] = (buffer[1]^0x53);
  823. ReadAttenID[2] = (buffer[2]^0x43);
  824. ReadAttenID[3] = (buffer[3]^0x44);
  825. ReadAttenID[4] = (buffer[4]^0x58);
  826. }
  827. else if(protocol_config_flg == 20)
  828. {
  829. ReadAttenID[0] = 0;
  830. ReadAttenID[1] = buffer[4];
  831. ReadAttenID[2] = buffer[5];
  832. ReadAttenID[3] = buffer[6];
  833. ReadAttenID[4] = buffer[7];
  834. }
  835. else
  836. {
  837. ReadAttenID[0] = 0;
  838. ReadAttenID[1] = buffer[1];
  839. ReadAttenID[2] = buffer[2];
  840. ReadAttenID[3] = buffer[3];
  841. ReadAttenID[4] = buffer[4];
  842. }
  843. return 1;
  844. }
  845. }
  846. }
  847. IWDG_Feed();
  848. }
  849. else
  850. {
  851. ReadAttenID[0] = 0;
  852. ReadAttenID[1] = 0;
  853. ReadAttenID[2] = 0;
  854. ReadAttenID[3] = 0;
  855. ReadAttenID[4] = 0;
  856. return 1;
  857. }
  858. return 0;
  859. }
  860. void DataProc_USART1(void)
  861. {
  862. // u8 i;
  863. // u16 reclen;
  864. // u32 IdData;
  865. // u8 IdBuf[10]={0};
  866. IWDG_Feed();
  867. 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' )
  868. {
  869. USART1_RX_CNT =0;
  870. NVIC_SystemReset(); //系统复位
  871. }
  872. if(USART1_RX_BUF[0]!='u' && USART1_RX_BUF[0]!=0xFA)
  873. {
  874. USART1_RX_CNT =0;
  875. return;
  876. }
  877. if(USART1_RX_CNT < 14)
  878. {
  879. return;
  880. }
  881. if(USART1_RX_BUF[USART1_RX_CNT-1] != 0xFB)
  882. {
  883. return;
  884. }
  885. if(USART1_RX_BUF[10]==0xA0 && USART1_RX_CNT != 21)
  886. {
  887. return;
  888. }
  889. if(USART1_RX_CNT == 14) //接收完一条指令标志
  890. {
  891. if(USART1_RX_BUF[0]==0xFA && USART1_RX_BUF[13]==0xFB)//包头包尾检测
  892. {
  893. USART1_REC_Status |= 0x8000;
  894. USART1_RX_CNT = 0;
  895. // SendUartData(USART1_RX_BUF,14,USART2);
  896. DegugPrintf(USART1_RX_BUF,14);
  897. SendUSART2Str("\n");
  898. }
  899. else
  900. {
  901. USART1_RX_CNT = 0;
  902. USART1_REC_Status = 0; //接收长度正确但包头包尾检测没通过
  903. }
  904. }
  905. else if(USART1_RX_CNT == 21)
  906. {
  907. if(USART1_RX_BUF[0]==0xFA && USART1_RX_BUF[20]==0xFB)//包头包尾检测
  908. {
  909. USART1_REC_Status |= 0x8000;
  910. USART1_RX_CNT = 0;
  911. // SendUartData(USART1_RX_BUF,21,USART2);
  912. DegugPrintf(USART1_RX_BUF,21);
  913. SendUSART2Str("\n");
  914. }
  915. else
  916. {
  917. USART1_RX_CNT = 0;
  918. USART1_REC_Status = 0; //接收长度正确但包头包尾检测没通过
  919. }
  920. }
  921. else if(USART1_RX_CNT == 17)
  922. {
  923. if(USART1_RX_BUF[0]==0xFA && USART1_RX_BUF[16]==0xFB)//包头包尾检测
  924. {
  925. USART1_REC_Status |= 0x8000;
  926. USART1_RX_CNT = 0;
  927. // SendUartData(USART1_RX_BUF,21,USART2);
  928. DegugPrintf(USART1_RX_BUF,17);
  929. SendUSART2Str("\n");
  930. }
  931. else
  932. {
  933. USART1_RX_CNT = 0;
  934. USART1_REC_Status = 0; //接收长度正确但包头包尾检测没通过
  935. }
  936. }
  937. if(USART1_REC_Status & 0x8000) //接收一帧数据完成
  938. {
  939. // DegugPrintf(USART1_RX_BUF,strlen(USART1_RX_BUF));
  940. // printf("len=%d\n",USART1_RX_CNT);
  941. IWDG_Feed();
  942. // printf("Rec = %d\r\n",USART1_RX_CNT);
  943. ReceiveBuf->SequenceID[0] = USART1_RX_BUF[6];
  944. ReceiveBuf->SequenceID[1] = USART1_RX_BUF[7];
  945. ReceiveBuf->SequenceID[2] = USART1_RX_BUF[8];
  946. ReceiveBuf->SequenceID[3] = USART1_RX_BUF[9];
  947. ReceiveBuf->Command = USART1_RX_BUF[10]; //命令
  948. // ReceiveBuf->Tag = USART1_RX_BUF[11]; //Tag 命令类型
  949. // ReceiveBuf->Length = USART1_RX_BUF[12]; //数据长度
  950. SendBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
  951. SendBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
  952. SendBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
  953. SendBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
  954. //USART1_REC_Status = 0;
  955. //USART1_RX_CNT =0;
  956. switch(ReceiveBuf->Command)
  957. {
  958. case Reset: //复位
  959. // printf("Reset\n");
  960. //数据处理
  961. ReceiveBuf->Command = 0; //清零
  962. break;
  963. case GetFirmversion: //获取固件版本
  964. // printf("GetFirmversion\n");
  965. //数据处理
  966. ReceiveBuf->Command = 0; //清零
  967. break;
  968. case SetReaderID: //设置阅读器ID
  969. // printf("SetReaderID\n");
  970. //数据处理
  971. ReceiveBuf->Command = 0; //清零
  972. break;
  973. case GetReaderID: //获取阅读器ID
  974. // printf("GetReaderID\n");
  975. //数据处理
  976. ReceiveBuf->Command = 0; //清零
  977. break;
  978. case DelWhite: //删除白名单
  979. // printf("DelWhite\n");
  980. //数据处理
  981. ReceiveBuf->Command = 0; //清零
  982. break;
  983. case OpenWhite: //打开白名单
  984. // printf("OpenWhite\n");
  985. //数据处理
  986. ReceiveBuf->Command = 0; //清零
  987. break;
  988. case CloseWhite: //关闭白名单
  989. // printf("CloseWhite\n");
  990. //数据处理
  991. ReceiveBuf->Command = 0; //清零
  992. break;
  993. case AddWhite: //添加白名单
  994. // printf("AddWhite\n");
  995. //数据处理
  996. ReceiveBuf->Command = 0; //清零
  997. break;
  998. case GetWhite: //获取白名单
  999. // printf("GetWhite\n");
  1000. //数据处理
  1001. ReceiveBuf->Command = 0; //清零
  1002. break;
  1003. case Heart: //心跳
  1004. // printf("Heart\n");
  1005. //数据处理
  1006. ReceiveBuf->Command = 0; //清零
  1007. break;
  1008. case GetWhiteState: //获取白名单状态
  1009. // printf("GetWhiteState\n");
  1010. //数据处理
  1011. ReceiveBuf->Command = 0; //清零
  1012. break;
  1013. case WriteNum: //写号 A0
  1014. //FA 00 00 00 00 00 00 00 00 00 A0 01 05 00 01 02 03 04 00 00 FB --21byte
  1015. // printf("写 2.4G 卡号\n");
  1016. // DegugPrintf(USART1_RX_BUF,21);
  1017. // SendUSART2Str("\n");
  1018. {
  1019. u8 tmp;
  1020. //数据处理
  1021. USART1_RX_CNT = 0;
  1022. WrNumOkBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
  1023. WrNumOkBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
  1024. WrNumOkBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
  1025. WrNumOkBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
  1026. WrNumOkBuf->Command = ReceiveBuf->Command;
  1027. ReceiveBuf->Value[0] = USART1_RX_BUF[13];
  1028. ReceiveBuf->Value[1] = USART1_RX_BUF[14];
  1029. ReceiveBuf->Value[2] = USART1_RX_BUF[15];
  1030. ReceiveBuf->Value[3] = USART1_RX_BUF[16];
  1031. ReceiveBuf->Value[4] = USART1_RX_BUF[17];
  1032. idBuf[0] = USART1_RX_BUF[13];
  1033. idBuf[1] = USART1_RX_BUF[14];
  1034. idBuf[2] = USART1_RX_BUF[15];
  1035. idBuf[3] = USART1_RX_BUF[16];
  1036. idBuf[4] = USART1_RX_BUF[17];
  1037. newIdFlag = 1; //2.4G写号
  1038. count = 0; //计时器计数清零
  1039. NRF24L01_RX_Mode(SPI_SEND);
  1040. SendUSART2Str("0000\n");
  1041. while(1) //
  1042. {
  1043. tmp = DataProc_A0_WriteNum_SPI2();
  1044. if(tmp)
  1045. break;
  1046. }
  1047. SendUSART2Str("0000\n");
  1048. ReadAttenID[0]=0;
  1049. ReadAttenID[1]=0;
  1050. ReadAttenID[2]=0;
  1051. ReadAttenID[3]=0;
  1052. ReadAttenID[4]=0;
  1053. ResNum_1356[0]=0;
  1054. ResNum_1356[1]=0;
  1055. ResNum_1356[2]=0;
  1056. ResNum_1356[3]=0;
  1057. ResNum_1356[4]=0;
  1058. GetNum_1356[0]=0;
  1059. GetNum_1356[1]=0;
  1060. GetNum_1356[2]=0;
  1061. GetNum_1356[3]=0;
  1062. GetNum_1356[4]=0;
  1063. // USART1_REC_Status = 0;
  1064. // USART1_RX_CNT =0;
  1065. // for(i=0;i<30;i++)
  1066. // {
  1067. // USART3_RX_BUF[i] = 0;
  1068. // }
  1069. ReceiveBuf->Command = 0; //清零
  1070. }
  1071. break;
  1072. case WriteNumState: //写号结果上报
  1073. ReceiveBuf->Command = 0; //清零
  1074. break;
  1075. case Read_1345Num: //读13.45M卡号 A2
  1076. //FA 00 00 00 00 00 00 00 00 00 A2 00 00 FB
  1077. if(first_clear_flag == 0)
  1078. {
  1079. memset(ResNum_1356,0,5);
  1080. //memset(oldResNum_1356,0,5);
  1081. first_clear_flag = 1;
  1082. }
  1083. if((ResNum_1356[1] == 0) && (ResNum_1356[2] == 0)&& (ResNum_1356[3] == 0)&&(ResNum_1356[4] ==0))
  1084. {
  1085. break;
  1086. }
  1087. //ledL;
  1088. USART1_RX_CNT = 0;
  1089. ReadNumBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
  1090. ReadNumBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
  1091. ReadNumBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
  1092. ReadNumBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
  1093. ReadNumBuf->Command = ReceiveBuf->Command;
  1094. ReadNumBuf->Tag1 = 1;
  1095. ReadNumBuf->Length1 = 6;
  1096. ReadNumBuf->Value1[0] = 0; //len=0 无效
  1097. ReadNumBuf->Value1[1] = ResNum_1356[0];
  1098. ReadNumBuf->Value1[2] = ResNum_1356[1];
  1099. ReadNumBuf->Value1[3] = ResNum_1356[2];
  1100. ReadNumBuf->Value1[4] = ResNum_1356[3];
  1101. ReadNumBuf->Value1[5] = ResNum_1356[4];
  1102. // SendUartData(uartSendRdNumData,22,USART2);
  1103. SendConsoleData((char*)uartSendRdNumData,22); //回显,返回给PC端
  1104. delay_ms(50);
  1105. DegugPrintf(uartSendRdNumData,22);
  1106. SendUSART2Str("\n");
  1107. //ledH;
  1108. ResNum_1356[0]=0;
  1109. ResNum_1356[1]=0;
  1110. ResNum_1356[2]=0;
  1111. ResNum_1356[3]=0;
  1112. ResNum_1356[4]=0;
  1113. // USART1_REC_Status = 0;
  1114. // USART1_RX_CNT =0;
  1115. // for(i=0;i<30;i++)
  1116. // {
  1117. // USART3_RX_BUF[i] = 0;
  1118. // }
  1119. first_clear_flag = 0;
  1120. ReceiveBuf->Command = 0; //清零
  1121. // ledH;
  1122. break;
  1123. case Read_24Num: //A3 读一次2.4G卡号
  1124. //FA 00 00 00 00 00 00 00 00 00 A3 00 00 FB
  1125. /* USART1_RX_CNT = 0;
  1126. NRF24L01_RX_Mode(SPI_SEND);
  1127. count_A34 = 0;
  1128. rspFlag_A3 = 1;
  1129. {
  1130. u8 tmp;
  1131. SendUSART2Str("3333\n");
  1132. while(1) //
  1133. {
  1134. tmp = DataProc_A3_nrf();
  1135. if(tmp)
  1136. break;
  1137. }
  1138. SendUSART2Str("3333\n");
  1139. ReadNumBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
  1140. ReadNumBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
  1141. ReadNumBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
  1142. ReadNumBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID与请求方同步
  1143. ReadNumBuf->Command = ReceiveBuf->Command;
  1144. ReadNumBuf->Tag1 = 1;
  1145. ReadNumBuf->Length1 = 6;
  1146. ReadNumBuf->Value1[0] = 0; //len=0 无效
  1147. ReadNumBuf->Value1[1] = idResA3Buf[0];
  1148. ReadNumBuf->Value1[2] = idResA3Buf[1];
  1149. ReadNumBuf->Value1[3] = idResA3Buf[2];
  1150. ReadNumBuf->Value1[4] = idResA3Buf[3];
  1151. ReadNumBuf->Value1[5] = idResA3Buf[4];
  1152. DegugPrintf(uartSendRdNumData,22);
  1153. SendUSART2Str("\n");
  1154. SendConsoleData(uartSendRdNumData,22); //回显,返回给PC端
  1155. delay_ms(50);
  1156. //写号清零防止对下一次写号造成影响
  1157. ReadAttenID[0]=0;
  1158. ReadAttenID[1]=0;
  1159. ReadAttenID[2]=0;
  1160. ReadAttenID[3]=0;
  1161. ReadAttenID[4]=0;
  1162. ResNum_1356[0]=0;
  1163. ResNum_1356[1]=0;
  1164. ResNum_1356[2]=0;
  1165. ResNum_1356[3]=0;
  1166. ResNum_1356[4]=0;
  1167. GetNum_1356[0]=0;
  1168. GetNum_1356[1]=0;
  1169. GetNum_1356[2]=0;
  1170. GetNum_1356[3]=0;
  1171. GetNum_1356[4]=0;
  1172. // USART1_REC_Status = 0;
  1173. // USART1_RX_CNT =0;
  1174. // for(i=0;i<30;i++)
  1175. // {
  1176. // USART3_RX_BUF[i] = 0;
  1177. // }
  1178. ReceiveBuf->Command = 0; //清零
  1179. }
  1180. */ break;
  1181. case ReadCardNum: //A4 读考勤
  1182. USART1_RX_CNT = 0;
  1183. //SendConsoleByte(protocol_config_flg); //回显,返回给PC端
  1184. //NRF24L01_RX_Mode_NOACK(SPI_SEND);//NOACK模式,读江西考勤
  1185. #if APP
  1186. NRF24L01_RX_Mode_NOACK(SPI_SEND,protocol_config_flg);//NOACK??,?????
  1187. #else
  1188. NRF24L01_RX_Mode_NOACK(SPI_SEND);////NOACK模式,读江西考勤
  1189. #endif
  1190. count_A34 = 0;
  1191. {
  1192. u8 tmp;
  1193. SendUSART2Str("4444\n");
  1194. while(1) //
  1195. {
  1196. tmp = DataProc_A4_nrf();
  1197. if(tmp)
  1198. break;
  1199. }
  1200. SendUSART2Str("4444\n");
  1201. ReadNumBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
  1202. ReadNumBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
  1203. ReadNumBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
  1204. ReadNumBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID
  1205. ReadNumBuf->Command = ReceiveBuf->Command;
  1206. ReadNumBuf->Tag1 = 1;
  1207. ReadNumBuf->Length1 = 6;
  1208. ReadNumBuf->Value1[0] = 0; //
  1209. ReadNumBuf->Value1[1] = ReadAttenID[0];
  1210. ReadNumBuf->Value1[2] = ReadAttenID[1];
  1211. ReadNumBuf->Value1[3] = ReadAttenID[2];
  1212. ReadNumBuf->Value1[4] = ReadAttenID[3];
  1213. ReadNumBuf->Value1[5] = ReadAttenID[4];
  1214. SendConsoleData((char*)uartSendRdNumData,22); //回显,返回给PC端
  1215. delay_ms(50);
  1216. DegugPrintf(uartSendRdNumData,22);
  1217. SendUSART2Str("\n");
  1218. //写号清零防止对下一次写号造成影响
  1219. ReadAttenID[0]=0;
  1220. ReadAttenID[1]=0;
  1221. ReadAttenID[2]=0;
  1222. ReadAttenID[3]=0;
  1223. ReadAttenID[4]=0;
  1224. ResNum_1356[0]=0;
  1225. ResNum_1356[1]=0;
  1226. ResNum_1356[2]=0;
  1227. ResNum_1356[3]=0;
  1228. ResNum_1356[4]=0;
  1229. GetNum_1356[0]=0;
  1230. GetNum_1356[1]=0;
  1231. GetNum_1356[2]=0;
  1232. GetNum_1356[3]=0;
  1233. GetNum_1356[4]=0;
  1234. //memset(ResNum_1356,0,5);
  1235. ReceiveBuf->Command = 0; //
  1236. }
  1237. break;
  1238. #if APP1
  1239. case Config24Agreement:
  1240. protocol_config_flg = USART1_RX_BUF[13];
  1241. ConfigKaoQinBuf->SequenceID[0] = ReceiveBuf->SequenceID[0];
  1242. ConfigKaoQinBuf->SequenceID[1] = ReceiveBuf->SequenceID[1];
  1243. ConfigKaoQinBuf->SequenceID[2] = ReceiveBuf->SequenceID[2];
  1244. ConfigKaoQinBuf->SequenceID[3] = ReceiveBuf->SequenceID[3]; //SequenceID
  1245. ConfigKaoQinBuf->Command = ReceiveBuf->Command;
  1246. ConfigKaoQinBuf->Control =0x01;
  1247. SendConsoleData((char*)uartSendConfigKaoQinData,14); //回显,返回给PC端
  1248. delay_ms(50);
  1249. #if 1
  1250. StoreBuffer[0] = protocol_config_flg;//修改协议版本
  1251. SavePara_to_flash(); //保持数据到Flash
  1252. //Readpara_from_flash(); //读出数据并打印出来
  1253. //protocol_config_flg = Save_Buffer[2*0];
  1254. #endif
  1255. //SendConsoleByte(protocol_config_flg); //回显,返回给PC端
  1256. //SendConsoleByte(0xff);
  1257. //SendConsoleByte(protocol_config_flg);
  1258. //写号清零防止对下一次写号造成影响
  1259. ReadAttenID[0]=0;
  1260. ReadAttenID[1]=0;
  1261. ReadAttenID[2]=0;
  1262. ReadAttenID[3]=0;
  1263. ReadAttenID[4]=0;
  1264. ResNum_1356[0]=0;
  1265. ResNum_1356[1]=0;
  1266. ResNum_1356[2]=0;
  1267. ResNum_1356[3]=0;
  1268. ResNum_1356[4]=0;
  1269. GetNum_1356[0]=0;
  1270. GetNum_1356[1]=0;
  1271. GetNum_1356[2]=0;
  1272. GetNum_1356[3]=0;
  1273. GetNum_1356[4]=0;
  1274. ReceiveBuf->Command = 0; //??
  1275. break;
  1276. #endif
  1277. case UpdateFirmversion: //升级固件
  1278. // printf("UpdateFirmversion\n");
  1279. //数据处理
  1280. ReceiveBuf->Command = 0; //清零
  1281. break;
  1282. case UpdateFirm: //升级固件开关
  1283. // printf("UpdateFirm\n");
  1284. //数据处理
  1285. ReceiveBuf->Command = 0; //清零
  1286. break;
  1287. case UpdateFirmState: //升级固件结果上报
  1288. // printf("UpdateFirmState\n");
  1289. //数据处理
  1290. ReceiveBuf->Command = 0; //清零
  1291. break;
  1292. default:
  1293. break;
  1294. }
  1295. USART1_REC_Status = 0; //参数清零
  1296. //USART1_RX_CNT = 0;
  1297. }
  1298. }
  1299. void DataProc_Nrf24l01(void)
  1300. {
  1301. u8 buffer[32];
  1302. u8 temp;
  1303. IWDG_Feed();
  1304. if(newIdFlag==1)
  1305. {
  1306. newIdFlag = 0;
  1307. recFlag = 0;
  1308. txBuf[0] = 0xfc;
  1309. txBuf[1] = 0x11;
  1310. 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])
  1311. {
  1312. txBuf[2] = idBuf[0];
  1313. txBuf[3] = idBuf[1];
  1314. txBuf[4] = idBuf[2];
  1315. txBuf[5] = idBuf[3];
  1316. txBuf[6] = idBuf[4];
  1317. }
  1318. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  1319. SPI1_CE =0;
  1320. NRF24L01_Write_Reg(SPI_REV,FLUSH_TX,0xff);//清除TX FIFO寄存器
  1321. NRF24L01_Write_Buf(SPI_REV,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节,自动发送
  1322. NRF24L01_RX_Mode(SPI_REV); //设置24L01为接收模式
  1323. rspFlag = 1;
  1324. count=0;
  1325. IWDG_Feed();
  1326. }
  1327. if(rspFlag) //处理答题卡上电发送的数据
  1328. {
  1329. IWDG_Feed();
  1330. if(count<=2000) //1.5S内未收到2.4G回复信号,则rspFlag失效
  1331. {
  1332. if(SPI1_IRQ == 0) //24L01收到答题卡上电发送的数据
  1333. {
  1334. IWDG_Feed();
  1335. temp = NRF24L01_RxPacket(SPI_REV,buffer);
  1336. // SendConsoleByte(temp);
  1337. if((temp&0x40)==0x40) //接收到的数据显示
  1338. {
  1339. buffer[RX_PLOAD_WIDTH]='\0';
  1340. led0=~led0;
  1341. // SendConsoleData(buffer,RX_PLOAD_WIDTH); //
  1342. if((buffer[1]==idBuf[0])&& (buffer[2]==idBuf[1])&&(buffer[3]==idBuf[2])&&(buffer[4]==idBuf[3])&&(buffer[5]==idBuf[4])) //收到改写后编号
  1343. {
  1344. 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]))
  1345. {
  1346. if(rspFlag == 1)
  1347. {
  1348. rspFlag++;
  1349. txBuf[1] = 0x12; ///0xfc + cmd(0x12) +id +crc ----cmd=0x12 确认
  1350. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  1351. NRF24L01_Write_Buf(SPI_REV,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节,自动发送
  1352. }
  1353. else
  1354. {
  1355. rspFlag=0; //如果ID已经是最新,rspFlag清零,不处理接收数据
  1356. count = 0; //计数清零
  1357. idBuf[0]=0;
  1358. WrNumOkBuf->Control = 0x10; //写号完成
  1359. SendConsoleData((char*)uartSendWrNumOkData,14); //回显,返回给PC端
  1360. }
  1361. }
  1362. }
  1363. else
  1364. {
  1365. SPI1_CE =0;
  1366. txBuf[1] = 0x11;
  1367. txBuf[7] = CheckSum(txBuf,7); //写号到txBuf
  1368. NRF24L01_Write_Buf(SPI_REV,W_ACK_PAYLOAD,txBuf,8); //写数据到TX BUF 8个字节
  1369. SPI1_CE =1;
  1370. }
  1371. }
  1372. }
  1373. }
  1374. else //2s超时回复
  1375. {
  1376. IWDG_Feed();
  1377. rspFlag=0; //如果ID已经是最新,rspFlag清零,不处理接收数据
  1378. count = 0; //计数清零
  1379. idBuf[0]=0;
  1380. idBuf[1]=0;
  1381. idBuf[2]=0;
  1382. idBuf[3]=0;
  1383. idBuf[4]=0;
  1384. GetNum_1356[0]=0;
  1385. GetNum_1356[1]=0;
  1386. GetNum_1356[2]=0;
  1387. GetNum_1356[3]=0;
  1388. GetNum_1356[4]=0;
  1389. WrNumOkBuf->Control = 0x30; //错误
  1390. SendConsoleData((char*)uartSendWrNumOkData,14); //回显,返回给PC端
  1391. }
  1392. }
  1393. }
  1394. //------------------------------------------------------------------
  1395. //函数名:void TIM3_IRQHandler(void)
  1396. //输入参数:null
  1397. //返回参数:null
  1398. //说明:定时器3中断服务
  1399. //------------------------------------------------------------------
  1400. void TIM3_IRQHandler(void)
  1401. {
  1402. if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) //判断是否为定时器3溢出中断
  1403. {
  1404. TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清中断标记
  1405. count ++;
  1406. count_A34 ++;
  1407. count_num ++;
  1408. if(HW_Open)
  1409. {
  1410. GPIO_SetBits(GPIOC,GPIO_Pin_9); //打开红外扫描枪
  1411. HW_OpenCount ++;
  1412. if(HW_OpenCount>=400)
  1413. {
  1414. GPIO_ResetBits(GPIOC,GPIO_Pin_9); //关闭红外扫描枪
  1415. HW_OpenCount = 0;
  1416. HW_Open = 0;
  1417. }
  1418. }
  1419. if(count_num==5000)
  1420. {
  1421. count_num = 0;
  1422. // printf("1356_0=%x\r\n",GetNum_1356[0]);
  1423. // printf("1356_1=%x\r\n",GetNum_1356[1]);
  1424. // printf("1356_2=%x\r\n",GetNum_1356[2]);
  1425. // printf("1356_3=%x\r\n",GetNum_1356[3]);
  1426. // printf("1356_4=%x\r\n",GetNum_1356[4]);
  1427. //
  1428. // printf("2.4G_0=%x\r\n",idBuf[0]);
  1429. // printf("2.4G_1=%x\r\n",idBuf[1]);
  1430. // printf("2.4G_2=%x\r\n",idBuf[2]);
  1431. // printf("2.4G_3=%x\r\n",idBuf[3]);
  1432. // printf("2.4G_4=%x\r\n",idBuf[4]);
  1433. }
  1434. flag_1ms = 1;
  1435. }
  1436. }