用了定时器的输入捕获功能来测得频率,有两个中断,一个是上升沿信号中断,一个是计数器溢出中断,我现在不懂那个500 是咋回事,测着测着突然出来这么一个,每次都是实际值的一半
代码,上升沿中断和溢出中断, 第一次溢出后开始取数,用上这次上升沿的数值减去一次上升沿的数值得出信号源频率值,但是偶尔会出现捕获值翻倍,好像漏掉了一次上升沿一样,我开启了 溢出判断也并没有进入贴一下中断服务函数
void TIMER2_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_UP))
{
timer_interrupt_flag_clear(TIMER2,TIMER_INT_UP);
timer2_counter_overflow++;
timer2_interrupt_clear();
update_flag++;
if(timer2_counter_overflow==2)
{
timer2_interrupt_disable(); //double time capture,????????g???????2.????????
timer_disable(TIMER2);
timer2_counter_overflow=0;
timer2_interrupt_clear();
}
}
if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_CH0))
{
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH0);
/* !!!!!!!!!! */interrupt_flag1++; /* !!!!!!!!!!!!!! */
/* read channel 0 capture value */
/* read channel 0 capture value */
if((timer2_counter_overflow==1) && (RESET == timer_flag_get(TIMER2,TIMER_INTF_CH0OF)))//wu yi chu
{
if(0 == ccnumber0)
{
CH0_CAPTURE_OLD = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_0)+1;
ccnumber0 = 1;
}
else if(1 == ccnumber0)
{
CH0_CAPTURE_NEW = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_0)+1;
if(CH0_CAPTURE_NEW > CH0_CAPTURE_OLD)
{
ic1value = CH0_CAPTURE_NEW - CH0_CAPTURE_OLD;
}
else{
ic1value = ((0xFFFF - CH0_CAPTURE_OLD) + CH0_CAPTURE_NEW);
}
fre_highcapture_1= 10000000/ ic1value;
/* !!!!!!!!!! */fre_highcapture_1_OLD=fre_highcapture_1;
if(fre_highcapture_1<9980)
{
/* !!!!!!!!!! */int_CH0_flag=1; /* !!!!!!!!!!!!!!!!! */
}
if(fre_highcapture_1>10012)
{
/* !!!!!!!!!! */int_CH0_flag=1; /* !!!!!!!!!!!!!!!!! */
}
int_CH0_flag=1;
ccnumber0 = 0;
}
}
if((timer2_counter_overflow==1) &&(SET == timer_flag_get(TIMER2,TIMER_INTF_CH0OF)))//you yi chu
{
timer_flag_clear(TIMER2,TIMER_INTF_CH0OF);
/* !!!!!!!!!! */fre_highcapture_1=fre_highcapture_1_OLD;
/* !!!!!!!!!! */timer2_interrupt_disable(); /* !!!!!!!!!! */
/* !!!!!!!!!! */timer_disable(TIMER2); /* !!!!!!!!!!!!!!! */
int_CH0_flag=1;
ccnumber0 = 0;
}
}
if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_CH1))
{
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH1);
/* !!!!!!!!!! */ interrupt_flag2++; /* !!!!!!!!!!!! */
/* read channel 0 capture value */
if((timer2_counter_overflow==1) && (RESET == timer_flag_get(TIMER2,TIMER_INTF_CH1OF)))
{
if(0 == ccnumber1)
{
CH1_CAPTURE_OLD = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_1)+1;
ccnumber1 = 1;
}
else if(1 == ccnumber1)
{
CH1_CAPTURE_NEW = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_1)+1;
if(CH1_CAPTURE_NEW > CH1_CAPTURE_OLD)
{
ic2value = CH1_CAPTURE_NEW - CH1_CAPTURE_OLD;
}
else{
ic2value = ((0xFFFF - CH1_CAPTURE_OLD) + CH1_CAPTURE_NEW);
}
fre_highcapture_2= 10000000/ ic2value;
/* !!!!!!!!!! */fre_highcapture_2_OLD=fre_highcapture_2;
if(fre_highcapture_2<9980)
{
/* !!!!!!!!!! */int_CH1_flag=1; /* !!!!!!!!!!!!!!!!! */
}
if(fre_highcapture_2>10012)
{
/* !!!!!!!!!! */int_CH1_flag=1; /* !!!!!!!!!!!!!!!!! */
}
int_CH1_flag=1;
ccnumber1 = 0;
}
}
if((timer2_counter_overflow==1) &&(SET == timer_flag_get(TIMER2,TIMER_INTF_CH1OF)))
{
timer_flag_clear(TIMER2,TIMER_INTF_CH1OF);
fre_highcapture_2=fre_highcapture_2_OLD;
/* !!!!!!!!!! */timer2_interrupt_disable(); /* !!!!!!!!!! */
/* !!!!!!!!!! */timer_disable(TIMER2); /* !!!!!!!!!!!!!!! */
int_CH1_flag=1;
ccnumber1 = 0;
}
}
if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_CH2))
{
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH2);
/* !!!!!!!!!! */ interrupt_flag3++;/* !!!!!!!!!! */
if((timer2_counter_overflow==1) && (RESET == timer_flag_get(TIMER2,TIMER_INTF_CH2OF)))
{
if(0 == ccnumber2)
{
CH2_CAPTURE_OLD = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_2)+1;
ccnumber2 = 1;
}
else if(1 == ccnumber2)
{
CH2_CAPTURE_NEW = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_2)+1;
if(CH2_CAPTURE_NEW > CH2_CAPTURE_OLD)
{
ic3value = CH2_CAPTURE_NEW - CH2_CAPTURE_OLD;
}
else
{
ic3value = ((0xFFFF - CH2_CAPTURE_OLD) + CH2_CAPTURE_NEW);
}
fre_highcapture_3= 10000000/ ic3value;
if(fre_highcapture_3<9980)
{
/* !!!!!!!!!! */int_CH2_flag=1; /* !!!!!!!!!!!!!!!!! */
}
if(fre_highcapture_3>10012)
{
/* !!!!!!!!!! */int_CH2_flag=1; /* !!!!!!!!!!!!!!!!! */
}
int_CH2_flag=1;
ccnumber2 = 0;
}
}
if((timer2_counter_overflow==1) && (SET == timer_flag_get(TIMER2,TIMER_INTF_CH2OF)))
{
timer_flag_clear(TIMER2,TIMER_INTF_CH2OF);
/* !!!!!!!!!! */fre_highcapture_3=fre_highcapture_3_OLD;
/* !!!!!!!!!! */timer2_interrupt_disable(); /* !!!!!!!!!! */
/* !!!!!!!!!! */timer_disable(TIMER2); /* !!!!!!!!!!!!!!! */
int_CH2_flag=1;
ccnumber2 = 0;
}
}
if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_CH3))
{
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH3);
/* !!!!!!!!!! */interrupt_flag4++;/* !!!!!!!!!! */
if((timer2_counter_overflow==1) && (RESET == timer_flag_get(TIMER2,TIMER_INTF_CH3OF)))
{
if(0 == ccnumber3)
{
CH3_CAPTURE_OLD = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_3)+1;
ccnumber3 = 1;
}
else if(1 == ccnumber3)
{
CH3_CAPTURE_NEW = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_3)+1;
if(CH3_CAPTURE_NEW > CH3_CAPTURE_OLD)
{
ic4value = CH3_CAPTURE_NEW - CH3_CAPTURE_OLD;
}
else{
ic4value = ((0xFFFF - CH3_CAPTURE_OLD) + CH3_CAPTURE_NEW);
}
fre_highcapture_4 = 10000000/ ic4value;
/* !!!!!!!!!! */fre_highcapture_4_OLD=fre_highcapture_4;
if(fre_highcapture_4<9980)
{
/* !!!!!!!!!! */int_CH3_flag=1; /* !!!!!!!!!!!!!!!!! */
}
if(fre_highcapture_4>10012)
{
/* !!!!!!!!!! */int_CH3_flag=1; /* !!!!!!!!!!!!!!!!! */
}
int_CH3_flag=1;
ccnumber3 = 0;
}
}
if((timer2_counter_overflow==1) && (SET == timer_flag_get(TIMER2,TIMER_INTF_CH3OF)))
{
timer_flag_clear(TIMER2,TIMER_INTF_CH3OF);
/* !!!!!!!!!! */fre_highcapture_4=fre_highcapture_4_OLD;/* !!!!!!!!!! */
/* !!!!!!!!!! */timer2_interrupt_disable(); /* !!!!!!!!!! */
/* !!!!!!!!!! */timer_disable(TIMER2); /* !!!!!!!!!!!!!!! */
int_CH3_flag=1;
ccnumber3 = 0;
}
}
}
|