Large multiplication

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

Post Reply
henker
Posts: 45
Joined: Mon Dec 22, 2008 11:22 pm
Location: Prospect, NS, Canada
Has thanked: 7 times
Been thanked: 2 times
Contact:

Large multiplication

Post 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

User avatar
DavidA
Matrix Staff
Posts: 1076
Joined: Fri Apr 23, 2010 2:18 pm
Location: Matrix Multimedia Ltd
Has thanked: 58 times
Been thanked: 258 times
Contact:

Re: Large multiplication

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

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Large multiplication

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

henker
Posts: 45
Joined: Mon Dec 22, 2008 11:22 pm
Location: Prospect, NS, Canada
Has thanked: 7 times
Been thanked: 2 times
Contact:

Re: Large multiplication

Post 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

henker
Posts: 45
Joined: Mon Dec 22, 2008 11:22 pm
Location: Prospect, NS, Canada
Has thanked: 7 times
Been thanked: 2 times
Contact:

Re: Large multiplication

Post 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

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Large multiplication

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

Post Reply