找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4930|回复: 8
打印 上一主题 下一主题
收起左侧

NRF24L01一对一收发的STM32源码

[复制链接]
跳转到指定楼层
楼主
本程序使用STM32F013ZET6编写
按下按键(PE3)为发送模式,按下另一按键(PE4)为接收模式
经实测,接收和发送之间的距离在无干扰的情况下,大约为30米。



所有资料51hei提供下载:
NRF24L01代码.7z (2.91 MB, 下载次数: 111)



单片机源程序如下:

  1. /**
  2.   ******************************************************************************
  3.   * @file           : main.c
  4.   * @brief          : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f1xx_hal.h"

  41. /* USER CODE BEGIN Includes */
  42. #include "lcd.h"
  43. #include "bsp_NRF24L01.h"
  44. #include "string.h"
  45. /* USER CODE END Includes */

  46. /* Private variables ---------------------------------------------------------*/
  47. ADC_HandleTypeDef hadc3;

  48. SPI_HandleTypeDef hspi2;

  49. SRAM_HandleTypeDef hsram1;

  50. /* USER CODE BEGIN PV */
  51. /* Private variables ---------------------------------------------------------*/

  52. /* USER CODE END PV */

  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. static void MX_GPIO_Init(void);
  56. static void MX_FSMC_Init(void);
  57. static void MX_ADC3_Init(void);
  58. static void MX_SPI2_Init(void);

  59. /* USER CODE BEGIN PFP */
  60. /* Private function prototypes -----------------------------------------------*/

  61. /* USER CODE END PFP */

  62. /* USER CODE BEGIN 0 */

  63. /* USER CODE END 0 */

  64. /**
  65.   * @brief  The application entry point.
  66.   *
  67.   * @retval None
  68.   */
  69. int main(void)
  70. {
  71.   /* USER CODE BEGIN 1 */

  72.   /* USER CODE END 1 */

  73.   /* MCU Configuration----------------------------------------------------------*/

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

  76.   /* USER CODE BEGIN Init */

  77.   /* USER CODE END Init */

  78.   /* Configure the system clock */
  79.   SystemClock_Config();

  80.   /* USER CODE BEGIN SysInit */

  81.   /* USER CODE END SysInit */

  82.   /* Initialize all configured peripherals */
  83.   MX_GPIO_Init();
  84.   MX_FSMC_Init();
  85.   MX_ADC3_Init();
  86.   MX_SPI2_Init();
  87.   /* USER CODE BEGIN 2 */
  88.   LCD_Init();
  89.         HAL_SPI_MspDeInit(&hspi2);
  90.         POINT_COLOR=RED;
  91.         NRF24L01_SPI_Init();
  92.         uint16_t ADCVALUE;
  93. float wendu;
  94.         uint8_t dianzi[8];
  95.         uint8_t E3;
  96.         uint8_t E4;
  97.   /* USER CODE END 2 */

  98.   /* Infinite loop */
  99.   /* USER CODE BEGIN WHILE */
  100.   while (1)
  101.   {

  102.   /* USER CODE END WHILE */

  103.   /* USER CODE BEGIN 3 */
  104.                
  105.                
  106.                
  107.                 //ADC开启、转换、显示到屏幕上
  108.     HAL_ADC_Start(&hadc3);
  109.                 ADCVALUE=HAL_ADC_GetValue(&hadc3);
  110.                 wendu=(float )ADCVALUE/4096.0f*100;
  111.                 sprintf((char*)dianzi,"wendu:%f",wendu);
  112.                 LCD_ShowxNum(30,0,wendu,3,16,0);
  113.                 HAL_Delay(100);
  114.                
  115. //        //蜂鸣器报警
  116. //                if(wendu<20)
  117. //                {
  118. //                HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,1);
  119. //                }
  120. //                 else if(wendu>80)
  121. //                {
  122. //                HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,1);
  123. //                }
  124. //                else
  125. //                {
  126. //                HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,0);
  127. //                }



  128.                  
  129.                  
  130.                  
  131.                 //将温度发送出去、通过按键控制收发
  132.                 if(        HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)==0)
  133.                 { HAL_Delay(500);
  134.                         if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)==0)
  135.                         {
  136.                                 E3=0;E4=1;
  137.                         }
  138.                 }
  139.           if(        HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==0)
  140.                 {HAL_Delay(500);
  141.                         if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==0)
  142.                         {
  143.                                 E4=0;E3=1;
  144.                         }
  145.                 }
  146.                
  147.                
  148.                
  149.                 if(E3==0&&E4==1)
  150.                 {LCD_Clear(WHITE);        
  151.                         while(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4))
  152.                         {
  153.                                 HAL_Delay(100);
  154.                                 HAL_ADC_Start(&hadc3);
  155.                 ADCVALUE=HAL_ADC_GetValue(&hadc3);
  156.                 wendu=(float )ADCVALUE/4096.0f*100;
  157.                 sprintf((char*)dianzi,"wendu:%f",wendu);
  158.                                 //
  159.                 NRF24L01_TX_Mode();
  160.                 NRF24L01_TxPacket(dianzi);
  161.                 if(NRF24L01_TxPacket(dianzi)==TX_OK)
  162.                 {                        
  163.                         LCD_ShowString(90,90,200,200,16,(uint8_t*)"success");
  164.                         LCD_ShowxNum(30,0,wendu,3,16,0);
  165.                         NRF24L01_CE_HIGH();
  166.                 }
  167.                 else
  168.                 {
  169.                   LCD_ShowString(30,120,200,200,16,(uint8_t*)"fail");
  170.                         LCD_ShowxNum(30,0,wendu,3,16,0);
  171.                 }
  172.                         }
  173.                         
  174. //                else
  175. //                { LCD_Clear(WHITE);
  176. //                  LCD_ShowString(30,120,200,200,16,(uint8_t*)"fail");
  177. //                        LCD_ShowxNum(30,0,wendu,3,16,0);
  178. //                }
  179.                
  180.          }
  181.                
  182.                 if(E4==0&&E3==1)
  183.                 { LCD_Clear(WHITE);        
  184.                         while(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3))
  185.                         {
  186.                 HAL_Delay(100);
  187.                 HAL_ADC_Start(&hadc3);
  188.                 ADCVALUE=HAL_ADC_GetValue(&hadc3);
  189.                 wendu=(float )ADCVALUE/4096.0f*100;
  190.                 sprintf((char*)dianzi,"wendu:%f",wendu);
  191.                                 //
  192.                 NRF24L01_RX_Mode();
  193.                 NRF24L01_RxPacket(dianzi);
  194.                 LCD_ShowString(90,90,200,200,16,dianzi);
  195.                 NRF24L01_CE_HIGH();
  196.                 LCD_ShowxNum(30,0,wendu,3,16,0);
  197.                 }
  198.         
  199. //                else
  200. //                {
  201. //                LCD_ShowString(30,120,200,200,16,(uint8_t*)"FAIL");
  202. //                }
  203.                 }
  204.                
  205. //               
  206. //                else
  207. //                {
  208. //                LCD_ShowString(30,150,200,200,16,(uint8_t*)"link");
  209. //               
  210. //                }
  211. //               
  212.                
  213.                
  214.                
  215.                
  216.         
  217.                
  218.                
  219.                
  220.                
  221.                
  222.                
  223.                
  224.         }}
  225.   /* USER CODE END 3 */



  226. /**
  227.   * @brief System Clock Configuration
  228.   * @retval None
  229.   */
  230. void SystemClock_Config(void)
  231. {

  232.   RCC_OscInitTypeDef RCC_OscInitStruct;
  233.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  234.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  235.     /**Initializes the CPU, AHB and APB busses clocks
  236.     */
  237.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  238.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  239.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  240.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  241.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  242.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  243.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  244.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  245.   {
  246.     _Error_Handler(__FILE__, __LINE__);
  247.   }

  248.     /**Initializes the CPU, AHB and APB busses clocks
  249.     */
  250.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  251.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  252.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  253.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  254.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  255.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  256.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  257.   {
  258.     _Error_Handler(__FILE__, __LINE__);
  259.   }

  260.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  261.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  262.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  263.   {
  264.     _Error_Handler(__FILE__, __LINE__);
  265.   }

  266.     /**Configure the Systick interrupt time
  267.     */
  268.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  269.     /**Configure the Systick
  270.     */
  271.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  272.   /* SysTick_IRQn interrupt configuration */
  273.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  274. }

  275. /* ADC3 init function */
  276. static void MX_ADC3_Init(void)
  277. {

  278.   ADC_ChannelConfTypeDef sConfig;

  279.     /**Common config
  280.     */
  281.   hadc3.Instance = ADC3;
  282.   hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE;
  283.   hadc3.Init.ContinuousConvMode = DISABLE;
  284.   hadc3.Init.DiscontinuousConvMode = DISABLE;
  285.   hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  286.   hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  287.   hadc3.Init.NbrOfConversion = 1;
  288.   if (HAL_ADC_Init(&hadc3) != HAL_OK)
  289.   {
  290.     _Error_Handler(__FILE__, __LINE__);
  291.   }

  292.     /**Configure Regular Channel
  293.     */
  294.   sConfig.Channel = ADC_CHANNEL_0;
  295.   sConfig.Rank = ADC_REGULAR_RANK_1;
  296.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  297.   if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  298.   {
  299.     _Error_Handler(__FILE__, __LINE__);
  300.   }

  301. }

  302. /* SPI2 init function */
  303. static void MX_SPI2_Init(void)
  304. {

  305.   /* SPI2 parameter configuration*/
  306.   hspi2.Instance = SPI2;
  307.   hspi2.Init.Mode = SPI_MODE_MASTER;
  308.   hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  309.   hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
  310.   hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  311.   hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  312.   hspi2.Init.NSS = SPI_NSS_SOFT;
  313.   hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  314.   hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  315.   hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  316.   hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  317.   hspi2.Init.CRCPolynomial = 10;
  318.   if (HAL_SPI_Init(&hspi2) != HAL_OK)
  319.   {
  320.     _Error_Handler(__FILE__, __LINE__);
  321.   }

  322. }

  323. /** Configure pins as
  324.         * Analog
  325.         * Input
  326.         * Output
  327.         * EVENT_OUT
  328.         * EXTI
  329. */
  330. static void MX_GPIO_Init(void)
  331. {

  332.   GPIO_InitTypeDef GPIO_InitStruct;

  333.   /* GPIO Ports Clock Enable */
  334.   __HAL_RCC_GPIOE_CLK_ENABLE();
  335.   __HAL_RCC_GPIOA_CLK_ENABLE();
  336.   __HAL_RCC_GPIOB_CLK_ENABLE();
  337.   __HAL_RCC_GPIOG_CLK_ENABLE();
  338.   __HAL_RCC_GPIOD_CLK_ENABLE();

  339.   /*Configure GPIO pin Output Level */
  340.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|BEEP_Pin, GPIO_PIN_RESET);

  341.   /*Configure GPIO pin Output Level */
  342.   HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);

  343.   /*Configure GPIO pins : PE3 PE4 */
  344.   GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
  345.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  346.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  347.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  348.   /*Configure GPIO pins : PB0 BEEP_Pin */
  349.   GPIO_InitStruct.Pin = GPIO_PIN_0|BEEP_Pin;
  350.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  351.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  352.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  353.   /*Configure GPIO pins : PG6 PG7 */
  354.   GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  355.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  356.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  357.   HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  358.   /*Configure GPIO pin : PG8 */
  359.   GPIO_InitStruct.Pin = GPIO_PIN_8;
  360.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  361.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  362.   HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  363. }

  364. /* FSMC initialization function */
  365. static void MX_FSMC_Init(void)
  366. {
  367.   FSMC_NORSRAM_TimingTypeDef Timing;

  368.   /** Perform the SRAM1 memory initialization sequence
  369.   */
  370.   hsram1.Instance = FSMC_NORSRAM_DEVICE;
  371.   hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  372.   /* hsram1.Init */
  373.   hsram1.Init.NSBank = FSMC_NORSRAM_BANK4;
  374.   hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
  375.   hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
  376.   hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
  377.   hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
  378.   hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  379.   hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
  380.   hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
  381.   hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
  382.   hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
  383.   hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
  384.   hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  385.   hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
  386.   /* Timing */
  387.   Timing.AddressSetupTime = 15;
  388.   Timing.AddressHoldTime = 15;
  389.   Timing.DataSetupTime = 255;
  390.   Timing.BusTurnAroundDuration = 15;
  391.   Timing.CLKDivision = 16;
  392.   Timing.DataLatency = 17;
  393.   Timing.AccessMode = FSMC_ACCESS_MODE_A;
  394.   /* ExtTiming */

  395.   if (HAL_SRAM_Init(&hsram1, &Timing, NULL) != HAL_OK)
  396.   {
  397.     _Error_Handler(__FILE__, __LINE__);
  398.   }

  399. ……………………

  400. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2018-5-22 18:45 | 只看该作者
因为缺少原理图此贴先不送积分
回复

使用道具 举报

板凳
ID:331083 发表于 2018-5-24 14:08 | 只看该作者

RE: NRF24L01一对一收发的STM32源码

admin 发表于 2018-5-22 18:45
因为缺少原理图此贴先不送积分

原理。。。好吧,这个是我的疏忽






回复

使用道具 举报

地板
ID:331083 发表于 2018-5-24 14:13 | 只看该作者
我要补充原理么,好吧
两个无线模块想要通信,首先收发要用同一个频率。
其次,收发地址是已经默认的,无需修改,只要引用就好。
然后,一个模块(A)工作在发送模式,一旦另一个模块接收到这个数据包,会给A一个信号,表示已经通信成功,两者就可以正常通信啦。



回复

使用道具 举报

5#
ID:295783 发表于 2018-5-25 14:48 | 只看该作者
可以具体说说嘛
你这个是用24l01传送温度数据 如果我想加一些别的功能 你有啥建议吗
我最近也在尝试着做32的无线传输和通讯
希望你可以给我一点帮助
回复

使用道具 举报

6#
ID:331083 发表于 2018-6-5 18:18 | 只看该作者
程序新手 发表于 2018-5-25 14:48
可以具体说说嘛
你这个是用24l01传送温度数据 如果我想加一些别的功能 你有啥建议吗
我最近也在尝试着做3 ...

可以传输你想要的数据,只要收发在同一个频率,两者即可实现通信
回复

使用道具 举报

7#
ID:331083 发表于 2018-6-5 18:20 | 只看该作者
可以互相传输你想传输的数据,未必只是温度,我只是拿温度来做实验,你也可以用别的
回复

使用道具 举报

8#
ID:243748 发表于 2018-6-7 18:22 来自手机 | 只看该作者
很好,希望能用
回复

使用道具 举报

9#
ID:341045 发表于 2018-6-10 10:44 | 只看该作者
好资料, 收藏了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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