簡體   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