Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC C18 External Interrupts INT0

Status
Not open for further replies.

natraj20

New Member
Hello guys,

Problem with the very basics.
I am stuck with the external interrupt INT0 at RB0.
Controller - PIC 18F47J53
MPLAB IDE, Microchip C Compiler

I could not figure out what is wrong with the following code. Kindly help me out to make this work!!

#include <p18f47j53.h>
#include <delays.h>
#include <portb.h>

#pragma config OSC = HS
#pragma config WDTEN = OFF
#pragma config XINST = OFF

void InterruptServiceHigh(void);
void InterruptServiceLow(void);

int x;

void main(void)
{

INTCONbits.INT0IF = 0;
INTCONbits.INT0IE = 1;
INTCON2bits.INTEDG0 = 1;

INTCON2bits.RBPU = 0;
//EnablePullups();

TRISE = 0;
TRISBbits.TRISB0 = 1;

RCONbits.IPEN = 1;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;

while(1)
{
PORTE = 0x01;
}

}


// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh(void)
{
_asm
goto InterruptServiceHigh
_endasm
}


// Low priority interrupt vector

#pragma code InterruptVectorLow = 0x18
void InterruptVectorLow(void)
{
_asm
goto InterruptServiceLow
_endasm
}



// Interrupt Service Routine

// Interrupt pragma for high priority

#pragma code
#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh()
{
// function statements

if(INTCONbits.INT0IF)
{
x = 0;

while(x <= 2)
{
PORTE = 0x01;

Delay10KTCYx(120); // Delay of 1 sec with 12 MHz

PORTE = 0x02;

Delay10KTCYx(120);

x++;
}
INTCONbits.INT0IF = 0;
}

// return from high priority interrupt

}


// Interrupt pragma for low priority

#pragma code
#pragma interrupt InterruptServiceLow
void InterruptServiceLow()
{
// function statements

// return from low priority interrupt

}
 
Hello guys,

Problem with the very basics.
I am stuck with the external interrupt INT0 at RB0.
Controller - PIC 18F47J53
MPLAB IDE, Microchip C Compiler

I could not figure out what is wrong with the following code. Kindly help me out to make this work!!
Code:
#include <p18f47j53.h>
#include <delays.h>
#include <portb.h>

#pragma config OSC = HS
#pragma config WDTEN = OFF
#pragma config XINST = OFF

void InterruptServiceHigh(void);
void InterruptServiceLow(void);

int x;

void main(void)
{

    INTCONbits.INT0IF = 0;
    INTCONbits.INT0IE = 1;
    INTCON2bits.INTEDG0 = 1;

    INTCON2bits.RBPU = 0;
    //EnablePullups();

    TRISE = 0;
    TRISBbits.TRISB0 = 1;

    RCONbits.IPEN = 1;
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;

    while(1)    
    {
        PORTE = 0x01;
    }

}


//  High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh(void)
{
    _asm
    goto InterruptServiceHigh
    _endasm
}


//  Low priority interrupt vector

#pragma code InterruptVectorLow = 0x18
void InterruptVectorLow(void)
{
    _asm
    goto InterruptServiceLow
    _endasm
}



//  Interrupt Service Routine

// Interrupt pragma for high priority

#pragma code
#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh()
{
    // function statements

    if(INTCONbits.INT0IF)
    {
        x = 0;
            
        while(x <= 2)
            {
                PORTE = 0x01;
            
                Delay10KTCYx(120);    // Delay of 1 sec with 12 MHz
            
                PORTE = 0x02;
        
                Delay10KTCYx(120);
            
                x++;
            }
        INTCONbits.INT0IF = 0;
    }

    // return from high priority interrupt

}


//  Interrupt pragma for low priority

#pragma code
#pragma interrupt InterruptServiceLow
void InterruptServiceLow()
{
    // function statements

    // return from low priority interrupt
}

I placed your source between code tags (Use the # key when you post. It keeps your formatting, so it is easier to read your code.

I didn't take a close look, but one thing which jumped out was that you are calling a delay from your interrupt service routine. That isn't a good habit to get into, and often makes a program fail.
The preferred way to do this it to set a flag variable in the ISR, and check for that flag in your endless loop. ie: while(1){ if(interrupt_flag) do what i want; call as many delays as I want;}
regards

EDIT: You may find that flashing just a single LED as a subroutine, from within main is a good debugging technique. You can put the call to it most anywhere to know if the chip gets to it.
 
Last edited:
Here is your code. You missed to put in values if ANCON0 and ANCON1 to configure the pins as digital(datasheet page 371).



Code:
#include <p18f47j53.h>
#include <delays.h>
#include <portb.h>

#pragma config OSC = HS
#pragma config WDTEN = OFF
#pragma config XINST = OFF

void InterruptServiceHigh(void);
void InterruptServiceLow(void);

int x;

void main(void)
{
	[B][COLOR="red"]ANCON1=0xff// sets all Analog 
	ANCON0=0xff;//pins as digital[/COLOR][/B]
    INTCONbits.INT0IF = 0;
    INTCONbits.INT0IE = 1;
    INTCON2bits.INTEDG0 = 1;

    INTCON2bits.RBPU = 0;
    //EnablePullups();

    TRISE = 0;
    TRISBbits.TRISB0 = 1;

    RCONbits.IPEN = 1;
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;

    while(1)    
    {
        PORTE = 0x01;
    }

}


//  High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh(void)
{
    _asm
    goto InterruptServiceHigh
    _endasm
}


//  Low priority interrupt vector

#pragma code InterruptVectorLow = 0x18
void InterruptVectorLow(void)
{
    _asm
    goto InterruptServiceLow
    _endasm
}



//  Interrupt Service Routine

// Interrupt pragma for high priority

#pragma code
#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh()
{
    // function statements

    if(INTCONbits.INT0IF)
    {
        x = 0;
            
        while(x <= 2)
            {
                PORTE = 0x01;
            
                Delay10KTCYx(120);    // Delay of 1 sec with 12 MHz
            
                PORTE = 0x02;
        
                Delay10KTCYx(120);
            
                x++;
            }
        INTCONbits.INT0IF = 0;
    }
}




//  Interrupt pragma for low priority

#pragma code
#pragma interrupt InterruptServiceLow
void InterruptServiceLow()
{
    // function statements

    // return from low priority interrupt
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top