捕捉实现了。同步也实现了。
TIM2负责输入捕捉,然后在TIM2中断里给TIM3的ARR和CCR赋值,但又碰到一个问题。我TIM2输入的脉冲不是持续运行的。时断时续
我该用什么办法控制TIM3比较好? 目前的想法是SYSTICK,每隔多少时间,读一次IC2Value = TIM_GetCapture2(TIM2);,大于0,使能TIM3,=0就禁止TIM3,还有更好的办法吗?
//输入捕获
static void TIM2_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_DeInit(TIM2);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xffff;
//TIM_TimeBaseStructure.TIM_Prescaler = 719;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
//TIM输入2,3或4选择对应地与IC1或IC2或IC3或IC4相连
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
//TIM捕获在捕获输入上每探测到一个边沿执行一次 TIM_ICPSC_DIV2\TIM_ICPSC_DIV4\TIM_ICPSC_DIV8
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
//选择输入比较滤波器 0x0 - 0xF
TIM_ICInitStructure.TIM_ICFilter = 0x0; //
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
/* Select the TIM3 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);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_ISR(void)
{
Uint16 TIM3_Period; //ARR
Uint16 TIM3_CCR3_Val; //CCR3
Uint16 IC2Value;
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); //清除中断标志位
IC2Value = TIM_GetCapture2(TIM2);
if(IC2Value != 0)
{
//DutyCyle = (TIM_GetCapture1(TIM3) * 100) / IC2Value; //占空比计算
//TIM3_Period = 72000000 / IC2Value; //0 -72M
//TIM3_Period = 1000000 / IC2Value; //71 -1M
//TIM3_Period = 100000 / IC2Value; //719 -100K
/*
同频捕获同频率输出
TIM_Prescaler = 71, 即(71+1)=72分频,时钟频率降为1M
TIM2和TIM3同频率,TIM2每个时钟周期捕获到的脉冲不用计算,可以直接输出给TIM3
*/
TIM3_Period = IC2Value;
TIM3_CCR3_Val = ((TIM3_Period + 1)/2);
}
else
{
TIM3_Period = 0;
TIM3_CCR3_Val = 0;
}
/* 同步改变TIM3步进脉冲 */
TIM3->ARR=TIM3_Period;//设定计数器自动重装值
TIM3->CCR3=TIM3_CCR3_Val;
}
中断处理里还有一个现象,因为我TIM2和TIM3是同时钟频率的。TIM2捕捉到的就直接赋值给TIM3了,经初步检测,好像没问题。不知道这样做行不行?
|