本帖最后由 PhycoKnight 于 2015-8-31 17:16 编辑
void IR_PWM_Cfg(u32 arr, u32 duty_circle)
{
TIM_OC_InitTypeDef OCConfig;
//TIM2
Tim2Handle.Instance = TIM2;
Tim2Handle.Init.Prescaler = 60 - 1; //计数器频率被分频为1MHz
Tim2Handle.Init.Period = arr;
Tim2Handle.Init.ClockDivision = 0;
Tim2Handle.Init.CounterMode = TIM_COUNTERMODE_DOWN;
HAL_TIM_PWM_Init(&Tim2Handle);
OCConfig.OCMode = TIM_OCMODE_PWM1;
OCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
OCConfig.OCFastMode = TIM_OCFAST_DISABLE;
OCConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
OCConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
OCConfig.Pulse = (uint32_t)(arr+1)*(duty_circle-1)/duty_circle;
HAL_TIM_PWM_ConfigChannel(&Tim2Handle, &OCConfig, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&Tim2Handle, TIM_CHANNEL_2);
// HAL_TIM_PWM_Start_IT(&Tim2Handle, TIM_CHANNEL_2); //注意:这个是打开捕获的外部中断的,不是更新中断。
这个配置应该不难吧。还要注意打开时钟,然后GPIO口设置为复用推挽
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|