|
|
View previous topic :: View next topic |
Author |
Message |
arp Guest
|
integer division |
Posted: Fri Feb 08, 2008 6:25 am |
|
|
Dear sir,
could you pls see the following and suggest me where i have gone wrong.
Pls see the following code
Code: | #include <16F877A.h>
#define SAMPLE 2
volatile int16 var1;
void func1(){
int8 samplecnt;
int16 var1Temp;
var1Temp = 0;
samplecnt = 0 ;
while(samplecnt < SAMPLE){
samplecnt++;
var1Temp += var1;
}
var1 = (var1Temp / SAMPLE);
}
main(void)
{
int8 a;
while (1){
func1();
a = SAMPLE;
}
} | The query is : i am not getting the result as i want . Yes i made it sure that integer variable var1 will not hold more than '300' and samplecnt will be max 10.
say If SAMPLE is 2 or 4 then division is Ok i.e i can go to next step in simulation(MPLAB). But if i change it to 3 or 5 etc..then the cursor vanishes when it comes to var1 = (var1Temp / SAMPLE);
Why this problem ??
Thanks & Regards
Arup |
|
|
D-Kens
Joined: 09 May 2005 Posts: 35 Location: Toulouse (France)
|
|
Posted: Fri Feb 08, 2008 8:16 am |
|
|
Euh... Maybe that's a stupid question, but what are you trying to do ? Looks like, basically, your function will only do "var1=var1", as you're adding var1 "samples" times to a null variable, then dividing by the number of samples.
By the way, why don't you set an initial value to var1 ? Where is that value given ? You use it without initialisation ? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Feb 08, 2008 8:30 am |
|
|
This is quite simple.
If you are dividing by a power of 2 i.e 2, 4, 8, 16, 32 etc then the compiler showing how clever it is optimises this into rotate/shift right instructions.
x shifted right by 1 is the same as dividing it by 2 and shifting right by 2 = divide by 4, shift right by 3 = divide by 8 etc.
Now because you are using > 8 bit numbers it has to use rotate so it can carry the bit over.
When you step through this all looks great and happens quite quickly.
If you divide by any other value the compiler uses a built in function hidden away in the include libraries, When you step through this in C view the simulator gets a bit lost but if you continue to step then it comes back.
If you goto asm view you can see this happening.
Enjoy. |
|
|
arp Guest
|
integer division |
Posted: Tue Feb 12, 2008 5:43 am |
|
|
Thanks Mr. Wayne_
Quote: | If you divide by any other value the compiler uses a built in function hidden away in the include libraries, When you step through this in C view the simulator gets a bit lost but if you continue to step then it comes back. |
Yes. I got it .Thanks.
Rgrds
Arup
And Hey D-Kens
Quote: | Euh... Maybe that's a stupid question, but what are you trying to do ? Looks like, basically, your function will only do "var1=var1", as you're adding var1 "samples" times to a null variable, then dividing by the number of samples.
By the way, why don't you set an initial value to var1 ? Where is that value given ? You use it without initialisation ? |
Thanks for your STUPID ANSWER.
read the full Query before answering.
if can't do that then stay quiet. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|