• AVR Freaks

Hot!Why does this code takes up high percentage of program memory?

Author
pauline95
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2019/09/27 00:36:34
  • Location: 0
  • Status: offline
2020/07/10 02:42:49 (permalink)
0

Why does this code takes up high percentage of program memory?

Hi. I am using PIC16F887. The memory usage of my code was 40% initially but it increases to 92% after I added the code below to it. Can someone please help me to figure out why is this code takes up such as huge amount of memory?
 
void __interrupt() ISR(){ 
    if (RBIF){
        if (RB0 == 0){
            TMR1ON = 1;
        }
        else if (RB0 == 1){
            
            dummy++;
            
            TMR1ON = 0;
            
            time = ( (TMR1H << 8) + TMR1L ) * 0.000001; // Combine TMR1L & TMR1H and calculate duration
            PF = cos(50*3.142*2*time);
            
            
            
            if(dummy==1){
                
                PF*=100;
                
                num[0] = (int)PF / 100;
                num[1] = (int)PF / 10;
                num[2] = (int)PF % 10;
                
                LCD_Cmd(0xC0); // Go to first line
                DisplayString("PF: "); // Print first line
                LCD_Data(num[0]+'0');
                LCD_Data('.');
                LCD_Data(num[1]+'0');
                LCD_Data(num[2]+'0');
                
            }
            
            TMR1H = 0;
            TMR1L = 0;

            time = 0;
            
            
        }
        
        char portb = PORTB;
        RBIF = 0;
    }
    
    if(TMR1IF)
    {
        TMR1IF=0; // Clear timer interrupt flag
    }
}

#1

11 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 03:08:35 (permalink)
    +3 (3)
    I would guess you added the floating point calculations.
    FP takes a lot of code.
    It is a very BAD idea to do FP inside an interrupt service.
     
    It is also a bad idea to do slow operations like writing to LCD, inside an ISR.
     
    post edited by ric - 2020/07/10 03:12:43

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    pauline95
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2019/09/27 00:36:34
    • Location: 0
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 04:10:18 (permalink)
    0
    ric
    It is a very BAD idea to do FP inside an interrupt service.
     



    When you say it's bad to do FP in ISR, is it because it uses more memory as compared to doing it outside the ISR?
    #3
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 04:22:48 (permalink)
    +2 (2)
    I mean you should NOT do slow things in an ISR.
    You need to do the minimum possible to clear the interrupt condition, and get out again ASAP.
    While you are inside the ISR, all your other code stops running.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    pcbbc
    Super Member
    • Total Posts : 1703
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 05:16:54 (permalink)
    +3 (3)
    And, if your ISR takes too long to execute, you may miss other interrupts.
     
    What do you want to do with your sin values?
    Depending on application you may be better with a lookup table.
     
    Remember that you only need to store 1/4 of the values:
    90..180 = sin(180 - x)
    180..270 = -sin(x - 180)
    270..360 = -sin(360 - x)
    #5
    pauline95
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2019/09/27 00:36:34
    • Location: 0
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 07:56:44 (permalink)
    0
    pcbbc
    Depending on application you may be better with a lookup table.
     



    How do I do that? Can you please elaborate more on that? I'm trying yo calculate the power factor of ac appliance.
    #6
    NKurzman
    A Guy on the Net
    • Total Posts : 18858
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 10:06:28 (permalink)
    +2 (2)
    A look up table would mean you pre-calculate you values on a calculator or Spreadsheet and put them in an Array.
    You then look up the one you need in the Array.
    #7
    PStechPaul
    Super Member
    • Total Posts : 2814
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 10:34:29 (permalink)
    +2 (2)
    It looks like you are calculating PF by means of phase angle between current and voltage based on zero crossings. A better way is to measure the true RMS value of power and divide that by the product of volts and amps (VA). The phase angle measurement can be susceptible to noise and distortion. But with your method, you don't need to use all those floating point calculations, and certainly not in an ISR which runs at 120 Hz. And display update should be about 3 times per second.

     
    #8
    Jerry Messina
    Super Member
    • Total Posts : 540
    • Reward points : 0
    • Joined: 2003/11/07 12:35:12
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/10 11:38:39 (permalink)
    +3 (3)
    ric
    It is also a bad idea to do slow operations like writing to LCD, inside an ISR.

     
    Not to mention the fact that writing that sequence of instructions to an LCD inside an ISR almost guarantees that the LCD can't be used in the main routine without disabling interrupts first.
     
    #9
    Hen
    Senior Member
    • Total Posts : 86
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/12 21:20:32 (permalink)
    0
    pauline95
    pcbbc
    Depending on application you may be better with a lookup table.
     

    How do I do that?

    I think your chip is too limited to get either speed or precision but if you can step up to lets say PIC18 you don't hit the *wall* as easy.
    #10
    mlp
    boots too small
    • Total Posts : 946
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/13 07:13:58 (permalink)
    +2 (2)
    Not strictly related to OP's question, but in the long run it doesn't matter how much program memory is occupied as long as it's <= 100% for the finished code. At any given time an 8-bit PIC will be running a single program which should reasonably expect to have all MCU resources available to it - any unused Flash is wasted real estate.
     
    Some optimizations-for-size (e.g. divide-by-subtracting) can make the running code much slower.
     
    Some optimizations-for-speed (e.g. look-up tables, loop unrolling ...) can make program memory usage expand significantly.
     
    Back in the day, we discussed the possibility of modifying XC8 so that it could first optimize-for-size and then optimize-for-speed up to the limit of available program memory, so users could get the best of both worlds. Sadly we never had enough Copious Free Time to implement this, and we figured that explaining the behaviour would take up a lot of support time ("My program always takes up 100% of Flash - why?"), so it never happened.
     

    Mark (this opinion available for hire)
    #11
    Jan Audio
    Senior Member
    • Total Posts : 159
    • Reward points : 0
    • Joined: 2018/09/24 08:12:24
    • Location: 0
    • Status: offline
    Re: Why does this code takes up high percentage of program memory? 2020/07/13 07:40:00 (permalink)
    0
    You also included and used LCD things.
    And you included cosinus, it is huge also.
    Remove all that to start with you see the difference.
    #12
    Jump to:
    © 2020 APG vNext Commercial Version 4.5