better coding methods to freeing active ram in small pics


Closed Thread
Results 1 to 12 of 12

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: better coding methods to freeing active ram in small pics

    so as a bit of summery to date , what i have seen and do to reduce code size and ram allocation are as follows.
    But this is no where near complete , but add / correct where needed !!!
    1. Use the correct variable size – eg bit byte word
    2. Where multi flags are required of setup myflags as a byte, with variable = myflags.0, for each flag
    Code:
      Myflags var byte 
    Timeout_flag_0 var  Myflags.0 
    Timeout_flag_1 var  Myflags.1
    Fade_flags_0     var Myflags.2 
    Fade_flags_1     var Myflags.3

    3. Keep variables that are used in subroutine to min and reuse in each subroutine ( don’t forget to set to 0 in each routine at start)
    4. Keep Global variables to small number and to a byte
    5. When doing a read such as pulsin , and using a word variable , if accuracy is not as a big issue for other processing then dont assign words to an array with the results / divide the result so a byte value in array is used
    Code:
     
    FOR X = 0 TO 31                  ' grab 32 incoming pulses
           PULSIN IR,1,leader          ' now measuring high-going pulse widths   ( pulsin_max set to 21600 = 108ms )
           BtnVal(x) = leader/2        ' leader divide by 2 so array Btnval(x) is byte (<255) not word value  
      NEXT X
    6 where practical for code reading and structure , replace case select code with if , then statements


    Code:
               if DByte[4] = $6F then                  ' Red - remote 1 
                  read remotetype + Red ,STDByte[4]      ' read eeprom remote 2 location + button offset   
                  STDByte[3] = (~ STDByte[4])            ' bitwise Not the value of byte4 to be byte3 
               endif
               if DByte[4] = $4F then                  ' Btn01 - remote 1 
                  read remotetype + Btn01 ,STDByte[4]    ' read eeprom remote 2 location + button offset   
                  STDByte[3] = (~ STDByte[4])            ' bitwise Not the value of byte4 to be byte3 
               endif
               if DByte[4] = $57 then                  ' Btn02 - remote 1 
                  read remotetype + Btn02 ,STDByte[4]    ' read eeprom remote 2 location + button offset   
                  STDByte[3] = (~ STDByte[4])            ' bitwise Not the value of byte4 to be byte3 
               endif
               if DByte[4] = $67 then                  ' Btn03 - remote 1 
                  read remotetype + Btn03 ,STDByte[4]    ' read eeprom remote 2 location + button offset   
                  STDByte[3] = (~ STDByte[4])            ' bitwise Not the value of byte4 to be byte3 
               endif
    7 replace if x= 1 And y=1 then statements , with multi if statements , uses less words in compile


    8. when doing a read from a table in eeprom , its better to multi reads than keep in variables
    9. assigning constants with a variable name and use as the offset in lookup table and then doing a read at that location is better than using a variable for each name in the table
    10 love DT_int14.bas , routines but has high hit on ram on small pics , so better to use for all interrupts for timers , IOC etc , than setting up other code to monitor interrupt flags outside of DT routines
    11 math calculations , not sure , not had to do much of those where its been an issue ??

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    Did you find this post helpful? Yes | No

    Default Re: better coding methods to freeing active ram in small pics

    Also, check this thread out for more tips.

    http://www.picbasic.co.uk/forum/show...highlight=code
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  3. #3
    Join Date
    Feb 2011
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: better coding methods to freeing active ram in small pics

    What is your issue programming or outputting?

    Ran into the same brickwall and used the onboard NV ram for stuffing the output messages.
    Used the debug instead of serout/hserout it uses less space
    Then Used gosub for calling the output and read the text one letter and used char+128 to indicate EOL/EOM.

    The gosub used much less space using individual functions:
    so if you had a lot of conditions; set them up as a matrix and work them against the matrix, calling all from the NV ram.
    Amazingly ugly but did it on a bet. Even had room for a copyright

    Next time just buy the larger [read better] part, they wind up being cheaper
    12f1840 / 16f182x / 16f193[8/9]

    Newark.com has decent pricing. [last years pricing, but you get the idea]

    Code:
    PIC12F683	8dip	20MHZ 2Kprog 128nvB 256EE 6I/O $1.41 or	$1.15	
    PIC12F1822	8 dip	2kprog 256ram 128nvB 6 i/o	$1.20
    PIC12F1840-I/P		 8 PDIP	IO6 Prog7K 256B RAM 128nvB 32MHz 3tmr  	$1.18

    Using the NV -- Ofuzzy1 & W_Alarm are now text strings residing in the NV
    Code:
    CharBump        con $80
    Ofuzzy1 data @$10,   "(C)2011 Ofuzzy1 AllRightsReserved-Not for Life Critical Use", "!" + CharBump
    W_Alarm  Data        "Alarm", "_" + CharBump
    Code:
    gosub Print_Copyright
    ...
    
    Print_Copyright:
    gosub Print_SP2 : gosub CR_LF3 ': gosub CR_LF
    zb= Ofuzzy1 : gosub Print_Phrase
    gosub CR_LF3 ': gosub CR_LF
    return

    to print a phrase out use this:
    ab, bb, zb are byte sized VAR
    Code:
    Print_Phrase:   ' Call with zb = ROM_Memory_Location [byte]
    bb = 1
      Do until !BB '  = 0
        read zb, ab
        if ab > CharBump-1 then 
            ab = ab - CharBump
            bb = 0
        endif    
        debug ab
        zb= zb + 1
      loop
    return
    to call get the phrase ready to print use this:
    Code:
    Print_Copyright:
    gosub CR_LF
    zb= Ofuzzy1 : gosub Print_Phrase
    return
    Need 3 or 2 or 1 line feeds?
    Code:
    CR_LF3:
    debug LF,cr
    CR_LF2:
    debug LF,cr
    CR_LF:
    debug CR,LF
    return

Similar Threads

  1. amount of ram
    By vicce67 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th November 2012, 18:15
  2. Re-programming SM PICS on small PCBs
    By tekart in forum General
    Replies: 0
    Last Post: - 11th April 2011, 18:07
  3. Active low input?
    By CosMecc in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 8th August 2010, 21:31
  4. Alternative Methods ...
    By T.Jackson in forum Off Topic
    Replies: 3
    Last Post: - 9th June 2008, 14:13

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