Page 1 of 1

Large multiplication

Posted: Mon May 30, 2011 4:24 pm
by henker
Hello all,

I'm somewhat in a jam.

I have a multiplication to do however it only works partially:
T2 = 11 * (C6 +24) * (200 - TEMP) * (200 - TEMP) / 2^20

I have made a C Code box with the following lines:

long t2;
t2 = 11 * (FCV_C6 + 24); This parts is alwas 561
t2 = t2 * (200-FCV_TEMP); TEMP can be anywhere from -150 to 199
t2 = t2 * (200-FCV_TEMP);
t2 = t2 >> 20;
FCV_T2 = t2;

The problem is that the calculation works untill TEMP = -55. As soon as the TEMP is lower as -55 the T2 goes to 0

My question is; Can a PIC18F4685 actally handly such a calculation?
If it does, what am I doing wrong?

Thanks
Henk

Re: Large multiplication

Posted: Tue May 31, 2011 8:46 am
by DavidA
Hello,

I would imagine that you are using a BYTE to store one of your variables, the maximum value a byte can hold is 255. Try changing them to integers. If this isnt the case, attach your fcf file to the post and ill take a look at it.

Re: Large multiplication

Posted: Tue May 31, 2011 9:11 am
by Steve
You could try this instead:

Code: Select all

long t2;
t2 = 11 * (FCV_C6 + 24);
t2 = t2 * (long)(200-FCV_TEMP);
t2 = t2 * (long)(200-FCV_TEMP); 
t2 = t2 >> 20;
FCV_T2 = t2;

Re: Large multiplication

Posted: Tue May 31, 2011 12:40 pm
by henker
Thanks Guys,

I'm using all INT so that shouldn't be a problem.
I will try your comments later Steve.

Thanks for the help.

Henk

Re: Large multiplication

Posted: Wed Jun 01, 2011 1:10 am
by henker
OK, I got it working.
This is the solution:

t2 = 11 * (FCV_C6 + 24);
t2 = t2 * ((long) 200-FCV_TEMP);
t2 = t2 * ((long) 200-FCV_TEMP);
t2 = t2 >> 20;
FCV_T2 = t2;

Thanks for bringing me in the right direction.

Henk

Re: Large multiplication

Posted: Wed Jun 01, 2011 8:57 am
by Steve
Hi Henk,

Glad you got it working. Instead of "long", you can probably get away with "int" or "signed int" (this may save a small amount of processing and variable space if that's important to you).