void TIM2_PWM_Init(u16 arr,u16 psc)
{
TIM_OC_InitTypeDef TIM2_CHxHandler;
TIM2_Handler.Instance=TIM2;
TIM2_Handler.Init.Prescaler=psc;
TIM2_Handler.Init.CounterMode=TIM_COUNTERMODE_UP;
TIM2_Handler.Init.Period=arr;
TIM2_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&TIM2_Handler);
TIM2_CHxHandler.OCMode=TIM_OCMODE_PWM1;
TIM2_CHxHandler.Pulse=(arr+1)/2-1;
TIM2_CHxHandler.OCPolarity=TIM_OCPOLARITY_LOW;
HAL_TIM_PWM_ConfigChannel(&TIM2_Handler,&TIM2_CHxHandler,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&TIM2_Handler,TIM_CHANNEL_2);
}
void TIM1_PWM_Init(u16 arr,u16 psc)
{
TIM_OC_InitTypeDef TIM1_CHxHandler;
TIM1_Handler.Instance=TIM1;
TIM1_Handler.Init.Prescaler=psc;
TIM1_Handler.Init.CounterMode=TIM_COUNTERMODE_UP;
TIM1_Handler.Init.Period=arr;
TIM1_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&TIM1_Handler);
TIM1_CHxHandler.OCMode=TIM_OCMODE_PWM1;
TIM1_CHxHandler.Pulse=(arr+1)/2-1;
TIM1_CHxHandler.OCPolarity=TIM_OCPOLARITY_LOW;
HAL_TIM_PWM_ConfigChannel(&TIM1_Handler,&TIM1_CHxHandler,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&TIM1_Handler,TIM_CHANNEL_1);
HAL_TIM_PWM_Stop(&TIM1_Handler,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&TIM1_Handler,TIM_CHANNEL_1);
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{if(htim==&TIM1_Handler)
{
__HAL_RCC_TIM1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_8; //PA3
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP; //éÏà-
GPIO_Initure.Speed=GPIO_SPEED_HIGH;
GPIO_Initure.Alternate= GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
}
else if(htim==&TIM2_Handler)
{
__HAL_RCC_TIM2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
//PB3
GPIO_Initure.Pin=GPIO_PIN_3;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP;
GPIO_Initure.Speed=GPIO_SPEED_HIGH;
GPIO_Initure.Alternate= GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
}
}
int main(void)
{
HAL_Init();
Stm32_Clock_Init(360,25,2,8);
delay_init(180);
uart_init();
LED_Init();
TIM1_PWM_Init(500-1,90-1);
TIM2_PWM_Init(500-1,90-1);
while(1)
{
delay_ms(100);
LED0=!LED0;
}
}
上面是我的定时器TIM1和TIM2的配置,但是TIM1没有脉冲输出,TIM2输出正常,不知道什么原因,希望有做过的人给一下指导意见!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
标准库:
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
一周热门 更多>