Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags
Link
user31481
user31481
Bumped by Community user
Bumped by Community user
Bumped by Community user
deleted 195 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Arduino Mega 2560 Interuptinterrupt pins and port mapping with rotary encoder

I am trying to use port manipulation in interrupts to read a rotary encoder. II found a code for an UNOa Uno, so the port call for pins 2 and 3 are different. II think on the Mega they are PORTH3 and PORTH4 respectively. My code is not working. DoDo I have my ports wrong? I'veI've tried reading up on this and I'm not fully understanding the Bxxxxxxxx part. II think that might be what's incorrect. Here is part of my code with one of the interrupts.

 static int pinA = 2;  
// Our first hardware interrupt pin is digital pin 2
    static int pinB = 3;  
// Our second hardware interrupt pin is digital pin 3
    volatile byte aFlag = 0;  
// let'slet us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
    volatile byte bFlag = 0;  
// let'slet us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
    volatile byte encoderPos = 0;  
//this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
    volatile byte oldEncPos = 0;  
//stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
    volatile byte reading = 0; 
    
void setup () {
  pinMode(pinA, INPUT);
  // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT);
 
  attachInterrupt(0, PinA, RISING);
  // set an interrupt on PinA, 
                                      //looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1, PinB, RISING);
  // set an interrupt on PinB, 
                                      //looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
}
      
        void PinA() {
  //CCW
              cli();
  //stop interrupts happening before we read pin values
              reading = PORTH & 0xC;
  // read all eight pin values then strip away all but pinA and pinB's values
              if (reading == B00001100 && aFlag) {
    //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
                encoderPos --; //decrement the encoder's position count
                bFlag = 0; //reset flags for the next turn
                aFlag = 0; //reset flags for the next turn
              }
          else if (reading == B00000100) bFlag = 1;
  //signal that we're expecting pinB to signal the transition to detent from free rotation
          sei(); //restart interrupts
        }
        
      

Arduino Mega 2560 Interupt pins and port mapping with rotary encoder

I am trying to use port manipulation in interrupts to read a rotary encoder. I found a code for an UNO, so the port call for pins 2 and 3 are different. I think on the Mega they are PORTH3 and PORTH4 respectively. My code is not working. Do I have my ports wrong? I've tried reading up on this and I'm not fully understanding the Bxxxxxxxx part. I think that might be what's incorrect. Here is part of my code with one of the interrupts.

 static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
    static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
    volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
    volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
    volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
    volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
    volatile byte reading = 0; 
    
void setup (){
pinMode(pinA, INPUT); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT);
 
attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, 
                                      //looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, 
                                      //looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
}
      
        void PinA(){ //CCW
              cli(); //stop interrupts happening before we read pin values
              reading = PORTH & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
              if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
                encoderPos --; //decrement the encoder's position count
                bFlag = 0; //reset flags for the next turn
                aFlag = 0; //reset flags for the next turn
              }
          else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
          sei(); //restart interrupts
        }
        
      

Arduino Mega 2560 interrupt pins and port mapping with rotary encoder

I am trying to use port manipulation in interrupts to read a rotary encoder. I found code for a Uno, so the port call for pins 2 and 3 are different. I think on the Mega they are PORTH3 and PORTH4 respectively. My code is not working. Do I have my ports wrong? I've tried reading up on this and I'm not fully understanding the Bxxxxxxxx part. I think that might be what's incorrect. Here is part of my code with one of the interrupts.

static int pinA = 2; 
// Our first hardware interrupt pin is digital pin 2
static int pinB = 3; 
// Our second hardware interrupt pin is digital pin 3
volatile byte aFlag = 0; 
// let us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; 
// let us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; 
//this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; 
//stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0;

void setup () {
  pinMode(pinA, INPUT);
  // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT);
  attachInterrupt(0, PinA, RISING);
  // set an interrupt on PinA,
  //looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1, PinB, RISING);
  // set an interrupt on PinB,
  //looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
}

void PinA() {
  //CCW
  cli();
  //stop interrupts happening before we read pin values
  reading = PORTH & 0xC;
  // read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && aFlag) {
    //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  } else if (reading == B00000100) bFlag = 1;
  //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}
deleted 2 characters in body
Source Link
kumquat
  • 61
  • 1
  • 2
  • 6

I am trying to use port manipulation in interrupts to read a rotary encoder. I found a code for an UNO, so the port call for pins 2 and 3 are different. I think on the Mega they are PORTE 0x10PORTH3 and 0x20PORTH4 respectively. My code is not working. Do I have my ports wrong? I've tried reading up on this and I'm not fully understanding the Bxxxxxxxx part. I think that might be what's incorrect. Here is part of my code with one of the interrupts.

 static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
    static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
    volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
    volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
    volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
    volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
    volatile byte reading = 0; 
    
void setup (){
pinMode(pinA, INPUT); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT);

attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, 
                                      //looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, 
                                      //looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
}
      
        void PinA(){ //CCW
              cli(); //stop interrupts happening before we read pin values
              reading = PORTEPORTH & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
              if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
                encoderPos --; //decrement the encoder's position count
                bFlag = 0; //reset flags for the next turn
                aFlag = 0; //reset flags for the next turn
              }
          else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
          sei(); //restart interrupts
        }
        
      

I am trying to use port manipulation in interrupts to read a rotary encoder. I found a code for an UNO, so the port call for pins 2 and 3 are different. I think on the Mega they are PORTE 0x10 and 0x20 respectively. My code is not working. Do I have my ports wrong? I've tried reading up on this and I'm not fully understanding the Bxxxxxxxx part. I think that might be what's incorrect. Here is part of my code with one of the interrupts.

 static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
    static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
    volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
    volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
    volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
    volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
    volatile byte reading = 0; 
    
void setup (){
pinMode(pinA, INPUT); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT);

attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, 
                                      //looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, 
                                      //looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
}
      
        void PinA(){ //CCW
              cli(); //stop interrupts happening before we read pin values
              reading = PORTE & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
              if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
                encoderPos --; //decrement the encoder's position count
                bFlag = 0; //reset flags for the next turn
                aFlag = 0; //reset flags for the next turn
              }
          else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
          sei(); //restart interrupts
        }
        
      

I am trying to use port manipulation in interrupts to read a rotary encoder. I found a code for an UNO, so the port call for pins 2 and 3 are different. I think on the Mega they are PORTH3 and PORTH4 respectively. My code is not working. Do I have my ports wrong? I've tried reading up on this and I'm not fully understanding the Bxxxxxxxx part. I think that might be what's incorrect. Here is part of my code with one of the interrupts.

 static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
    static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
    volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
    volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
    volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
    volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
    volatile byte reading = 0; 
    
void setup (){
pinMode(pinA, INPUT); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT);

attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, 
                                      //looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, 
                                      //looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
}
      
        void PinA(){ //CCW
              cli(); //stop interrupts happening before we read pin values
              reading = PORTH & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
              if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
                encoderPos --; //decrement the encoder's position count
                bFlag = 0; //reset flags for the next turn
                aFlag = 0; //reset flags for the next turn
              }
          else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
          sei(); //restart interrupts
        }
        
      
Source Link
kumquat
  • 61
  • 1
  • 2
  • 6
Loading