没有使用stm32CubeMX的时候总为stm32的外设配置头疼。过去写代码有时候写完代码和别人比较一下,嗯~~一样的!!!运行一下吧。我(ca)怎么不动,怎么没数据!啊!!!写了一天了,白写。再见!STM32,再见!嵌入式!再见!我的时间。
STM32CubeMX是基于eclipse 的一个插件,用来对STM32产品的配置及初始化代码的生成。
下载地址: STM32CubeMX_STM32初始化代码生成器 版本:5.3.0 更新时间:2019-07-17 官网下载
不过需要注意的是下载请登录,没有就注册一个。哈哈!
很老套的项目:GPIO引脚初始化。
哦,对了生成代码后记得点击打开工程open!!
STM32CobeMX在设计时还是非常注重代码格式的,并已将代码区块分好。这样的程序更加规范易看。
//文档说明
/* USER CODE BEGIN Header */
/* USER CODE END Header */
//系统自动生成头文件和用户所定义的头文件
/* Includes ----------------------------------*/
#include "main.h"
/* Private includes --------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
//用户自定义类型定义
/* Private typedef ---------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
//用户C宏定义
/* 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);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
//用户代码
/* Private user code --------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
//主函数
int main()
{
//用户代码区
/* 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 CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** 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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
/*Configure GPIO pin : PB5 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
//用户代码的实现
/* 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 */
/* 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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */