本程序使用STM32F013ZET6編寫
按下按鍵(PE3)為發送模式,按下另一按鍵(PE4)為接收模式
經實測,接收和發送之間的距離在無干擾的情況下,大約為30米。
0.png (45.25 KB, 下載次數: 29)
下載附件
2018-5-22 18:43 上傳
所有資料51hei提供下載:
NRF24L01代碼.7z
(2.91 MB, 下載次數: 111)
2022-12-16 04:44 上傳
點擊文件名下載附件
Keil代碼下載 下載積分: 黑幣 -5
單片機源程序如下:
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2018 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- /* USER CODE BEGIN Includes */
- #include "lcd.h"
- #include "bsp_NRF24L01.h"
- #include "string.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc3;
- SPI_HandleTypeDef hspi2;
- SRAM_HandleTypeDef hsram1;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_FSMC_Init(void);
- static void MX_ADC3_Init(void);
- static void MX_SPI2_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_FSMC_Init();
- MX_ADC3_Init();
- MX_SPI2_Init();
- /* USER CODE BEGIN 2 */
- LCD_Init();
- HAL_SPI_MspDeInit(&hspi2);
- POINT_COLOR=RED;
- NRF24L01_SPI_Init();
- uint16_t ADCVALUE;
- float wendu;
- uint8_t dianzi[8];
- uint8_t E3;
- uint8_t E4;
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
-
-
-
- //ADC開啟、轉換、顯示到屏幕上
- HAL_ADC_Start(&hadc3);
- ADCVALUE=HAL_ADC_GetValue(&hadc3);
- wendu=(float )ADCVALUE/4096.0f*100;
- sprintf((char*)dianzi,"wendu:%f",wendu);
- LCD_ShowxNum(30,0,wendu,3,16,0);
- HAL_Delay(100);
-
- // //蜂鳴器報警
- // if(wendu<20)
- // {
- // HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,1);
- // }
- // else if(wendu>80)
- // {
- // HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,1);
- // }
- // else
- // {
- // HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,0);
- // }
-
-
-
- //將溫度發送出去、通過按鍵控制收發
- if( HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)==0)
- { HAL_Delay(500);
- if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)==0)
- {
- E3=0;E4=1;
- }
- }
- if( HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==0)
- {HAL_Delay(500);
- if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==0)
- {
- E4=0;E3=1;
- }
- }
-
-
-
- if(E3==0&&E4==1)
- {LCD_Clear(WHITE);
- while(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4))
- {
- HAL_Delay(100);
- HAL_ADC_Start(&hadc3);
- ADCVALUE=HAL_ADC_GetValue(&hadc3);
- wendu=(float )ADCVALUE/4096.0f*100;
- sprintf((char*)dianzi,"wendu:%f",wendu);
- //
- NRF24L01_TX_Mode();
- NRF24L01_TxPacket(dianzi);
- if(NRF24L01_TxPacket(dianzi)==TX_OK)
- {
- LCD_ShowString(90,90,200,200,16,(uint8_t*)"success");
- LCD_ShowxNum(30,0,wendu,3,16,0);
- NRF24L01_CE_HIGH();
- }
- else
- {
- LCD_ShowString(30,120,200,200,16,(uint8_t*)"fail");
- LCD_ShowxNum(30,0,wendu,3,16,0);
- }
- }
-
- // else
- // { LCD_Clear(WHITE);
- // LCD_ShowString(30,120,200,200,16,(uint8_t*)"fail");
- // LCD_ShowxNum(30,0,wendu,3,16,0);
- // }
-
- }
-
- if(E4==0&&E3==1)
- { LCD_Clear(WHITE);
- while(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3))
- {
- HAL_Delay(100);
- HAL_ADC_Start(&hadc3);
- ADCVALUE=HAL_ADC_GetValue(&hadc3);
- wendu=(float )ADCVALUE/4096.0f*100;
- sprintf((char*)dianzi,"wendu:%f",wendu);
- //
- NRF24L01_RX_Mode();
- NRF24L01_RxPacket(dianzi);
- LCD_ShowString(90,90,200,200,16,dianzi);
- NRF24L01_CE_HIGH();
- LCD_ShowxNum(30,0,wendu,3,16,0);
- }
-
- // else
- // {
- // LCD_ShowString(30,120,200,200,16,(uint8_t*)"FAIL");
- // }
- }
-
- //
- // else
- // {
- // LCD_ShowString(30,150,200,200,16,(uint8_t*)"link");
- //
- // }
- //
-
-
-
-
-
-
-
-
-
-
-
-
- }}
- /* USER CODE END 3 */
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
- PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* ADC3 init function */
- static void MX_ADC3_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Common config
- */
- hadc3.Instance = ADC3;
- hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE;
- hadc3.Init.ContinuousConvMode = DISABLE;
- hadc3.Init.DiscontinuousConvMode = DISABLE;
- hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc3.Init.NbrOfConversion = 1;
- if (HAL_ADC_Init(&hadc3) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_0;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /* SPI2 init function */
- static void MX_SPI2_Init(void)
- {
- /* SPI2 parameter configuration*/
- hspi2.Instance = SPI2;
- hspi2.Init.Mode = SPI_MODE_MASTER;
- hspi2.Init.Direction = SPI_DIRECTION_2LINES;
- hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
- hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
- hspi2.Init.NSS = SPI_NSS_SOFT;
- hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
- hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi2.Init.CRCPolynomial = 10;
- if (HAL_SPI_Init(&hspi2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOE_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOG_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|BEEP_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
- /*Configure GPIO pins : PE3 PE4 */
- GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
- /*Configure GPIO pins : PB0 BEEP_Pin */
- GPIO_InitStruct.Pin = GPIO_PIN_0|BEEP_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pins : PG6 PG7 */
- GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
- /*Configure GPIO pin : PG8 */
- GPIO_InitStruct.Pin = GPIO_PIN_8;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
- }
- /* FSMC initialization function */
- static void MX_FSMC_Init(void)
- {
- FSMC_NORSRAM_TimingTypeDef Timing;
- /** Perform the SRAM1 memory initialization sequence
- */
- hsram1.Instance = FSMC_NORSRAM_DEVICE;
- hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
- /* hsram1.Init */
- hsram1.Init.NSBank = FSMC_NORSRAM_BANK4;
- hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
- hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
- hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
- hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
- hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
- hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
- hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
- hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
- hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
- hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
- hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
- hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
- /* Timing */
- Timing.AddressSetupTime = 15;
- Timing.AddressHoldTime = 15;
- Timing.DataSetupTime = 255;
- Timing.BusTurnAroundDuration = 15;
- Timing.CLKDivision = 16;
- Timing.DataLatency = 17;
- Timing.AccessMode = FSMC_ACCESS_MODE_A;
- /* ExtTiming */
- if (HAL_SRAM_Init(&hsram1, &Timing, NULL) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
|