Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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(); //13ʼ 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-73λЧ
  481. *Ƶӣ4*2^prer256
  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_PRIWDG_RLRIJ
  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****/