Page 1 of 1

Public Variable not visible in Module Routine

Posted: Mon Apr 04, 2011 11:43 pm
by TonyR
I have an 18F87J50 program with three modules which compiles OK. I need a public variable called Hightime.

When I add the line "Public Dim Hightime As Word" in main, the program still compiles OK. As below.

When I try to use Hightime in module "TIMER3 ISR.bas" I get "Identifier not declared: Hightime". Is there a secret place where these public variables should be declared to be global?

Thanks anyone!!

Code: Select all

Device = 18F87J50                      ' Setup the device/clock information
Clock = 48

Config FOSC = EC

#option USART_BRGH = true       ' Use 16bit BRG so setbaudrate works
#option USART_BRG16 = true 

Include "USART-2.bas" 
Include "convert.bas"
Include "TIMER3 ISR.bas"

Dim LED As PORTE.1
Dim ADC0 As Word
Dim Freq As LongWord 
Public Dim Hightime As Word

This where I'm trying to use Hightime:

Code: Select all

Module TIMER3_Interrupt_Service_Routine

Const CCPR3_register_address=4018   
Public Dim CCPR3_register_word As Word Absolute CCPR3_register_address
Dim CCPR3_interrupt_flag As PIR3.0             
Dim Numberoftimes As Byte
                                                                            
Interrupt TIMER3_Interrupt()

    For Numberoftimes = 1 To Hightime
        High(PORTC.1)
    Next         
    Low(PORTC.1)
    
    CCPR3_interrupt_flag = 0                        'clear CCP3 interrupt flag
End Interrupt


Posted: Tue Apr 05, 2011 1:41 pm
by Jerry Messina
If you want HighTime to be visible to all three modules, a common solution is to create a "globals.bas" file and put the definition in there, and then 'include "globals.bas"' anywhere you need to.

If you're coming to SF from other flavors of BASIC, the visibility rules take a little getting used to. The easiest way to look at is to take a look at your "TIMER3 ISR.bas" module. Can YOU see a definition of HighTime anywhere (looking in any 'include' files as well)? If you can't, then the compiler can't either.

Posted: Tue Apr 05, 2011 2:18 pm
by Senacharim
On the other hand, if you've any practice coding in C the variable scope is exactly as it should be.

^_^

Follow Jerry's advice, it's the most straightforward path towards your stated goal in this instance.

Posted: Tue Apr 05, 2011 8:29 pm
by TonyR
Thanks Gentlemen, I got it working by moving the public dim statement into the Timer3 ISR module, I was expecting SF to act like VB. Will be aware from now on.

Posted: Tue Apr 05, 2011 8:43 pm
by octal
Hi TonyR,
there are no real "global" variables in Swordfish Basic.
The rule is that to let any module XXX "see" any other module YYY variables, there are two rules:
1- Module YYY need to declare the variables (const, func, subs, ...) as "PUBLIC"
2- module XXX must include module YYY


From these rules, it's easy to understand that rule2 cannot be fullfilled by the "main program"!!!
NO MODULE CAN INCLUDE THE MAIN PROGRAM.

the main program can include any module, the reverse is impossible, so the public vars of the main program (usually called "global" vars in other BASIC dialects) can **NOT** be seen by any other module.
So the only solution remaining to you is the solution proposed by Jerry, i.e. create a third module that will include the "common" (or globally) vars, declared as public, and include this module in all the others and in the main program.

Regards