The Netherlands
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« on: January 25, 2013, 07:29:24 pm » |
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 // 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/useitPerhaps I'm getting it wrong. Some help is needed here 
|
|
|
|
« Last Edit: February 09, 2013, 05:30:51 am by corprius »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #1 on: January 25, 2013, 07:58:56 pm » |
Interrupt variables need to be volatile, eg. 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
|
|
|
|
|
Logged
|
|
|
|
|
The Netherlands
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #2 on: January 26, 2013, 02:38:22 pm » |
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: attachInterrupt(0, rotEncoder, CHANGE); // ISR for rotary encoder on pin 2 instead of attachInterrupt(2, rotEncoder, CHANGE); // ISR for rotary encoder on pin 2 .
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15310
Measurement changes behavior
|
 |
« Reply #3 on: January 26, 2013, 02:46:40 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
The Netherlands
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #4 on: January 26, 2013, 02:50:39 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15310
Measurement changes behavior
|
 |
« Reply #5 on: January 26, 2013, 02:52:08 pm » |
Ah, good to see. So could you at least post your two ISR functions? Lefty
|
|
|
|
|
Logged
|
|
|
|
|
The Netherlands
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #6 on: January 26, 2013, 03:06:01 pm » |
Of course, but they are they same. Assuming that you mean the ISR function that works and the one that doesn't... This one works (Arduino Uno) // Rotary encoder interrupt service routine volatile boolean rotating=false;
void rotEncoder(){ rotating=true; // If motion is detected in the rotary encoder, set the flag to true } This one does not (ATmega644). volatile boolean rotating=false; void rotEncoder() { rotating=true; // If motion is detected in the rotary encoder, set the flag to true }
|
|
|
|
« Last Edit: January 26, 2013, 03:15:34 pm by corprius »
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15310
Measurement changes behavior
|
 |
« Reply #7 on: January 26, 2013, 03:08:49 pm » |
static volatile rotating=false; Missing the variable type for that statement? Lefty
|
|
|
|
|
Logged
|
|
|
|
|
The Netherlands
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #8 on: January 26, 2013, 03:19:04 pm » |
Sorry I messed up the code while pasting it. I corrected it. I was hoping that the missing variable type was the problem, but it's not. Messing up the pasted code was clearly a PEBKAC ("Problem Exists Between Keyboard And Chair") 
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15310
Measurement changes behavior
|
 |
« Reply #9 on: January 26, 2013, 03:21:20 pm » |
Sorry I messed up the code while pasting it. I corrected it. I was hoping that the missing variable type was the problem, but it's not. Messing up the pasted code was clearly a PEBKAC ("Problem Exists Between Keyboard And Chair")  While in the Air Force we would have wrote it up as "found short between the headsets".  Lefty
|
|
|
|
|
Logged
|
|
|
|
|
The Netherlands
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #10 on: January 26, 2013, 03:28:21 pm » |
haha.., there are numerous variations to it.
Could it have something to do with the fuses? Mine differ from sanguino's fuses.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #11 on: January 26, 2013, 06:04:33 pm » |
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 ... 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.
Do you actually have a Sanguino?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #12 on: January 26, 2013, 09:49:30 pm » |
attachInterrupt(2, rotEncoder, CHANGE); // ISR for rotary encoder on pin 2 INT0 is typically on PD2. With that corrected, your code works.
|
|
|
|
|
Logged
|
|
|
|
|
The Netherlands
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #13 on: February 02, 2013, 11:02:20 am » |
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 ... 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.
Do you actually have a Sanguino? Sorry, been away for a few days... No I do not have the Sanguino, I was only inspired by it. I designed my own controller that can be mounted piggyback onto any 20x4 HD44780 based display. I use it to control my DAC. Here's al link to my creation. http://ce-designs.net/index.php/my-projects/other-builds/ce644-the-es9018-i2c-controllerattachInterrupt(2, rotEncoder, CHANGE); // ISR for rotary encoder on pin 2 INT0 is typically on PD2. With that corrected, your code works. So you're saying I should change it to: attachInterrupt(0, rotEncoder, CHANGE); // ISR for rotary encoder on pin 2 ??? Already tried that without success.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #14 on: February 02, 2013, 03:31:31 pm » |
To save me reading all that, can you please state which pin, on the Atmega644 chip, you are attaching the device which is to generate an interrupt?
|
|
|
|
|
Logged
|
|
|
|
|
|