跳转至

rocketpi_led

功能展示

led gif

1.进入mcu选择器界面

  • 打开STM32cubeMX,点击ACCESS TO MCU SELECTOR

image-20251106011342611

2.选择芯片型号

  • 步骤1:在Commercial Part Number 输入 F401RE
  • 步骤2:选择STM32F401RET6 (其他芯片可自行选择)
  • 步骤3:点击Start Project

image-20251106011635971

3.晶体振荡器选择

  • 步骤1:点击System Core
  • 步骤2:点击RCC
  • 步骤3:High Speed Clock(HSE) 选择 Crystal/Ceramic Resonator

image-20251106012005990

4.时钟配置

  • 步骤1:点击Clock Configuration
  • 步骤2:input frequency输入8 (因为 rocketpi 硬件设计使用外部晶振为8Mhz,所以此处输入8)
  • 步骤3:选择HSE
  • 步骤4:在HCLK的输入框中输入84,接着回车,回车之后可能会弹出一个确认框,点击OK,会自动配置时钟,等待配置完毕

image-20251106013613146

5.配置调试接口

  • 步骤1:点击SYS
  • 步骤2:Debug选择栏 选择Serial Wire

image-20251106014323818

6.配置LED的io为输出

在配置之前看原理图,我们需要看led的原理图,找到LED的原理图部分,从原理可以获得的信息

  • 三个LED连接的引脚分别为 PA1,PB10,PB14
  • 三个LED都是低电平点亮

image-20251106014804410

图形化配置

  • 步骤1:搜索框中输入PA1,回车,这时候PA1的引脚会闪烁
  • 步骤2:点击闪烁的PA1
  • 步骤3:将PA1设置为GPIO_Output

image-20251106015144572

接下来另外两个led的io PB10,PB14 操作也和上述一样

  • 配置完成后确保这三处地地方一致

image-20251106015611887

点击GPIO,可以看到我们的配置的三个LED出现在这里了

image-20251106020244581

6.配置LE的电气属性

由于三个LED都是低电平点亮,一般 初始化的过程,我们会配置关闭LED,

  • 分别点击 PA1,PB10,PB14,将GPIO output level 属性设置为high,配置后烧录程序默认会关闭led
  • 默认的GPIO mode 是推挽输出,所以此处我们不需要更改,至此led的配置已经完成了

image-20251106020625380

7.生成代码

  • 步骤1:点击Project Manager
  • 步骤2:填写此次要创建的工程名字,(注意这里一定不要使用中文名)
  • 步骤3:这个工程保存的路径(注意这里一定不要有中文路径,否则生成的工程会有问题)
  • 步骤4:生成的工程选择 MDK-ARM 版本为v5.27(版本可以根据自己安装keil版本选择,一般来说高版本的keil可以向下兼容)

image-20251106021327300

  • 点击Code Generator
  • 软件包选择 copy only the necessary files (仅复制使用到的库文件)
  • 生成的文件选择 Generate peripheral initialization as a pair .c/.h files perpheral

image-20251106022353098

最后,点击GENERATE CODE 即可生成代码

image-20251106022513985

8.打开keil工程

  • 点击OPen Project即可打开刚创建好的工程(电脑已经安装了keil的前提下)

image-20251106022629403

9.编译

image-20251106023109038

确保编译日志 0错误,0警告(这一步出问题了就是工程名字或者工程路径存在中文)

image-20251106023207979

10.编写led的闪烁代码

  • 找到main.c文件,在while中加入这段代码,一个三个led等闪烁的代码就做好了

代码释意

  • 可以看做三段代码,第一段保持第一个led打开,其他两个led关闭,延时500ms
  • 第二段保持第二个led打开,其他两个led关闭,延时500ms
  • 第三段保持第三个led打开,其他两个led关闭,延时500ms
  • 这三段放在循环中,就可以看到led不断地闪烁了
   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);  
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET);
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
   HAL_Delay(500);

   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);  
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET);
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
   HAL_Delay(500);

   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);  
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET);
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
   HAL_Delay(500);

image-20251106023725046

11.下载验证结果

驱动以及测试代码

Core/Src/main.c
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);  
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET);
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
     HAL_Delay(500);

     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);  
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET);
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
     HAL_Delay(500);

     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);  
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET);
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
     HAL_Delay(500);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 84;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses 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();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */