Después de ejecutar el bloque de interrupción, la ejecución se detiene y nunca regresa de la rutina de interrupción al bloque while(1) en el método principal.
Para comprobar la funcionalidad, he escrito el siguiente código para probar la interrupción.
/*
* 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);
}
}
El mensaje que veo en el LED es "Terminar método ISR" y nunca llega al infinito después de ejecutar el ISR.
Revisé las instrucciones RETFIE, pero estoy usando C y no lenguaje ensamblador. He usado la declaración normal de "retorno" de C al final de la rutina de interrupción, pero parece que la ejecución se atasca allí.
¿Qué estoy haciendo mal? ¿Alguna corrección a este código, por favor?
Al escribir una ISR en C, nunca debe usar return
a menos que sepa lo que está haciendo. La mayoría de las MCU mantienen instrucciones diferentes para "regresar de la subrutina" y "regresar de la interrupción".
Si su compilador se traduce return
para regresar de la subrutina, obtendrá exactamente el tipo de error que describe. ¿Por qué escribe return
en primer lugar? Es una void
función, el retorno no tiene sentido.
Dado que se trata de un PIC, otra posibilidad probable es que su programa haya superado la profundidad de la pila o simplemente se haya quedado sin memoria de pila. Si su pila está dañada, el contador del programa saltará al medio de la nada al regresar de la interrupción. Observe el puntero de la pila mientras ejecuta un solo paso con un depurador. ¿Está el SP dentro de la pila asignada en todo momento?
return
a menudo se usa en void
funciones para salir antes o simplemente para hacer que el código sea más legible.
mitu raj
Vinay Sathyanarayana
mitu raj
Vinay Sathyanarayana
Vinay Sathyanarayana
Vinay Sathyanarayana
mitu raj
Vinay Sathyanarayana
Vinay Sathyanarayana
Miguel
Vinay Sathyanarayana
Roktá
mitu raj
Vinay Sathyanarayana
Vinay Sathyanarayana
Vinay Sathyanarayana