INTERRUPT PIC 18F4620 MIKROBASIC PRO

Post your requests and ideas on the future development of mikroBasic PRO for PIC.
Post Reply
Author
Message
Frank73
Posts: 33
Joined: 09 Jan 2012 15:56

INTERRUPT PIC 18F4620 MIKROBASIC PRO

#1 Post by Frank73 » 31 Aug 2018 18:44

Hi good day!
I am using the interrupts of the PIC 18F4620, to be more precise configure the interruption 0 Per Pin.
The question is that the instruction does not allow me to print a text on the screen because it generates error and does not allow compiling. what is this about?
Attachments
INTERRUPT.jpg
INTERRUPT.jpg (459.98 KiB) Viewed 3528 times

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: INTERRUPT PIC 18F4620 MIKROBASIC PRO

#2 Post by janni » 31 Aug 2018 23:00

The error message is basically self-explanatory - reentrancy means calling a routine from a thread while another thread is executing it. When program flow is broken and another thread starts (like interrupt) it may not call the same fragment of code (same routine) if the state of this fragment and all resources it used are not somehow saved to be restored later - otherwise results of return to the previous thread would be unpredictable. As mB for PIC, due to small resources of 8bit PIC processors, does not save state of local variables or parameters of a routine, only routines without parameters or local variables may be called from both main code and ISR. (And compiler doesn't recognize cases when a routine, though called from two threads, is in fact never called simultaneously - in such case one may use the new directive masked as procedure, ReentrancyCheckOff, that was introduced in v 7.20.)

BTW, there are other reasons to avoid calls from ISR - unless one chooses to perform all program tasks in ISR (in such case main program loop usually does virtually nothing), interrupts are used only for time-critical tasks and therefore their servicing is kept short (meaning using little execution time) or may miss some events that cause interrupts. Happily enough, use of hardware stack is not such an issue in PIC18 processors and one would need a really complex program to cause stack overflow when calling routines from both ISR and main code. In PIC16 processors, though, it's better to be careful about it.

Frank73
Posts: 33
Joined: 09 Jan 2012 15:56

Re: INTERRUPT PIC 18F4620 MIKROBASIC PRO

#3 Post by Frank73 » 02 Sep 2018 04:09

Hello Janni!

I appreciate Your Answer but I can not agree with you.
I took the trouble to make this same code in mikroc, and to my surprise, it worked perfectly.
here we see clearly an error of the compiler that must be taken into account and corrected.
if it works in mikroc you should also do the same in Mikrobasic
Attachments
Mikroc.png
Mikroc.png (156.35 KiB) Viewed 3377 times

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: INTERRUPT PIC 18F4620 MIKROBASIC PRO

#4 Post by janni » 02 Sep 2018 13:08

Hello Frank,

Reentrancy rules are the same in mC as in mB - here's what Help states in mC:
Functions reentrancy is allowed if the function has no parameters and local variables, or if the local variables are placed in the Rx space.
It's not visible in the picture you attached whether you use the GLCD_Write_Text function both in ISR and main code, like it took place in your mB example, but if you do, there will be reentrancy problem.

Frank73
Posts: 33
Joined: 09 Jan 2012 15:56

Re: INTERRUPT PIC 18F4620 MIKROBASIC PRO

#5 Post by Frank73 » 08 Sep 2018 03:36

Hi good day!

I would like to find an answer to the problem that I have exposed
But a response with content and not a confused and disoriented response!

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: INTERRUPT PIC 18F4620 MIKROBASIC PRO

#6 Post by janni » 08 Sep 2018 17:11

And good day to you, too :)
Frank73 wrote:I would like to find an answer to the problem that I have exposed
You have not exposed any new problem - it's well known limitation of the compiler.
But a response with content and not a confused and disoriented response!
Thanks for the compliments :wink: . You may naturally choose to not believe me, but the truth is that reentrancy is treated the same way in mC as in mB for PIC. If by "content" you mean a picture, like the ones you posted, here you are
reentrancy_in_mC.png
reentrancy_in_mC.png (35.14 KiB) Viewed 3363 times
It's the same mC compiler example you used with added calls to GLCD library, identical to those in your original mB code. If you want an explanation why your version of code in C passed compilation then post the code, not a picture.

Frank73
Posts: 33
Joined: 09 Jan 2012 15:56

Re: INTERRUPT PIC 18F4620 MIKROBASIC PRO

#7 Post by Frank73 » 17 Sep 2018 17:52

Hello Janni!

I apologize if I somehow offended you, my purpose is not to enter into controversies, in some way. I just want to be clear about these errors, if they are due to my lack of knowledge of something I have overlooked, or if on the contrary it is a compiler error.

'*******************************************
'* TIMER Y INTERRUPCION *
'* AGOSTO 25 DE 2018 *
'* *
'* *
'* *
'*******************************************
program TIMER_F

SYMBOL LED = PORTC.7
SYMBOL SW1 = PORTB.0

DIM COUNT AS WORD

DIM I2C1_Scl as sbit at RC3_bit
I2C1_Sda as sbit at RC4_bit
I2C1_Scl_Direction as sbit at TRISC3_bit
I2C1_Sda_Direction as sbit at TRISC4_bit

DIM GLCD_DataPort as byte at PORTD

DIM GLCD_CS1 as sbit at RB7_bit
GLCD_CS2 as sbit at RB6_bit
GLCD_RS as sbit at RB5_bit
GLCD_RW as sbit at RB4_bit
GLCD_EN as sbit at RB3_bit
GLCD_RST as sbit at RB2_bit

DIM GLCD_CS1_Direction as sbit at TRISB7_bit
GLCD_CS2_Direction as sbit at TRISB6_bit
GLCD_RS_Direction as sbit at TRISB5_bit
GLCD_RW_Direction as sbit at TRISB4_bit
GLCD_EN_Direction as sbit at TRISB3_bit
GLCD_RST_Direction as sbit at TRISB2_bit

SUB PROCEDURE INICIO

INTCON.GIE = 1
INTCON.INT0IE = 1
INTCON2.INTEDG0 = 1
ADCON0 = 0
ADCON1 = %1111
TRISA = %011111
PORTA = %01111
TRISB = %00000001
PORTB = %00000001
TRISC = %00001111
PORTC = %00001111
TRISD = %00000000
PORTD = %00000000
TRISE = %111
PORTE = %111
DELAY_MS(50)
Glcd_Init()
COUNT = 0
END SUB



SUB PROCEDURE INTERRUPT
IF (INTCON.INT0IF = 1) THEN 'PREGUNTA SI HAY CAMBIO DE ESTADO EL PIN RB.0 = INT0
Glcd_Write_Text("FRANK ROMERO", 32, 1, 1)
LED = 1
DELAY_MS(500)
LED = 0
INTCON.INT0IF = 0
END IF
END SUB

main:
INICIO
DELAY_MS(100)
Glcd_Fill(0)
Glcd_Write_Text("BOGOTA DC", 32, 0, 1)

end.



sbit GLCD_CS2 at RB1_bit;
sbit GLCD_RS at RB2_bit;
sbit GLCD_RW at RB3_bit;
sbit GLCD_EN at RB4_bit;
sbit GLCD_RST at RB5_bit;

sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction at TRISB2_bit;
sbit GLCD_RW_Direction at TRISB3_bit;
sbit GLCD_EN_Direction at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;

unsigned cnt;

void interrupt() {
if (TMR0IF_bit) {
Glcd_Write_Text("Hello world!", 10, 2, 1);
TMR0IF_bit = 0; // clear TMR0IF
}
}

void main() {
ANSELB = 0; // Configure AN pins as digital
TRISB = 0; // PORTB is output
LATB = 0xFF; // Initialize PORTB

TMR0L = 0; // Timer0 initial value
T0CON = 0xC7; // Set TMR0 to 8bit mode and prescaler to 256
GIE_bit = 1; // Enable global interrupt
TMR0IE_bit = 1; // Enable Timer0 interrupt
cnt = 0; // Initialize cnt

do {
if (cnt >= 122) {
LATB = ~PORTB; // Toggle PORTB LEDs
cnt = 0; // Reset cnt
}
} while(1);
}

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: INTERRUPT PIC 18F4620 MIKROBASIC PRO

#8 Post by janni » 17 Sep 2018 22:51

Well, the explanation why your C example compiles is simple - Glcd_Write_Text is called only in ISR so there is no reentrancy.
In your BASIC code this function is called from both ISR and main code and thus reentrancy error message appears. As I've written before, it's well known limitation of the compiler. Due to limited resources of 8-bit PIC processors it's impractical to save full state of a routine (or chain of routines) before jump to ISR and thus full reentrancy cannot be allowed.

Post Reply

Return to “mikroBasic PRO for PIC Wish List”