本帖最后由 通讯不上 于 2024-2-21 14:02 编辑
使用定时器一个通道捕获频率时偶尔出现频率只有一半的问题。检查重复捕获标志位没有异常。信号源和电路应该没有问题,用pwm模式捕获的话是正常的,求助!!!部分代码:
void inline pwm_compute(uint8_t idx)
{
// uint32_t first_val = 0;
uint16_t cap_val = 0u; //进来先读值
uint8_t i = 0; //避免在处理这个中断时update没来得及处理导致计算不准确
switch (idx)
{
case 0:
cap_val = TIM_GetCap1(TIM5);
break;
case 1:
cap_val = TIM_GetCap2(TIM5);
break;
case 2:
cap_val = TIM_GetCap3(TIM5);
break;
case 3:
cap_val = TIM_GetCap4(TIM5);
break;
default:
break;
}
if(ic_info[idx].ic_capnum == 0)
{
/* Get the Input Capture value */
ic_info[idx].ic_freq_delval = cap_val;
//TIM_ConfigOc1Polarity(TIM5,TIM_IC_POLARITY_FALLING);
ic_info[idx].ic_capnum = 2; // 1是有占空比
ic_info[idx].ic_overcnt = 0;
}
else if(ic_info[idx].ic_capnum == 1)
{
/* Get the Input Capture value */
ic_info[idx].ic_duty_delval = ic_info[idx].ic_overcnt * 65535 + cap_val - ic_info[idx].ic_freq_delval;
TIM_ConfigOc1Polarity(TIM5,TIM_IC_POLARITY_RISING);
ic_info[idx].ic_capnum = 2;
}
else if(ic_info[idx].ic_capnum == 2)
{
/* Get the Input Capture value */
// first_val = ic_info[idx].ic_freq_delval;
if(cap_val < ic_info[idx].ic_freq_delval && ic_info[idx].ic_overcnt == 0)
{
i = 1;
}
ic_info[idx].ic_freq_delval = (ic_info[idx].ic_overcnt +i)* 65535 + cap_val - ic_info[idx].ic_freq_delval;
//dev_printf("del1 =%d",ic_info[idx].ic_freq_delval);
//TIM_ConfigOc1Polarity(TIM5,TIM_IC_POLARITY_RISING);
ic_info[idx].ic_freq = (uint32_t) (18000000 * 1.0 / ic_info[idx].ic_freq_delval + 0.5);
ic_info[idx].ic_duty = (uint32_t) ((float)ic_info[idx].ic_duty_delval * 1.0 / ic_info[idx].ic_freq_delval)*1000;
//ic_info[idx].ic_capnum = 0;
if(ic_info[idx].ic_freq < 480)
{
dev_printf("freq =%d\n",ic_info[idx].ic_freq);
// first_val = 0;
}
//全波形测量
ic_info[idx].ic_freq_delval = cap_val;
ic_info[idx].ic_overcnt = 0;
}
void PI_IRQHandlerCallback(void)
{
uint8_t i = 0;
if(TIM_GetIntStatus(TIM5, TIM_INT_UPDATE) == SET)
{
for(i = 0;i<4;i++)
{
if(TIM_GetIntStatus(TIM5, (TIM_INT_CC1<<i)) == SET) //如果已经捕获了那就不要加了,避免多加一
{
continue;
}
ic_info.ic_overcnt++;
}
TIM_ClrIntPendingBit(TIM5, TIM_INT_UPDATE);
}
if(TIM_GetIntStatus(TIM5, TIM_INT_CC1) == SET)
{
/* Clear TIM5 Capture compare interrupt pending bit */
TIM_ClrIntPendingBit(TIM5, TIM_INT_CC1);
pwm_compute(0);
if(bd_r && bd_r->pi1)
{
dev_pi_get_fd(bd_r->pi1,ic_info[0].ic_freq,ic_info[0].ic_duty);
}
}
if(TIM_GetIntStatus(TIM5, TIM_INT_CC2) == SET)
{
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClrIntPendingBit(TIM5, TIM_INT_CC2);
pwm_compute(1);
if(bd_r && bd_r->pi2)
{
dev_pi_get_fd(bd_r->pi2,ic_info[1].ic_freq,ic_info[1].ic_duty);
}
}
if(TIM_GetIntStatus(TIM5, TIM_INT_CC3) == SET)
{
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClrIntPendingBit(TIM5, TIM_INT_CC3);
pwm_compute(2);
if(bd_r && bd_r->pi3)
{
dev_pi_get_fd(bd_r->pi3,ic_info[2].ic_freq,ic_info[2].ic_duty);
}
}
if(TIM_GetIntStatus(TIM5, TIM_INT_CC4) == SET)
{
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClrIntPendingBit(TIM5, TIM_INT_CC4);
pwm_compute(3);
if(bd_r && bd_r->pi4)
{
dev_pi_get_fd(bd_r->pi4,ic_info[3].ic_freq,ic_info[3].ic_duty);
}
}
}
}
|