1
\$\begingroup\$

I'm trying to trigger the interrupt service routine method in PIC16F18877 microcontroller, but the code never gets into the isr() method. Not sure what and where am i going wrong. Here is the code i've wrote.

    // PIC16F18877 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = HS     // External Oscillator mode selection bits (HS (crystal oscillator) above 4MHz; PFM set to high power)
#pragma config RSTOSC = EXT1X   // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF       // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


#define _XTAL_FREQ 4000000
#include <xc.h>

void interrupt isr(void)
{
    if(IOCIE == 1 && IOCIF == 1) //if interrupt on change is enabled, and IOC flag is set,
    {
        if(IOCBF1 == 1) //if interrupt occured on port bit RB1 and the RB1 interrupt flag is set,
        {
            for(int i = 0; i < 5; i++) //blink LED on RC1 five times
            {
                RC1 = 1;
            }
            IOCBF1 = 0; //clear RB1 interrupt flag
            IOCIF = 0; //clear interrupt on change flag
        }
    }
}

void main(void) 
{

    ANSELBbits.ANSB1 = 0; //disable analog input on RB1
    WPUB = 0xFF; //enable weak pull up resistors on all port B pins
    TRISB1 = 1; //RB1 is input pin
    IOCBP1 = 1; //interrupt on change positive trigger enabled on RB1;
    IOCBN1 = 1; //interrupt on change negative trigger enabled on RB1;
    IOCBF1 = 0; //clear RB1 interrupt flag
    IOCIE = 1; //enable INTERRUPT ON CHANGE 
    GIE = 1; //enable GLOBAL INTERRUPT

    TRISC1 = 0; //RC1 as output
    RC1 = 0; //set RC1 output to low

    while(1)
    {

    }
}

The above code never enters the isr() method. Any guidance is much appreciated. Thanks.

\$\endgroup\$
  • 2
    \$\begingroup\$ I don't think this is the issue, but you should not have long-running code in an ISR. Depending on how you are triggering it, you may be getting overlaps (if you have switch contact bounce). Having an ISR that runs for 2 seconds is way too long. \$\endgroup\$ – Ron Beyer Aug 22 at 17:23
  • \$\begingroup\$ Its not even going there!! I want to check if it enters the isr(). I can reduce the delay to 10 ms to check if the RC1 toggles..!!! Nothing happens :( \$\endgroup\$ – Vinay Sathyanarayana Aug 22 at 17:26
  • \$\begingroup\$ Can you show how you are triggering RB1? A diagram of how you have the pin wired would help understand how the trigger works. I'm guessing you have it jumpered over to RC1? Do you have a pull-up/pull-down on RB1 or RC1? \$\endgroup\$ – Ron Beyer Aug 22 at 17:38
  • \$\begingroup\$ Make sure RB1 is changing. For example, copy it into RC1 inside while in main and monitor RC1. \$\endgroup\$ – Oleg Mazurov Aug 22 at 17:38
  • \$\begingroup\$ Also I don't see where you set TRISB1 = 1; to set RB1 as an input. \$\endgroup\$ – Ron Beyer Aug 22 at 17:44
1
\$\begingroup\$

I'm able to hit the isr() with the below code. I'm not sure where i went wrong earlier, but the problem is now fixed, and i'm able to enter the respective interrupt block with button press on RB1, RB2, RB3 and RB4. Thank you all for your valuable inputs and time.

/*
 * File:   step_control.c
 * Author: vsathyan
 *
 * Created on August 22, 2019, 1:04 PM
 */


// PIC16F18877 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = HS     // External Oscillator mode selection bits (HS (crystal oscillator) above 4MHz; PFM set to high power)
#pragma config RSTOSC = EXT1X   // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF       // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


#define _XTAL_FREQ 4000000
#include <xc.h>

void interrupt isr(void)
{
    __delay_ms(100);
    if(RB1 == 0)
    {
        if(IOCIE == 1 && IOCIF == 1) //if interrupt on change is enabled, and IOC flag is set,
        {
            if(IOCBF1 == 1) //if interrupt occured on port bit RB1 and the RB1 interrupt flag is set,
            {
                for(int i = 0; i < 2; i++) //blink LED on RA1 five times
                {
                    RA1 = 1;
                    __delay_ms(200);
                    RA1 = 0;
                    __delay_ms(200);
                }
                IOCBF1 = 0; //clear RB1 interrupt flag
                IOCIF = 0; //clear interrupt on change flag
            }
        }
    }
    if(RB2 == 0)
    {
        if(IOCIE == 1 && IOCIF == 1) //if interrupt on change is enabled, and IOC flag is set,
        {
            if(IOCBF2 == 1) //if interrupt occured on port bit RB2 and the RB2 interrupt flag is set,
            {
                for(int i = 0; i < 2; i++) //blink LED on RA2 five times
                {
                    RA2 = 1;
                    __delay_ms(200);
                    RA2 = 0;
                    __delay_ms(200);
                }
                IOCBF2 = 0; //clear RB2 interrupt flag
                IOCIF = 0; //clear interrupt on change flag
            }
        }
    }
    if(RB3 == 0)
    {
        if(IOCIE == 1 && IOCIF == 1) //if interrupt on change is enabled, and IOC flag is set,
        {
            if(IOCBF3 == 1) //if interrupt occured on port bit RB3 and the RB3 interrupt flag is set,
            {
                for(int i = 0; i < 2; i++) //blink LED on RA3 five times
                {
                    RA3 = 1;
                    __delay_ms(200);
                    RA3 = 0;
                    __delay_ms(200);
                }
                IOCBF3 = 0; //clear RB3 interrupt flag
                IOCIF = 0; //clear interrupt on change flag
            }
        }
    }
    if(RB4 == 0)
    {
        if(IOCIE == 1 && IOCIF == 1) //if interrupt on change is enabled, and IOC flag is set,
        {
            if(IOCBF4 == 1) //if interrupt occured on port bit RB4 and the RB4 interrupt flag is set,
            {
                for(int i = 0; i < 2; i++) //blink LED on RA4 five times
                {
                    RA4 = 1;
                    __delay_ms(200);
                    RA4 = 0;
                    __delay_ms(200);
                }
                IOCBF4 = 0; //clear RB4 interrupt flag
                IOCIF = 0; //clear interrupt on change flag
            }
        }
    }
    else
    {
        if(IOCIF == 1) { IOCIF = 0; }
    }
}

void main(void) 
{
    ANSELB = 0x00; //disable analog input on RB1
    WPUB = 0xFF; //enable weak pull up resistors on all port B pins
    TRISB = 0xFF; //all port B pins are inputs
    IOCBP = 0xFF; //interrupt on change positive trigger enabled on port B;
    IOCBN = 0xFF; //interrupt on change negative trigger enabled on port B;
    IOCBF = 0x00; //clear RB1 interrupt flag
    IOCIE = 1; //enable INTERRUPT ON CHANGE 
    GIE = 1; //enable GLOBAL INTERRUPT

    TRISA = 0x00; //set PORTA as output port
    PORTA = 0x00; //set all PORTA bit values to 0

    while(1)
    {

    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.