My PIC can't do the math! Can yours?


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Sayzer,

    Your PIC's math is working great!

    Change the last part of your code to this:

    FOR XA=0 to 150
    XB=XB-2
    IF XF = XA THEN
    XF = XB
    GOTO HERE
    ENDIF
    NEXT XA

    HERE:
    LCDOUT $FE,1,”Temp:”,#XF

    Your code is actually changing the value of XF twice in the region you thought was flawed (e.g., for 46464, XB = 149. Your If Then sets XF = 149. Your loop continues and finds XF = 149. 303 - (149+1)*2 = 3 ... the math is correct)

    Paul Borgmeier
    Salt Lake City, Utah
    USA

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Thumbs up

    Thanks Paul. I fixed the problem considering your catch.
    I make the loop value the end number so it exits the loop.


    Code:
    XB=303
    for XA=0 to 150
    
       XB=XB-2
       if xf=XA then 
         xf=XB
         XA=150    'once we have the match, lets exit immediately as Paul mentioned above.
       endif
     
    next XA

    Also, Thanks to you Jumper. I see what you are saying about ADCON1 but I have no issue with that register. I am now getting the correct results;

    I found the bug :-)


    ------------
    Last edited by sayzer; - 11th May 2006 at 14:30.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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


    Did you find this post helpful? Yes | No

    Lightbulb

    If this is all your program is supposed to do, i'd use something like this instead. Much faster.

    Code:
    ADCON1.7 = 1                          'right justified
    Loop:
        ADCIN 1,TempRead
        IF tempread>800 THEN tempread=800 'Min. temp.
        IF tempread<650 THEN tempread=650 'Max. temp.
        XF = 301 - ((TempRead - 650)<<1)  '301-((TempRead-650)*2)
        Lcdout $fe,1,”Temp:”, #XF
    GoTo loop
    /Ingvar

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Question Sometimes the rules are to be broken, I guess!

    Thanks for all replies.

    When I use "right justified" ADCON1 register, as Jumper and Ingvar suggested, the scale does not behave linear. Thus, I can not match the temp reading with Celsius.

    Currently, “left justified” ADCON1 register provides a scale that is perfectly linear and the formula works great (thanks to Paul's catch).

    By the way, Ingvar's code is indeed nice but I can not use it.



    ----------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. SMS via pic
    By kenandere in forum GSM
    Replies: 15
    Last Post: - 10th March 2010, 11:00
  2. HSERIN & Interupts (aka controlling PIC programs from a remote PC)
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 17th June 2009, 15:46
  3. pic to pic ir link versus wired link : help please anyone
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th May 2008, 22:01
  4. PIC Math - the need for speed
    By HenrikOlsson in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 14th October 2005, 09:45
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th March 2005, 00:14

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