运行ISR,直到发生X

问题描述 投票:0回答:1

我需要有关我的程序的帮助(主要目标是。如果OCR等于timer0值,则目标是输入ISR。在下面的代码中,您可以看到我走了多远。现在开始解决我的问题:我想在程序进入ISR后立即将某些数字输出保持在高/低状态,并希望以这种方式保留它们,直到计时器达到其峰值为止。ISR中是否有“堕胎命令”?例如:如果x == 255返回主循环?

感谢您的帮助,请保持友善,而不是专业人士。附言我知道还有很多工作要做。我认为我也应该尝试弃用Digital.Write

#include <avr/io.h>           // this header is necessary to adress the registers
#include <avr/interrupt.h>    // this header is necessary for the ISR 

volatile bool posTOnegU = true;
int igbt_high_u = 11;
int igbt_high_v = 10;
int igbt_high_w = 9;

int igbt_low_u = 6;
int igbt_low_v = 5;
int igbt_low_w = 3;
int enable_RFE = 4; 
volatile int i = 0;  

void posHalbU()
{
    digitalWrite(igbt_low_v, LOW);                   
    digitalWrite(igbt_low_w, LOW);
    digitalWrite(igbt_high_u, LOW);
}

void negHalbU()
{
    digitalWrite(igbt_high_v, LOW);
    digitalWrite(igbt_high_w, LOW);
    digitalWrite(igbt_low_u, LOW);
}

void lookup_done(int laufvariable, int size_of_lookup, bool change_state)
{
  if(laufvariable == size_of_lookup)
  {
    change_state = !change_state;
    laufvariable = 0;
  }
}

int look_up[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,
44,47,49,52,54,57,59,61,64,66,69,71,73,76,78,80,83,85,88,90,92,94,97,99,
101,103,106,108,110,113,115,117,119,121,124,126,128,130,132,134,136,138,140,142,144,146,
148,150,152,154,156,158,160,162,164,166,168,169,171,173,175,177,178,180,182,184,185,187,188,190,192,193,
195,196,198,199,201,202,204,205,207,208,209,211,212,213,215,216,217,219,220,221,222,223,224,225,226,227,
228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,246,
247,247,247,248,248,248,248,249,249,249,249,249,250,250,250,250,249,249,249,249,249,248,
248,248,248,247,247,247,246,246,245,245,244,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234,
233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,217,216,215,213,212,211,209,208,207,205,204,
202,201,199,198,196,195,193,192,190,188,187,185,184,182,180,178,177,175,173,171,169,168,166,164,162,160,
158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,121,119,117,115,113,110,108,106,
103,101,99,97,94,92,90,88,85,83,80,78,76,73,71,69,66,64,61,59,57,54,52,49,47,44,42,39,37,34,32,30,
27,24,22,19,17,15,12,10,7,5,2,1};
int amount_of_values = sizeof(look_up)/sizeof(int);

void setup() 
{
  noInterrupts();             // disable interrupts till setup is done
  TCNT0 = 0;                  // initialize timer0
  TCCR0A = 0;                 // initialize timer control register 
  OCR0A = look_up[0];         // output compare register is set to the FIRST value of look up table. Values change in the ISR
  TIMSK0 = 0b00000010;        // interrupt triggered if timer_value == ocr_value. TIMSK: Timer/counter interrpt mask register
  interrupts();               // 

  pinMode(enable_RFE, OUTPUT);
  ///// KONFIGURATION PWM /////
  pinMode(igbt_high_u, OUTPUT);
  pinMode(igbt_high_v, OUTPUT);
  pinMode(igbt_high_w, OUTPUT);
  pinMode(igbt_low_u, OUTPUT);
  pinMode(igbt_low_v, OUTPUT);
  pinMode(igbt_low_w, OUTPUT);
}

void loop() 
{
    digitalWrite(igbt_high_v, HIGH);
    digitalWrite(igbt_high_w, HIGH);
    digitalWrite(igbt_low_u, HIGH);
    digitalWrite(igbt_low_v, HIGH);
    digitalWrite(igbt_low_w, HIGH);
    digitalWrite(igbt_high_u, HIGH);
}



ISR(TIMER0_COMPA_vect)
{
  //Serial.print(look_up[i]);     //dont use seriual print in an isr 
  if(posTOnegU == true)
  {
    posHalbU();
  }
  else
  {
    negHalbU();
  }
  i++;                            //get next value of look up table 
  OCR0A = look_up[i];             // output compare register gets next value 
  lookup_done(i, amount_of_values, posTOnegU);

}

timer and isr

c arduino interrupt
1个回答
0
投票

loop功能中,您具有

digitalWrite(igbt_high_v, HIGH);
digitalWrite(igbt_high_w, HIGH);
digitalWrite(igbt_low_u, HIGH);
digitalWrite(igbt_low_v, HIGH);
digitalWrite(igbt_low_w, HIGH);
digitalWrite(igbt_high_u, HIGH);

我假设在主函数中连续调用此函数。这就是将输出的值设置为HIGH的原因。

如果仅想在ISR中更改这些值,则可以从loop功能中删除上述各行。

请注意,在这种情况下,您还需要一个条件才能在ISR中将值设置为HIGH

© www.soinside.com 2019 - 2024. All rights reserved.