您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

598 行
14KB

  1. /**
  2. ******************************************************************************
  3. **说明:003项目写码程序
  4. **功能:答题卡写码
  5. **作者:Bruce
  6. **日期:2017.6.8
  7. ******************************************************************************
  8. **/
  9. /* Includes ------------------------------------------------------------------*/
  10. #include "stm32f10x.h"
  11. #include "printf.h"
  12. #include "USART2.h"
  13. #include "LED.h"
  14. #include "delay.h"
  15. #include "nrf24L01.h"
  16. #include "DataProc.h"
  17. #include "tim.h"
  18. #include "flash.h"
  19. #include "LCD.h"
  20. /* Functions ---------------------------------------------------------*/
  21. void RCC_Configuration(void);
  22. void NVIC_Configuration(void);
  23. void IWDG_Init(u8 prer,u16 rlr);
  24. void IWDG_Feed(void);
  25. extern unsigned char txBuf[32];
  26. extern u8 protocol_config_flg;
  27. extern u8 first_clear_flag;
  28. #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  29. #define SYSCLK_FREQ_24MHz 24000000
  30. #else
  31. #define SYSCLK_FREQ_72MHz 72000000
  32. #endif
  33. /** @addtogroup STM32F10x_StdPeriph_Template
  34. * @{
  35. */
  36. /* Private typedef -----------------------------------------------------------*/
  37. /* Private define ------------------------------------------------------------*/
  38. /**
  39. * @brief Main program.
  40. * @param None
  41. * @retval None
  42. */
  43. #if 1
  44. #define SCAN_ADDRESS (unsigned char*)"\x34\x43\x10\x10\x01"
  45. #define SCAN_SETUPVALUE 0x07
  46. #define SCAN_TX_ADR_WIDTH (5) //3???????
  47. #define SCAN_RX_ADR_WIDTH (5) //3???????
  48. #define SCAN_RX_PLOAD_WIDTH (10) //32?????????
  49. #define SCAN_TX_PLOAD_WIDTH (10) //32?????????
  50. #define SCAN_RF_CRC (0x00)
  51. #define SCAN_RF_ENAA (0x01)
  52. #define SCAN_TX_CHNL (40) //??????1
  53. #define SCAN_RX_CHNL (40) //??????2
  54. #define SCAN_DYNPD (0x01)
  55. #define SCAN_FEATURE (0x07)
  56. void NRF24L01_TELPO_RX_Mode(SPIType spiNum,unsigned char* addr,
  57. unsigned char addrlen,
  58. unsigned char rf_channel,
  59. unsigned char rf_setup,
  60. unsigned char rf_crc,
  61. unsigned char rf_enaa,
  62. unsigned char len,
  63. unsigned char dynpd,
  64. unsigned char feature)
  65. {
  66. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+SETUP_AW,addrlen - 2);//???? --3??
  67. NRF24L01_Write_Buf(spiNum,NRF_WRITE_REG+RX_ADDR_P0,(u8*)addr,addrlen);//?RX????
  68. NRF24L01_Write_Buf(spiNum,NRF_WRITE_REG+TX_ADDR,(u8*)addr,addrlen);//?TX????
  69. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+EN_AA,rf_enaa); //????0?????
  70. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+EN_RXADDR,0x01);//????0?????
  71. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+RF_CH,rf_channel); //??RF????
  72. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+RF_SETUP,rf_setup);//??TX????,0db??,1Mbps,???????
  73. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+RX_PW_P0,len);//????0???????
  74. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+CONFIG, (0x3b | rf_crc));//???????????;PWR_UP,EN_CRC,16BIT_CRC,????
  75. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+DYNPD,dynpd);
  76. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+FEATURF,feature);
  77. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+STATUS,0x70); //??TX_DS?MAX_RT????
  78. NRF24L01_Write_Reg(spiNum,FLUSH_RX,0xff); //??RX FIFO???
  79. }
  80. void NRF24L01_RX_TelPo_CONFIG1(SPIType spiNum)
  81. {
  82. if(spiNum == SPI_PORT1){
  83. SPI1_CE =0;
  84. }else{
  85. SPI2_CE=0;
  86. }
  87. NRF24L01_TELPO_RX_Mode(spiNum,SCAN_ADDRESS,SCAN_RX_ADR_WIDTH,SCAN_RX_CHNL,
  88. SCAN_SETUPVALUE,SCAN_RF_CRC,SCAN_RF_ENAA,
  89. SCAN_RX_PLOAD_WIDTH,SCAN_DYNPD,SCAN_FEATURE);
  90. if(spiNum == SPI_PORT1){ //CE为高,进入接收模式
  91. SPI1_CE =1;
  92. }else{
  93. SPI2_CE=1;
  94. }
  95. }
  96. #endif
  97. u8 NRF24L01_RxPacket1(SPIType spiNum,u8 *rxbuf,unsigned char rxLen)
  98. {
  99. u8 sta;
  100. SPI_SetSpeed(spiNum,SPI_BaudRatePrescaler_16);
  101. //SPI_SetSpeed(spiNum,SPI_BaudRatePrescaler_8); //spi???9Mhz(24L01???SPI???10Mhz)
  102. sta=NRF24L01_Read_Reg(spiNum,STATUS); //?????????
  103. if(sta&RX_OK)//?????
  104. {
  105. NRF24L01_Read_Buf(spiNum,RD_RX_PLOAD,rxbuf,rxLen); //????
  106. NRF24L01_Write_Reg(spiNum,FLUSH_RX,0xff); //??RX FIFO???
  107. NRF24L01_Write_Reg(spiNum,NRF_WRITE_REG+STATUS,sta); //??TX_DS?MAX_RT????
  108. delay_5us(20);
  109. return 0;
  110. }
  111. return 1;//???????
  112. }
  113. int main(void)
  114. {
  115. /*!< At this stage the microcontroller clock setting is already configured,
  116. this is done through SystemInit() function which is called from startup
  117. file (startup_stm32f10x_xx.s) before to branch to application main.
  118. To reconfigure the default setting of SystemInit() function, refer to
  119. system_stm32f10x.c file
  120. */
  121. //SCB->VTOR = FLASH_BASE | 0x10000; /* Vector Table Relocation in Internal FLASH. */
  122. RCC_Configuration(); //系统时钟设置
  123. NVIC_Configuration(); //中断向量配置
  124. IWDG_Init(4,625); //(4*2^4)*625/40 ms = 1s 看门狗定时1s
  125. IWDG_Feed();
  126. TIM3_Int_Init(9,7199);
  127. led_init(); //LED IO配置
  128. #if LCD
  129. lcd_init(); //LCD 配置
  130. AttenuatorConfig(); //衰减器配置
  131. #endif
  132. USART_Configuration(); //串口1、3初始化配置 串口1波特率为115200 串口3波特率为4800
  133. USART2_Init(115200); //串口2初始化 波特率为115200 Debug
  134. NRF24L01_Init(); //24L01 IO配置
  135. IWDG_Feed();
  136. // while(NRF24L01_Check(SPI_REV)){ //检测NRF24L01 1是否存在
  137. // printf("The 2.4G Error!");
  138. // }
  139. #if 1
  140. while(NRF24L01_Check(SPI_SEND)){ //检测NRF24L01 2是否存在
  141. printf("The 2.4G Error!");
  142. }
  143. #endif
  144. Data_Init(); //包数据初始化
  145. IWDG_Feed();
  146. led0=~led0;
  147. delay_ms(200);
  148. led0=~led0;
  149. DegugPrintf("123456",6);
  150. SendUSART2Str("\n");
  151. IWDG_Feed();
  152. #if 0
  153. while(1)
  154. {
  155. NRF24L01_RX_TelPo_CONFIG1(SPI_SEND);
  156. while(1)
  157. {
  158. u8 Rx_Buffer[32] = {0};
  159. if (!NRF24L01_RxPacket1(SPI_SEND,Rx_Buffer,sizeof(Rx_Buffer)))
  160. {
  161. break;
  162. }
  163. IWDG_Feed();
  164. }
  165. IWDG_Feed();
  166. }
  167. #endif
  168. printf("\nPC对码软件通用版本V2.4-20190906\n");
  169. // SendUartData("123456",6,USART2);
  170. // printf("System Clock = %d\r\n",SystemCoreClock);
  171. NRF24L01_RX_Mode(SPI_SEND);
  172. // NRF24L01_RX_Mode_NOACK(SPI_SEND);
  173. //while(1)
  174. //{
  175. // RF24_CarrierTest(SPI_SEND,32);
  176. // delay_ms(100);
  177. // IWDG_Feed();
  178. //}
  179. #if 1
  180. Readpara_from_flash(); //读出数据并打印出来
  181. protocol_config_flg = Save_Buffer[2*0];
  182. switch(protocol_config_flg)
  183. {
  184. case 0:
  185. {
  186. printf("江西协议\r\n");
  187. #if LCD
  188. vDispClearLine(1);
  189. vDispStr(1,1,"JIANGXI",7);
  190. #endif
  191. break;
  192. }
  193. case 1:
  194. {
  195. printf("天波协议\r\n");
  196. #if LCD
  197. vDispClearLine(1);
  198. vDispStr(1,1,"TELPO",5);
  199. #endif
  200. break;
  201. }
  202. case 2:
  203. {
  204. printf("云南协议\r\n");
  205. #if LCD
  206. vDispClearLine(1);
  207. vDispStr(1,1,"YUNNAN",6);
  208. #endif
  209. break;
  210. }
  211. case 3:
  212. {
  213. printf("贵州协议\r\n");
  214. #if LCD
  215. vDispClearLine(1);
  216. vDispStr(1,1,"GUIZHOU",7);
  217. #endif
  218. break;
  219. }
  220. case 4:
  221. {
  222. printf("四川协议\r\n");
  223. #if LCD
  224. vDispClearLine(1);
  225. vDispStr(1,1,"SICHUAN",7);
  226. #endif
  227. break;
  228. }
  229. case 5:
  230. {
  231. printf("内蒙古协议\r\n");
  232. #if LCD
  233. vDispClearLine(1);
  234. vDispStr(1,1,"NEIMENGGU",9);
  235. #endif
  236. break;
  237. }
  238. case 6:
  239. {
  240. printf("安徽协议\r\n");
  241. #if LCD
  242. vDispClearLine(1);
  243. vDispStr(1,1,"ANHUI",5);
  244. #endif
  245. break;
  246. }
  247. case 7:
  248. {
  249. printf("山西鑫诺协议\r\n");
  250. #if LCD
  251. vDispClearLine(1);
  252. vDispStr(1,1,"SHANXI",6);
  253. #endif
  254. break;
  255. }
  256. case 8:
  257. {
  258. printf("VCOM测试\r\n");
  259. #if LCD
  260. vDispClearLine(1);
  261. vDispStr(1,1,"VCOM",4);
  262. #endif
  263. break;
  264. }
  265. case 9:
  266. {
  267. printf("辽宁电信\r\n");
  268. #if LCD
  269. vDispClearLine(1);
  270. vDispStr(1,1,"LIAONING.DIAN",13);
  271. #endif
  272. break;
  273. }
  274. case 10:
  275. {
  276. printf("辽宁移动\r\n");
  277. #if LCD
  278. vDispClearLine(1);
  279. vDispStr(1,1,"LIAONING.YI",11);
  280. #endif
  281. break;
  282. }
  283. case 11:
  284. {
  285. printf("Tp专用协议\r\n");
  286. #if LCD
  287. vDispClearLine(1);
  288. vDispStr(1,1,"TP",2);
  289. #endif
  290. break;
  291. }
  292. case 12:
  293. {
  294. printf("内蒙古移动协议\r\n");
  295. #if LCD
  296. vDispClearLine(1);
  297. vDispStr(1,1,"NEIMENGGU.YI",12);
  298. #endif
  299. break;
  300. }
  301. case 13:
  302. {
  303. printf("广西移动协议\r\n");
  304. #if LCD
  305. vDispClearLine(1);
  306. vDispStr(1,1,"GUANGXI.YI",10);
  307. #endif
  308. break;
  309. }
  310. case 14:
  311. {
  312. printf("四川电信协议\r\n");
  313. #if LCD
  314. vDispClearLine(1);
  315. vDispStr(1,1,"SICHUN.DIAN",11);
  316. #endif
  317. break;
  318. }
  319. case 15:
  320. {
  321. printf("福建真灼协议\r\n");
  322. #if LCD
  323. vDispClearLine(1);
  324. vDispStr(1,1,"FUJIANZHENZHUO",14);
  325. #endif
  326. break;
  327. }
  328. case 16:
  329. {
  330. printf("苏州木兰协议\r\n");
  331. #if LCD
  332. vDispClearLine(1);
  333. vDispStr(1,1,"SUZHOUMULANG",10);
  334. #endif
  335. break;
  336. }
  337. case 17:
  338. {
  339. printf("重庆移动协议\r\n");
  340. #if LCD
  341. vDispClearLine(1);
  342. vDispStr(1,1,"CHONGQIN.YI",11);
  343. #endif
  344. break;
  345. }
  346. case 18:
  347. {
  348. printf("山西移动协议\r\n");
  349. #if LCD
  350. vDispClearLine(1);
  351. vDispStr(1,1,"SHANXI.YI",9);
  352. #endif
  353. break;
  354. }
  355. case 19:
  356. {
  357. printf("甘肃移动协议\r\n");
  358. #if LCD
  359. vDispClearLine(1);
  360. vDispStr(1,1,"GANSU.YI",9);
  361. #endif
  362. break;
  363. }
  364. case 20:
  365. {
  366. printf("信达丰协议\r\n");
  367. #if LCD
  368. vDispClearLine(1);
  369. vDispStr(1,1,"XDF",3);
  370. #endif
  371. break;
  372. }
  373. case 21:
  374. {
  375. printf("德海协议\r\n");
  376. #if LCD
  377. vDispClearLine(1);
  378. vDispStr(1,1,"DH",2);
  379. #endif
  380. break;
  381. }
  382. case 22:
  383. {
  384. printf("安徽电信协议\r\n");
  385. #if LCD
  386. vDispClearLine(1);
  387. vDispStr(1,1,"AHDX",4);
  388. #endif
  389. break;
  390. }
  391. }
  392. #endif
  393. #if 0
  394. NRF24L01_RX_TelPo_CONFIG1(SPI_SEND);
  395. while(1)
  396. {
  397. u8 Rx_Buffer[32] = {0};
  398. if (!NRF24L01_RxPacket(SPI_SEND,Rx_Buffer))
  399. {
  400. break;
  401. }
  402. }
  403. #endif
  404. /* Infinite loop */
  405. //SendConsoleByte(protocol_config_flg); //回显,返回给PC端
  406. //NRF24L01_RX_Mode_NOACK_VCOM(SPI_SEND);
  407. first_clear_flag = 1;
  408. delay_ms(50);
  409. while (1)
  410. {
  411. // while(1) //
  412. // {
  413. // if(DataProc_A4_nrf())
  414. // break;
  415. // }
  416. DataProc_1356(); //13.56M数据处理
  417. #if LCD
  418. DataProc_RecNrf24L01();
  419. #endif
  420. DataProc_USART1(); //串口1数据处理,与PC端软件通信
  421. // DataProc_Nrf24l01_SPI2();
  422. }
  423. }
  424. void RCC_Configuration(void)
  425. {
  426. #if 0
  427. ErrorStatus HSEStartUpStatus; //枚举变量,定义高速时钟的启动状态
  428. RCC_DeInit(); //RCC系统重置,用于Debug
  429. RCC_HSEConfig(RCC_HSE_ON); //使能高速时钟源HSE
  430. HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待HSE稳定
  431. if(HSEStartUpStatus == SUCCESS)
  432. {
  433. RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK
  434. RCC_PCLK2Config(RCC_HCLK_Div1); // PCLK2 = HCLK
  435. RCC_PCLK1Config(RCC_HCLK_Div2); // PCLK1 = HCLK/2
  436. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  437. RCC_PLLCmd(ENABLE);
  438. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}
  439. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  440. while(RCC_GetSYSCLKSource() != 0x08){}
  441. }
  442. RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 |RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB , ENABLE);
  443. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  444. #else
  445. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
  446. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  447. /* Select AHB clock(HCLK) as SysTick clock source */
  448. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  449. /* SysTick interrupt each 10ms with HCLK equal to 72MHz */
  450. SysTick_Config(240000);
  451. #endif
  452. }
  453. void NVIC_Configuration(void)
  454. {
  455. NVIC_InitTypeDef NVIC_InitStructure;
  456. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  457. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //设置串口2中断
  458. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级1
  459. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级2
  460. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
  461. NVIC_Init(&NVIC_InitStructure);
  462. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //设置串口3中断
  463. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级1
  464. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级1
  465. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
  466. NVIC_Init(&NVIC_InitStructure);
  467. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //设置串口1中断
  468. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
  469. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级1
  470. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
  471. NVIC_Init(&NVIC_InitStructure);
  472. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //设置定时器3全局中断
  473. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
  474. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级为0
  475. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
  476. NVIC_Init(&NVIC_InitStructure);
  477. }
  478. /*
  479. *看门狗初始化函数
  480. *prer:分频数:0-7,低3位有效
  481. *分频因子:4*2^prer,最大256
  482. *rlr:重装载寄存器值,低11位有效
  483. *Tout=((4*2^prer)*rlr)/40 (ms)
  484. */
  485. void IWDG_Init(u8 prer,u16 rlr)
  486. {
  487. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); // 使能对寄存器IWDG_PR和IWDG_RLR的操作
  488. IWDG_SetPrescaler(prer); //设置IWDG预分频值:prer
  489. IWDG_SetReload(rlr); //设置IWDG重载值
  490. IWDG_ReloadCounter(); //按照设置值,重载IWDG计数器
  491. IWDG_Enable(); //使能IWDG
  492. }
  493. /**
  494. * 喂狗
  495. */
  496. void IWDG_Feed(void)
  497. {
  498. IWDG_ReloadCounter(); /*reload*/
  499. }
  500. #ifdef USE_FULL_ASSERT
  501. /**
  502. * @brief Reports the name of the source file and the source line number
  503. * where the assert_param error has occurred.
  504. * @param file: pointer to the source file name
  505. * @param line: assert_param error line source number
  506. * @retval None
  507. */
  508. void assert_failed(uint8_t* file, uint32_t line)
  509. {
  510. /* User can add his own implementation to report the file name and line number,
  511. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  512. /* Infinite loop */
  513. while (1)
  514. {
  515. }
  516. }
  517. #endif
  518. /**
  519. * @}
  520. */
  521. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/