这个ISR例程如何工作?

时间:2016-10-05 16:29:59

标签: c++ debugging arduino-uno atmega

我此刻试图为步进电机写一个步进信号,最近发现atmel写了这个application note, 并提供了一些目的的代码。我必须提供电机的信号是脉冲调制信号,其中信号的频率决定了速度,而不是它们提供的4引脚信号。该代码可在github(链接)上获得。

但我目前正在质疑控制电机所有阶段(停止,加速,运行,减速)的ISR程序。 更具体地说,它如何跟踪负责改变状态的step_count。

#pragma vector=TIMER1_COMPA_vect
__interrupt void speed_cntr_TIMER1_COMPA_interrupt( void )
{
  // Holds next delay period.
  unsigned int new_step_delay;
  // Remember the last step delay used when accelrating.
  static int last_accel_delay;
  // Counting steps when moving.
  static unsigned int step_count = 0;
  // Keep track of remainder from new_step-delay calculation to incrase accurancy
  static unsigned int rest = 0;

  OCR1A = srd.step_delay;

  switch(srd.run_state) {
    case STOP:
      step_count = 0;
      rest = 0;
      // Stop Timer/Counter 1.
      TCCR1B &= ~((1<<CS12)|(1<<CS11)|(1<<CS10));
      status.running = FALSE;
      break;

    case ACCEL:
      sm_driver_StepCounter(srd.dir);
      step_count++;
      srd.accel_count++;
      new_step_delay = srd.step_delay - (((2 * (long)srd.step_delay) + rest)/(4 * srd.accel_count + 1));
      rest = ((2 * (long)srd.step_delay)+rest)%(4 * srd.accel_count + 1);
      // Chech if we should start decelration.
      if(step_count >= srd.decel_start) {
        srd.accel_count = srd.decel_val;
        srd.run_state = DECEL;
      }
      // Chech if we hitted max speed.
      else if(new_step_delay <= srd.min_delay) {
        last_accel_delay = new_step_delay;
        new_step_delay = srd.min_delay;
        rest = 0;
        srd.run_state = RUN;
      }
      break;

    case RUN:
      sm_driver_StepCounter(srd.dir);
      step_count++;
      new_step_delay = srd.min_delay;
      // Chech if we should start decelration.
      if(step_count >= srd.decel_start) {
        srd.accel_count = srd.decel_val;
        // Start decelration with same delay as accel ended with.
        new_step_delay = last_accel_delay;
        srd.run_state = DECEL;
      }
      break;

    case DECEL:
      sm_driver_StepCounter(srd.dir);
      step_count++;
      srd.accel_count++;
      new_step_delay = srd.step_delay - (((2 * (long)srd.step_delay) + rest)/(4 * srd.accel_count + 1));
      rest = ((2 * (long)srd.step_delay)+rest)%(4 * srd.accel_count + 1);
      // Check if we at last step
      if(srd.accel_count >= 0){
        srd.run_state = STOP;
      }
      break;
  }
  srd.step_delay = new_step_delay;
}

正如我所看到的那样,在ISR开始时step_count被设置为零,并且在ACCEL,RUN或DECEL状态下递增。

快速调试确实显示变量增加到我想要的值,但我真的无法弄清楚如何。

我知道遗漏的东西非常简单。

2 个答案:

答案 0 :(得分:3)

这三个变量是静态的。这意味着他们只会被初始化一次:

// Remember the last step delay used when accelrating.
static int last_accel_delay;
// Counting steps when moving.
static unsigned int step_count = 0;
// Keep track of remainder from new_step-delay calculation to incrase accurancy
static unsigned int rest = 0;

如果您遵循代码,您会发现它们用于保持状态从一个中断到下一个中​​断。

答案 1 :(得分:2)

您需要了解static在应用于函数局部变量时的工作原理。

static unsigned int step_count = 0;表示第一次进入函数step_count设置为0.每次进入函数时都不会设置它。

static表示它不是堆栈变量,它与全局变量位于相同的内存空间中,除了它只能在声明它的函数内访问。