////here is PWM and pin initializing and setup section
# include "PWM_OUT. H"
Void TIM3_GPIO_Config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_PinRemapConfig (GPIO_Remap_SWJ_JTAGDisable, ENABLE);//turn off the JTAG, would use the PB4, none can't drive motor
GPIO_PinRemapConfig (GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_4;//PWM output pin
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_7;//v output pin
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init (GPIOC, & amp; GPIO_InitStructure);
}
Void TIM3_PWM_Init (void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
Under-16 CCR1_Val=250;//duty ratio 50%
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
TIM3 Configuration: generate four PWM signals with the four company's duty cycles:
TIM3CLK=72 MHz, Prescaler=0 x0, TIM3 counter clock=72 MHz
TIM3 ARR Register=999=& gt; TIM3 clock Frequency=TIM3 counter/(ARR + 1)
TIM3 Frequency=72 KHz.
TIM3 Channel1 duty cycle=(TIM3_CCR1/TIM3_ARR) * 50%=100
TIM3 Channel2 duty cycle=(TIM3_CCR2/TIM3_ARR) * 37.5%=100
TIM3 Channel3 duty cycle=(TIM3_CCR3/TIM3_ARR) * 25%=100
TIM3 we duty cycle=(TIM3_CCR4/TIM3_ARR) * 12.5%=100
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
TIM_TimeBaseStructure. TIM_Prescaler=17;
TIM_TimeBaseStructure. TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure. TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit (TIM3, & amp; TIM_TimeBaseStructure);
TIM_OCInitStructure. TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure. TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure. TIM_Pulse=CCR1_Val;
TIM_OCInitStructure. TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init (TIM3, & amp; TIM_OCInitStructure);
TIM_OC1PreloadConfig (TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig (TIM3, ENABLE);
TIM_Cmd (TIM3, ENABLE);
}
//////the following is the main function
Int main (void)
{
LED_GPIO_Config ();
TIM3_GPIO_Config ();
TIM3_PWM_Init ();
While (1)
{
TIM_Cmd (TIM3, DISABLE);
GPIO_ResetBits (GPIOC, GPIO_Pin_7);
Delay (0 XFFFF);
TIM_Cmd (TIM3, ENABLE);
Delay (0 XFFFFFFF);
TIM_Cmd (TIM3, DISABLE);
GPIO_SetBits (GPIOC, GPIO_Pin_7);
Delay (0 XFFFF);
TIM_Cmd (TIM3, ENABLE);
Delay (0 XFFFFFFF);
}
}
CodePudding user response:
PWM channel 1, I can delete the other 2, 3, 4