新手入门
- 积分
- 17
- 金钱
- 17
- 注册时间
- 2020-8-4
- 在线时间
- 6 小时
|
10金钱
void PWM_Init(u16 arr, u16 psc){
TIM_HandleTypeDef TIM4_Handler;
TIM_OC_InitTypeDef TIM4_CHnHandler;
TIM4_Handler.Instance = TIM4;
TIM4_Handler.Init.Prescaler = psc;
TIM4_Handler.Init.CounterMode = TIM_COUNTERMODE_DOWN;
TIM4_Handler.Init.Period = arr;
TIM4_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&TIM4_Handler);
TIM4_CHnHandler.OCMode = TIM_OCMODE_PWM1;
TIM4_CHnHandler.Pulse = arr / 2;
TIM4_CHnHandler.OCPolarity = TIM_OCPOLARITY_HIGH;
HAL_TIM_PWM_ConfigChannel(&TIM4_Handler, &TIM4_CHnHandler, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&TIM4_Handler, TIM_CHANNEL_1);
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_TIM4_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_Initure.Pin = GPIO_PIN_12; //PD12
GPIO_Initure.Mode = GPIO_MODE_AF_PP;
GPIO_Initure.Pull = GPIO_PULLUP;
GPIO_Initure.Speed = GPIO_SPEED_HIGH;
GPIO_Initure.Alternate = GPIO_AF2_TIM4;
HAL_GPIO_Init(GPIOD, &GPIO_Initure);
}
void TIM_SetTIM4Compare1(u32 compare)
{
TIM4->CCR1 = compare;
}
我是按照PWM例程中定时器2pwm的配置方式配置定时器4的pwm输出的,接上小灯的时候调试灯没亮,为什么定时器4的pwm无法使用?上面的配置除了什么问题?
|
|