8bit Multiplication of 16bit values

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

8bit Multiplication of 16bit values

Post by Jay Dee »

Hi Guys,
I am looking at methods of multiplying unsigned integers (U16) with an PIC18F4455 (ECIO40P).
If the product of the two arguments is greater than 2^16 (65536) the value is truncated by integer rounding, an stuff.
So if I have two values,
Arg1 = 2048
Arg1 = 1024
Product = 2097152
What the best way of getting the first 4 or 5 significant figures back?
I can compensate for the final value simply being out by a 10,100,1000 etc..
I'm resigned to loosing some precision but after a few sequential calculations the total error is creeping up.
I'm playing with some maths at the moment but Im sure there is a 'propper' method rather than my bodgy maths. :)
J.

Docara
Posts: 315
Joined: Sun Jun 23, 2013 1:29 pm
Has thanked: 28 times
Been thanked: 61 times
Contact:

Re: 8bit Multiplication of 16bit values

Post by Docara »

Hi Jay,

Have you set your 'answer' variable to Long or Ulong?

Tested with 'PrintFormattedNumber' on a LCD display
Flowcode1.fcfx
(5.61 KiB) Downloaded 279 times

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: 8bit Multiplication of 16bit values

Post by Jay Dee »

Thanks Docara,
Yeah I totally forgot about extending the resultant to the required Bit size, I think that will fix it.
I'm now just playing in hardware, trying to get values to display on the LCD or i2C LCD...
Did yours work in hardware?
J.

Docara
Posts: 315
Joined: Sun Jun 23, 2013 1:29 pm
Has thanked: 28 times
Been thanked: 61 times
Contact:

Re: 8bit Multiplication of 16bit values

Post by Docara »

Hi Jay,

Thanks for the update.

The hardware side I find is not a problem, Trying to decide when how to use strings v numbers I'm struggling with. Well and the lack of thorough documentation!! So far, for me, the most relible and consistant solution to display large numbers has been to convert it to a string (cant rember the command - on a different pc) so you can PrintString in the LCD Macro. The beauty of this method is I can use the string functions to suppress leading zeros to help positioning within the display by easily combining (concatenating) words/letter with desired numbers. I hate the flickering of the display when you have to clear the display before changing the displayed number, blanks in the string sort out the old value being displayed.

Doc

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: 8bit Multiplication of 16bit values

Post by Jay Dee »

Hi Doc,
Ben has resolved my problems with the hardware, I was not correctly 'casting' values in some of my equations. With the i2C display Ben had to tweak a component but its all working now.

What Ben described was that you need a ULong value to exist in the equation so that it would cast the result correctly so for 16 bit numbers (U16)
instead of doing the obvious..
ULong_X = U16_A * U16_B

I now do,
ULong_X = U16_A // This casts the first 16bit value into the 32bit Value
then
ULong_X = ULong_X * U16_B //The equation now already holds a 32bit value and the result of the equation is correctly cast to a 32bit result.

Well, thats at least how I understood it....and it works well on my little 8bit ECIO40P.
I do find I have to often divide results by 1000 or so, just to stop values growing to huge for the resultant channel and subsequent calculations.

I like your method of converting to strings, I may try that... I've played with various methods to reduce perceived flicker during screen value updates. very much a learning process!
cheers, J.

Post Reply