如何使用STM32F103ZET6使用TIM5和PF4输出PWM波形
时间: 2024-06-12 11:05:25 浏览: 102
实验9 PWM输出实验.zip_PWM输出_stm32f103zet6_stm32f103zet6 pwm
5星 · 资源好评率100%
步骤如下:
1. 打开STM32CubeMX软件,在Pinout选项卡中将PF4配置为TIM5_CH4功能。
2. 在Configuration选项卡中,选择TIM5,并启用PWM输出模式。
3. 配置TIM5的时钟源和分频系数,以及PWM的周期和占空比。
4. 在代码中初始化TIM5,并启用PWM输出。
5. 在主循环中更新PWM占空比,以改变输出波形。
下面是示例代码:
```
#include "stm32f1xx_hal.h"
TIM_HandleTypeDef htim5;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM5_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM5_Init();
HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_4);
while (1)
{
// Update PWM duty cycle to change output waveform
__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_4, 500);
HAL_Delay(1000);
__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_4, 1000);
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__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_TIM5_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim5.Instance = TIM5;
htim5.Init.Prescaler = 7199;
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
htim5.Init.Period = 999;
htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 500;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim5);
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOF_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim_pwm)
{
if (htim_pwm->Instance == TIM5)
{
__HAL_RCC_TIM5_CLK_ENABLE();
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (htim->Instance == TIM5)
{
__HAL_RCC_GPIOF_CLK_ENABLE();
/**TIM5 GPIO Configuration
PF4 ------> TIM5_CH4
*/
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
}
}
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim_pwm)
{
if (htim_pwm->Instance == TIM5)
{
__HAL_RCC_TIM5_CLK_DISABLE();
}
}
void Error_Handler(void)
{
while (1)
{
}
}
```
在上面的示例代码中,PWM的周期为10ms,占空比从50%到100%不断变化,即输出的波形周期为10ms,高电平占空比从50%到100%不断变化。可以根据需要修改周期和占空比,以输出不同的波形。
阅读全文