找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2695|回复: 2
收起左侧

STM32C8T6 nrf24l01透传源码

[复制链接]
ID:72395 发表于 2018-11-14 14:09 | 显示全部楼层 |阅读模式
STM32C8T6 nrf24l01 透传

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f1xx_hal.h"
  4. #include "gpio.h"

  5. /* USER CODE BEGIN Includes */
  6. #include "string.h"
  7. #include "usart/bsp_debug_usart.h"
  8. #include "NRF24L01/bsp_NRF24L01.h"
  9. #include "app_eeprom.h"
  10. #define   GLOBALS              1
  11. #include "Comm_GLOBALS.h"
  12. /* USER CODE END Includes */

  13. /* Private variables ---------------------------------------------------------*/
  14. const char AT_cmd[] = "AT+";
  15. /* USER CODE BEGIN PV */
  16. /* Private variables ---------------------------------------------------------*/

  17. /* USER CODE END PV */

  18. /* Private function prototypes -----------------------------------------------*/
  19. void SystemClock_Config(void);

  20. /* USER CODE BEGIN PFP */
  21. /* Private function prototypes -----------------------------------------------*/

  22. /* USER CODE END PFP */

  23. /* USER CODE BEGIN 0 */

  24. /* USER CODE END 0 */

  25. /**
  26.   * @brief  The application entry point.
  27.   *
  28.   * @retval None
  29.   */
  30. int main(void)
  31. {
  32.     /* USER CODE BEGIN 1 */

  33.     /* USER CODE END 1 */

  34.     /* MCU Configuration----------------------------------------------------------*/

  35.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  36.     HAL_Init();

  37.     /* USER CODE BEGIN Init */

  38.     /* USER CODE END Init */

  39.     /* Configure the system clock */
  40.     SystemClock_Config();

  41.     /* USER CODE BEGIN SysInit */

  42.     /* USER CODE END SysInit */

  43.     /* Initialize all configured peripherals */
  44.     MX_GPIO_Init();
  45.     /* USER CODE BEGIN 2 */
  46.     /* init eeprom */
  47.     app_ee_init();
  48.     /* 初始化串口并配置串口中断优先级 */
  49.     rt_hw_usart_init();
  50.     NRF24L01_SPI_Init();                    //初始化NRF24L01
  51.     while(NRF24L01_Check())
  52.     {
  53.         printf("硬件查寻不到NRF24L01无线模块\n");
  54.         HAL_Delay(1000);
  55.     }
  56.     NRF24L01_Init();
  57.                 HAL_Delay(100);
  58.     /* USER CODE END 2 */

  59.     /* Infinite loop */
  60.     /* USER CODE BEGIN WHILE */
  61.     while (1)
  62.     {

  63.         /* USER CODE END WHILE */

  64.         /* USER CODE BEGIN 3 */
  65.         if(NRF_RX_CNT)
  66.         {
  67.             serial_tx(&uart1, USART_TX_BUF,NRF_RX_CNT );
  68.             NRF_RX_CNT = 0;
  69.         }
  70.         if(USART_RX_CNT)
  71.         {
  72.             if( strncmp  ( (char*)NRF_Tx_Data,AT_cmd,2 ) == 0 )
  73.             {
  74.                 switch(NRF_Tx_Data[3])
  75.                 {
  76.                 case '1':
  77.                     BaudRate = 115200;
  78.                     STMFLASH_Write(BaudRate_EE_ADDR, (uint16_t *)&BaudRate, 2 );
  79.                     rt_hw_usart_init();
  80.                     break;
  81.                 case '2':
  82.                     BaudRate = 9600;
  83.                     STMFLASH_Write(BaudRate_EE_ADDR, (uint16_t *)&BaudRate, 2 );
  84.                     rt_hw_usart_init();
  85.                     break;
  86.                 }
  87.             }
  88.             else
  89.             {
  90.                 RF24L01_Set_Mode( MODE_TX );
  91.                 NRF24L01_TxPacket(NRF_Tx_Data,USART_RX_CNT);
  92.                 RF24L01_Set_Mode( MODE_RX );
  93.             }

  94.             USART_RX_CNT = 0;
  95.         }
  96.     }
  97.     /* USER CODE END 3 */

  98. }

  99. /**
  100.   * @brief System Clock Configuration
  101.   * @retval None
  102.   */
  103. void SystemClock_Config(void)
  104. {

  105.     RCC_OscInitTypeDef RCC_OscInitStruct;
  106.     RCC_ClkInitTypeDef RCC_ClkInitStruct;

  107.     /**Initializes the CPU, AHB and APB busses clocks
  108.     */
  109.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  110.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  111.     RCC_OscInitStruct.HSICalibrationValue = 16;
  112.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  113.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  114.     {
  115.         _Error_Handler(__FILE__, __LINE__);
  116.     }

  117.     /**Initializes the CPU, AHB and APB busses clocks
  118.     */
  119.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  120.                                   |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  121.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  122.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  123.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  124.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  125.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  126.     {
  127.         _Error_Handler(__FILE__, __LINE__);
  128.     }

  129.     /**Configure the Systick interrupt time
  130.     */
  131.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  132.     /**Configure the Systick
  133.     */
  134.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  135. //    /* SysTick_IRQn interrupt configuration */
  136. //    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  137. }

  138. /* USER CODE BEGIN 4 */

  139. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  140. {
  141.     if(GPIO_Pin == NRF24L01_IRQ_PIN)
  142.     {
  143.         NRF_RX_CNT = NRF24L01_RxPacket(Rec_Data);
  144.         memcpy(USART_TX_BUF, Rec_Data, NRF_RX_CNT);

  145.     }
  146. }
  147. //回调函数
  148. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  149. {
  150.     if(htim->Instance == TIM2 )
  151.     {
  152.         HAL_TIM_Base_Stop_IT(&htim2);
  153.         memcpy(NRF_Tx_Data, USART_RX_BUF, USART_RX_STA);
  154.         USART_RX_CNT = USART_RX_STA;
  155.         USART_RX_STA = 0;
  156.     }
  157.     __HAL_TIM_CLEAR_IT(&htim2, TIM_FLAG_UPDATE);
  158. }
  159. /* USER CODE END 4 */

  160. /**
  161.   * @brief  This function is executed in case of error occurrence.
  162.   * @param  file: The file name as string.
  163.   * @param  line: The line in file as a number.
  164.   * @retval None
  165.   */
  166. void _Error_Handler(char *file, int line)
  167. {
  168.     /* USER CODE BEGIN Error_Handler_Debug */
  169.     /* User can add his own implementation to report the HAL error return state */
  170.     while(1)
  171.     {
  172.     }
  173.     /* USER CODE END Error_Handler_Debug */
  174. }

  175. #ifdef  USE_FULL_ASSERT
  176. /**
  177.   * @brief  Reports the name of the source file and the source line number
  178.   *         where the assert_param error has occurred.
  179.   * @param  file: pointer to the source file name
  180.   * @param  line: assert_param error line source number
  181.   * @retval None
  182.   */
  183. void assert_failed(uint8_t* file, uint32_t line)
  184. {
  185.     /* USER CODE BEGIN 6 */
  186.     /* User can add his own implementation to report the file name and line number,
  187.        tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  188.     /* USER CODE END 6 */
  189. }
  190. #endif /* USE_FULL_ASSERT */

  191. /**
  192.   * @}
  193.   */

  194. /**
  195.   * @}
  196.   */

  197. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
nRF24l01.rar (5.23 MB, 下载次数: 74)
回复

使用道具 举报

ID:426095 发表于 2018-11-15 22:04 | 显示全部楼层
这个可以有
回复

使用道具 举报

ID:383374 发表于 2020-2-2 02:14 来自手机 | 显示全部楼层
这个分主从机吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表