Continue to Site

# what require less memory? case statement or equation methode?

Status
Not open for further replies.

#### shreyas_patel21

##### Full Member level 3
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?"

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.

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.

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:

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....

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!
Code:
result = voltage * ( 10512 - 5152 ) + 176 ;
msb = ( result & ( 0xFF00 ) ) >> 8 ;
lsb = ( result && ( 0x00FF ) ) ;

+++

and
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?

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:

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.

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

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

#### Attachments

• C Progaramming.rar
19.1 KB · Views: 91

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 ----------

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

but still doubt there s no exact answer

thank you all!

...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.

+++

Status
Not open for further replies.