PIC XC8 compiler data space question

Discussion in 'Embedded Systems and Microcontrollers' started by bug13, Dec 7, 2015.

  1. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Hi guys

    I think I am about to run out of data space in my pic, so how reliable of the data space info generated by the mplab X when a project is compiled?

    eg, the data space info below:
    Thanks a lot!
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    You should be able to compute it yourself by adding up all of the space in your variables. Data sizes are listed in the compiler manual. Here is the one for integers

    upload_2015-12-7_16-38-12.png


    But I would say the memory summary is accurate.


    This really should be in the embedded forum.
     
    bug13 likes this.
  3. JohnInTX

    Moderator

    Jun 26, 2012
    2,340
    1,022
    Moved to Embedded Systems and Microcontrollers.

    The summary is usually pretty close and includes the memory that the compiler assigns for run time variables (software stack etc).

    If you start running low, review your code to see if you can put constants in ROM, declare temp variables inside functions etc. so that the compiler knows it can reuse them.

    XC8 is pretty good at packing in the bytes. Maybe you need a bigger PIC. Which one are you using?
     
    Last edited: Dec 7, 2015
    bug13 likes this.
  4. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Good to know that the compiler is pretty good.

    I am using pic18f46j50, 64K rom, 3.7K ram. unfortunately, my codes are almost done, and I don't really want to change if I don't have to.
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    I would take a look at your code. See if you can make use of the stack rather than declaring everything global there may be some variables you can declare inside functions.
     
    bug13 likes this.
  6. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    What do you mean by making use of the stack? Would you explain a bit more?
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Say for example you have a variable.



    Code (Text):
    1.  
    2.  
    3. int x;
    4.  
    5. void foo()
    6. {
    7.  
    8.   x =1
    9.  
    10. }
    11.  
    In this case x will remain in memory the life of your program. It is fine if x is going to be used my a lot of functions.


    But say it is only used by foo. Then you could do this.

    Code (Text):
    1.  
    2.  
    3. void foo()
    4. {
    5.    int x = 1;
    6.  
    7.  
    8. }
    9.  
    In this case the space x takes up will be freed when foo exits. It uses the stack.
     
    bug13 likes this.
  8. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Got it, thanks for explaining!!
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Look at section titled How Can I Reduce RAM Usage? in the XC8 manual for other tips. In mine it is section 3.6.3.
     
    bug13 and JohnInTX like this.
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Another little trick you can use to save a bit of memory.

    Code (Text):
    1.  
    2.  
    3. Say you have 8 flags
    4.  
    5.   unsigned char flag1;
    6.   unsigned char flag2;
    7.   unsigned char flag3;
    8.   unsigned char flag4;
    9.   unsigned char flag5;
    10.   unsigned char flag6;
    11.   unsigned char flag7;
    12.   unsigned char flag8;
    13.  
    14. This takes 8 bytes of memory when it really just needs to take 8.
    15.  
    16. You can use the bit operator.
    17.  
    18. struct flagstype
    19. {
    20.   unsigned char flag1 : 1;
    21.   unsigned char flag2 : 1;
    22.   unsigned char flag3 : 1;
    23.   unsigned char flag4 : 1;
    24.   unsigned char flag5 : 1;
    25.   unsigned char flag6 : 1;
    26.   unsigned char flag7 : 1;
    27.   unsigned char flag8 : 1;
    28.  
    29. };
    30.  
    31. This only takes on byte.
    32.  
    33.  
    34. If you have a 2 counters that only need to go to 15  you can do this
    35.  
    36. struct countertype
    37. {
    38.   unsigned char counter1 : 4;
    39.   unsigned char counter2 : 4;
    40.  
    41. };
    42.  
    43.  
    44.  
    45.  
    46.  
     
    Last edited: Dec 9, 2015
    bug13 likes this.
  11. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    Wouldn't a four bit counter top out at 15?
     
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Duh! Not thinking yeah 15. Too long a day today. o_O
     
Loading...