Public Variable not visible in Module Routine

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Public Variable not visible in Module Routine

Post by TonyR » Mon Apr 04, 2011 11:43 pm

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


Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Apr 05, 2011 1:41 pm

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.

User avatar
Senacharim
Posts: 139
Joined: Tue Aug 10, 2010 5:19 pm
Location: Ventura, CA

Post by Senacharim » Tue Apr 05, 2011 2:18 pm

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.
Surviving Member
Bermuda Triangle Battalion
from 2026 to 1992

Voted "Most likely to time travel"--Class of 2024.

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Post by TonyR » Tue Apr 05, 2011 8:29 pm

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.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Tue Apr 05, 2011 8:43 pm

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

Post Reply