Maths - Summing small numbers with decimals


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891

    Default Maths - Summing small numbers with decimals

    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"?
    Roger

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Not sure about the best solution but here is how to split a number up into two EEPROM spaces
    Code:
    WRITE 0,NUM.BYTE1
    
    WRITE 1,NUM.BYTE0
    Read it back the same way.
    HTH
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default

    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=120590965 5">
    Attached Images Attached Images  
    Last edited by flotulopex; - 19th March 2008 at 06:54.
    Roger

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Yep. DATA is at program time, WRITE is for run time.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Help!!!

    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

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex View Post
    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.

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default ...

    Y a plus qu'a cracher au bénitier!!!
    Roger

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex View Post
    Y a plus qu'a cracher au bénitier!!!
    Again, typical small U.S. school's got me here...I don't get it
    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.

  9. #9
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default and...

    .. is it working with any type of PIC or is this LONG feature limited to a certain type of µcs?
    Roger

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex View Post
    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  11. #11
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Aha!

    J'ai ze feeling qu'on me taquine un peu...

    Going to do some searching in the samples then.

    Thanks for the advice.
    Roger

Similar Threads

  1. Replies: 4
    Last Post: - 15th April 2009, 01:54

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts