Chris
Chris

Reputation: 683

My first PIC32MX ISR not firing, code is hanging

I'm just getting started with a PIC32MX340F12, and MPLABX. My first attempt was to write a timer interrupt, so I worked with the datasheet, compiler manual, and examples and came up with the below. But it doesn't work... the interrupt never fires, and in fact if I leave both the timer interrupt enable (T1IE=1) and the general interrupt enable active ("ei"), it runs for a few seconds then hangs (says "target halted" in debug mode). If I remove either of those, it just runs indefinitely but still no timer interrupt. So I appear to have a pretty bad problem somewhere in my ISR syntax. Does it jump out at anyone?

Like I said I'm just getting started so I'm sure it's a pretty dumb oversight. And as you may notice I like to work as directly as possible with registers and compiler directives (rather than manufacturer supplied functions), I feel like I learn the most that way.

Thanks!

#include <stdio.h>
#include <stdlib.h>
#include "p32mx340f512h.h"
#include <stdint.h>

int x = 0;

int main(int argc, char** argv) 
{
    INTCONbits.MVEC = 1;  // turn on multi-vector interrupts
    T1CON = 0;            // set timer control to 0
    T1CONbits.TCKPS = 1;  // set T1 prescaler to 8
    PR1 = 62499;          // set t1 period
    TMR1 = 0;             // initialize the timer
    T1CONbits.ON = 1;     // activate the timer

    IPC1bits.T1IP = 5;    // T1 priority to 5
    IPC1bits.T1IS = 0;    // T1 secondary priority to 
    IFS0bits.T1IF = 0;    // clear the T1 flag
    IEC0bits.T1IE = 1;    // enable the T1 interrupts

    asm volatile("ei");   // enable interrupts

    while (1)
    {
         x++;   

         if (x > 10000)
         {
             x = 0;
         }
    }
    return (EXIT_SUCCESS);
}

bool zzz = false;

void __attribute__((interrupt(IPL5AUTO))) T1Handler(void)
{
    IFS0bits.T1IF = 0;
    zzz = true;
}

Upvotes: 1

Views: 564

Answers (1)

Carlos Querol
Carlos Querol

Reputation: 11

Embedded systems are somewhat specialized, and this is a specific one I'm not familiar with.

However, from working with other systems, you may have to associate the Int Handler function address (T1Handler) with the interrupt it is handling. (Unless the framework you are using is doing that for you under the covers when building?)

Are all those names you are using automatically mapped for you by the build system? If not, you may need to call some kind HW init or framework init at the top of main, before using them.

Some HW init/reset may be needed also, before the HW can be programmed.

Hope some of this helps.


Upvotes: 1

Related Questions