Pic18F4550 external interrupt problem  - Page 1
Forum section: electronics / general

author: Bibiricat
author_rank: Contributor
timestamp: May 20, 2013, 04:29:55 pm
content: Hello.A friend of mine is trying to make a PORTB external interrupt 0, bit 0 to happen, and i helped him , but now he modified my code, and i can't find anything wrong with it, and yet it doesn't work .I was wondering if anyone can have a look, and point what can be wrong with it.The code was supposed to enter a interrupt routine and do a small active "0" impulse.Code:#include <p18f4550.h>#include <delay.h>#pragma code REMAPPED_RESET_VECTOR=0x1000// Car on utilise le Bootloader HID de Microchipextern void _startup (void);void _reset (void){_asm goto _startup _endasm}#pragma code//#pragma config XINST = OFF//pragma config WDT = OFF //Disable watchdog timervoid InterruptServiceHigh(void);void InterruptServiceLow(void);/***************************************************************/// High priority interrupt vector#pragma code InterruptVectorHigh = 0x1008void InterruptVectorHigh(void){_asm goto InterruptServiceHigh _endasm}#pragma code/**************************************************************/// Low priority interrupt vector#pragma code InterruptVectorLow = 0x1018void InterruptVectorLow(void){_asm goto InterruptServiceLow _endasm}#pragma code/**************************************************************/// Interrupt Service Routine// Interrupt pragma for high priority#pragma interrupt InterruptServiceHighvoid InterruptServiceHigh(void){if(INTCONbits.INT0IF){PORTBbits.RB7=0;Delay10TCYx (120);PORTBbits.RB7=1;}INTCONbits.INT0IF=0; // reset INT0 external interrupt flagINTCONbits.RBIF = 0;}#pragma code/*************************************************************/#pragma interrupt InterruptServiceLowvoid InterruptServiceLow(void){}#pragma code/*************************************************************/void main(void){/************* Configure Ports ***************************************/TRISB=0b00000001; // Set portb bit zero & one as inputPORTB = 0;PORTBbits.RB7=1;/***** Configure external interrupt INT0 *****/INTCONbits.INT0IF = 1; // SET INT0 FlagINTCONbits.INT0IE = 1; // Enables the INT0 external interruptINTCON2bits.INTEDG0 = 1; // Interrupt on rising edgeINTCON2bits.RBPU = 1; // All PORTB pull-ups are disabledINTCONbits.RBIE = 1; //1= Enables the RB port change interruptINTCONbits.RBIF = 0; //1 = At least one of the RB7:RB4 pins changed state (must be cleared in software)INTCON2bits.INTEDG0 = 0; //0 = Interrupt on falling edgeINTCON2bits.RBIP = 1;//RBIP: RB Port Change Interrupt Priority bit 1 = High priorityRCONbits.IPEN = 1; // Enable priority levels on interruptsINTCONbits.GIE = 1; // Enables all high priority interruptsINTCONbits.PEIE = 1; // Enables all low priority interruptsINTCONbits.INT0IF=0; // reset INT0 external interrupt flagwhile(1){if(PORTBbits.RB0 == 0)//test button press{PORTBbits.RB2 = 1;}else PORTBbits.RB2 = 0;}}

author: AlfBaz
author_rank: Super Contributor
timestamp: May 20, 2013, 04:50:06 pm
content: Just a quick lookUse LATBbits instead of PORTBbits throughout you code. If it works then search the data sheet for Read Modify Write to find out why

author: Bibiricat
author_rank: Contributor
timestamp: May 20, 2013, 05:00:41 pm
content: It did work with PORTB . I had some problems with LATB , so i only use PORTB.

author: afho
author_rank: Contributor
timestamp: May 20, 2013, 05:16:00 pm
content: Hi,some parts of your code confuses meFist you are configuring both interrupts INT0 and Interrupt on change for PORTB. INT0 on rising edge and Int on change for falling edge. If you only need RB0 i would use only INT0 and disable the interrupt on change feature. Than you also have to change the INTEDG0 to generate the interrupt on falling edge.The interrupt on change for PORTB has a little feature that costs me some trys too to get working:QuoteThis mean you have to read or write to PORTB if you want to happen the interrupt more than one time, even when you dont want to use the information.

author: Bibiricat
author_rank: Contributor
timestamp: May 20, 2013, 05:26:56 pm
content: I was frustrated and i configured everything .I just want INT0 to work.

author: Bibiricat
author_rank: Contributor
timestamp: May 30, 2013, 10:50:00 am
content: I finally got time to redo all the code, and managed to get it working again. This is the final code, that works with pic18f4550 (with bootloader).Code:#include<p18f4550.h> // Always include the header file#define REMAPPED_RESET_VECTOR_ADDRESS 0x1000#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x1008#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x1018void InterruptHandlerHigh(void);void InterruptHandlerLow(void);/***************Bootloader************************************/#pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESSextern void _startup (void);void _reset (void){_asm goto _startup _endasm}#pragma code//**************************************************************// V E C T O R R E M A P P I N G *****************************//**************************************************************#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESSvoid Remapped_High_ISR(void) {_asm goto InterruptHandlerHigh _endasm}#pragma code#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESSvoid Remapped_Low_ISR(void) {_asm goto InterruptHandlerLow _endasm}#pragma code/**************Interrupts*************************************/#pragma interrupt InterruptHandlerHighvoid InterruptHandlerHigh(void) {if ( INTCONbits.INT0IF == 1){INTCONbits.INT0IF=0;if(PORTBbits.RB7 == 0)//test interrupt{PORTBbits.RB7 = 1;}else PORTBbits.RB7 = 0;}}#pragma code/***************************LOW ISR *******************/#pragma interruptlow InterruptHandlerLowvoid InterruptHandlerLow(void) {}void INT0INIT(void){TRISBbits.RB0=1;RCONbits.IPEN = 1;//enable priority levels on interruptsINTCONbits.GIEH = 1;//enable all high-priority interrupts When IPEN = 1:INTCONbits.GIEL = 1;//enable all low-priority interrupts When IPEN = 1:INTCONbits.INT0IE = 1;//enable INT0 external interruptINTCON2bits.RBPU = 0; // All PORTB pull-ups are disabledINTCON2bits.INTEDG0 = 1;// Interrupt on falling edgeINTCONbits.INT0IF=0; // reset INT0 external interrupt flag}/**************Prog principal*********************************/void main(void){TRISB=0b00000001;PORTB=0b00000000;PORTBbits.RB7=1;INT0INIT();while(1){if(PORTBbits.RB0 == 0)//test button press{PORTBbits.RB2 = 0;}else PORTBbits.RB2 = 1;}}