News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Issue with scope of variables declared in ISR

Started by kcsl, Feb 10, 2025, 11:48 AM

Previous topic - Next topic

kcsl

This compiles without error, but I don't think it should as the variable rdFrameId is declared in the ISR so shouldn't be available elsewhere.




    Device 18F27Q83             
    Declare Xtal 40         


    On_Hardware_Interrupt GoTo ISR_Handler   
   

MainLoop:
    rdFrameId = 1   
    Stop
   

ISR_Handler:
    Context Save 
   
    Dim rdFrameId                       As Dword

    Context Restore
   


Using 4.0.5.4


Regards,
Joe
There's no room for optimism in software or hardware engineering.

John Lawton

I don't see a problem. DIM is not an executable statement, it just isn't usual/good coding practice to use a DIM statement in an ISR.

AIUI, the compiler will scan your code and assign memory for the variable as if it was in the main code.

In the case of Procedures, the compiler generates variables with a coded name so they aren't the same as any variables with apparently the same name in the main code.

John


kcsl

I've always treated the ISR as a form of procedure that the program executes on an interrupt.
In my mind, this was reinforced by having a context save/restore at the start/end of the code block and the way the manual says "When an interrupt occurs, it will immediately leave the main program and jump to the interrupt handling subroutine".

If that's not the case, and in fact it's just a label the code branches to, and on reflection it doesn't use the brackets to denote a procedure, then you're right and I stand corrected. And now I'm wondering where else I've done this and if I've left a ticking bug somewhere.

Regards,
Joe
There's no room for optimism in software or hardware engineering.

John Lawton

Hi Joe,

as I understand it, the compiler parses the source code and assigns variables whenever and wherever it sees a DIM statement. It then processes the remaining code.

So a variable isn't being created as part of the ISR code, even though you may have written it like that, it is created earlier in the process.

John


top204

#4
The ISR handler is just a standard label that the interrupt vector jumps too, and it is not a procedure!

So any variables created at that point are just normal global types. However, it is always better to create the variables before they are used in a code listing.