繁体   English   中英

Arduino 可以同时处理中断和 Timer1 功能吗?

[英]Can an Arduino handle both an interrupt and Timer1 function?

我想用 arduino 构建和编程一个速度控制器,形成一个角磨机。 因此我买了一个电机速度控制器模块(它上面有一个电位器,我将用我的 arduino 的模拟输出替换)和一个红外避障模块(它也可以用于 rpm 测量)。

arduino 应该使用传感器测量轴的旋转速度(因此我在代码中使用了中断功能)。

然后将该 rpm 值传递给我计算的控制器(控制器使用 Timer1 功能运行,以保持循环时间恒定),并使用模拟输出将计算值传递给电机速度控制器。 此外,角磨机的实际速度也会显示在 I2C 显示器上。

现在我的问题是 arduino 是否能够同时运行中断和 Timer1 功能,或者它们会相互干扰吗?

(Controller 的值已经使用 Winfact 的 Boris 进行了测试)

我的代码:

#include <LiquidCrystal_I2C.h>

#include <TimerOne.h>

//Regler: Siehe Haager S.147
//RPM Counter: Siehe https://forum.arduino.cc/index.php?topic=634139.0


const int X_input=1;
const int U_output=3; 
int X=0;
int U=0, W=0;
const float kr=0.1;
const float Tn=0.12;
const float Tv=0.3;
const float T1=1.0e6;
const float T=0.01;
double w_k, u_k, e_k, e_k1, e_k2, u_k1, u_k2, x_k, d1, d2, c0, c1, c2;

float value = 0;
float rev = 0;
int rpm;
int oldtime = 0;
int time;
LiquidCrystal_I2C lcd(0x27, 20, 4);




void setup() {
  //RPM Counter:
  attachInterrupt(digitalPinToInterrupt (2), RPM_Count, RISING); //interrupt pin

  //Regler:
  Timer1.initialize(T*1.0e6);
  Timer1.attachInterrupt(regler);
  d1=(T+2*T1)/(T+T1);
  d2=-T1/(T+T1);
  c0=kr*(1+T/Tn+Tv/(T+T1));
  c1=kr*((T*T+T*Tn-2*Tv*Tn)/(T*Tn+T1*Tn)-T/Tn-2);
  c2=kr*(Tv+T1)/(T+T1);
  e_k1=0.0, e_k2=0.0, u_k1=0.0, u_k2=0.0;

  //Display:
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("------Drehzahl------");
}

void regler(){


  detachInterrupt(digitalPinToInterrupt(2));      
  time = millis() - oldtime;   
  rpm = (rev / time) * 60000;   
  oldtime = millis();          
  rev = 0;
  attachInterrupt(digitalPinToInterrupt (2), RPM_Count, RISING);

  w_k=rpm;
  X=analogRead(X_input);
  x_k=X*1000.0/1024-500;
  e_k2=e_k1;
  e_k1=e_k;
  e_k=w_k-x_k;
  u_k2=u_k1;
  u_k1=u_k;
  u_k=d1*u_k1 + d2*u_k2 + c0*e_k + c1*e_k1 + c2*e_k2;
  U=256.0/320.0*u_k + 128;
  if(U>255) U=255;
  if(U<0) U=0;
  analogWrite(U_output, U);
}

void RPM_Count() {
  rev++;
}


void loop() {
  lcd.setCursor(0,1);
  lcd.print(rpm);
  lcd.print(" U/min");
}

定时器 1:
Timer1 是一个 16 位定时器。
在 Arduino 世界中,伺服库在 Arduino Uno 上使用 timer1
引脚 9 和 10:由定时器 1 控制

定时器2:
Timer2 是一个和 timer0 一样的 8 位定时器。
在 Arduino 工作中,tone() 函数使用 timer2。
引脚 11 和 3:由定时器 2 控制

详情: https : //www.robotshop.com/community/forum/t/arduino-101-timers-and-interrupts/13072
我还使用基于传感器值的 RPM 控制,但仅使用了我部分重写/扩展的 timer1 的功能。 到目前为止没有问题。
Timer1 使用中断来处理定时器溢出,因此检查源代码是否这可能是您的应用程序的问题,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM