实现方法:1.配置TIMx
//F=TIMCLK{36M,72M}/.(TIM_Prescaler+1)/(TIM_Period+1)
TIM_TimeBaseStructure.TIM_Prescaler = 0; ////时钟预分频数 例如 :时钟频率=72/(时钟预分频+1)。 //72M/7200=10k
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 799;// 自动重装载寄存器周期的值(定时时间)累计 0xFFFF个频率后产生个更新或者中断(也是说定时时间到)。
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
2.配置TIMx的OC 就是输出比较
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM2 ; //PWM1只能产生频率较低的 TIM_OCMode_Toggle
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = dutytime; //控制占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, ENABLE);
3.配置OC的中断.必须配置
//TIM1中断源设置,开启相应通道的捕捉比较中断
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
4.启动TIMx和OC
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
5.配置TIMx的OC的中断句柄
!!这里必须注意三点:
(1)即使OC句柄没有被挂起.依旧必须清除它的挂起标志位
(2)必须重装OC的dutytime
(3)必须清除OC中断标志
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1 );
// TIM_ClearITPendingBit(TIM1, TIM_IT_CC2 );
//TIM_OCInitStructure.TIM_Pulse = dutytime; //注意 这两句必须去掉!
//TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_CC1);
TIM_ClearFlag(TIM1, TIM_FLAG_CC2);
结束.至此.可以在TIMx的通道检测到方波.用于触发AD
|