[SOLVED] PIC18F4550 Interrupt on Change issue

Status
Not open for further replies.

georgiod9

Newbie level 4
Joined
Dec 21, 2015
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
83
Hello everyone,

Im trying to light up a LED using interrupt on change with 18F4550. In my schematic, I have the trigger on RB4. Whenever I press that button, the interrupt is supposed to set my LED to 1. This doesn't happen on PORTB4. I tried it on Ports B5,B6, and B7 they all worked, but the problem remains on PORTB4.

The code is as follows:
Code:
sbit LED at RC0_bit;
void interrupt();

void main() 
{
    INTCON = 0x88;
    SPPCON = 0x00;          //I tried disabling the SPP function
    SPPCFG.CSEN = 0;
    
    TRISB = 0xFF;
 
    PORTB = 0;
    TRISC = 0;
    PORTC = 0;
    
    while(1)
    {

    }

}

void interrupt()
{

        LED = 1;
        INTCON.RBIF = 0;
}
Any help is appreciated. Thanks!

- - - Updated - - -

Update:
Issue was solved by setting the bits PCFG3, PCFG2, PCFG1 and PCFG0 on ADCON1 as digital I/O ports instead of the default which is analog. Setting the listed bits to 1 will allow us to use RB0, RB1, RB3 and RB4 as digital I/O as per the datasheet.
Therefore, ALWAYS read the datasheet thoroughly before beginning your application.
 

Please read Section 10.2 of the MCUs data sheet, especially the paragraph just under the "Note": you still have a bit to go in the ISR code. For a start, you need to read the PORT register to reset the 'change' detection circuit.
It is not important now as you only have one interrupt source, but for these devices that have only one or two interrupt vectors (the PIC18F4550 has two; high and low if you want to use them both, or the ne as you have your device configured) it is good to get in to the habit of always checking the trigger. The typical code in the ISR is something like:
Code:
void interrupt()  // Or whatever for the priority interrupt structure
{
    if( INTCONbits.RBIF && INTCONbits.RBIE)
    {
        // Put your code here
        INTCONbits.RBIF = 0;  // Reset the flag
    }
    // Put code here to check for other interrupt sources
}
(I generally program wit the free XC8/XC16 compilers so I tend to use their syntax - change as required).
The reason you check for both the IF and IE bits is that the IF bit can still be set by the hardware but the ISR will only be triggered if the IE bit is also set. If you have several interrupt sources then it is possible for you to see an IF bit set for a device that does not have the IE bit set. Therefore you need to check for both.
(B the way, if you want to clear the IF bit at the start of your code rather than at the end as I've done then that is OK as well - there have been wars fought over less but some people do hold string opinions on this.)
While I totally agree with your conclusion, the analog/digital thing is common to all Microchip devices and is one of the most common sources of problems for people starting out.
Of course there are good reasons why analog is the default but, as with "never play with the DEBUG config setting", "connect ALL Vss and Vdd pins and use bypass capacitors" and "always check your CONFIG settings, especially the JTAG one" and similar common problems, some of them have to be learned the hard way.
Susan
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…