目录
3. 时钟系统配置(PLL、SYSCLK、AHB、APB1、APB2等)
5. Cortex内核配置(SYS(DEBUG)配置、NVIC(优先级分组))
1. 新建工程,选择芯片型号
打开软件,找到“File”,点击“New Project...”。
第一次新建工程可能需要下载一些东西,等待下载完成。
进入如下界面。
根据使用的单片机型号进行搜索,双击搜索到的内容。
进入如下界面。
2. 时钟模块配置(设置HSE、LSE、MCO)
点击“System Core”,选择“RCC”,按照如下进行配置HSE和LSE。
MCO 指的是 STM32 微控制器中的主时钟输出(Main Clock Output)。在 STM32 微控制器中,MCO 是一个非常有用的功能,可以将系统时钟(HCLK)或者外部时钟信号输出到指定的引脚上,用于外部设备的时钟同步或者调试等目的。
通过配置 MCO 功能,可以选择输出不同频率的时钟信号,并且可以选择输出源为系统时钟(HCLK)、外部晶振时钟、PLL 时钟等。这使得开发人员可以方便地监视和利用 STM32 微控制器的时钟信号,用于系统调试、性能优化和外部设备的时钟同步等应用。
要使用 MCO 功能,通常需要在 STM32CubeMX 或者代码中进行相应的配置,设置 MCO 的输出源和输出频率。然后将 MCO 引脚连接到外部设备上,即可实现时钟信号的输出。
不过这里我们暂时不使用该功能,因此不用勾选
3. 时钟系统配置(PLL、SYSCLK、AHB、APB1、APB2等)
由于我使用的是STM32F103C8T6最小系统板,因此这里根据STM32F1系列的用户手册,进行配置。
其他配置这里暂时用不到。
4. GPIO引脚配置
寻找自己想要配置的引脚,如实引脚过多可以在搜索框搜索,搜索完对应引脚会进行闪烁,如下图:
点击闪烁的引脚进行配置。
这里我们将PA1引脚,配置为输出模式,如下图:
然后按照下图点击:
点击黑框框住的部分,显示如下,其中“LOW”表示是低电平触发。“High”表示是高电平触发。
对于“GPIO mode”,其中“Output Push Pull”表示推挽输出,“Output Open Pull”表示开漏输出。
配置上下拉模式,这里既不进行上拉,也不下拉,所以选择“No pull-up and no pull-down”。
输出速度,低中高速,根据需求进行选择。
最后一项是宏定义,根据喜好对引脚进行命名。
5. Cortex内核配置(SYS(DEBUG)配置、NVIC(优先级分组))
SYS
NVIC
具体介绍参考:
6. 生成工程代码
以上配置完成,点击如下按钮,即可生成代码:
其中绿色圈住表示关闭文件,但是可以在之前创建的文件保存路径中找到文件进行打开,黑色表示直接打开。
7. 编写用户程序
生成的主程序,在此程序进行相关更改。
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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)
{
/* 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};
/** 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.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();
}
/** 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 */