3
\$\begingroup\$

After executing the interrupt block, the execution halts and never returns out of the interrupt routine to the while(1) block in the main method.

To check the functionality, I've written the below code to test the interrupt.

 /*
 * File:   stepper.c
 * Author: vsathyan
 *
 * Created on August 30, 2019, 11:04 AM
 */

// 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 = OFF      // Brown-out reset enable bits (Brown-out reset disabled)
#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 = OFF     // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not 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 = not_available// Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

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

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

#define _XTAL_FREQ 4000000

//LCD DISPLAY INTERFACE - PORTD & PORTE
#define RS RD2
#define EN RD3
#define D4 RC4
#define D5 RC3
#define D6 RD0
#define D7 RD1

#define TRIS_RS TRISD2
#define TRIS_EN TRISD3
#define TRIS_D4 TRISC4
#define TRIS_D5 TRISC3
#define TRIS_D6 TRISD0
#define TRIS_D7 TRISD1

#define BUZZER RC1
#define TRIS_BUZZER TRISC1

#define OUTPUT 0
#define INPUT 1

#define PRESSED 1
#define RELEASED 0
#define _PRESSED 0
#define _RELEASED 1

#define SET 1
#define CLEAR 0

#define HIGH 1
#define LOW 0

#define UP 1
#define DOWN 0

//INPUT CONFIGURATION
#define EMERGENCY_BUTTON RB0
#define MENU_OPTION RB1
#define VALUE_UP RB2
#define VALUE_DOWN RB3
#define SET_VALUE RB4
#define HOME_LIMIT_SWITCH RB5
#define END_LIMIT_SWITCH RB6

#define TRIS_EMERGENCY_BUTTON TRISB0
#define TRIS_MENU_OPTION TRISB1
#define TRIS_VALUE_UP TRISB2
#define TRIS_VALUE_DOWN TRISB3
#define TRIS_SET_VALUE TRISB4
#define TRIS_HOME_LIMIT_SWITCH TRISB5
#define TRIS_END_LIMIT_SWITCH TRISB6

//OUTPUT CONFIGURATION
#define PULSE RA0
#define DIRECTION RA1
#define ENABLE RA2

#define TRIS_PULSE TRISA0
#define TRIS_DIRECTION TRISA1
#define TRIS_ENABLE TRISA2

//function declarations
void showMessage(char *message1, char *message2);
void initializeVariables();

//program variables
char fValue[16];
int option;

#include <xc.h>
#include <stdio.h>
#include <string.h>
#include "stepper.h"
#include "lcd162.h"


//function declarations
void showMessage(char *message1, char *message2);
void showMessageWithDelay(char *message1, char *message2, int delay);
void initialize();
void initializeVariables();
void setStringValue(char stringValue, float floatValue);
void delay_ms(int delay);
void delay_us(int delay);

void interrupt isr()
{
    if(IOCIF) //if interrupt on change flag is set
    {
        if(IOCBF1) //if interrupt on port RB1 is set
        {
            showMessageWithDelay(" RB1 Interrupt  ", "                ", 3000); //display a message in LCD for 3 seconds
            IOCBF1 = 0;
        }
        IOCIF = 0;
    }
    showMessageWithDelay(" End ISR Method ", "                ", 3000); //display a message in LCD for 3 seconds
    return; //return to main program execution
}

void main(void) 
{
    initialize(); //initialize input pins, output pins, variables
    Lcd_Start(); //initialize 16x2 LCD
    Lcd_Clear(); //clear LCD;
    showMessageWithDelay("Automatic Speed ", "Controller V0.1 ", 3000); //display start message
    int i = 0;
    while(1)
    {
        i = i + 1;
        setStringValue(fValue, i);
        showMessageWithDelay("  Inside While  ", fValue, 1000); //message displayed when inside infinite while loop
    }
}
void showMessage(char *message1, char *message2)
{
    Lcd_Clear();
    Lcd_Set_Cursor(1,1);
    Lcd_Print_String(message1);
    Lcd_Set_Cursor(2,1);
    Lcd_Print_String(message2);
}
void showMessageWithDelay(char *message1, char *message2, int delay)
{
    Lcd_Clear();
    Lcd_Set_Cursor(1,1);
    Lcd_Print_String(message1);
    Lcd_Set_Cursor(2,1);
    Lcd_Print_String(message2);
    delay_ms(delay);
}
void initialize()
{
    ANSELA = 0x00;
    ANSELB = 0x00; //disable analog input on port B
    ANSELC = 0x00;
    ANSELD = 0x00;
    ANSE0 = 0;
    ANSE1 = 0;
    ANSE2 = 0;

    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

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

    TRIS_RS = OUTPUT;
    TRIS_EN = OUTPUT;
    TRIS_D4 = OUTPUT;
    TRIS_D5 = OUTPUT;
    TRIS_D6 = OUTPUT;
    TRIS_D7 = OUTPUT;

    TRIS_BUZZER = OUTPUT;

    TRIS_PULSE = OUTPUT;
    TRIS_DIRECTION = OUTPUT;
    TRIS_ENABLE = OUTPUT;

    PULSE = 0;
    ENABLE = 0;
    DIRECTION = 0;

    IOCIE = 1; //enable INTERRUPT ON CHANGE 
    PEIE = 1; //enable PERIPHERAL INTERRUPT
    GIE = 1; //enable GLOBAL INTERRUPT
    initializeVariables();

}
void initializeVariables()
{
    option = 0;
}
void setStringValue(char stringValue, float floatValue)
{
    sprintf(stringValue, "%.3f", floatValue);
}
void delay_ms(int delay)
{
    for(int i = 0; i < delay; i++)
    {
        __delay_ms(1);
    }
}
void delay_us(int delay)
{
    for(int i = 0; i < delay; i++)
    {
        __delay_us(1);
    }
}

The message i see on LED is " End ISR Method " and it never gets into the infinite while after executing the ISR.

I checked for RETFIE instruction, but i'm using C and not assembly language. I have used the normal C "return" statement at the end of the interrupt routine, but seems the execution gets stuck there.

What am i doing wrong? Any corrections to this code please?

\$\endgroup\$
  • 1
    \$\begingroup\$ the routine isr is attached with what interrupt source ? \$\endgroup\$ – Mitu Raj Aug 30 at 7:37
  • \$\begingroup\$ I'm not sure what you are asking. Should i attach an interrupt some where? I used to do the same above for PIC16F887, and it would work. Not sure what i'm missing here. Could you please help explain more on "attaching an interrupt to source" ? thanks. \$\endgroup\$ – Vinay Sathyanarayana Aug 30 at 7:41
  • \$\begingroup\$ well who causes the interrupt..thats the question....which peripheral and which is the flag associated with it \$\endgroup\$ – Mitu Raj Aug 30 at 7:42
  • \$\begingroup\$ I have a push button switch (active low) connected to RB1. When i press this button, it gets into the isr method. \$\endgroup\$ – Vinay Sathyanarayana Aug 30 at 7:43
  • \$\begingroup\$ I'm using interrupt on change on port B, so the flags are IOCIF (interrupt on change flag), and IOCBF0, IOCBF1 and so on till IOCBF7 for port B individual pins. In my case, i'm using RB1, so the flag responsible is IOCBF1.. \$\endgroup\$ – Vinay Sathyanarayana Aug 30 at 7:45
3
\$\begingroup\$

When writing an ISR in C, you should never use return unless you know what you are doing. Most MCUs keep different instructions for "return from subroutine" and "return from interrupt".

If your compiler translates return to return from subroutine, you would get exactly the kind of bug you describe. Why do you write return in the first place - it's a void function, return doesn't make sense.

Since this is a PIC, another likely possibility is that your program has blown the stack depth, or is just out of stack memory. If your stack is corrupted, the program counter will jump into the middle of nowhere upon returning from the interrupt. Observe the stack pointer while single-stepping with a debugger. Is the SP inside the allocated stack at all times?

\$\endgroup\$
  • 2
    \$\begingroup\$ I don't agree with your last statement return is often used in void functions to exit earlier or just to make the code more readable. \$\endgroup\$ – Prateek Dhanuka Aug 30 at 10:17
  • \$\begingroup\$ @PrateekDhanuka Posting return at the end of a void function only serves to make the code less readable. The point here is that you can't use it anywhere, if the compiler translates return to "return from subroutine". \$\endgroup\$ – Lundin Aug 30 at 10:55
  • 1
    \$\begingroup\$ @Prateek You are right in case of c applications you write for linux or windows. But not here. \$\endgroup\$ – Mitu Raj Aug 30 at 11:20
  • \$\begingroup\$ Yeah, I understand that the return is what caused problems here. I was just referring to the last line. I think 'return doesn't make sense' isn't accurate. \$\endgroup\$ – Prateek Dhanuka Aug 30 at 11:24
  • 2
    \$\begingroup\$ @VinaySathyanarayana You can answer your own question here, if that is the exact solution to your question. \$\endgroup\$ – Mitu Raj Aug 30 at 18:06

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.