chipKIT® Development Platform

Inspired by Arduino™

How to Use attachInterrupt()

Created Sun, 12 Jun 2011 00:02:37 +0000 by abotics


abotics

Sun, 12 Jun 2011 00:02:37 +0000

I'm using the UNO32 board and would like to be able to use both pin#7(INT2/RD9) & pin#8(INT3/RD10) as interruptable inputs, but I can't seem to get this to work. How do you know the interrupt number to use? I will probably need to look through the source files to see how these are being allocated, but figured someone might have already done this already.

/*
  OpenRobot_UNO32_Test
  This is a test program for the UNO32 controlled Open-Robot.
  Created by Abraham L. Howell, 6.11.2011.
 */

int LED5_Pin = 43; 
int PWMA_Pin = 6;
int AIn1_Pin = 4;
int AIn2_Pin = 9;
int PWMB_Pin = 5;
int BIn1_Pin = 3;
int BIn2_Pin = 2;
int PWM_Count=0;
int leftFrontEye = 11;
int rightFrontEye = 12;
int leftEncoder_Pin = 7;
int rightEncoder_Pin = 8;
bool LED5_State = 0;

void setup() {                
  // initialize the digital pin as an output.
  // Pin 43 has LED5 connected on the UNO32 board.
  pinMode(LED5_Pin, OUTPUT);  
  pinMode(AIn1_Pin, OUTPUT);  
  pinMode(AIn2_Pin, OUTPUT);
  pinMode(BIn1_Pin, OUTPUT);
  pinMode(BIn2_Pin, OUTPUT);
  pinMode(leftFrontEye, INPUT);
  pinMode(rightFrontEye, INPUT);  
  
  /* Configure left & right wheel encoder inputs. */
  pinMode(leftEncoder_Pin, INPUT);
  pinMode(rightEncoder_Pin, INPUT);
  /* Attach corresponding ISRs. Left Encoder is on Pin#7 [INT2] & Right Encoder is on Pin#8 [INT3]. */
  attachInterrupt(0, LeftEncoderISR, CHANGE);
  //attachInterrupt(1, RightEncoderISR, CHANGE);
}

void SetMotorSpeed(int leftMotorSpeed, int rightMotorSpeed)
{
  /* Set direction lines based upon sign of speed. */
  if (leftMotorSpeed>0){
    digitalWrite(BIn1_Pin, HIGH);
    digitalWrite(BIn2_Pin, LOW);
  } else {
    digitalWrite(BIn1_Pin, LOW);
    digitalWrite(BIn2_Pin, HIGH); 
  }
  /* Set direction lines based upon sign of speed. */
  if (rightMotorSpeed>0){
    digitalWrite(AIn1_Pin, LOW);
    digitalWrite(AIn2_Pin, HIGH);
  } else {
    digitalWrite(AIn1_Pin, HIGH);
    digitalWrite(AIn2_Pin, LOW);
  }
  /* Set PWM Duty Cycles. */
  analogWrite(PWMA_Pin, abs(rightMotorSpeed));
  analogWrite(PWMB_Pin, abs(leftMotorSpeed));
}

void LeftEncoderISR()
{
  if (LED5_State) {
    digitalWrite(LED5_Pin, LOW);    // set the LED off
    LED5_State = 0;
  } else {
    digitalWrite(LED5_Pin, HIGH);    // set the LED on
    LED5_State = 1;
  }
}

void RightEncoderISR()
{
  
}

void loop() {
  interrupts();
  while (1) {
    bool leftFrontEyeState, rightFrontEyeState;
    
    leftFrontEyeState = digitalRead(leftFrontEye);
    rightFrontEyeState = digitalRead(rightFrontEye);
    
    //LED5_State = digitalRead(leftEncoder_Pin);
    //digitalWrite(LED5_Pin, LED5_State);
    
    /*if (leftFrontEyeState && rightFrontEyeState){
      SetMotorSpeed(75, 75);
    } 
    else if (leftFrontEyeState && !rightFrontEyeState){
      SetMotorSpeed(-75, 75);
    } 
    else if (!leftFrontEyeState && rightFrontEyeState){
      SetMotorSpeed(75, -75);
    } 
    else if (!leftFrontEyeState && !rightFrontEyeState){
      SetMotorSpeed(-75, -75);
    }*/
  }
}

serveurperso

Tue, 13 Sep 2011 05:32:12 +0000

Hi,

attachInterrupt(0, LeftEncoderISR, CHANGE);

The CHANGE interrupt trigger is not implemented at this time on Chipkit:(

You should use RISING or FALLING, but does not work for a 100% software quadrature decoding

Pascal


GeneApperson

Tue, 13 Sep 2011 17:15:06 +0000

Just to clarify this:

The PIC32 microcontrollers only support rising and falling edge triggering for external interrupts. It isn't possible to do LOW or CHANGE on these devices, only RISING or FALLNG.

Gene Apperson Digilnet


spencoid

Fri, 18 Nov 2011 08:58:08 +0000

Has anything changed about the state of change interrupts? Is it necessary to use four rising and falling interrupts to decode a quadrature encoder? Any other way to do it in software completely? I have some neat little chips made by LSI (S 7184) that work great but would like to use the chipkit instead of possible. Not a big deal because the LS7184 is a little 8 pin DIP and works great and has several options like pulse multiplier etc but it is difficult to find and I hate designing anything with single source devices.


GeneApperson

Tue, 13 Dec 2011 07:19:42 +0000

The external interrupts on a PIC32 device only support rising or falling edge sensitivity.

The PIC32 devices also have Input Change Notification interrupts. This can generate an interrupt when the input value on certain pins changes (the ones that contain CNxx in the signal name for the pin). The pin change interrupts are not currently supported in the chipKIT core functions. It is something that I plan to add, in the hopefully, not too distant future.

You can DIY using the change notification pins. The documentation for their use can be found in Section 12.2.6 (page 12-4) and Section 12.3.3 (page 12-15) in Section 12 I/O Ports of the PIC32 Family Reference Manual.

Gene Apperson Digilent