GD32F103初始化
时间: 2023-05-17 17:06:54 浏览: 114
STM32F103_初始化程序_STM32F103_
5星 · 资源好评率100%
可以使用标准的STM32 HAL库进行GD32F103的初始化,具体步骤如下:
1. 配置系统时钟,包括外部晶振、PLL倍频等参数。
2. 配置GPIO口,包括输入输出模式、上下拉电阻等参数。
3. 配置定时器、串口、SPI等外设,包括时钟源、波特率、数据格式等参数。
4. 启动外设,开始工作。
以下是一个简单的GD32F103初始化代码示例:
#include "stm32f1xx_hal.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
while (1)
{
// main loop
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
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.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
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();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
static void MX_USART1_UART_Init(void)
{
USART_InitTypeDef USART_InitStruct;
__HAL_RCC_USART1_CLK_ENABLE();
USART_InitStruct.BaudRate = 115200;
USART_InitStruct.WordLength = USART_WORDLENGTH_8B;
USART_InitStruct.StopBits = USART_STOPBITS_1;
USART_InitStruct.Parity = USART_PARITY_NONE;
USART_InitStruct.Mode = USART_MODE_TX_RX;
USART_InitStruct.CLKPolarity = USART_POLARITY_LOW;
USART_InitStruct.CLKPhase = USART_PHASE_1EDGE;
USART_InitStruct.CLKLastBit = USART_LASTBIT_DISABLE;
if (HAL_USART_Init(&huart1, &USART_InitStruct) != HAL_OK)
{
Error_Handler();
}
}
阅读全文