+ Post New Thread
Results 1 to 12 of 12
  1. #1
    Full Member level 3
    Points: 1,781, Level: 9

    Join Date
    Jan 2011
    Posts
    181
    Helped
    15 / 15
    Points
    1,781
    Level
    9

    what require less memory? case statement or equation methode?

    I am working on pic18f46k22

    I have adjusted DAC output for controlling 1 to 12 V

    I used case statement like if 1V is selected then DAC output should be 100mV and so on..

    example code:

    Code:
    switch(voltage)
    	{
    		case 1:
    				msb = 0x00;
    				lsb = 0xb0;
    				break;
    		case 2:
    
    				msb = 0x14;
    				lsb = 0x20;
    				break;
    		case 3:
    				msb = 0x29;
    				lsb = 0x10;
    				break;
    		case 4:
    				msb = 0x3e;
    				lsb = 0x00;
    				break;
    		case 5:
    
    				msb = 0x51;
    				lsb = 0x70;
    				break;
    		case 6:
    
    				msb = 0x66;
    				lsb = 0x60;
    				break;
    		case 7:
    
    				msb = 0x7a;
    				lsb = 0x90;
    				break;
    		case 8:
    
    				msb = 0x8e;
    				lsb = 0xb0;
    				break;
    		case 9:
    
    				msb = 0xa2;
    				lsb = 0xe0;
    				break;
    		case 10:
    
    				msb = 0xb8;
    				lsb = 0xa0;
    				break;
    		case 11:
    
    				msb = 0xd2;
    				lsb = 0x30;
    				break;
    		case 12:
    
    				msb = 0xe0;
    				lsb = 0xf0;
    				break;
    		default:
    				msb = 0;
    				lsb=0;
    				break;
    	}
    msb and lsb is for adjusting DAC.


    I can also make equation instead of case statements

    my question is "what requires less memory of pic case statement or equation?"

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 1
    Points: 5,693, Level: 17

    Join Date
    Jul 2004
    Location
    Hungary
    Posts
    476
    Helped
    189 / 189
    Points
    5,693
    Level
    17

    Re: what require less memory? case statement or equation methode?

    What means your "equation method"? A multiple IF-ELSEIF .... ENDIF?
    A long switch statement (like yours) is equal to a multiple IF-ELSEIF (same instructions), but more readable.
    However, it's working with constants only, if you compare the voltage with variables, use the IFs.



  3. #3
    Administrator
    Points: 69,512, Level: 64
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,832
    Helped
    2355 / 2355
    Points
    69,512
    Level
    64
    Blog Entries
    4

    Re: what require less memory? case statement or equation methode?

    Part of the art of embedded programming is optimizing your code for speed, efficiency and compactness. When faced with that question, "what requires less memory of pic case statement or equation?"

    Write your routine both ways, compile each and then compare them.

    Most compiler have utilities which can give you a report stating the amount of RAM and ROM your program consumes.



  4. #4
    Super Moderator
    Points: 28,512, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,476
    Helped
    1078 / 1078
    Points
    28,512
    Level
    41
    Blog Entries
    6

    Re: what require less memory? case statement or equation methode?

    176
    5152
    10512
    15872
    Shreyas,

    Taking a look on first numbers ( converted from hexa to decimal numerical base ), we can observe that behaviour seems to match a linear rule.
    A most accurate way is using root-mean-square approximation.

    So, to obtain that equation, you can derive a slope and multiply against index.
    Bellow, I just used 1st samples to perform the inference :
    Code:
    result = voltage * ( 10512 - 5152 ) + 176 ;
    msb = ( result & ( 0xFF00 ) ) >> 8 ;
    lsb = ( result && ( 0x00FF ) ) ;
    However, due now will be required a multiplication instruction from uC, processing time could be higher.
    You should do what bigdogguru sugested : Try some compiler optimization options and check what fit better your resources constraints limitations.



    +++
    Last edited by andre_teprom; 9th June 2011 at 13:10.



    •   AltAdvertisment

        
       

  5. #5
    Full Member level 4
    Points: 2,211, Level: 10
    navenmou's Avatar
    Join Date
    Sep 2010
    Location
    Bangalore, India
    Posts
    228
    Helped
    49 / 49
    Points
    2,211
    Level
    10
    Blog Entries
    2

    Re: what require less memory? case statement or equation methode?

    In Embedded programming we need to use the memory very efficiently and make the code very fast execution...I think switch case statement will take less memory than equation...or else take two code styles and check with compiler so that you will clearly come to know about that....



    •   AltAdvertisment

        
       

  6. #6
    Full Member level 3
    Points: 1,781, Level: 9

    Join Date
    Jan 2011
    Posts
    181
    Helped
    15 / 15
    Points
    1,781
    Level
    9

    Re: what require less memory? case statement or equation methode?

    thank you all for very helpful suggestions!

    when i tried both the ways one by one, the case statement was taking less memory...!

    but I couldnt understand it why?
    because i think equation should occupy less memory.

    ---------- Post added at 15:28 ---------- Previous post was at 15:23 ----------

    thank you!
    Quote Originally Posted by andre_teprom View Post

    Code:
    result = voltage * ( 10512 - 5152 ) + 176 ;
    msb = ( result & ( 0xFF00 ) ) >> 8 ;
    lsb = ( result && ( 0x00FF ) ) ;




    +++
    and
    Quote Originally Posted by yanamaddinaveen View Post
    In Embedded programming we need to use the memory very efficiently and make the code very fast execution...I think switch case statement will take less memory than equation...or else take two code styles and check with compiler so that you will clearly come to know about that....

    I also thought that this type of equation should occupy less memory but when i compiled the code and was surprised by the result!!

    what could be the reason behind this?



  7. #7
    Administrator
    Points: 69,512, Level: 64
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,832
    Helped
    2355 / 2355
    Points
    69,512
    Level
    64
    Blog Entries
    4

    Re: what require less memory? case statement or equation methode?

    Compilers are like people, the each have their own personality, strong points and weaknesses. If you were to compile those same routines on another compiler, chances are you would come up with totally different results. Knowing the quirks and strengths of your compiler is one of the necessities of being a successful embedded programmer.

    All compilers optimize to varying degrees, coding in C is never an exact translation, while code in assembly is translated exactly from mnemonic and operands to machine code. Assembly language is still in use today for primarily that reason, there are no surprises, it's very compact and efficient code.
    Last edited by bigdogguru; 10th June 2011 at 11:12.



  8. #8
    Full Member level 6
    Points: 2,878, Level: 12

    Join Date
    Nov 2009
    Location
    Sao Paulo, Brazil
    Posts
    390
    Helped
    37 / 37
    Points
    2,878
    Level
    12

    Re: what require less memory? case statement or equation methode?

    Quote Originally Posted by bigdogguru View Post
    Compilers are like people, the each have their own personality, strong points and weaknesses. If you were to compile those same routines on another compiler, chances are you would come up with totally different results. Knowing the quirks and strengths of your compiler is one of the necessities of being a successful embedded programmer.

    All compilers optimize to varying degrees, coding in C is never an exact translation, while code in assembly is translated exactly from mnemonic and operands to machine code. Assembly language is still in use today for primarily that reason, there are no surprises, it's very compact and efficient code.
    Nah, the reason on this case it is not due the compiler i belive, but due the hardware itself. As far as i know pic18f46k22 does not have any MAC hardware so this: (result = voltage * ( 10512 - 5152 ) + 176 ;) would take forever to be acomplishied, and the compiler (no matter if it is the most optimized in the world) is going to use a quitte big code, specially becouse he is working with non-8bit variables on a mcu with a 8 bit working register....

    Usually most well know compillers are quitte good reliable on common tasks ... i belive that to know your hardware is the MOST important point on an embedded designe.



  9. #9
    Full Member level 4
    Points: 2,211, Level: 10
    navenmou's Avatar
    Join Date
    Sep 2010
    Location
    Bangalore, India
    Posts
    228
    Helped
    49 / 49
    Points
    2,211
    Level
    10
    Blog Entries
    2

    Re: what require less memory? case statement or equation methode?

    Usually switch case has take less memory because switch need not check every case it just jumps to case where it match....internally it has one table. when ever we use variable for case match then it checks and jump to matched case statement and execute it....i hope this will help full to you



    •   AltAdvertisment

        
       

  10. #10
    Member level 3
    Points: 627, Level: 5

    Join Date
    Dec 2010
    Posts
    59
    Helped
    7 / 7
    Points
    627
    Level
    5
    Blog Entries
    1

    Re: what require less memory? case statement or equation methode?

    Hi Shreyas,
    Check the following file for similar embedded programming facts.



  11. #11
    Full Member level 3
    Points: 1,781, Level: 9

    Join Date
    Jan 2011
    Posts
    181
    Helped
    15 / 15
    Points
    1,781
    Level
    9

    Re: what require less memory? case statement or equation methode?

    Quote Originally Posted by yanamaddinaveen View Post
    Usually switch case has take less memory because switch need not check every case it just jumps to case where it match....internally it has one table. when ever we use variable for case match then it checks and jump to matched case statement and execute it....i hope this will help full to you
    thank you i think it might be true.

    ---------- Post added at 14:30 ---------- Previous post was at 14:29 ----------

    Quote Originally Posted by e shade View Post
    Hi Shreyas,
    Check the following file for similar embedded programming facts.
    this was helpful.

    but still doubt there s no exact answer

    thank you all!



  12. #12
    Super Moderator
    Points: 28,512, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,476
    Helped
    1078 / 1078
    Points
    28,512
    Level
    41
    Blog Entries
    6

    Re: what require less memory? case statement or equation methode?

    Quote Originally Posted by yanamaddinaveen View Post
    ...Usually switch case has take less memory because switch need not check every case it just jumps to case where it match....internally it has one table...
    I agree with this theory, specially because PIC have some set of instrucions ( addwf-retlw ) wich optimize that lookup table feature.

    +++



--[[ ]]--