+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Member Henk57's Avatar
    Join Date
    Jan 2014
    Posts
    282
    Thumbs Up
    Received: 20
    Given: 24
    Total Downloaded
    192.60 MB

    1 Not allowed!

    Default Free up some memory by reducing Print command?

    I want to add BME280 to my Sprouter, but I have 3k2 free space, and I need at least 4k3 free, and a bit more, for some simple calc.

    I have a Main-Menu with 23 items and 2-lines on a 2x16 LCD.

    My thoughts are;
    Replace 45k22 with 46k22 but thats not easy todo,
    Or;
    Replace 46 Print commands into,
    Code:
    MainMenu:  ' Rotary Switch
       Cls
            While RotarySw = 1 : Wend
           GoSub pauze
      waarde = menu_waarde     
       menuteller = 0
        Menu_item = 1      
        While 1 = 1
            ;
           menu_waarde = waarde     
            Select waarde
                Case 0            '1234567890123456
                    Print At 1,1, "-> Time         "         ;      
                    Print At 2,1, "-  Settings1   ”     ; 
                Case 1            '1234567890123456
                    Print At 1,1, “Settings1     ”     ; 
                    Print At 2,1, "-  Settings2  "     ; 
    ;
    ; To
               Case 22 ; 
    ;
    Wend
    …into 2 print commands;
    Code:
    Dim printArrayline1[16]   As Byte 
    Dim printArrayline2[16]   As Byte 
    
    MainMenu:  ' Rotary Switch
       Cls
            While RotarySw = 1 : Wend
           GoSub pauze
      waarde = menu_waarde     
       menuteller = 0
        Menu_item = 1      
        While 1 = 1
            ;
           menu_waarde = waarde     
            Select waarde
                Case 0            '1234567890123456
                    printArrayline1 = "-> Time         "         ;      
                    printArrayline2 = "-  Settings1   ”     ; 
                Case 1            '1234567890123456
                    printArrayline1 = “Settings1     ”     ; 
                    printArrayline2 = "-  Settings2  "     ; 
    ;
    ; To
               Case 22 ; 
    ;
    Print Str printArrayline1
    Print Str printArrayline2
    ;
    Wend
    I hope it will free up more than 1k, and if not I can eventualy free-up to about 60 Print commands
    I know the Print-command is memory consuming command but the;
    Question now is how much memory will be free-up after this, anyone?

  2. #2
    Prolific Poster towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    1,543
    Thumbs Up
    Received: 100
    Given: 136
    Total Downloaded
    2.40 GB

    1 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    Question now is how much memory will be free-up after this, anyone?
    Probably wrong but compile without using Print, then compile with Print and subtract one fro the other. I suspect that the Print command itself isn't that big, all the clever formatting is a different part of the compiler.
    George

  3. #3
    Senior Member Stephen Moss's Avatar
    Join Date
    Jan 2006
    Posts
    401
    Thumbs Up
    Received: 24
    Given: 3
    Total Downloaded
    1.32 GB

    1 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    I had a look at an old program (18F device) and every time I used the print command to print 2 lines of 16 character text it required 20 lines of assembler in addition to that taken by the compilers print routine which we can assume to be a fixed and necessary constant.
    In another program (PIC24 device) I just created two variables & called a subroutine containing two print commands...
    Code:
    Dim Line_1 As String * 16
    Dim Line_1 As String * 16
    and
    Code:
    LDC:  'Print Subroutine
    Print At 1,1, Line_1
    Print At 2,1, Line_2
    DelayMS 1500 'allow time to read display before update
    Return
    calling the print subroutine when I needed it, i.e.
    Code:
    If x = 1 then Line_1 = "Fred" : Else : Line_1 = "Wilmer"
    GoSub LCD
    Obviously as I am using fewer instances of the print command with the subroutine method if I had used that on the 18F device I presume I would be saving 20 lines of assembler code every time I call it although as the programs I looked at have different amounts of data to print and it appears (in my limited understanding) different assembler code due to the different device types it is difficult to quantify what saving was made using the Print subroutine method in this instance.

    As towlerg said the only way to quantify the saving is to compile both methods and see the difference in compile size. Another way of potentially saving code space if your device has an EPROM is to store all your fixed string data there and read it into your print variables as required before calling your print subroutine. I say potentially because I do not know the code overhead involved in x number of reads to the EPROM and that overhead may result in little to no space saving.

    Otherwise to the best of my knowledge (which may be incorrect) the string data is stored as a data table in the flash memory and thus presumably consuming code space. If that is correct and neither of the methods above save enough space then I would think the only way to save more space would be to reduce the number of strings you want to print or carefully examine your code and see if you can make any savings elsewhere using loops and other subroutines over a succession of direct line instructions.

  4. #4
    Member Henk57's Avatar
    Join Date
    Jan 2014
    Posts
    282
    Thumbs Up
    Received: 20
    Given: 24
    Total Downloaded
    192.60 MB

    0 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    I tried, but the outcome is dissapointed I lose 4% free space if I convert it to string array.
    Even 2x Dim printArrayline(x)[16] consume 2% and fill both array's another 2% (4% of 32K is about 1280 Bytes).

    So what remains, avoid the double use of Print command in MainMenu (thus only 1 line on LCD and scroll nothing),
    and cut out pieces of code which can be avoided, but then free up at least of 1k would be difficult.

    Thanks Stephen for explaning, that print command use about 20 lines of assembler code.
    If I can reduce the Print command about 30 times in total, but then I must also remove some pieces of code, before I reach 1k2..1k5 to free-up more memory.

    Henk.

  5. #5
    Member Henk57's Avatar
    Join Date
    Jan 2014
    Posts
    282
    Thumbs Up
    Received: 20
    Given: 24
    Total Downloaded
    192.60 MB

    0 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    After all I decide to replace the 45K22 into 46K22 that was easier, than rewriting a lot of code with the question,
    is there just more free memory left.

    Thank you,
    Henk.

  6. #6
    Fanatical Contributor Les's Avatar
    Join Date
    Feb 2002
    Posts
    3,336
    Thumbs Up
    Received: 294
    Given: 111
    Total Downloaded
    1.50 GB

    1 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    If using a lot of character strings, try and make as many as possible the same, because the compiler looks for character strings held in flash memory and does not duplicate them, but combines them so it only takes a single table held in flash memory. For example:

    Instead of:

    Print At 1,1, "Settings1 "
    Print At 2,1, "- Settings1 "


    Use:

    Print At 1,1, "Settings1 "
    Print At 2,1, "- ", "Settings1 "


    Notice how both the "Settings1 " strings are the same, so the compiler only uses a single table within the flash memory. This is standard optimisation done by the compiler.

    The first code snippet takes 310 bytes of flash, while the second takes 304 bytes. A small amount, but over a large program the savings add up.

    Also, instead of using multiple Print commands, try and combine them where possible:

    Print At 1,1, "Settings1 ", At 2 , 1 , "- ", "Settings1 "

    This is the same as the second snippet above, but combines and takes 300 bytes of code memory. This is because the Print command has to inform the library routines what is happening, and that it is Print that is operating now, so combining in the same Print means it only has to tell the library once.

    Also. If you have large expressions within your code, separate them because large expressions require the compiler to use worse case scenarios with variable sizes, so it uses more stack RAM, which needs loading and retrieving. This also takes extra mnemonics. i.e. Flash memory

    Also make sure variable sizes match the expressions. Larger variable types require more flash memory to manage them. So, for example, a Dword addition takes more flash than a Word addition because there are more bytes within a Dword to manipulate, but be careful of expression overflows, where a calculation will overflow a smaller variable type. With separated expressions, this is very easy to see and manage.

    As I've said many times. "When using any microcontroller, think low level while writing high level". This is one of the big flaws with using high level languages. Users mistakenly think that "if it all fits on a single line, it must produce smaller code all the time". Which is incorrect 99% of the time because a compiler has to work with "worse case scenarios" then, to make sure it gets it right, which takes more flash and RAM.
    Last edited by Les; 14th December 2017 at 14:31.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  7. #7
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Posts
    2,681
    Thumbs Up
    Received: 78
    Given: 19
    Total Downloaded
    2.45 GB

    0 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    Quote Originally Posted by Les View Post
    Also, instead of using multiple Print commands, try and combine them where possible:

    Print At 1,1, "Settings1 ", At 2 , 1 , "- ", "Settings1 "
    I learned something new from this thread. I did not see this in the manual.
    Regards

    Joseph

  8. #8
    Fanatical Contributor Les's Avatar
    Join Date
    Feb 2002
    Posts
    3,336
    Thumbs Up
    Received: 294
    Given: 111
    Total Downloaded
    1.50 GB

    2 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    The "At" directive is a standard modifier, so follows the rules of the other modifiers, in that there can be multiples of them.

    To make things more readable in a program, I usually use the line extender:

    Code:
    Print At 1, 1, "Settings1     ",_
          At 2, 1, "-  ", "Settings1     "
    

    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  9. #9
    Member Oldhack's Avatar
    Join Date
    Aug 2010
    Posts
    153
    Thumbs Up
    Received: 8
    Given: 0
    Total Downloaded
    640.58 MB

    0 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    Hello
    It is good to know this, I have had Print eat up memory to many times on small chips, It's time for your book?????

    Gary

  10. #10
    Member Henk57's Avatar
    Join Date
    Jan 2014
    Posts
    282
    Thumbs Up
    Received: 20
    Given: 24
    Total Downloaded
    192.60 MB

    0 Not allowed!

    Default Re: Free up some memory by reducing Print command?

    Hello Gary.

    That reminds me some years ago, I use several times Print command for the 16F628A.
    I could never find in the manual how much bytes are used for each command with var’s.
    So I had to minimize then in use, to free up memory.

    I used then, SHOut for remote control my Tascam recorder with a keyboard-stroke, F0,F4,F5,F8. resp. Release-Button, Stop, Play, Record.
    With count-down timer control, then I notice that the drift in internal Osc, after each power-on.
    One of the days there was a tolerance of 10 seconds in 24 hours, other day there was more then 20 seconds in 24 hours.

    At some point I must skip more Print command’s, if I only knew this before…

    Henk

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. [SOLVED !] PIC24EP256GP206 error with Print command
    By Jaz in forum Proton Plus Compiler v3
    Replies: 1
    Last Post: 23rd June 2016, 16:11
  2. Custom Print Command
    By wastrix in forum Proton Plus Compiler v3
    Replies: 4
    Last Post: 29th January 2010, 05:57
  3. Problems with the Print At y,x, command
    By kodiak in forum Proton Plus Compiler v3
    Replies: 27
    Last Post: 6th August 2007, 23:05
  4. Print Control Command Operation
    By mikedownunder in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 6th June 2006, 13:28
  5. BUG found in print command
    By Hans Chr in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 28th September 2005, 23:55

Members who have read this thread : 41

Actions :  (Set Date)

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

Tags for this Thread

Posting Permissions

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