AST-CAN485 interrupt not working

Just working on my AST CAN485 board. The interrupt does not work . Have simple code . Just cannot get the led to blink , it’s Led is On . Nothing happen when I ground the pin 9. Have tried pin 6,7 & 8 still no action.

int MyPin = 9 ;

int Led =13;

volatile int State = LOW;

void setup() {

pinMode(Led, OUTPUT);

attachInterrupt(digitalPinToInterrupt(MyPin), Blink, CHANGE);

}

void loop() {

digitalWrite(Led, State); // turn the LED on (HIGH is the voltage level)

}

void Blink () {

State = !State;

}

Please help

Have you tried other modes than just “CHANGE”?

Yes just did that , still the same no action ##

What’s the external trigger circuit?

Switch to Ground.

Unless you have an external pullup, you need to turn on pullups on your input pin (otherwise, it will stay low or at least vary between 0 when grounded and possibly 1 depending on noise picked up by the lead). Try adding

pinMode(MyPin, INPUT_PULLUP);

to your setup() before the attachInteruppt()

/mike

Hi Mike, just did that added pinMode(MyPin,INPUT_PULLUP);

above attachInterrupt(digitalPinToInterrupt(MyPin), Blink, CHANGE);

Still the same.

I have just check the switch and led on another blink program it works fine.

Devan

Asked same question on Amtel forum

https://www.avrfreaks.net/forum/ast-can … ot-working

Believe the interrupt is not enable. Can any one help

Could you update your sketch to open the serial port and display the value of EXTERNAL_NUM_INTERRUPTS and let me know the result? I’ve got an idea, but I don’t have one of these boards to test the theory myself.

Note that in order to accomplish the request in my previous post, you’ll need to include wiring_private.h so that that macro will be defined in local scope.

Ok, I did some experimenting this afternoon and I think I know what your issue is. The AST-CAN485 uses an AT90CAN128 processor, so when compiling your sketch the macro AVR_AT90CAN128 gets defined. Unfortunately, when wiring_private.h gets included, this causes EXTERNAL_NUM_INTERRUPTS to be defined as 2 instead of 8 as expected. In the function attachInterrupt() a check is made so ensure that the interrupt number argument passed in is less than EXTERNAL_NUM_INTERRUPTS. If it is then the interrupt is enabled and the callback function is stored so it can be called when the interrupt occurs. In your case, the interrupt number is 7 since you’re trying to use pin 9, but since EXTERNAL_NUM_INTERRUPTS is being incorrectly defined as 2, the interrupt never gets enabled. To solve the issue, add AVR_AT90CAN128 as an additional check for defining EXTERNAL_NUM_INTERRUPTS as 8 in wiring_private.h (see code below).

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega256RFR2__) || \
    defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__) || defined(__AVR_AT90CAN128__)
    #define EXTERNAL_NUM_INTERRUPTS 8
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
    #define EXTERNAL_NUM_INTERRUPTS 3
#elif defined(__AVR_ATmega32U4__)
    #define EXTERNAL_NUM_INTERRUPTS 5
#else
    #define EXTERNAL_NUM_INTERRUPTS 2
#endif

Hi M,

Being a intermediate coder. I am trying to understand what you saying. Where do I find wiring_private.h.

And you want me to add:-

#elif defined(AVR_AT90CAN128 )

#define EXTERNAL_NUM_INTERRUPTS 8

Hi M,

Did find the a wiring_private.h file. sitting Arduino-hardware but in the Documents\Arduino\hardware\LeoStickBoardProfile-master. Not for AST-CAN485. But include the statement (AVR_AT90CAN128 )

#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)|| defined(AVR_AT90CAN128)

#define EXTERNAL_NUM_INTERRUPTS 8

#else

#define EXTERNAL_NUM_INTERRUPTS 2

#endif

Still no change

Looking at the AST-CAN library there is no call out for wiring_private.h

But notice call for

//_____ I N C L U D E S ________________________________________________________

#include “config.h”

#include “can_compiler.h”

#include <avr/io.h>

#include <avr/interrupt.h>

But don’t have avr/io or avr /interrupt files anyway in my PC

Still Stump

Dave

Sorry forgot to mention still does not work after update

Good morning, Devan. The file wiring_private.h gets included when WInterrupts.c gets compiled which is where the attachInterrupt() function is defined. On my computer (Windows 10 machine) both of these files are located at <Arduino_install_directory>\hardware\arduino\avr\cores\arduino. I think if you do a file search starting where the Arduino IDE is installed on your machine, you should be able to find it. I hope that helps.

Hi M,

Seem the file location of my Arduino Window App program. So I have clean up my Arduino IDE and installed classic Arduino IDE 1.8.12. Located wiring_private.h and added

|| defined(AVR_AT90CAN128).

My original code did work but this code work.

const int buttonPin = 9; // the number of the pushbutton pin

const int ledPin = 13; // the number of the LED pin

// variables will change:

volatile int buttonState = 0; // variable for reading the pushbutton status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT_PULLUP);

// Attach an interrupt to the ISR vector

attachInterrupt(0, pin_ISR, CHANGE);

}

void loop() {

// Nothing here!

}

void pin_ISR() {

buttonState = digitalRead(buttonPin);

digitalWrite(ledPin, buttonState);

}

But still stumped it works on any ButtonPin selected how??

And do I include second interrupt??

Also notice this does not work

attachInterrupt(MyPin, pin_ISR, CHANGE);

but this work

attachInterrupt(0, pin_ISR, CHANGE);

On any pin

Appricate comment still does solve the requirements

I would suggest trying

attachInterrupt(k,pin_ISR,CHANGE)

with k ranging from 0 to 7. If it works for 0 and 1 but not 2-7, then it means your update to wiring_private.h did not accomplish what it was intended to.

Yes you are correct. Works for 0 & 1 only.

And correct again reverting the wiring_private.h back to original.

Still works as above