想写一个tim定时器输出一个频率可变的pwm波形的程序,用for循环改变tim定时器的自动重载值。可是从第一个for循环跳到第二个for循环的时候tim定时器不输出pwm波形了,用断点判断程序是执行了tim的初始化和配置步骤的。
for(freq=3000;freq<=7000;freq+=100)
{
TIM2_Init(freq);
}
for(freq=freq-100;freq<=freq+100;freq+=10)
{
TIM2_Init(freq);
}
下面是定时器的配置函数
void TIM2_cfg(int freq)
{
PrescalerValue = 0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
freq=42000000/freq-1;
TIM_TimeBaseStructure.TIM_Period =freq;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = freq/2;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
} |