您好、专家!
我正面临一个 wierd 问题,如果我在一个 PWM ISR(50 us time)内执行下面的函数, testCounterSpeedMA 会递增 多次,并且它会随机递增!
有时它会以2、11、7等增量递增。
同样的函数,如果我运行在较慢的任务,例如在一个10ms 计时器内,当速度发生变化时,它会正确地增加1。
为什么会发生这种情况?
我想从 PWM ISR 执行此函数、并且只要速度发生变化、就让计数器只递增一次。
void checkForSpeedChange(MOTOR_Vars_t *pMotor)
{
volatile static int16_t prevSpeedMA = 0;
volatile static uint16_t testCounterSpeedLoopResetMA = 0;
if(prevSpeedMA != pMotor->speedRefRPM)
{
testCounterSpeedMA++;
//Log speed change
pMotor->speedChange = true;
//Store the previous value
prevSpeedMA = pMotor->speedRefRPM;
}
}