简体   繁体   English

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

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

I would like to build and program a speed controller form an angle grinder with an arduino.我想用 arduino 构建和编程一个速度控制器,形成一个角磨机。 Therefore i bought a Motor Speed Controller Module (it has a potentiometer on it which i will replace with an analog output of my arduino) and an infrared obstacle avoidance module (it can also be used form rpm measuring).因此我买了一个电机速度控制器模块(它上面有一个电位器,我将用我的 arduino 的模拟输出替换)和一个红外避障模块(它也可以用于 rpm 测量)。

The arduino should measure the rotation speed of the shaft using the sensor (I therefore use an Interrupt function in the code). arduino 应该使用传感器测量轴的旋转速度(因此我在代码中使用了中断功能)。

That rpm value is then being passed to a controller I calculated (the controller runs with a Timer1 function, to keep the cycle time constant) and a analog output is used to pass the calculated value to the Motor Speed Controller.然后将该 rpm 值传递给我计算的控制器(控制器使用 Timer1 功能运行,以保持循环时间恒定),并使用模拟输出将计算值传递给电机速度控制器。 Also the actual speed of the angle grinder is then being displayed on a I2C display.此外,角磨机的实际速度也会显示在 I2C 显示器上。

Now my question is if the arduino is capable of running both an Interrupt and a Timer1 function at the same time, or will they interfere with each other?现在我的问题是 arduino 是否能够同时运行中断和 Timer1 功能,或者它们会相互干扰吗?

(The values of the Controller have been tested using Winfact's Boris) (Controller 的值已经使用 Winfact 的 Boris 进行了测试)

My code:我的代码:

#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");
}

Timer1:定时器 1:
Timer1 is a 16bit timer. Timer1 是一个 16 位定时器。
In the Arduino world the Servo library uses timer1 on Arduino Uno在 Arduino 世界中,伺服库在 Arduino Uno 上使用 timer1
Pins 9 and 10: controlled by timer1引脚 9 和 10:由定时器 1 控制

Timer2:定时器2:
Timer2 is a 8bit timer like timer0. Timer2 是一个和 timer0 一样的 8 位定时器。
In the Arduino work the tone() function uses timer2.在 Arduino 工作中,tone() 函数使用 timer2。
Pins 11 and 3: controlled by timer2引脚 11 和 3:由定时器 2 控制

Details: https://www.robotshop.com/community/forum/t/arduino-101-timers-and-interrupts/13072详情: https : //www.robotshop.com/community/forum/t/arduino-101-timers-and-interrupts/13072
I also use a RPM control based on sensor values, but used only functions for the timer1 I partly rewrote/extended.我还使用基于传感器值的 RPM 控制,但仅使用了我部分重写/扩展的 timer1 的功能。 So far no problems.到目前为止没有问题。
Timer1 uses interrupts to handle timer overflows so check in the source code wether this might be a problem for your application, Timer1 使用中断来处理定时器溢出,因此检查源代码是否这可能是您的应用程序的问题,

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

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