0
votes

I try to use external interrupts (push button) to run subroutine which blink a LED, but when pushing nothing happens??

the code doesn't enter the interrupt routine, I used variable cpt to check.

the button is wired correctly (I tried it directly to led and works)

I simulated the code with Proteus and still not working

#define _XTAL_FREQ 20000000
#include <pic18f4550.h>

// BEGIN CONFIG
#pragma config OSC = HS

static int cpt = 1;

void IntExternal_INT(void) {

    TRISB0 = 1; // PORT B0 as input
    INT0E = 1;
    INTCONbits.GIE = 1;
    INTEDG0 = 1; //: Interrupt Edge Select bit : 1 = Interrupt on rising edge of RB0/INT pin

    //  0 = interrupt on falling edge
    INT0F = 0;

}

void delay() {
    volatile int i, j;
    for (i = 0; i < 2000; i++)
        for (j = 0; j < 10; j++);
}

void interrupt ISR(void) {
    cpt++;

    if (INT0IF) //If External Edge INT Interrupt
    {

        LATDbits.LATD0 = 1; // RD-0 to High
        LATDbits.LATD1 = 1; // RD-1 to High

        delay();

        LATDbits.LATD0 = 0; // RD-0 to LOW
        LATDbits.LATD1 = 0; // RD-1 to LOW

        delay();

        INT0IF = 0; // clear the interrupt
    }
}

void main(void) {
    IntExternal_INT();
    TRISD = 0xF0; // PORT B Setting: Set all the pins in port D to Output.
    while (1) {
        if (cpt % 2 == 0) {

            delay();


            LATDbits.LATD0 = 1; // RD-0 to High
            LATDbits.LATD1 = 1; // RD-1 to High



            delay();


            LATDbits.LATD0 = 0; // RD-0 to LOW
            LATDbits.LATD1 = 0; // RD-1 to LOW


        }
    }
}

on the real scheme, I used pull upresistor for MCLR->vss and LED->vss

enter image description here

1
Create a global variable initialized to 0. In your ISR, increment it immediately (i.e., not inside your if-else conditional). In your main(), print it continuously or set a breakpoint. Does the value ever change? If no, use a scope to probe the ISR pin. Does the level change when you press the button? If yes, then you aren't setting up your interrupts correctly in software. - indiv
What does "doest" mean? Does? or Doesn't? - Chris Stratton
@ChrisStratton it means doesnt :) - user2948075
I fixed it by setting interrupt port de digital - user2948075

1 Answers

1
votes

You need to enable Peripheral interrupts as well as Global interrupts:

PEIE = 1

or

INTCONbits.PEIE = 1

just before you run

INTCONbits.GIE = 1;