• AVR Freaks

Hot!External interrupts on PIC24F16KL402 not working

Author
Maximus
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2015/01/28 16:25:44
  • Location: 0
  • Status: offline
2019/09/14 14:10:57 (permalink)
0

External interrupts on PIC24F16KL402 not working

Hi, I'm using the PIC24F16KL402 MCU to control and handle data from an accelerometer. I want to use the accelerometer interrupt signal to trigger the MCU to save the samples, but I'm not able to set up the external interrupts correctly. Right now I'm trying to make INT1 work, but I will need to use INT0 too.
 
I'm following the exact steps stated in the datasheet, but there must be something else I'm missing somewhere.
 
Here's the process:
 
1) Setting NSTDIS (I don't want nested interrupts).
2) I set the interrupt priority to 7 (INT1IP bits to 1).
3) I set the polarity to positive (INT1EP = 0).
4) Clear the interrupt flag (INT1IF = 0);
5) Enable interrupt (INT1IE = 1).
 
I also made sure to set the corresponding pins as inputs with the TRISB register.
Just to make sure I was setting everything up correctly, I send the contents of the registers through the UART to check out on the terminal, and this is what I got.
 
===============
INTCON1: 0x8000
INTCON2: 0x0000
IEC0: 0x0001
IEC1: 0x0010
IFS0: 0x0000
IFS1: 0xA001
IPC0: 0x4407
IPC5: 0x0007
ANSA: 0x0000
ANSB: 0x0000
TRISA: 0x009F
TRISB: 0xFCFF
===============
 
In the interrupt routine I just send a "1" character through the UART and clear the interrupt flag.
 
I'm using a cable connected to the INT1 pin and manually putting it to 1 and 0 logic value, and it never gets to trigger the interrupt as I never get to see the "1" character on the terminal.
 
What might be the problem?
Thanks!
#1

4 Replies Related Threads

    Howard Long
    Super Member
    • Total Posts : 706
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: External interrupts on PIC24F16KL402 not working 2019/09/14 15:42:23 (permalink)
    4 (1)
    Here's some code for you running on a Microstick I 3V board.
     
    Asserting INT0 rising edge turns off the LED (RA0)
     
    Asserting INT1 rising edge turns on the LED (RA0)
     


    // PIC24F16KL402 Configuration Bit Settings
    // 'C' source line config statements
    // FBS
    #pragma config BWRP = OFF // Boot Segment Write Protect (Disabled)
    #pragma config BSS = OFF // Boot segment Protect (No boot flash segment)
    // FGS
    #pragma config GWRP = OFF // General Segment Flash Write Protect (General segment may be written)
    #pragma config GSS0 = OFF // General Segment Code Protect (No Protection)
    // FOSCSEL
    #pragma config FNOSC = FRC // Oscillator Select (Fast RC Oscillator (FRC))
    #pragma config SOSCSRC = DIG // SOSC Source Type (Digital Mode for use with external clock on SCLKI)
    #pragma config LPRCSEL = LP // LPRC Power and Accuracy (Low Power/Low Accuracy)
    #pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-speed Start-up disabled))
    // FOSC
    #pragma config POSCMD = NONE // Primary Oscillator Mode (Primary oscillator disabled)
    #pragma config OSCIOFNC = OFF // CLKO Pin I/O Function (CLKO output signal enabled)
    #pragma config POSCFREQ = HS // Primary Oscillator Frequency Range (Primary Oscillator/External Clock frequency >8MHz)
    #pragma config SOSCSEL = SOSCLP // SOSC Power Selection Configuration bits (Secondary Oscillator configured for low-power operation)
    #pragma config FCKSM = CSECMD // Clock Switching and Monitor Selection (Clock Switching Enabled; Fail-safe Clock Monitor Disabled)
    // FWDT
    #pragma config WDTPS = PS32768 // Watchdog Timer Postscale Select bits (1:32768)
    #pragma config FWPSA = PR128 // WDT Prescaler bit (WDT prescaler ratio of 1:128)
    #pragma config FWDTEN = OFF // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
    #pragma config WINDIS = OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected (windowed WDT disabled))
    // FPOR
    #pragma config BOREN = BOR0 // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware; SBOREN bit disabled)
    #pragma config PWRTEN = OFF // Power-up Timer Enable (PWRT disabled)
    #pragma config I2C1SEL = PRI // Alternate I2C1 Pin Mapping bit (Default SCL1/SDA1 Pins for I2C1)
    #pragma config BORV = LPBOR // Brown-out Reset Voltage bits (Low-power Brown-Out Reset occurs around 2.0V)
    #pragma config MCLRE = ON // MCLR Pin Enable bit (RA5 input disabled; MCLR enabled)
    // FICD
    #pragma config ICS = PGx1 // ICD Pin Placement Select (EMUC/EMUD share PGC1/PGD1)
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    #include <xc.h>
    void __attribute__ ((interrupt,auto_psv)) _INT0Interrupt(void)
    {
    LATAbits.LATA0=0;
    IFS0bits.INT0IF = 0;
    }
    void __attribute__ ((interrupt,auto_psv)) _INT1Interrupt(void)
    {
    LATAbits.LATA0=1;
    IFS1bits.INT1IF = 0;
    }
    int main(void)
    {
    TRISAbits.TRISA0=0;

    INTCON1bits.NSTDIS=1; // 1 => Disbale interrupt nesting

    CNPD2bits.CN23PDE=1; // INT0/RB7/CN23
    TRISBbits.TRISB7=1; // INT0/RB7/CN23
    INTCON2bits.INT0EP=0; // 0 => positive edge
    IPC0bits.INT0IP=7;
    IFS0bits.INT0IF=0;
    IEC0bits.INT0IE=1;
    CNPD1bits.CN12PDE=1; // INT1/RB14/CN12/AN10
    ANSBbits.ANSB14=0; // INT1/RB14/CN12/AN10
    TRISBbits.TRISB14=1; // INT1/RB14/CN12/AN10
    INTCON2bits.INT1EP=0; // 0 => positive edge
    IPC5bits.INT1IP=7;
    IFS1bits.INT1IF=0;
    IEC1bits.INT1IE=1;

    while (1)
    {
    Nop();
    }
    return 0;
    }

    post edited by Howard Long - 2019/09/14 15:44:40
    #2
    Maximus
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2015/01/28 16:25:44
    • Location: 0
    • Status: offline
    Re: External interrupts on PIC24F16KL402 not working 2019/09/14 21:27:19 (permalink)
    0
    Thanks for your reply.
     
    I found the problem, and it's quite embarrasing. Turns out that when you are using UART2, usually that means that UART1 is not working.
     
    Yes, exactly. I don't know why, but my interrupt routines were trying to send data through UART1 when it should be using UART2; most likely a typo. I fixed it and it now works like a charm.
     
    Thanks for your time looking for that solution. It still helped me realize there had to be something else going on other than the configuration of the MCU.
    #3
    Howard Long
    Super Member
    • Total Posts : 706
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: External interrupts on PIC24F16KL402 not working 2019/09/16 08:52:18 (permalink)
    0
    U1TX is on the same pin as INT0.
    #4
    Maximus
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2015/01/28 16:25:44
    • Location: 0
    • Status: offline
    Re: External interrupts on PIC24F16KL402 not working 2019/09/16 08:55:42 (permalink)
    0
    Yes, but even if it weren't, it wasn't connected to the computer terminal, so it would never send anything.
    It was just an everyday fail, I guess.
    #5
    Jump to:
    © 2019 APG vNext Commercial Version 4.5