Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

dim and include declaration in a include file

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Fri Feb 09, 2024 12:47 pm    Post subject: dim and include declaration in a include file Reply with quote

hi
I have a lot of global variables and include files and would like to call them in a new include file, I searched the forum but couldn't find it
I would like do
Code:
' include files  ********************************************************************
$include "declaredim.inc"
Call Dimdeclare()


where all include file and all declare var are in the sub dimdeclare()

but that dont work

is it impossible ?

jp :cry:

(BASCOM-AVR version : 2.0.8.6 )

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5922
Location: Holland

blank.gif
PostPosted: Fri Feb 09, 2024 3:17 pm    Post subject: Reply with quote

the variables can not be dynamic declared. they must be known at compile time.
this will not work:
Code:
if  a=1 then
  dim b as byte
else
  dim b as word
end if
 

you can of course #IF with conditional compilation. it is just to make the point.

the way is :
Code:
$include "myvars.inc"
..other code
print b

 
and in the myvars.inc you have :
Code:
dim b as byte
dim L as long


i do not see the benefit from an additional call to some sub?

_________________
Mark
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sat Feb 10, 2024 12:57 pm    Post subject: Re: dim and include declaration in a include file Reply with quote

Duval JP wrote:
Code:
' include files  ********************************************************************
$include "declaredim.inc"
Call Dimdeclare()

where all include file and all declare var are in the sub dimdeclare()
but that dont work
is it impossible ?

Yes and No.

What you need to know is, that the compiler notices a variable at the very moment it is dim'd.
Code:
Dim a, b, c As Byte
  a = b + c

works, while
Code:
  a = b + c
Dim a, b, c As Byte

does not.
To get no errors, dimensioning of globals must be at top of code, so any following code which uses such a global, sees it already dim'd.

About $include:

Imagine your bas main-code been printed out on endless paper, the same with the inc-dimensioning.
At the very position of the $include "...inc" directive the main code's paper tape is cut open and the inc is inserted and glued together again.
In the end the compiler sees one long paper tape, where he has to crunch on and to finally spit out hex opcode.

incld.inc
Code:
Dim a,b,c as Byte

dimdec.bas
Code:
$Regfile="m328pdef.dat"
$Crystal=16000000
$hwstack=40
$swstack=16
$framesize=32

$include "incld.inc"

a = b + c

works, the resulting 'tape' looks like:

result
Code:
$Regfile="m328pdef.dat"
$Crystal=16000000
$hwstack=40
$swstack=16
$framesize=32

'$include "incld.inc"
<--- Dim a,b,c as Byte

a = b + c

So yes, to bundle dims in one inc works, but selectively dim, for example by two subs with different dim-sets doesn't.
Which already becomes clear by the fact you can dimension a variable within a sub with Dim (not by Local) and at the moment the compiler sees it, it is known as a global var.

Dimensioning it a second time in a second sub will give you an 'variable already dimensioned' error.
As you see the idea of dimensioning by calling a sub is nonsense.
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Sun Feb 11, 2024 9:58 am    Post subject: Reply with quote

thank you both Mark and MWS

my problem is that in a large program I start the effective code at line 250, I would like to reduce the first 230 lines, hide them .
I'm looking for something like folding code

but It is not a big pb
JP Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sun Feb 11, 2024 10:03 am    Post subject: Reply with quote

You can simply fold the code by inserting it into valid condition.

Code:
Const Region = 1

#If Region                   ' <--- this can easly be folded
Line 1
'bunch of code here
.
.
.
Line 230
#endif
 


You can even name it as "Const Variables = 1" and then #If Variables -> #endif.

_________________
Check B-Flash -my MCS bootloader app for Android
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 11, 2024 10:12 am    Post subject: Reply with quote

Duval JP wrote:
for something like folding code

Include them this way and it should fold:
Code:
#IF 1 = 1
' 250 lines
#ENDIF

If this doesn't fit you, use #include and put all 250 lines there, it does exactly what you want. The inc can be opened in a separate window, thus it can be looked up quickly.

Beside, this thread again shows the value of a basic rule every TO should follow:

Describe exactly what the problem & goal is. Do not ask for solutions based on your own, already botched up ideas.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 11, 2024 5:59 pm    Post subject: Reply with quote

There's one left:
Create a bookmark at line 250 with CTRL+K+[number]
Jump to bookmark with CTRL+Q+[number]

[number] 1 - 8 from the alphanumeric keyboard, num-block doesn't work.
Back to top
View user's profile
sielcon

Bascom Member



Joined: 16 Mar 2006
Posts: 74
Location: Argentina

argentina.gif
PostPosted: Wed Feb 14, 2024 1:55 am    Post subject: Reply with quote

Hello Duval JP, I understand your problem, in all my projects I use includes to clarify the code, in fact my main ends up being clear and concise, I'll leave you an example.

Code:

$regfile = "m328Pdef.dat"
$crystal = 16000000
$hwstack = 100
$swstack = 100
$framesize = 100
Config Clockdiv = 1
Config Submode = New
Debug On


'*******************************************************************************
$include "defini.inc"
$include "subs.inc"
$include "interrupt.inc"
 


This way you edit all the includes in separate files and it becomes much more bearable in long projects.

Kind regards
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum