四路PWM波的输出
初始化函数
void TIM3_PWM_Init
(u16 arr,u16 psc
)
{
GPIO_InitTypeDef GPIO_InitStructure
;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure
;
TIM_OCInitTypeDef TIM_OCInitStructure
;
RCC_APB1PeriphClockCmd
(RCC_APB1Periph_TIM3, ENABLE
);//初始化TIM3的时钟
RCC_APB2PeriphClockCmd
(RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB , ENABLE
); //初始化IO口的始终
GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_6
; //TIM_CH1
GPIO_InitStructure.GPIO_Mode
= GPIO_Mode_AF_PP
; //复用推挽输出
GPIO_InitStructure.GPIO_Speed
= GPIO_Speed_50MHz
;
GPIO_Init
(GPIOA,
&GPIO_InitStructure
);
GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_7
; //TIM_CH2
GPIO_Init
(GPIOA,
&GPIO_InitStructure
);
GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_0
; //TIM_CH3
GPIO_Init
(GPIOB,
&GPIO_InitStructure
);
GPIO_InitStructure.GPIO_Pin
= GPIO_Pin_1
; //TIM_CH4
GPIO_Init
(GPIOB,
&GPIO_InitStructure
);
TIM_TimeBaseStructure.TIM_Period
= arr
;
TIM_TimeBaseStructure.TIM_Prescaler
=psc
;
TIM_TimeBaseStructure.TIM_ClockDivision
= 0
;
TIM_TimeBaseStructure.TIM_CounterMode
= TIM_CounterMode_Up
;
TIM_TimeBaseInit
(TIM3,
&TIM_TimeBaseStructure
);
TIM_OCInitStructure.TIM_OCMode
= TIM_OCMode_PWM1
;
TIM_OCInitStructure.TIM_OutputState
=TIM_OutputState_Enable
;
TIM_OCInitStructure.TIM_Pulse
=0
;
TIM_OCInitStructure.TIM_OCPolarity
=TIM_OCPolarity_High
;
TIM_OC1Init
(TIM3,
&TIM_OCInitStructure
);
TIM_OC2Init
(TIM3,
&TIM_OCInitStructure
);
TIM_OC3Init
(TIM3,
&TIM_OCInitStructure
);
TIM_OC4Init
(TIM3,
&TIM_OCInitStructure
);
TIM_CtrlPWMOutputs
(TIM3,ENABLE
);
TIM_OC1PreloadConfig
(TIM3, TIM_OCPreload_Enable
);
TIM_OC2PreloadConfig
(TIM3, TIM_OCPreload_Enable
);
TIM_OC3PreloadConfig
(TIM3, TIM_OCPreload_Enable
);
TIM_OC4PreloadConfig
(TIM3, TIM_OCPreload_Enable
);
TIM_ARRPreloadConfig
(TIM3, ENABLE
);
TIM_Cmd
(TIM3, ENABLE
);
}
这里要注意对于一些IO口初始化后不能输出PWM波,可能是因为该IO口被用作别的用途了(比如Jlink),应该先禁掉 调用 void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
main函数
TIM3_PWM_Init
(7199,0
);
TIM3-
>CCR1
=0
;
TIM3-
>CCR2
=0
;
TIM3-
>CCR3
=0
;
TIM3-
>CCR4
=0
;