Clearing Index variable arrays


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default Clearing Index variable arrays

    Hi guys ,
    i have a several large indexed arrays , that upon startup sometimes have values other than zero ,

    should this happen ?

    cheers

    Sheldon

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: Clearing Index variable arrays

    I assumed in the past when the chip is programed the variables are allocated space within the program area and I believe that the programmer does not initialise the variables to zero so if the variable space had previously been set those values will persist. I have come across this before and I always initialise my variables to known values if required. I wonder if chips are shipped with all bits set to zero or are some set during testing in the factory or just randomly as part of the manufacturing process.

    Anyway as I said I have noticed this behaviour before and I initialise my variables within the program.

  3. #3
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Clearing Index variable arrays

    thanks steve

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default Re: Clearing Index variable arrays

    Hi,
    Variables are stored in RAM - not in program memory (FLASH).
    You can use the PBP command CLEAR to initialize ALL declares variables to 0 or you can use a FOR-NEXT loop iterating thru your array and write 0 (or whatever) to each location.

    (And, when the chip is erased (program memory) all bits in the FLASH memory are set to one).

    /Henrik.

  5. #5
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: Clearing Index variable arrays

    Quote Originally Posted by HenrikOlsson View Post
    Hi,
    Variables are stored in RAM - not in program memory (FLASH).
    You can use the PBP command CLEAR to initialize ALL declares variables to 0 or you can use a FOR-NEXT loop iterating thru your array and write 0 (or whatever) to each location.

    (And, when the chip is erased (program memory) all bits in the FLASH memory are set to one).

    /Henrik.
    Thanks Henrik I knew you would put me straight, obviously it is RAM now you have told me. Why is the RAM not CLEAR to start with?

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default Re: Clearing Index variable arrays

    Hi Steve,
    If you look at the datasheet for the 18F1220, section 4.0 you'll find the following:
    Most registers are unaffected by a Reset. Their status
    is unknown on POR and unchanged by all other
    Resets. The other registers are forced to a “Reset
    state”, depending on the type of Reset that occurred.
    If you look further into that section you'll find a long a list of the Special Function Registers and what state they are in at POR and other types of resets. But, I hear you say, that's not the RAM where my variables are stored is it? Well, if you look at figure 5-6 (Data Memory Map) you'll see that the SFRs (all the peripheral control registers etc) and the general purpose RAM (where your declared variables are stored) are all just registers within the same block of volatile memory - which then brings as back to the quote from the Reset section: Their status is unknown on POR and unchanged by all other Resets.

    Another useful hint, not directly related to variables in RAM but anyway, which I learned from Darrel (of course) not too long ago is that if you look at any register for any peripheral or what not there's a note above each bit where it says for example R/W-1 this means that that particular bit in that particular register is Readable and Writeable AND that it's POR state is 1. If you look at the CONFIG Words it might say R/P-1 which means it's Readable, Programable (but not writable at runtime) and it's erased state (since this is program memory, not RAM) is 1.

    Does that make sense?

    /Henrik.

  7. #7
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: Clearing Index variable arrays

    Certainly does make sense. Thanks yet again Henrik.

  8. #8
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Clearing Index variable arrays

    interesting i did not know that on the data sheet , or missed its real meaning ,

Similar Threads

  1. How to index Port variable
    By jpadin1 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th February 2010, 10:36
  2. Clearing Out RS232 registers
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th July 2009, 02:03
  3. accessing ports pins using an index variable
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th March 2008, 20:36
  4. Clearing LCD
    By Michael in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 27th July 2006, 22:13
  5. storing arrays and reading arrays
    By Yue in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 5th March 2004, 22:03

Members who have read this thread : 1

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