- 论坛徽章:
- 0
|
1,效率低的代码
IDINT32U Pulse1=0,Pulse2=0;
if(g_stTPM[TpmNum].Capture[ChalNum].CapNum == 0
||g_stTPM[TpmNum].Capture[ChalNum].CapNum == g_stTPM[TpmNum].Capture[ChalNum].TargetCapNum-1
|| g_stTPM[TpmNum].Capture[ChalNum].CapNum == (g_stTPM[TpmNum].Capture[ChalNum].TargetCapNum-1)/2)
{
g_stTPM[TpmNum].Capture[ChalNum].ChannelVal[g_stTPM[TpmNum].Capture[ChalNum].SaveNum] = TPM_TPM1CxV(ChalNum);
g_stTPM[TpmNum].Capture[ChalNum].Overflow[g_stTPM[TpmNum].Capture[ChalNum].SaveNum] = g_stTPM[TpmNum].Overflow;
g_stTPM[TpmNum].Capture[ChalNum].SaveNum ++;
}
g_stTPM[TpmNum].Capture[ChalNum].CapNum++;
g_stTPM[TpmNum].Capture[ChalNum].TotalCapNum++;
if(g_stTPM[TpmNum].Capture[ChalNum].CapNum == g_stTPM[TpmNum].Capture[ChalNum].TargetCapNum)
{
g_stTPM[TpmNum].Capture[ChalNum].CapNum = 0;
g_stTPM[TpmNum].Capture[ChalNum].SaveNum = 0;
Pulse1 = (g_stTPM[TpmNum].Capture[ChalNum].Overflow[1]*65536 + g_stTPM[TpmNum].Capture[ChalNum].ChannelVal[1])
-(g_stTPM[TpmNum].Capture[ChalNum].Overflow[0]*65536 + g_stTPM[TpmNum].Capture[ChalNum].ChannelVal[0]);
Pulse2 = (g_stTPM[TpmNum].Capture[ChalNum].Overflow[2]*65536 + g_stTPM[TpmNum].Capture[ChalNum].ChannelVal[2])
-(g_stTPM[TpmNum].Capture[ChalNum].Overflow[1]*65536 + g_stTPM[TpmNum].Capture[ChalNum].ChannelVal[1]);
if((Pulse1 > Pulse2+DELTA) || (Pulse1< Pulse2-DELTA))
{
return;
}
g_stTPM[TpmNum].Capture[ChalNum].Freq = BUS_CLOCK* (g_stTPM[TpmNum].Capture[ChalNum].TargetCapNum-1)/(Pulse1+Pulse2) ;// calculate the frequency
if(g_stTPM[TpmNum].Capture[ChalNum].Freq > 1000
&& g_stTPM[TpmNum].Capture[ChalNum].TargetCapNum == CAP_LOW_FREQ_NUM)
{
g_stTPM[TpmNum].Capture[ChalNum].TargetCapNum = CAP_HIGH_FREQ_NUM;
return;
}
TPM_ChIntDis(TpmNum,ChalNum);// disable channel interrupt
}
2,效率高一点的代码
IDINT32U Pulse1=0,Pulse2=0;
if(g_stCapture.CapNum == 0 ||g_stCapture.CapNum == g_stCapture.TargetCapNum-1 || g_stCapture.CapNum == (g_stCapture.TargetCapNum-1)/2)
{
g_stCapture.ChannelVal[g_stCapture.SaveNum] = TPM_TPM1CxV(CH0);
g_stCapture.Overflow[g_stCapture.SaveNum++] = g_stTPMInfo.Overflow;
}
g_stCapture.CapNum++;
g_stCapture.TotalCapNum++;
if(g_stCapture.CapNum == g_stCapture.TargetCapNum)
{
g_stCapture.CapNum = 0;
g_stCapture.SaveNum =0;
Pulse1 = (g_stCapture.Overflow[1] -g_stCapture.Overflow[0])*65536
+ g_stCapture.ChannelVal[1] - g_stCapture.ChannelVal[0];
Pulse2 = (g_stCapture.Overflow[2] -g_stCapture.Overflow[1])*65536
+ g_stCapture.ChannelVal[2] - g_stCapture.ChannelVal[1];
if((Pulse1 > Pulse2+DELTA) || (Pulse1< Pulse2-DELTA))
{
return;
}
g_stCapture.Freq = BUS_CLOCK* (g_stCapture.TargetCapNum-1)/(Pulse1+Pulse2) ;// calculate the frequency
if(g_stCapture.Freq > 1000 && g_stCapture.TargetCapNum == CAP_HIGH_FREQ_NUM)
{
g_stCapture.TargetCapNum = CAP_HIGH_FREQ_NUM;
return;
}
TPM_ChIntDis(g_stTPMInfo.TpmNum,g_stTPMInfo.ChalNum);// disable channel interrupt
}
|
|