void TIM2_PWM_INIT(void) //TIM3配置PWM输出参数
{
u32 temp_fre,temp_duty;
TempE = 30; //设置频率初始值
TempF = 4; //设置初始占空比1 / 2
temp_fre = (1000000 / TempE) - 1;
temp_duty = ((temp_fre + 1) / TempF);
TIM_TimeBaseStructure.TIM_Period = temp_fre; //TIM3通道4的频率
TIM_TimeBaseStructure.TIM_Prescaler = 72;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = temp_duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_PWM_IN_INIT(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
/* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
}
请看上面的例子!使用TIM2的通道4输出PWM,同时通道2作为PWM捕获输入;发现个现象,PWM输入的频率最小值不能小于PWM输出的值。程序里写的是30HZ,那么输入的PWM波形频率不能小于30HZ;然后,用示波器观察,TIM2的PWM输出频率不能小于50HZ,程序里设置的30HZ,按照理论计算72分频后,1000000/65536 < 20HZ的。可是没办法输出低于50HZ的PWM,请知道的指点一下啊!没办法,我要用STM32F103C8T6来输出4路频率可调,脉宽可调的PWM,还要输入一路PWM,定时器1,2,3,4都用完了,要是再多个定时器就好了! |