本帖最后由 南宫云明 于 2014-8-13 10:43 编辑
void rf_rev_deal()
{
static unsigned int last_cap_v = 0; //保存上一次的捕获值
unsigned int cur_cap_v = 0;
unsigned int cur_pulse_time = 0; //本次脉冲时间差
if((TIM2->CCER1 & TIM2_CCER1_CC1P) == 0) //上升沿
{
TIM2->CCER1 |= TIM2_CCER1_CC1P; //下降沿
}
else
{
TIM2->CCER1 &= (u8)(~TIM2_CCER1_CC1P);
}
cur_cap_v = TIM2_GetCapture1(); //获取捕获值
if(cur_cap_v >= last_cap_v) //计算两次电平差值,计算脉冲时间
cur_pulse_time = cur_cap_v - last_cap_v;
else
cur_pulse_time = 0xffff - last_cap_v + cur_cap_v;
TIM2_ClearITPendingBit(TIM2_IT_CC1);
}
以上时定时器2通道1的捕获中断程序,
还有附上定时器2捕获初始化代码:
void tim2_config()
{
TIM2_ICInit(TIM2_CHANNEL_1, TIM2_ICPOLARITY_RISING, TIM2_ICSELECTION_DIRECTTI, TIM2_ICPSC_DIV1, 0x00);
TIM2_ITConfig(TIM2_IT_CC1, ENABLE);//捕获1中断使能
TIM2_PrescalerConfig(TIM2_PRESCALER_16, TIM2_PSCRELOADMODE_IMMEDIATE);//16分频
TIM2_Cmd(ENABLE);//使能定时器1
}
|