Go Down

Topic: <SOLVED> ATmega644 does not catch the interupt (Read 2 times) previous topic - next topic

corprius

Jan 26, 2013, 01:29 am Last Edit: Feb 09, 2013, 11:30 am by corprius Reason: 1
Hi,

I'm trying to use the attachInterrupt() with the ATmega644 for sensing a rotary encoder.
For some reason the uC doesn't catch the interrupt. This is my code

Code: [Select]
// Rotary encoder interrupt service routine
boolean rotating=false;
void rotEncoder(){
rotating=true; // If motion is detected in the rotary encoder, set the flag to true
}

void setup(){
       // Attach Interrupts
attachInterrupt(2, rotEncoder, CHANGE);  // ISR for rotary encoder on pin 2

pinMode(4, INPUT);  // Button switch or Encoder pin for volume up
pinMode(2, INPUT);  // Button switch or Encoder pin for volume down
pinMode(3, INPUT);  // Button switch or pin for encoder built-in switch

digitalWrite(2, HIGH); // If H/W debouncing is implemented, set to LOW
digitalWrite(4, HIGH); // If H/W debouncing is implemented, set to LOW
digitalWrite(3, HIGH); // Enable pull-up resistor
}

void loop(){

       if (rotating){
           // do something
       }
}



When I take a look at the Sanguino website it states that the ATmega's interrupt pins are pin 2, 10 & 11. According to the site I should use int 2 to attach the interrupt routine to pin 2.
http://sanguino.cc/useit

Perhaps I'm getting it wrong. Some help is needed here  :~

Nick Gammon

Interrupt variables need to be volatile, eg.

Code: [Select]

volatile boolean rotating=false;


Also your code doesn't actually do anything on the interrupt so how do you know it fails?

http://www.gammon.com.au/interrupts

corprius

I did not paste the code, because it is quite large. I know it fails because when I use the exact same code on my arduino Uno the interrupt gets fired. I can see the result on a display and I can hear the volume go up or down, so I know it's working on the Uno. I use it to control a DAC btw

For the Uno I use this line:
Code: [Select]
attachInterrupt(0, rotEncoder, CHANGE);  // ISR for rotary encoder on pin 2 instead of
Code: [Select]
attachInterrupt(2, rotEncoder, CHANGE);  // ISR for rotary encoder on pin 2.


retrolefty

Are you reading digital input pins inside the ISR routines (most quadrature decoder ISR do have to), and you should be adjusting for the different pin mapping of the Sanguino then the Uno ISR routine uses?

What modified IDE version are you using, as the Sanguino folks haven't seemed to have kept up with the arduino IDE versions sense version 18 or so, not that would keep you from still using an older IDE modified to include the Sanguino 644P stuff.

Lefty


Go Up