Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pic32 Interrupts
12-05-2012, 05:52 PM, (This post was last modified: 12-05-2012, 07:53 PM by KiloOne.)
#1
Pic32 Interrupts
I am having a hard time figuring out how to use interrupts on the Pic32's

The library functions I thought were available by the Wiki page are only for p8.

The interrupt example programs do not help.

I see that p32 has interrupt.c but can't figure out how to generate and monitor an interrupt for a state change of, say RB4.

Is there some documentation (other than the Pic32 datasheet which is a level I prefer not to use at this point) that can help me.

Thanks,
Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
17-05-2012, 12:12 AM,
#2
RE: Pic32 Interrupts
So I am now at a stage where I have to learn how to do this on the MICRO, any hints?

I just want an interrupt on a rising edge of an input line to increment, in software, a global variable by one count every interrupt.

Thanks,
Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
17-05-2012, 08:25 AM,
#3
RE: Pic32 Interrupts
Hi Dale

I didn't test this code but it should work as expected.

/*-----------------------------------------------------
Author: --<Jean-Pierre Mandon>
Date: Thu May 17 08:10:53 2012
Description: example of external interrupt with Pinguino 32

-----------------------------------------------------*/

volatile u32 counter=0; // counter is used to count pulse

void ISR_wrapper_vector_19(void) __attribute__ ((section (".vector_19")));
// vector 19 is the external input 4 interrupt vector
// INT4 is on pin CON1-13 on Pinguino-Micro

void ISR_wrapper_vector_19(void) { Int4Interrupt(); }


void Int4Interrupt(void) __attribute__ ((interrupt));



void Int4Interrupt(void) // interrupt treatment
{
if (IFS0bits.INT4IF) // interrupt flag for INT4
{
counter++;
IFS0bits.INT4IF=0; // clear the interrupt flag
}
}


void setup() {
IEC0bits.INT4IE=0; // disable this interrupt
INTCONbits.INT4EP=1; // edge polarity for this interrupt ( falling edge )
IPC4bits.INT4IP=1; // interrupt priority
IPC4bits.INT4IS=1; // interrupt sub priority
IEC0bits.INT4IE=1; // enable this interrupt
}

void loop() {


}

JP

(17-05-2012, 12:12 AM)KiloOne Wrote: So I am now at a stage where I have to learn how to do this on the MICRO, any hints?

I just want an interrupt on a rising edge of an input line to increment, in software, a global variable by one count every interrupt.

Thanks,
Dale
Reply
17-05-2012, 01:36 PM,
#4
RE: Pic32 Interrupts
JP,

Wow, that would have been difficult for me to come up with Big Grin

I will test and report back.

Thanks very much.
Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
18-05-2012, 03:07 PM,
#5
RE: Pic32 Interrupts
Works like a charm!

Thanks JP
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
25-05-2012, 01:09 PM,
#6
RE: Pic32 Interrupts
Hi there,

I'm using an MX220 and asked myself how to use the PPS external interrupts INT1-INT4.

The above example however has a fixed mapping, which is not usable in case of PPS to my understanding. The goal is to use the following pins:

INT1 on RPC2 (0110), which maps to "D3"
INT2 on RPC3 (0111), which maps to "D2"
INT3 on RPA1 (0000), which maps to "PGC" (PGEC3, ICSP-Header 5)
INT4 on RPA0 (0000), which maps to "PGD" (PGED3, ICSP-Header 4)

Can you provide any hints on how to set the relevant bits with the above syntax, which result in the desired mapping?

I might overlook something, but to me it looks like this doesn't work.

Thanks in advance.
Reply
28-05-2012, 10:19 AM, (This post was last modified: 28-05-2012, 10:22 AM by jpmandon.)
#7
RE: Pic32 Interrupts
Hi

First you need to remap INT1 signal to RPC2. A good example to do this is to check the io.c file in /p32/include/pinguino/core folder.

#include <system.c>

SystemUnlock();
CFGCONbits.IOLOCK=0; // unlock configuration
CFGCONbits.PMDLOCK=0;

INT1Rbits.INT1R=Ob0110; // Define RC2 as INT1 ( datasheet page 146 )

CFGCONbits.IOLOCK=1; // relock configuration
CFGCONbits.PMDLOCK=1;
SystemLock();

then use the same code as shown above to write an interrupt handler for the INT1 signal:

void ISR_wrapper_vector_7(void) __attribute__ ((section (".vector_7")));
// vector 7 is the external input 1 interrupt vector
// INT1 is on pin D3 on MX220

void ISR_wrapper_vector_7(void) { Int1Interrupt(); }


void Int1Interrupt(void) __attribute__ ((interrupt));



void Int1Interrupt(void) // interrupt treatment
{
if (IFS0bits.INT1IF) // interrupt flag for INT1
{
counter++;
IFS0bits.INT1IF=0; // clear the interrupt flag
}
}


void setup() {
IEC0bits.INT1IE=0; // disable this interrupt
INTCONbits.INT1EP=1; // edge polarity for this interrupt ( falling edge )
IPC1bits.INT1IP=1; // interrupt priority
IPC1bits.INT1IS=1; // interrupt sub priority
IEC0bits.INT1IE=1; // enable this interrupt
}

You can use the same syntax to create interrupt handler for INT2,INT3 and INT4.

Have fun
JP

PS: not tested this code but it should work as is.
Reply
28-05-2012, 11:03 AM,
#8
RE: Pic32 Interrupts
Not got time to test that but if MarSch does, then:
INT1Rbits.INT1R=Ob0110; // Define RC2 as INT1 ( datasheet page 146 )
looks to have a typo - "Ob0110" should be "0b0110"

HTH,

John
Reply
29-05-2012, 09:37 AM,
#9
RE: Pic32 Interrupts
(28-05-2012, 11:03 AM)pingotg Wrote: INT1Rbits.INT1R=Ob0110; // Define RC2 as INT1 ( datasheet page 146 )
looks to have a typo - "Ob0110" should be "0b0110"

It makes no sense to prefix a number with an O. It must be a zero, of course, to be correctly handled as binary notation.

/MarSch
Reply
29-05-2012, 09:35 PM,
#10
RE: Pic32 Interrupts
Hi there,

thanks a lot for the help, now it builds at least.

But unfortunately, that's it pretty. If I connect the 3.3V pin of the board with D3, I'd expect the ISR to be jumped in. However, nothing happens at all.

I made up the example below as simple as possible to prevent stupid errors. Maybe someone spots the problem, but I suspect it's not as easy and the Unlock/Lock functions don't do what they're expected to.

Any ideas?

/MarSch


Code:
/* ========================================================================== */
/*                                                                            */
/*   InterruptWithPeripheralPinSelect.c                                       */
/*   (c) 2012 Pinguino Forum (with hints from J.P. Mandon)                    */
/*                                                                            */
/*   This example shows how to use external interrupts INT1-INT4 on the       */
/*   MX 220 via Peripheral Pin Select (PPS).                                  */
/*                                                                            */
/*                                                                            */
/*   Helpful resources:                                                       */
/*     http://www.olimex.com/dev/pic32-pinguino-mx220.html                    */
/*     http://ww1.microchip.com/downloads/en/DeviceDoc/61168D.pdf             */
/*     Pinguino (p32/include): p32mx220f032d.h, interrupt.c, p32mxgeneric.h   */
/*                                                                            */
/* ========================================================================== */

// -----------------------------------------------------------------------------
//  Interrupt setup
// -----------------------------------------------------------------------------
  
// Initialize interrupt vector
// From p32mxgeneric.h
//   #define _IFS0_INT1IF_POSITION                    0x00000007  =   7
void ISR_wrapper_vector_7(void)  __attribute__ ((section (".vector_7")));

// Assign ISR handlers to interrupt vector
void ISR_wrapper_vector_7(void)  { Int1Interrupt(); }

// Declare interrupt handler
void Int1Interrupt(void) __attribute__ ((interrupt));


// -----------------------------------------------------------------------------
//  Global variables
// -----------------------------------------------------------------------------
  
// Assign pin variable
const int ledPin = 13;

// Define and initialize flag (declare volatile when used in ISRs)
volatile boolean blinkFlag = FALSE;


// -----------------------------------------------------------------------------
//  ISR handlers
// -----------------------------------------------------------------------------

// Implement interrupt handlers
void Int1Interrupt(void)
{
  if (IFS0bits.INT1IF) // interrupt flag for INT1
  {
    blinkFlag = TRUE;
    IFS0bits.INT1IF = 0; // clear the interrupt flag
  }
}


// -----------------------------------------------------------------------------
//  Setup pins and peripheral functionality
// -----------------------------------------------------------------------------
  
// Initialize MCU
void setup()
{
  // Unlock MCU assignment to use PPS
  SystemUnlock();
  CFGCONbits.IOLOCK=0;            // unlock configuration
  CFGCONbits.PMDLOCK=0;
  
  // Assign external interrupts
  // Bit assignments are listed on page 146 of the datasheet
  INT1Rbits.INT1R = 0b0110; // Define RPC2 as INT1 (MX220 pin D3)

  // Lock PPS facility again
  CFGCONbits.IOLOCK=1; // relock configuration
  CFGCONbits.PMDLOCK=1;
  SystemLock();
    
  // Interrupt setup
  IEC0bits.INT1IE=0; // disable this interrupt
  INTCONbits.INT1EP=0; // edge polarity for this interrupt ( rising edge )
  IPC1bits.INT1IP=1; // interrupt priority
  IPC1bits.INT1IS=1; // interrupt sub priority
  IEC0bits.INT1IE=1; // enable this interrupt
    
  // Initialize pin 13 (onboard LED green) as output
  pinMode(ledPin, OUTPUT);
  
  // Tell the world I'm alive
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  if (blinkFlag == TRUE)
  {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    blinkFlag = FALSE;
  }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)