简体   繁体   中英

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. 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).

The arduino should measure the rotation speed of the shaft using the sensor (I therefore use an Interrupt function in the code).

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. Also the actual speed of the angle grinder is then being displayed on a I2C display.

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?

(The values of the Controller have been tested using Winfact's 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:
Timer1 is a 16bit timer.
In the Arduino world the Servo library uses timer1 on Arduino Uno
Pins 9 and 10: controlled by timer1

Timer2:
Timer2 is a 8bit timer like timer0.
In the Arduino work the tone() function uses timer2.
Pins 11 and 3: controlled by timer2

Details: 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. 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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