How to use interrupt on Pinguino32

Pinguino32 doesn’t support interrupt routines without writing a code in an assembler file. We decided to open interrupt to everybody. To do this, we used some of the special attribute directive of GCC. Now you can deal with Interrupt without assembler.

This example show how to use the Timer 1 to increment a counter.

// Using interrupt with Pinguino32
// Jean-Pierre MANDON 2011

#include <interrupt.c>

unsigned int counter=0;

void ISR_wrapper_vector_4(void) __attribute__ ((section (".vector_4")));
// Put the ISR_wrapper in the good place

void ISR_wrapper_vector_4(void) { Tmr1Interrupt(); }
// ISR_wrapper will call the Tmr1Interrupt()

void Tmr1Interrupt(void) __attribute__ ((interrupt));
// Tmr1Interrupt is declared as an interrupt routine

// define here the code to execute when an interrupt occure
void Tmr1Interrupt(void)
if (IFS0bits.T1IF)		// Timer Interrupt flag
	TMR1=0;			// reset the timer register
	IFS0CLR=0x10;		// Clear the timer interrupt flag
	counter++;		// increment the counter

// configure timer 1 
void init_timer1(void)
IntConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);	// interrupt mode (interrupt.c)
T1CON=0;		// reset timer 1 configuration
TMR1=0;			// reset timer 1 counter register
PR1=0x9999;		// define the preload register
IPC1SET=0x7;		// select interrupt priority and sub-priority
IFS0CLR=0x10;		// clear interrupt flag
IEC0SET=0x10;		// enable timer 1 interrupt
T1CONSET=0x8010;	// start timer 1 and set prescaler

void setup()

void loop()
CDC.printf("%d\n\r",counter);	// counter is incremented at each timer 1 overflow

Be careful, some interruption are already used by Pinguino32. Vector_8 (timer 2) is used by the function millis(). vector_24 (Serial1 interrupt) and vector_32 (Serial2 interrupt) are used by the serial module. vector_35 ( RTCC ) is used by the real time clock module.
All the test have been made with a PIC32-PINGUINO-OTG board and Linux IDE.

4 thoughts on “How to use interrupt on Pinguino32”

  1. This could usefully be a sample, but first:

    Tmr1Interrupt is used before being declared.

    counter needs to be declared volatile!

    (I’ve not checked the init values so hope they’re OK.)


  2. John

    This code works as it is shown here.
    No need to declare counter as volatile, its value is modified in the same section ( an interrupt is not a thread ).
    Tmr1Interrupt need to be used prior to declare it. If you try to call it after declaring the subroutine, this program don’t compile.

  3. Hi Jean Pierre

    Some question about this timer stories and the interupt routines of the PIC32.
    What is the clock frequency used for the timer ? Is it the same as for the Pinguino18 (that is Fosc/4 where Fosc is 48 MHz) or is it different ?

  4. HI again

    looks like i have the answer in the source, but still not sure. Is it 40 MHz ? and for the PIC32MX220 (pdip) 20 MHz?? It look s also like this is configurable at the very begining of a program through the call to SystemClocksWriteSetting . Am i right??

Comments are closed.