Calculation Problem - value goes to zero after 65


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Thumbs up

    Ouch, i believe your "Shot at it" found it's target ..... sinking .... mayday. It's difficult to beat zero code, really difficult ...... sort of impossible.

    However, i want to point out that if you change the value of "Dec_Speed" you infact change the values in the array(ssmax17&18). This makes it impossible to use for other tasks, not a problem in this application(i hope). I bet you know that but some people find it difficult to get their head around this.

    Good job!

    /Ingvar

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    All true, I guess it's Bad Form to re-use variables like that.

    So here it is. The End all, Be all solution to Tom's Knot to MPH conversion.
    Code:
    @Knots = _ssmax + 17
    Knots    VAR  WORD  ext
    
    Dec_speed = Knots * 9881
    Dec_speed = R0 + R2.15 + Knots
    That way it preserves the original Knots value and calculates Dec_speed from it.

    Ok, now I really quit.

    Darrel

  3. #3
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default

    Darrel, Ingvar,

    I hope you do not mind dusting off this OLD thread.

    Quote by Ingvar... "I bet you know that but some people find it difficult to get their head around this."

    Boy was he right... Very interesting but difficult to follow for a newbie.


    I am also trying to convert knots and fractional knots to mph.h

    Darrel could you please explain/elaborate on the code from your last post...

    @Knots = _ssmax + 17
    Knots VAR WORD ext

    Dec_speed = Knots * 9881
    Dec_speed = R0 + R2.15 + Knots


    I thought that "@" usually proceeds "one line of assembly code"

    As I read through this thread, I was not able to follow with much clarity.

    In my gps program I end up with two variables containing integer knots and decimal knots.

    If we assume the variables used are knots (whole knots) and knotsten (fractional knots) how would I apply your final code to provide mph.h?

    Thanks much
    Dwight

  4. #4
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default

    I wish one could edit their own posts for a longer time than just 1 hour after posting.
    I would like to clarify my question posted above...
    Darrel, Ingvar,

    I hope you do not mind dusting off this OLD thread.

    Quote by Ingvar... "I bet you know that but some people find it difficult to get their head around this."

    Boy was he right... Very interesting but difficult to follow for a newbie.


    I am also trying to convert knots and fractional knots to mph.h

    Darrel could you please explain/elaborate on the code from your last post...

    @Knots = _ssmax + 17
    Knots VAR WORD ext

    Dec_speed = Knots * 9881
    Dec_speed = R0 + R2.15 + Knots

    1. I thought that "@" usually proceeds "one line of assembly code"?
    2. The last two lines of your code equates "Dec_speed" to one thing and then another thing. Doesn't the second line negate the first? Or, is the code shown out of context?
    3. What are the items "R0" and "R2.15"? variables? or math function?

    As I read through this thread, I was not able to follow with much clarity, sorry, newbie alert!! .

    In my gps program I end up with two variables containing integer knots and decimal knots.

    If we assume the variables used are knots (whole knots) and knotsten (fractional knots) how would I apply your final code to provide mph.h? (miles per hour & fractional miles per hour)

    Thanks much
    Dwight

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    1. I thought that "@" usually proceeds "one line of assembly code"?
    It does, and that's what it is.
    But he had the value stored in a specific location in an array, so you should just forget that part and make it ...

    Knots VAR WORD

    R0 and R2 are two of PBP's system variables.
    When you do a multiplication (Knots * 9881), R2 has the low word of the result which also gets copied to Dec_speed.
    And R0 has the High Word of the result, the same as if you did a Knots ** 9881, without having to do the multiplication a second time.

    So yes, the second Dec_speed does "negate" the first one, but the result is still in R0 and R2 so it doesn't matter.


    MPH = Knots * 1.150779
    And 9881 / 65536 = 0.150772, which is as close as we can get with integer math. That's the decimal part.
    The integer part is 1, so just adding Knots to the final result is the same thing.

    R2.15 is the highest bit in the low word, so it indicates if the high word should be rounded up or down.

    So all together, the multiplication does (Knots * 0.150772), the result is in R0, Round up if R2.15=1, then add Knots (knots * 1).

    Clear as mud.
    <br>
    Last edited by Darrel Taylor; - 9th March 2010 at 02:17.
    DT

  6. #6
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel,

    sorry it took so long for me to get back to this thread...

    here is my code as I have it now (seems to work ok)

    I enter the subroutine having parsed the NMEA $GPRMC recieved from the GPS.

    VAR "Knots" contains the integar value, VAR "Knotss" contains the decimal or fractional value of speed data recieved from the GPS.
    Code:
    CalcMPH:
      arraywrite speed,[dec2 knots,dec2 knotss] 'combine knots and knotss into 4 byte array
      arrayread speed,[dec4 kk]                 'now get the 4 digit dec value of the array
      
      mph  = kk * 9881
      mph  = R0 + R2.15 + kk 'mph now contains 4 digit value of mph/tenths/hundredths
      
      mphh = mph//100      'isolate tenths and hundredths 
      mphh = mphh/10       'eliminate the hundreths and keep tenths
      mph = mph/100        'now keep integer mph, first two digits of 4 digit value
    return


    I would like to learn more about the use of system variables (R0, R2...) can you point me to where these are documented? I searched the 12f683 document and did not find them.


    Thanks for your help.
    Dwight

Similar Threads

  1. Problem to compile my program
    By wagner in forum Off Topic
    Replies: 5
    Last Post: - 7th July 2008, 20:17
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. Microcode Studio 18f2455 problem?????
    By volkan in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 21st May 2007, 21:04
  4. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 15:23
  5. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59

Members who have read this thread : 0

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