Author Topic: Pic18F4550 external interrupt problem  (Read 5383 times)

0 Members and 1 Guest are viewing this topic.

Offline Bibiricat

  • Contributor
  • Posts: 43
Pic18F4550 external interrupt problem
« on: May 20, 2013, 04:29:55 pm »
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: [Select]
#include <p18f4550.h>
#include <delay.h>


#pragma code REMAPPED_RESET_VECTOR=0x1000 // Car on utilise le Bootloader HID de Microchip
extern void _startup (void);
void _reset (void)
{
   _asm goto _startup _endasm
}
#pragma code

//#pragma config XINST = OFF
//pragma config WDT = OFF //Disable watchdog timer

void InterruptServiceHigh(void);
void InterruptServiceLow(void);
/***************************************************************/
// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x1008
void InterruptVectorHigh(void)
{
_asm goto InterruptServiceHigh _endasm
}
#pragma code
/**************************************************************/
// Low priority interrupt vector

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

/**************************************************************/
// Interrupt Service Routine
// Interrupt pragma for high priority


#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh(void)
{
if(INTCONbits.INT0IF)
{
PORTBbits.RB7=0;
Delay10TCYx (120);
PORTBbits.RB7=1;
}   
INTCONbits.INT0IF=0; // reset INT0 external interrupt flag
INTCONbits.RBIF = 0;
}
#pragma code
/*************************************************************/
#pragma interrupt InterruptServiceLow
void InterruptServiceLow(void){
}
#pragma code

/*************************************************************/


void main(void)
{   

/************* Configure Ports ***************************************/


    TRISB=0b00000001; // Set portb bit zero & one as input
    PORTB = 0;

PORTBbits.RB7=1;

/***** Configure external interrupt INT0 *****/
INTCONbits.INT0IF = 1; // SET INT0 Flag
INTCONbits.INT0IE = 1; // Enables the INT0 external interrupt
INTCON2bits.INTEDG0 = 1; // Interrupt on rising edge

INTCON2bits.RBPU = 1; // All PORTB pull-ups are disabled

INTCONbits.RBIE = 1; //1= Enables the RB port change interrupt
INTCONbits.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 edge
INTCON2bits.RBIP = 1; //RBIP: RB Port Change Interrupt Priority bit 1 = High priority

RCONbits.IPEN = 1; // Enable priority levels on interrupts
INTCONbits.GIE = 1; // Enables all high priority interrupts
INTCONbits.PEIE = 1; // Enables all low priority interrupts

INTCONbits.INT0IF=0; // reset INT0 external interrupt flag


   
while(1)
{
if(PORTBbits.RB0 == 0) //test button press
{
PORTBbits.RB2 = 1;
}
else PORTBbits.RB2 = 0;



     }
}





« Last Edit: May 20, 2013, 04:32:23 pm by Bibiricat »
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2011
  • Country: au
Re: Pic18F4550 external interrupt problem
« Reply #1 on: May 20, 2013, 04:50:06 pm »
Just a quick look
Use LATBbits  instead of PORTBbits throughout you code. If it works then search the data sheet for Read Modify Write to find out why
 

Offline Bibiricat

  • Contributor
  • Posts: 43
Re: Pic18F4550 external interrupt problem
« Reply #2 on: May 20, 2013, 05:00:41 pm »
It did work with PORTB . I had some problems with LATB , so i only use PORTB.
 

Offline afho

  • Contributor
  • Posts: 15
Re: Pic18F4550 external interrupt problem
« Reply #3 on: May 20, 2013, 05:16:00 pm »
Hi,

some parts of your code confuses me  ;)

Fist 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:

Quote
Four of the PORTB pins (RB7:RB4) have an interrupton-change feature. Only pins configured as inputs can cause this interrupt to occur. Any RB7:RB4 pin configured as an output is excluded from the interrupton-change comparison. The pins are compared with the old value latched on the last read of PORTB. The “mismatch” outputs of RB7:RB4 are ORed together to generate the RB Port Change Interrupt with Flag bit, RBIF (INTCON<0>).

The interrupt-on-change can be used to wake the device from Sleep. The user, in the Interrupt Service Routine, can clear the interrupt in the following manner:
a) Any read or write of PORTB (except with the MOVFF (ANY), PORTB instruction). This will end the mismatch condition.
b) Wait one TCY delay (for example, execute one NOP instruction).
c) Clear flag bit, RBIF

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

 

Offline Bibiricat

  • Contributor
  • Posts: 43
Re: Pic18F4550 external interrupt problem
« Reply #4 on: May 20, 2013, 05:26:56 pm »
I was frustrated and i configured everything .
I just want INT0 to work.
 

Offline Bibiricat

  • Contributor
  • Posts: 43
Managed to get it working
« Reply #5 on: May 30, 2013, 10:50:00 am »
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: [Select]
#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    0x1018

void InterruptHandlerHigh(void);
void InterruptHandlerLow(void);

/***************Bootloader************************************/
#pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
extern 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_ADDRESS

void Remapped_High_ISR(void) {
    _asm goto InterruptHandlerHigh _endasm
}
#pragma code
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS

void Remapped_Low_ISR(void) {
    _asm goto InterruptHandlerLow _endasm
}
#pragma code
/**************Interrupts*************************************/
#pragma interrupt InterruptHandlerHigh

void 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 InterruptHandlerLow

void InterruptHandlerLow(void) {
}

void INT0INIT(void)
{
TRISBbits.RB0=1;
RCONbits.IPEN = 1; //enable priority levels on interrupts
INTCONbits.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 interrupt
INTCON2bits.RBPU = 0; // All PORTB pull-ups are disabled
INTCON2bits.INTEDG0 = 1;// Interrupt on falling edge
INTCONbits.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;

}
}


 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf