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.

PIC18 interrupt INT problem

Status
Not open for further replies.

haxan

New Member
Hi. I am simply attaching a switch with pin RB0 (INT0) and trying to count number of times it is pressed. I am using C18 compiler and including file portb.h which is suppose to handle all the interrupts.

The problem is that the interrupt routine is not being called

Here is my code.
Code:
#include <p18cxxx.h>
#include <delays.h>
#include <portb.h>    /* for the RB0/INT0 interrupt */

#pragma config WDT = OFF, LVP = OFF, OSC = HS, DEBUG = OFF

/* ~~~~ Interrupt Handler ~~~~ */
void i_handler (void);
#pragma code i_interrupt = 0x08
void i_int (void)
{
	_asm goto i_handler _endasm
}
#pragma code
#pragma interrupt i_handler
void i_handler (void)
{
	//CloseRB0INT();
	PORTCbits.RC6 ^= 1;
	INTCONbits.RBIF = 0;
	
}

void main(void){
	ADCON1 = 0xFF;
	TRISA  = 0b00011110;
	TRISB  = 0b11110000;
	TRISC  = 0x00;

	OpenRB0INT(PORTB_CHANGE_INT_ON & PORTB_PULLUPS_OFF & RISING_EDGE_INT & PORTB_INT_PRIO_HIGH);


	while(1)
	{
	}
}

can anyone please guide me
 
I never use the library functions as I have no idea what they do. What you should do is set a beakpoint on your while(1) and see if INTCON contains the right value. I suspect you need to enable the interrupts.

Mike.
 
I actually do not know how to setup the INTCON bits for this purpose.

I wrote the code in to replace the function but its still not working. Can you please take a look at it.

Code:
	INTCONbits.INT0IE 	= 1;
	INTCONbits.INT0IF	= 0;
	INTCONbits.PEIE		= 1;
	INTCONbits.GIE		= 1;
 
Fixed all problems and now the code is working.

The following code will suspend your PIC into sleep mode and will wake up by an external interrupt sent to INT0 (RB0 pin).

In general operation, the interrupt will be disabled. However just before going to sleep, the interrupt will be enabled and hence only work after when the PIC is in Sleep mode.

The PIC will go into sleep mode after timer hits 30000 (This value can be changed according to timeout)

Here is the working code in case someone else faces same problem:

Code:
#include <p18cxxx.h>
#include <delays.h>
#include <portb.h>    /* for the RB0/INT0 interrupt */

#pragma config WDT = OFF, LVP = OFF, OSC = HS, DEBUG = OFF

void main(void);
void EnableINT0(void);

ram unsigned long timer					= 0;

/* ~~~~ Interrupt Handler ~~~~ */
void i_handler (void);
#pragma code i_interrupt = 0x18
void i_int (void)
{
	_asm goto i_handler _endasm
}
#pragma code
#pragma interrupt i_handler
void i_handler (void)
{
	timer = 0;
	INTCONbits.GIEH	= 0;			// Disable High Priority Global Interrupts
	INTCONbits.INT0IF = 0;			// Clear INT0 Interrupt Flag
}

void main(void){

	ADCON1 = 0xFF;
	TRISB  = 0x01;                       // INT0 pin as input
	
	while(1)
	{
		timer++;
		if(timer > 30000){EnableINT0();Sleep();}
	}
}

void EnableINT0(void)
{
	OpenRB0INT(PORTB_CHANGE_INT_ON & PORTB_PULLUPS_OFF & RISING_EDGE_INT & PORTB_INT_PRIO_HIGH);
	RCONbits.IPEN 		= 1;		// Enable High Priority Interrupts
	INTCONbits.GIEH		= 1;		// Disable High Priority Global Interrupts
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top