chipKIT® Development Platform

Inspired by Arduino™

Need help about encoder

Created Fri, 09 Jan 2015 20:01:46 +0000 by tramin


tramin

Fri, 09 Jan 2015 20:01:46 +0000

i using Uno32 and try write the code to read encoder i just can use FALLING and RISING mode and i try CHANGE mode but nothing happens but i need to use CHANGE mode anyone can tell me please.

volatile signed long encoderLeftPos = 0;
volatile signed long encoderRightPos = 0;

volatile int encoderLeftLast  = LOW;
volatile int encoderRightLast = LOW;
int nL = LOW;
int nR = LOW;

void initEncoder();
void doEncoderLeft();
void doEncoderRight();

//-----------------------------------------------------------
void initEncoder()
{
  pinMode(encoderLeftPinA, INPUT);
  pinMode(encoderRightPinA, INPUT);   

  pinMode(encoderLeftPinB, INPUT);   
  pinMode(encoderRightPinB, INPUT);   

  attachInterrupt(1, doEncoderLeft, FALLING);  
  attachInterrupt(2, doEncoderRight,FALLING);
}

//-----------------------------------------------------------
void doEncoderLeft()
{
  // look for a low-to-high on channel A
  if (digitalRead(encoderLeftPinA) == HIGH) { 
    // check channel B to see which way encoder is turning
    if (digitalRead(encoderLeftPinB) == LOW) {
      encoderLeftPos = encoderLeftPos + 1;         // CW
    } else {
      encoderLeftPos = encoderLeftPos - 1;         // CCW
    }
  }
  else   // must be a high-to-low edge on channel A                                       
  { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoderLeftPinB) == HIGH) {   
      encoderLeftPos = encoderLeftPos + 1;          // CW
    } else {
      encoderLeftPos = encoderLeftPos - 1;          // CCW
    }
  }
}

//-----------------------------------------------------------
void doEncoderRight()
{
  // look for a low-to-high on channel A
  if (digitalRead(encoderRightPinA) == HIGH) {   
   // check channel A to see which way encoder is turning
    if (digitalRead(encoderRightPinB) == HIGH) {  
      encoderRightPos = encoderRightPos + 1;         // CW
    } else {
      encoderRightPos = encoderRightPos - 1;         // CCW
    }
  }
  // Look for a high-to-low on channel A
  else { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoderRightPinB) == LOW) {   
      encoderRightPos = encoderRightPos + 1;          // CW
    } else {
      encoderRightPos = encoderRightPos - 1;          // CCW
    }
  }
}

majenko

Fri, 09 Jan 2015 22:05:14 +0000

PI32 interrupts don't respond to change. They can either be RISING or FALLING but never both.

However, Change Notification interrupts always trigger on both rising and falling.

You can try using my ChangeNotification library to access them.


jmlynesjr

Sat, 10 Jan 2015 02:35:52 +0000

Also look on github/jmlynesjr for an encoder driver that uses majenko's pin change interrupt library.

James


unexpectedly

Tue, 20 Jan 2015 01:21:22 +0000

A little bit unrelated, but might give you ideas:

If their info above isn't enough, I was able to read a quadrature encoder with an AVR (arduino) via an intermediate IC. Here's a write up. I can share source code, too, if you'd like.