本帖最后由 JX1417 于 2023-4-23 09:33 编辑
void TIM8_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitType GPIO_InitStructure;
TIM_TimeBaseInitType TIM_TimeBaseStructure;
OCInitType TIM_OCInitStructure;
//时钟初始化
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_TIM8);
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
/* TIM1 CHx 输出 */
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF7_TIM8;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
//外设初始化
TIM_Base_Struct_Initialize(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.Period = arr;
TIM_TimeBaseStructure.Prescaler = psc;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_Base_Initialize(TIM8, &TIM_TimeBaseStructure);
//pwm通道配置
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM2;//使用pwm2模式
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;//pwm 输出使能--内部通过这个直接控制了pwm输出,不需要再次调用 void TIM_Capture_Compare_Ch_Enable(TIM_Module* TIMx, uint16_t channel);
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;//互补输出使能
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_LOW;//有效电平
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_LOW;//有效空闲电平
TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_SET;//空闲电平
TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;//互补空闲电平
TIM_Compare_Capture4_Get(TIM8);
TIM_OCInitStructure.Pulse = 0;
TIM_Output_Channel4_Initialize(TIM8, &TIM_OCInitStructure);
//使能定时器
TIM_On(TIM8);
//使能pwm输出--就是刹车寄存区的控制,普通定时器不需要
TIM_PWM_Output_Enable(TIM8);
} |