• 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.

Problem with external interruption

Status
Not open for further replies.

kyru27

New Member
I'm doing some tests with external interruptions, I have the following code:

Code:
#define USE_OR_MASKS
#include <p18f4620.h>
#include "portb.h"

//-------------------------------Configuration setting ----------------------------------------------
/**
	* Oscillator is configured as INT067
	* Fail safe monitor is enabled
	* watch dog timer is disabled
	* Extended instruction mode is disabled
	* oscillator switch over is enabled	
*/
//#if defined(__18F4685)		//If the selected device if PIC18F4685, then apply below settings else user will have to set
#pragma config OSC=INTIO67, FCMEN=ON, WDT=OFF, IESO=ON, XINST=OFF, LVP=OFF 
//#endif

#pragma interruptlow ISRBajaPrioridad
void ISRBajaPrioridad(void){
	Nop();
}

#pragma code PrioridadBaja = 0x18
void VectorBajaPrioridad(void){
	// Instruccion insertada en la dirección 0x18.-
	_asm goto ISRBajaPrioridad _endasm
}
#pragma code // Cerramos sección.-

#pragma interruptlow ISRAltaPrioridad
void ISRAltaPrioridad(void){
	Nop();
}

// Creamos una nueva sección de código a partir de la dirección 0x08.-
#pragma code PrioridadAlta = 0x08
void VectorAltaPrioridad(void){
	// Instruccion insertada en la dirección 0x08.-
	_asm goto ISRAltaPrioridad _endasm
}
#pragma code // Cerramos sección.-

unsigned char PORTResult[5]={0,0,0,0,0};	
 

void main(void)
{

unsigned char config=0;
	ADCON1 = 0xFF;
	TRISC=0x00;

//**************** configure INT0 with pullups enabled, falling edge *********************************
		config = PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON | PORTB_INT_PRIO_HIGH;;	
	    OpenRB0INT(config );					//configures INT0 & enables it

//*************** configure INT1 with pullups enabled, falling edge **********************************
		config=0;
		config = PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON | PORTB_INT_PRIO_LOW;;	
        OpenRB1INT( config);					//configures and enables INT1
  
//*************** configure INT2 with pullups enabled, falling edge **********************************
		config=0;
		config = PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON | PORTB_INT_PRIO_HIGH;;;	
        OpenRB2INT( config);					//configures and enables INT2

//************** configure Change Notification in PORTB  with pullups enabled, falling edge **************
		config=0;
		config = PORTB_CHANGE_INT_ON | PORTB_PULLUPS_ON;	
        OpenPORTB(config);					//configures and enables change notification in PORTB

//**************** Check for interrupts ***********************************************************
	while(1)
	{
		PORTC=0x01;
		PORTC=0x00;
	      if( INTCONbits.INT0IF == 1)			//Check for INT0
		  {			
			PORTResult[0]=1;					//Ser flag to indicate occurence of INT0
	        INTCONbits.INT0IF = 0;		  		//Clear INT0
		  }
		  
	      if(INTCON3bits.INT1IF == 1)			//Check for INT1
		  {
			PORTResult[1]=1;					//Ser flag to indicate occurence of INT1
	        INTCON3bits.INT1IF = 0;		  	//Clear INT1
		  }

	      if(INTCON3bits.INT2IF == 1)			//Check for INT2
		  {
			PORTResult[2]=1;					//Ser flag to indicate occurence of INT2
	        INTCON3bits.INT2IF = 0;		  	//Clear INT2		  
		  }

	      if(INTCONbits.RBIF == 1)				//Check for Change Notification interrupt
		  {
			PORTResult[4]=1;					//Ser flag to indicate occurence of Change Notification interrupt
	        INTCONbits.RBIF = 0;		  		//Clear Change Notification interrupt  
		  }
	}
	
	      CloseRB0INT();          //disable INT0
	      CloseRB1INT();          //disable INT1 
	      CloseRB2INT();          //disable INT2		  
	      ClosePORTB();           //disable Change notification	

}
The schematic doesn't have too much of complication, it's the microcontroller having 5 V to Vdd coneccted Vss to GND and RC0 wired to INT0 to simulate the interruption.

But despite getting the 5 V in INT0 no attention of the interruption is fired.

I've already tested in both PULL_UPS_ON and _OFF with no difference.

Any idea of what can be failing? Thanks.
 
Last edited:

kyru27

New Member
Don't know what happens with the code I posted, even if I put the high one, doesn't seem to work even in software (by software I'm meaning Debugger-> MPLAB SIM):

Anyway I have found another one that at least goes partially right in software (and as I'm just testing it's not that important to have a certain code as long as it works for what I want to show), I mean, by simulating on an stimulus workbook changes in the INT0 pin are correctly executed but not the ones from INT1 which also execute VectorInterrupcion instead of VectorInterrupcionBajo, it also doesn't fire the interruption on changes on pin 33 (INT0) that's connected to pin 15 (RC0) when tisting it with Pickit2. It's the following:

Code:
#include <p18f4620.h>
#include <delays.h>
#include <portb.h>
 
#pragma config OSC=INTIO67
 
#define true 1
 
volatile char bandera=0;
char a=0;
 
void int_extISR(void);
void int_extISRAlto(void);

 
// Creamos una nueva seccion de codigo a partir de la direccion 0x08.-
#pragma code Interrupcion = 0X0008
void VectorInterrupcion(void){
 _asm goto int_extISR _endasm
}
#pragma code // Cerramos seccion.-
 
// Rutina de Interrupcion.-
#pragma interrupt int_extISR
void int_extISR(void){
 if(INTCONbits.INT0IF==1){
   bandera=1;       // Indicamos que se ha producido la interrupción.-
   INTCONbits.INT0IF=0;  // Borramos bandera.-
 }
}

#pragma code Interrupcion2 = 0X0018
void VectorInterrupcionBajo(void){
 _asm goto int_extISRAlto _endasm
}
#pragma code // Cerramos seccion.-
 
// Rutina de Interrupcion.-
#pragma interruptlow int_extISRAlto
void int_extISRAlto(void){
Nop();
}
 
void main(void){
TRISC=0x00;
ADCON1=0x7f;
OSCCON=0b1110010; //Oscilador interno a 8MHz
TRISA=255; //puerto a entradas
TRISB=0x7f; //solo RB7 salida
LATBbits.LATB7=1; //RB7 puesto a uno
OpenRB0INT(PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_OFF | PORTB_INT_PRIO_HIGH);
OpenRB1INT(PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_OFF | PORTB_INT_PRIO_LOW);
PORTC=0x01;
PORTC=0x00;
PORTC=0x01;
RCONbits.IPEN=1;  // Habilitamos Prioridades
INTCONbits.GIEH = 1; //enable interrupts
INTCONbits.GIEL = 1; //enable low interrupts


while(true){
if(bandera==1){
bandera=0;
}
}
}
It's not fault of PULLUPS on or off as with both results it doesn't go.
 
Last edited:

kyru27

New Member
I found the problem for not atending the interruption vector it should be attending, I had to use this configs:

Code:
INTCON3bits.INT1IE=1;
INTCON3bits.INT1IP=0;

INTCON3bits.INT2IE=1;
INTCON3bits.INT2IP=0;
(I think they shouldn't be needed but whatever...)

But despite it works on software (MPLAB IDE) by simulating the toggling of a voltage, it still doesn't do it on hardware (Pickit2) wether I do send the output of RC0 to INT0, INT1 or INT2, or I connect or disconnect those interrupt pins to a 5 V supply.
 

kyru27

New Member
Sorry, I didn't understand you at first. The first example still doesn't even work in software (MPLAB IDE) for me, let alone in hardware, even if I enable the IPEN bit, so I thought you were refering to another thing.

About the schematic, adding some thing to what I already posted:

The schematic doesn't have too much of complication, it's the microcontroller having 5 V to Vdd coneccted Vss to GND and RC0 wired to INT0 (or INT1 or INT2), to simulate the interruption.



(I also connected and disconnected the INT0, INT1 and INT2 pins to a 5 V supply, in case 2 pins of a microcontroller couldn't be connected, whatever the reason).

Additional information: for my second code, yesterday it worked at least for PORTB interruptions, configurating them this way:

INTCONbits.RBIE=1; //Interrupcion de puerto B habilitada, prioridad alta

INTCON2bits.RBIP=1;

But today it doesn't even fire those interrupts :(, though I didn't change anything. Now it goes again... well, whatever.
 
Last edited:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
The first example still doesn't even work in software (MPLAB IDE) for me, let alone in hardware,
That's because in the first example you didn't enable GIEH and GIEL

Here is that code working

Code:
#define USE_OR_MASKS
#include <p18f4620.h>
#include "portb.h"
 
//-------------------------------Configuration setting ----------------------------------------------
/**
	* Oscillator is configured as INT067
	* Fail safe monitor is enabled
	* watch dog timer is disabled
	* Extended instruction mode is disabled
	* oscillator switch over is enabled	
*/

void ISRAltaPrioridad(void);
void ISRBajaPrioridad(void);

//#if defined(__18F4685)		//If the selected device if PIC18F4685, then apply below settings else user will have to set
#pragma config OSC=INTIO67, FCMEN=ON, WDT=OFF, IESO=ON, XINST=OFF, LVP=OFF 
//#endif

// Creamos una nueva sección de código a partir de la dirección 0x08.-
#pragma code PrioridadAlta = 0x08
void VectorAltaPrioridad(void){
	// Instruccion insertada en la dirección 0x08.-
	_asm goto ISRAltaPrioridad _endasm
}

#pragma code PrioridadBaja = 0x18
void VectorBajaPrioridad(void){
	// Instruccion insertada en la dirección 0x18.-
	_asm goto ISRBajaPrioridad _endasm
}

#pragma code // Cerramos sección.-
#pragma interrupt ISRBajaPrioridad
void ISRBajaPrioridad(void){
	INTCONbits.INT0IF = 0;
	INTCON3bits.INT2IF = 0;
	
}
#pragma code // Cerramos sección.-
#pragma interrupt ISRAltaPrioridad
void ISRAltaPrioridad(void){
	
	INTCONbits.RBIF = 0;
	INTCON3bits.INT1IF = 0;	
}
 

#pragma code // Cerramos sección.-
 
unsigned char PORTResult[5]={0,0,0,0,0};	
 
 
void main(void)
{
 
unsigned char config=0;
	ADCON1 = 0xF;
	TRISC=0x00;
 
//**************** configure INT0 with pullups enabled, falling edge *********************************
		config = PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON | PORTB_INT_PRIO_HIGH;;	
	    OpenRB0INT(config );					//configures INT0 & enables it
 
//*************** configure INT1 with pullups enabled, falling edge **********************************
		config=0;
		config = PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON | PORTB_INT_PRIO_LOW;;	
        OpenRB1INT( config);					//configures and enables INT1
 
//*************** configure INT2 with pullups enabled, falling edge **********************************
		config=0;
		config = PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON | PORTB_INT_PRIO_HIGH;;;	
        OpenRB2INT( config);					//configures and enables INT2
 
//************** configure Change Notification in PORTB  with pullups enabled, falling edge **************
		config=0;
		config = PORTB_CHANGE_INT_ON | PORTB_PULLUPS_ON;	
        OpenPORTB(config);					//configures and enables change notification in PORTB
 
//**************** Check for interrupts ***********************************************************
	RCONbits.IPEN = 1; 
	INTCONbits.GIEH = 1;
	INTCONbits.GIEL = 1;
	while(1)
	{
		PORTC=0x01;
		PORTC=0x00;
	      if( INTCONbits.INT0IF == 1)			//Check for INT0
		  {			
			PORTResult[0]=1;					//Ser flag to indicate occurence of INT0
	        INTCONbits.INT0IF = 0;		  		//Clear INT0
		  }
 
	      if(INTCON3bits.INT1IF == 1)			//Check for INT1
		  {
			PORTResult[1]=1;					//Ser flag to indicate occurence of INT1
	        INTCON3bits.INT1IF = 0;		  	//Clear INT1
		  }
 
	      if(INTCON3bits.INT2IF == 1)			//Check for INT2
		  {
			PORTResult[2]=1;					//Ser flag to indicate occurence of INT2
	        INTCON3bits.INT2IF = 0;		  	//Clear INT2		  
		  }
 
	      if(INTCONbits.RBIF == 1)				//Check for Change Notification interrupt
		  {
			PORTResult[4]=1;					//Ser flag to indicate occurence of Change Notification interrupt
	        INTCONbits.RBIF = 0;		  		//Clear Change Notification interrupt  
		  }
	}
 
	      CloseRB0INT();          //disable INT0
	      CloseRB1INT();          //disable INT1 
	      CloseRB2INT();          //disable INT2		  
	      ClosePORTB();           //disable Change notification	
 
}
This is what I tested
 

kyru27

New Member
Your last code works the same way as the second code I posted, it works right while simulating on MPLAB IDE but fails when debugging it with Pickit2 (when I fire the interruption it ignores it).

Anyway the RB0 pin seems to have some type of issue as it's giving 3,8 V when it should be giving 0 with the PIC only being connected to Vcc and GND so it might be causing malfunction on the other pins even if I ground it, so I guess I'll buy a new one to check.

Thanks a million for your help in any case.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top