2

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
}
  • Did you ever get this working? I too have been wanting to use a Arduino Mega to track the angular position of a Omron Encoder with 3 interrupts. One of the interrupts for he Index to reset to 0 every time it completes one revolution. I want to use it to track a rotating mast for for direction finding. There are pieces of code but nothing complete. Is yours complete? bkukuk@cox.net – Bkukuk62 Sep 23 '18 at 16:37
1

Re “pins 2 and 3 are different. I think on the Mega they are PORTH3 and PORTH4 respectively”, it's true that Arduino digital pins 2 and 3 belong to different ports on Uno vs Mega2560 boards.

Mega2560's have six INTx pins, vs two such on Uno's. On the Mega, INT0...INT3 are PD0...PD3, and INT4,INT5 are PE4,PE5. On the Uno, INT0,INT1 are PD2,PD3. Note, in an answer at Can external interrupts be OR'd together on the '328 (Uno)? I show a couple of routines that will display appropriate masks for pins on different Arduinos. See the sections “Using other pins for PCIs” and “ISR-framework-generating Sketch”.


Here are a few problems with the code shown in the question:

  1. aFlag is initialized to zero, and is never set nonzero in the code shown. So the first if condition in PinA() is never met.

  2. PinA() is an interrupt handler, connected to interrupts by your attachInterrupt() calls. Because hardware disables interrupts before it enters an interrupt handler, there is no need for the interrupt handler to turn off interrupts. That is, delete cli() in PinA(). Similarly, delete the sei() at the end of PinA() because hardware restores interrupt status when a RETI instruction executes.

  3. The Arduino software environment defines constants like B00001100 with binary values that match the name. That is, in the Arduino software environment, constant B00001100 has the value 0B00001100. In my opinion, that's a stupid and unnecessary feature; I suggest that in your code you use standard C notation like 0B00001100 when you want to use binary constant values.

  4. I regard writing encoderPos -- instead of encoderPos-- as a perversion and suggest deleting that unnecessary space.

  5. As you haven't shown the PinB() interrupt handler, I don't know how you were going to handle the encoderPos++ case. Judging from the code shown, you are using a method that won't correctly handle bounce and intermittent errors. I suggest using pin-change interrupts and a state-machine method, as illustrated in answers to:
    Reading from a KY-040 rotary encoder with Digispark
    How to read RPS for fast rotation with status change method in Arduino?
    Read RC receiver channels using Interrupt instead of PulseIn
    Can external interrupts be OR'd together on the '328 (Uno)?

0

On the mega, you'd read the pins with PINE.

So your code in PinA would change to:

reading = PINE & 0b00110000;

  if(reading == 0b00110000 && aFlag) 

  {  encoderPos --;
    bFlag = 0;
    aFlag = 0;
  }
  else if (reading == 0b00010000) bFlag = 1;

I found a great discussion talking about the masking required here: https://forum.arduino.cc/index.php?topic=561741.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.