full functionality of this site and enable such things as forum posting.
Digisol:
Hello!
I need to work with external interrupts on a custom Max32, I saw several examples, but mine won't work I wonder what am I doing wrong,
I will appreciate very much if somebody lend me a hand with this.
I need all 5 external interrups working (0-->5)
it always show 0, no matter what.
Here is my code for one interrupt:
#include<Timer.h>
static const short pin_RPM = 20;
int rpm, rpmMax=0, rpmMin=100;
volatile int M_Rev=0;
Timer Tmr_RPM;
void __attribute__((interrupt)) rpm_in()
{
M_Rev++;
clearIntFlag(_EXTERNAL_4_IRQ);
}
void ShowRPM() // It is called every second to convert Hertz to RPM
{
detachInterrupt(4);
rpm = M_Rev*60;
M_Rev = 0;
if(rpm > rpmMax) rpmMax = rpm;
if(rpm < rpmMin) rpmMin = rpm;
Serial.print("rpm: ");
Serial.print(rpm);
Serial.print(", Max: ");
Serial.print(rpmMax);
Serial.print(", Min: ");
Serial.println(rpmMin);
attachInterrupt(pin_RPM, rpm_in, RISING);
}
void setup()
{
char Dato;
short A;
Serial.begin(19200);
Serial.println("Reading RPM");
pinMode(pin_RPM, INPUT);
setIntVector(_EXTERNAL_4_VECTOR, rpm_in);
setIntPriority(_EXTERNAL_4_VECTOR, 4, 1);
clearIntFlag(_EXTERNAL_4_IRQ);
setIntEnable(_EXTERNAL_4_IRQ);
attachInterrupt(pin_RPM, rpm_in, RISING);
Tmr_RPM.every(1000, ShowRPM);
}
void loop()
{
Tmr_RPM.update();
}
matt:
For attachInterrupt()
you mustn't flag the callback function as an interrupt. It is wrapped in a calling function that is flagged as an interrupt. By flagging it as an interrupt you are changing the return from JR $ra
to ERET
which could cause untold problems.
Note that a function passed to attachInterrupt()
is the only kind of ISR that you don't flag - unless a specific library provides a wrapper - all the others need flagging.
Also you should use __USER_ISR
instead of __attribute__((interrupt))
so that the code is portable between subfamilies of PIC32 chips.
Also none of the setIntVector
, setIntPriority
etc are wanted - those are all taken care of by attachInterrupt
. And don't use detachInterrupt
to stop interrupts - instead use disableInterrupts
and restoreInterrupts
.
Your code should simply be:
#include<Timer.h>
static const short pin_RPM = 20;
int rpm, rpmMax=0, rpmMin=100;
volatile int M_Rev=0;
Timer Tmr_RPM;
void rpm_in() {
M_Rev++;
}
void ShowRPM() { // It is called every second to convert Hertz to RPM
uint32_t s = disableInterrupts();
rpm = M_Rev*60;
M_Rev = 0;
restoreInterrupts(s);
if(rpm > rpmMax) rpmMax = rpm;
if(rpm < rpmMin) rpmMin = rpm;
Serial.print("rpm: ");
Serial.print(rpm);
Serial.print(", Max: ");
Serial.print(rpmMax);
Serial.print(", Min: ");
Serial.println(rpmMin);
}
void setup() {
char Dato;
short A;
Serial.begin(19200);
Serial.println("Reading RPM");
pinMode(pin_RPM, INPUT);
attachInterrupt(pin_RPM, rpm_in, RISING);
Tmr_RPM.every(1000, ShowRPM);
}
void loop() {
Tmr_RPM.update();
}