看看这个
//--------------------------转速计算------------------------------*
#pragma CODE_SEG NON_BANKED
void interrupt 8 Timer0_ISR(void)
{
TFLG1_C0F=1; //清中断标志位
if(Pulse_Num==0) //捕捉到第一个脉冲上升沿
{
Pulse_Num=1;
PulseEnable=1;
Last_Period=TC0;
TSCR2_TOI = 1; //定时器溢出中断使能
}
else
{
if(PulseEnable==1)
{
Pulse_Num++;
Now_Period=TC0;
Period=65535*Overflowtime+Now_Period - Last_Period;
SpeedV=2812500/Period;
if(Pulse_Num<10)
{
Speed0[i]=SpeedV;
i++;
Speed=SpeedV;
Last_Period=Now_Period;
}
else{
}
if(Pulse_Num==9)
{
PulseEnable=0;
}
else{
}
}
else
{
Now_Period=TC0;
if(Overflowtime<3)
{
Period=65535*Overflowtime+Now_Period - Last_Period;
SpeedV=2812500/Period;
SpeedSum = SpeedSum-Speed0[0]+SpeedV;
Speed=SpeedSum/8;
Speed0[0]=Speed0[1];
Speed0[1]=Speed0[2];
Speed0[2]=Speed0[3];
Speed0[3]=Speed0[4];
Speed0[4]=Speed0[5];
Speed0[5]=Speed0[6];
Speed0[6]=Speed0[7];
Speed0[7]=SpeedV;
Overflowtime=0;
Last_Period=Now_Period;
}
else
{
Speed=0; //停机
TSCR2_TOI = 0; //定时器溢出中断禁止
Overflowtime=0;
Pulse_Num=0;
}
}
}
}
#pragma CODE_SEG NON_BANKED //定时器溢出中断
void interrupt 16 TimerOverFlow(void)
{
TFLG2_TOF = 1; //清楚定时器溢出中断标志位
Overflowtime++;
} |