Porting Interrupts

Status
Not open for further replies.

Fyod

Well-known member
I'm having problems trying to get interrupts made for AVR working on the Teensy.

This is the part of the code that's giving me trouble

Code:
#define PIN_MASK     0b00001100

void NazaDecoderLib::startPwmReader()
{
  pinMode(2, INPUT_PULLUP); // Pitch (Arduino D2 <-> Naza F2)
  pinMode(1, INPUT_PULLUP); // Roll  (Arduino D3 <-> Naza F1)
  cli();
  PCICR = 1 << PCIE2;
  PCMSK2 = PIN_MASK;
  sei();
}

void NazaDecoderLib::pwmInterruptHandler()
{
  uint8_t bit;
  uint8_t curr;
  uint8_t mask;
  uint32_t currentTime;
  uint32_t time;

  // get the pin states for the indicated port.
  curr = PIND & PIN_MASK;
  mask = curr ^ pcIntLast;
  pcIntLast = curr;

  currentTime = micros();

  // mask is pcint pins that have changed.
  for (uint8_t i = 0; i < 2; i++) {
    bit = 0b00000100 << i;
    if (bit & mask) {
      // for each pin changed, record time of change
      if (bit & pcIntLast) {
        time = currentTime - pwmData[i].fallTime;
        pwmData[i].riseTime = currentTime;
        if ((time >= 10000) && (time <= 26000))
          pwmData[i].edge = 1;
        else
          pwmData[i].edge = 0; // invalid rising edge detected
      }
      else {
        time = currentTime - pwmData[i].riseTime;
        pwmData[i].fallTime = currentTime;
        if ((time >= 800) && (time <= 2200) && (pwmData[i].edge == 1)) {
          pwmData[i].lastGoodWidth = time;
          pwmData[i].edge = 0;
        }
      }
    }
  }
}

ISR(PCINT2_vect)
{
  NazaDecoder.pwmInterruptHandler();
}

I get

Code:
NazaDecoderLib.cpp: In member function 'void NazaDecoderLib::startPwmReader()':
NazaDecoderLib.cpp:139:3: error: 'PCICR' was not declared in this scope
NazaDecoderLib.cpp:139:16: error: 'PCIE2' was not declared in this scope
NazaDecoderLib.cpp:140:3: error: 'PCMSK2' was not declared in this scope
NazaDecoderLib.cpp: At global scope:
NazaDecoderLib.cpp:184:4: error: expected constructor, destructor, or type conversion before '(' token

I have tried #include avr/io.h and interrupt.h, but that doesn't do anything. Interrupt.h is empty.
Not sure what to do next. The http://www.pjrc.com/teensy/interrupts.html info has the PCICR etc. names, but I'm not sure what to do with them, so I'm stuck.
 
On Teensy 3.1, all the pins have interrupt capability. You can probably just delete the AVR pin change code and use attachInterrupt.

For example:

Code:
void NazaDecoderLib::startPwmReader()
{
  pinMode(2, PCINT2_vect, INPUT_PULLUP); // Pitch (Arduino D2 <-> Naza F2)
  pinMode(1, PCINT2_vect, INPUT_PULLUP); // Roll  (Arduino D3 <-> Naza F1)   // is pin 1 a bug?
  attachInterrupt(2, CHANGE);
  attachInterrupt(3, CHANGE);
}

Change the interrupt handler to a normal function, like this:

Code:
void PCINT2_vect(void) {
{
  NazaDecoder.pwmInterruptHandler();
}

Or if you can cause NazaDecoder.pwmInterruptHandler to be a static function, it could be called directly from attachInterrupt. But attachInterrupt can't call non-static C++ functions.

Also, you'll probably need to change that pinMode() line to configure the correct pin.
 
Thanks, Paul! That is basically what I thought at first, but then I started overthinking it.
Can't wait to try it out.
 
Status
Not open for further replies.
Back
Top