我用的是stm32f103zet6,利用cubeide控制pwm输出使得PB5端口的led灯的亮度从亮到暗再从暗到亮循环往复地改变,生成一个这样的代码
时间: 2023-06-13 20:04:17 浏览: 117
好的,以下是基于STM32CubeIDE的PWM输出控制代码,可以实现你所需要的功能:
```c
#include "main.h"
/* TIM handler declaration */
TIM_HandleTypeDef htim2;
/* PWM duty cycle */
uint32_t dutyCycle = 0;
/* PWM direction */
uint8_t dir = 1;
/* TIM2 init function */
static void MX_TIM2_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* Peripheral clock enable */
__HAL_RCC_TIM2_CLK_ENABLE();
/* TIM2 interrupt Init */
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
/* TIM2 configuration */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 71;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
/* TIM2 PWM mode configuration */
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* TIM2 interrupt enable */
__HAL_TIM_ENABLE_IT(&htim2, TIM_IT_UPDATE);
/* TIM2 master configuration */
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
}
/* TIM2 interrupt callback */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2)
{
/* Update PWM duty cycle */
if (dir == 1)
{
dutyCycle += 10;
if (dutyCycle >= 1000)
{
dutyCycle = 1000;
dir = 0;
}
}
else
{
dutyCycle -= 10;
if (dutyCycle <= 0)
{
dutyCycle = 0;
dir = 1;
}
}
/* Set PWM duty cycle */
TIM_OC_InitTypeDef sConfigOC = {0};
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = dutyCycle;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
}
}
/* System Clock Configuration */
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();
}
}
/* GPIO initialization */
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO clock enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
/* GPIO configuration */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* Main function */
int main(void)
{
/* MCU Configuration */
HAL_Init();
/* System Clock Configuration */
SystemClock_Config();
/* GPIO initialization */
MX_GPIO_Init();
/* TIM2 initialization */
MX_TIM2_Init();
/* Start TIM2 PWM */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
/* Infinite loop */
while (1)
{
}
}
/* Error handler */
void Error_Handler(void)
{
while (1)
{
}
}
/* IRQ handler for TIM2 */
void TIM2_IRQHandler(void)
{
HAL_TIM_IRQHandler(&htim2);
}
```
这个代码中使用了 TIM2 定时器的 PWM 输出功能,通过改变占空比来实现 LED 亮度的变化。在 TIM2 的定时中断回调函数中,不断更新占空比并重新配置 PWM 占空比,从而实现 LED 亮度的循环变化。
其中,TIM2 的初始化配置如下:
- 预分频器 Prescaler = 71,即定时器时钟频率为 72 MHz / (71 + 1) = 1 MHz;
- 自动重装载寄存器 ARR = 999,即定时器溢出时间为 1 ms;
- PWM 模式下占空比寄存器 CCR1 初值为 0。
你可以将以上代码复制到 CubeIDE 的 main.c 文件中,并在 CubeMX 中配置 TIM2 为 PWM 输出模式,GPIOB5 为复用输出模式,然后编译烧录到 STM32F103ZET6 开发板上,即可看到 LED 的亮度按照你所需要的方式循环变化。
阅读全文