Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

what require less memory? case statement or equation methode?

Status
Not open for further replies.

shreyas_patel21

Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
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?"
 

zuisti

Advanced Member level 1
Joined
Jul 2, 2004
Messages
480
Helped
188
Reputation
376
Reaction score
184
Trophy points
1,323
Location
Hungary
Activity points
3,281
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.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
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.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,221
Helped
1,147
Reputation
2,313
Reaction score
1,123
Trophy points
1,403
Location
Brazil
Activity points
53,677
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:

navenmou

Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
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....
 

shreyas_patel21

Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
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?
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
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:

Sink0

Full Member level 6
Joined
Nov 25, 2009
Messages
390
Helped
37
Reputation
74
Reaction score
30
Trophy points
1,308
Location
Sao Paulo, Brazil
Activity points
4,186
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.
 

navenmou

Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
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
 

e shade

Member level 3
Joined
Dec 31, 2010
Messages
59
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Activity points
1,581
Hi Shreyas,
Check the following file for similar embedded programming facts.
 

Attachments


shreyas_patel21

Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
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.
this was helpful.

but still doubt there s no exact answer

thank you all!
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,221
Helped
1,147
Reputation
2,313
Reaction score
1,123
Trophy points
1,403
Location
Brazil
Activity points
53,677
...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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top