Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] enable interrupt in pic18 in c18 compliler

Status
Not open for further replies.

shreyas_patel21

Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
hello everyone,

how to enable external interrupt or IOC(interrupt on change) in pic18f46k22?
i am using microchip mplab c18 compiler.
here is the code which i tried:

Code:
#include <stdio.h>
#include <p18f46k22.h>

#define LED PORTDbits.RD2 
#define push_butten PORTDbits.RD5 

void ioc(void);

#pragma code low_vector=0x18

void low_interrupt (void)
{
	_asm GOTO ioc _endasm
}	
#pragma code

#pragma interruptlow ioc

void ioc(void)
{
	LED=push_butten;
}

void delay(int ms)	
{
	unsigned int x;
	T1CON=0x00;
	for(x=0;x<ms;x++)
	{
		TMR1L=0x00;
		T1CON=0x33;
		while(TMR1L<0xFA);
		T1CON = 0x0000;
	}
}

void main()
{
	OSCCONbits.IRCF0 =1;
	OSCCONbits.IRCF1 = 1;
	OSCCONbits.IRCF2 = 1;
	OSCTUNEbits.PLLEN = 1;
	    
    ANSELA = 0x00;
    ANSELB = 0x00;    
    ANSELC = 0x00;
    ANSELD = 0x00;
    ANSELE = 0x00;
            
	TRISA=0x00;
	TRISB=0X0000;	//output
    TRISC=0X0000;	//output
    TRISD=0X00;	//output
    TRISE=0X00;	//output
	LED=1;
	IOCB=0xff;
	INTCONbits.RBIE=1;
	while(1)
	{
		push_butten=1;
		delay(2500);
		push_butten=0;	
		delay(2500);
	}

}

thank you.
 

You should enable GIE and teh correspondent INTxIE register. For INT0IE both are on INTCON. DOnt forget to clear the flag!!

Cya
 

thanks Sink0,

I had already enabled that interrupt in
INTCONbits.RBIE=1;
line
i also checked by clearing the flag.
my updated code is:
Code:
#include <stdio.h>
#include <p18f46k22.h>



void delay(int ms)	
{
	unsigned int x;
	T1CON=0x00;
	for(x=0;x<ms;x++)
	{
		TMR1L=0x00;
		T1CON=0x33;
		while(TMR1L<0xFA);
		T1CON = 0x0000;
	}
}

void main()
{
	OSCCONbits.IRCF0 =1;
	OSCCONbits.IRCF1 = 1;
	OSCCONbits.IRCF2 = 1;
	OSCTUNEbits.PLLEN = 1;
	    
    ANSELA = 0x00;
    ANSELB = 0x00;    
    ANSELC = 0x00;
    ANSELD = 0x00;
    ANSELE = 0x00;
            
	TRISA=0x00;
	TRISB=0Xffff;	//output
    TRISC=0X0000;	//output
    TRISD=0X00;	//output
    TRISE=0X00;	//output
    TRISBbits.RB7=1;
    TRISBbits.RB6=1; 		
	LED=1;
	WPUB=0xff;
	IOCB=0xff;
	PORTB=0x0000;
	INTCONbits.RBIF=0;
	INTCONbits.RBIE=1;
	INTCONbits.RBIF=0;
	INTCON2bits.RBIP=0;
	INTCON2bits.RBPU=0;
	push_butten=1;
	while(1)
	{
		if(INTCONbits.RBIF==1)
		{
			LED=~LED;
			
			INTCONbits.RBIF=0;
		}

//		push_butten=1;
		delay(250);
//		push_butten=0;	
		delay(250);
	}

}

now i am getting interrupt but when interrupt pin gets logic high it always go in interrupt state.
and when logic 0 it never go to interrupt.

thank you

---------- Post added at 17:56 ---------- Previous post was at 17:07 ----------

i got the solution!
i forgot to end the mismatch by reading the port B before clearing the interrupt flag.
here is the working code.

Code:
#include <stdio.h>
#include <p18f46k22.h>

#define LED PORTDbits.RD2 
#define push_butten PORTDbits.RD5 

void delay(int ms)	
{
	unsigned int x;
	T1CON=0x00;
	for(x=0;x<ms;x++)
	{
		TMR1L=0x00;
		T1CON=0x33;
		while(TMR1L<0xFA);
		T1CON = 0x0000;
	}
}

void main()
{
	unsigned int mismatch;
        OSCCONbits.IRCF0 =1;
	OSCCONbits.IRCF1 = 1;
	OSCCONbits.IRCF2 = 1;
	OSCTUNEbits.PLLEN = 1;
	    
    ANSELA = 0x00;
    ANSELB = 0x00;    
    ANSELC = 0x00;
    ANSELD = 0x00;
    ANSELE = 0x00;
            
	TRISA=0x00;
	TRISB=0Xffff;	//output
    TRISC=0X0000;	//output
    TRISD=0X00;	//output
    TRISE=0X00;	//output
    TRISBbits.RB7=1;
   
	LED=1;
	WPUB=0xff;
	IOCB=0xff;
	PORTB=0x0000;
	INTCONbits.RBIF=0;
	INTCONbits.RBIE=1;
	INTCONbits.RBIF=0;
	INTCON2bits.RBIP=0;
	INTCON2bits.RBPU=0;
	push_butten=1;
	while(1)
	{
		if(INTCONbits.RBIF==1)
		{
			LED=~LED;
			mismatch=PORTB;
			INTCONbits.RBIF=0;
		}

		push_butten=1;
		delay(250);
		push_butten=0;	
		delay(250);
	}

}

i have connected push_butten pin to the interrupt pin.

thank you.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top