gpt4 book ai didi

c++ - 这个 ISR 程序是如何工作的?

转载 作者:行者123 更新时间:2023-11-30 01:09:40 29 4
gpt4 key购买 nike

我目前正在尝试为步进电机编写步进信号,最近发现 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;
}

在我看来,step_count 是否在 ISR 开始时设置为零,并在 ACCEL、RUN 或 DECEL 状态中递增。

快速调试确实显示变量增加到我想要的值,但我真的不知道如何。

我知道缺少一些非常简单的东西。

最佳答案

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

// 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;

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

关于c++ - 这个 ISR 程序是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39879324/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com