PDA

View Full Version : Maths - Summing small numbers with decimals



flotulopex
- 19th March 2008, 00:01
Hi,

I'm (or wanting to) adding some features to my battery charger.

The aim is to sum the milli-amperes that the charger has provided to the battery (some kind of energy counter).

The maximum instant load is 1400mAh; the charging time can be up to six hours (this is depending on the battery charge level at the beginning of the charging process) and the program checks the load every second.

If I store a 3600th of mAh every second, then I will get: 1400mAh / 3600 = 0,3888 and this is not easy to store unless it is modified.

In the worst case, there are going to be 21'600 "mAh" samples (3600 seconds * 6 hours).

I could simply multiply by 3 each measured load ("3" comes from a WORD sized var 65'535 / 21'600 samples) and when the time comes to display the result, divide the summed samples by 3 again. This would be like (1400mAh * 3) / 3600 = 1,1666. Since the stored result is "1", I have an important precision loss...

Another way could be to multiply the measured load by 100 like (1400mAh * 100) / 3600 = 38, then store this in two WORD sized vars (one would be High the other Low, but I don't know how to do this) and again, as before, divide the sum by 100 when it is time to display the result. Here also, I have a loss in precision, less than before but still a loss.

What is the best mathematical way to add these small numbers without making too much "calculation-storming"?

mackrackit
- 19th March 2008, 00:54
Not sure about the best solution but here is how to split a number up into two EEPROM spaces


WRITE 0,NUM.BYTE1

WRITE 1,NUM.BYTE0

Read it back the same way.
HTH

flotulopex
- 19th March 2008, 06:16
I'm in the office and can't test this right now so I ask further.

Do you know if I can store WORD sized numbers the same way you show me?

The DATA command allows to do this, but WRITE?

>>> just found the answer: Yes, it is possible. I'll have to try this when I get back home...
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2421&stc=1&d=1205909655">

mackrackit
- 19th March 2008, 09:24
Yep. DATA is at program time, WRITE is for run time.

flotulopex
- 22nd March 2008, 16:44
Since I'm using interrupts, storing data with WRITE might sometimes be too long in conjunction some ADC processes.

How do I store numbers greater than 65535 in variables and reuse them?

I'm lost.

skimask
- 22nd March 2008, 16:53
Since I'm using interrupts, storing data with WRITE might sometimes be too long in conjunction some ADC processes. How do I store numbers greater than 65535 in variables and reuse them? I'm lost.

PBP 2.50a handles LONG variables, 32 bits, numbers.

flotulopex
- 22nd March 2008, 17:35
Y a plus qu'a cracher au bénitier!!! :D

skimask
- 22nd March 2008, 18:34
Y a plus qu'a cracher au bénitier!!! :D
Again, typical small U.S. school's got me here...I don't get it :D
Me = uni-lingual ... there's a better word for that, but I don't know what it is off hand...

My point is that PBP can handle numbers over 65535 by using LONG variables...which you can then split up using the WORD or BYTE modifiers, then store those numbers if needed, or recover them, using the WORD, or BYTE variable modifiers.

flotulopex
- 22nd March 2008, 18:48
.. is it working with any type of PIC or is this LONG feature limited to a certain type of µcs?

Acetronics2
- 22nd March 2008, 19:15
Since I'm using interrupts, storing data with WRITE might sometimes be too long in conjunction some ADC processes.

How do I store numbers greater than 65535 in variables and reuse them?

I'm lost.


Roger ...

You can store them in temporary variables and send them to EEPROM when you've enough time ...

WHILE some ADC conversions are running in the background ... for example.

ah, ... yes, I see ... that needs some ... brain use ???


just open your PBP "samples" folder and look for "A2D10" ... instead of "ADCIN10"

...

Good night, Roger

Alain

flotulopex
- 22nd March 2008, 19:21
J'ai ze feeling qu'on me taquine un peu...

Going to do some searching in the samples then.

Thanks for the advice.