Variable organization


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie.

    Well, I suppose that most people will without much thinking follow in general that structure too.

    But what I tried to express with my limited english was this:

    Say you have a long program with many variable, some of them common to most routines like the CounterA, CounterB etc. As these are used for counting purposes, they are present in many subroutines and For-Next loops.

    Lets suppose now that an interrupt occurs in the middle of a subroutine and a counter variable is necessary for the interrupt itself.

    We need now a new variable, exclusively for this iterrupt,used only once.

    If accidentally one uses the, say, CounterA variable,which is at this particular time used in a subroutine, sure the program will go to the La-La-Land!

    I feel a little Skroutz with the blind use of variables.

    Ioannis

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Duhhhh.... Yes, I use CounterA, CounterB... DataA, DataB in my subroutines, but INTERRUPTS by their very nature will INTERRUPT the flow of things, so you wouldn't use the same variables within those routines unless the interrupt is supposed to change the way the main program works - I would have thought that was obvious!

    In the same way, if you nest subroutines within subroutines for a particular task... you can't have the same variables in each of those subroutines as they'll mess with each other.

    It doesn't mean that every subroutine has to have it's own exclusive variables, but you do need to keep track of what is being used where - and to that end, when you list your subroutine, have a header which simply reminds you what's used inside it...

    Code:
    	'
    	'	Subroutine Displays Message on LCD from Program CodeSpace
    	'	---------------------------------------------------------
    		' CounterA - General Byte Variable
    		' CounterC - General Word Variable
    		' DataA - General Byte variable
    		' Message - ID of Message to Display
    		' MessageLen - Number of characters to Output
    		' MessageSupport - Beginning of Message Table in Codespace
    		' XDataAddress - 16-bit Address in Codespace
    DisplayExternal:
    	@ GetAddress _MessageSupport, _XDataAddress	' Get Start address of Message Support Table
    	XDataAddress=XDataAddress+(Message*18)
    	ReadCode XDataAddress,MessageLen
    	XDataAddress=XDataAddress+1
    	ReadCode XDataAddress,DataA
    	If DataA>0 then LCDOut $FE,DataA
    	If MessageLen>0 then
    		For CounterA=1 to MessageLen
    			CounterC=XDataAddress+CounterA
    			ReadCode CounterC,DataA
    			LCDOut DataA
    			Next CounterA
    		endif
    	Return

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Hi, Ioannis

    I believe PBP Variables are GLOBAL Variables ... and You'd better turn to "C" if you want LOCAL variables.

    or "another Basiç " ... Booooooooooooooo.


    Identifier is declared in the function or procedure Scope extends from the point where it is declared to the end of the current routine. These identifiers are referred to as locals.

    Now, let's say I do not understand clearly what you mean ...

    You simply could add a memo to the variable name like "Count_inter" or " Count_LCD "for your example ...

    I do not think RAM is THE problem ... as you have to preserve the "eventually overwritten" Variables !!!


    BTW ... I didn't find the Max. Variable Length in the Holy Manual ... ( 32 Characters ??? )

    Alain

    PS: Tell me You won't implement a " Variable Stack " ... tell me you won't ... lol
    Last edited by Acetronics2; - 12th March 2009 at 08:44.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Actually, I assumed the maximum variable length to be 65534 bytes (though I've never gone beyond about 8k personally - after all you gotta leave some space for your actual program too!!!)...

    Word variable minus 2 (because you can't have 65536) and it has to be an even number because the way 18F parts store stuff in Program Codespace.

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Talking

    Quote Originally Posted by Melanie View Post
    Actually, I assumed the maximum variable length to be 65534 bytes (though I've never gone beyond about 8k personally - after all you gotta leave some space for your actual program too!!!)...

    Word variable minus 2 (because you can't have 65536) and it has to be an even number because the way 18F parts store stuff in Program Codespace.
    Hi, Mel ..

    I meant the Max Variable NAME length ....

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    PS: Tell me You won't implement a " Variable Stack " ... tell me you won't ... lol
    No, not yet Alain. I think Darrel did this a while back?

    As for the C i do not feel very comfort... That += and =- get me on my nerves!

    Melanie:

    I usually name the variable in a way that is obvious as to what they are doing. But I don't like the CounterA, B, C, D, ... Z variables all to be used for the same task in different places. But I see no other way as the meaning of Local in PBP is not defined.

    Thanks for the talking.

    Ioannis

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    If you're writing large complex programs, then your chosen PIC probably has so much RAM you're never going to use it all anyway... so why not have a dedicated set of variables for every subroutine and ISR... after all, that's what C does with it's local declarations.

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default

    Yes Melanie. That is what in practice I am doing. It would be nice though, if there was a way of recycling variables!

    Ioannis

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. WORD vs BYTE variable lengths
    By bartman in forum General
    Replies: 0
    Last Post: - 28th November 2005, 21:16
  5. Storing a variable in EEPROM
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th October 2005, 13:53

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts