Strange Results in Math


Closed Thread
Results 1 to 7 of 7
  1. #1

    Default Strange Results in Math

    I'm completely stumped on this one. I read 5 temperature sensors, convert the data read in ºC, ºF and K. Seems straight forward until I start doing the math. This is what I get on the output

    Code:
    +026.37ºC  +079.47ºF  +299.52 K
    +026.37ºC  +079.47ºF  +299.52 K
    +026.37ºC  +079.58ºF  +299.58 K
    +026.37ºC  +079.92ºF  +299.77 K
    +026.37ºC  +079.81ºF  +299.71 K
    I can't figure why there is a difference in the numbers when they are all calculated from the same subroutine. Any ideas?

    Code:
    DEFINE  LOADER_USED 1DEFINE  OSC 16
    DEFINE  HSER_BAUD 9600
    DEFINE  HSER_CLROERR 1
    DEFINE  HSER_RCSTA 90h
    DEFINE  HSER_TXSTA 24h
    
    @ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H
    @ __CONFIG _CONFIG2L, _BOR_ON_2L & _PWRT_ON_2L & _BORV_45_2L
    @ __CONFIG _CONFIG2H, _WDT_ON_2H 
    @ __CONFIG _CONFIG4L, _STVR_ON_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
    
        data_bit        var     bit             ' Bit shift variable if not using lookup table
        sensor_error    var     bit             ' Sensor CRC error indicator flag
        sign_c          var     byte            ' +/- Sign for celcius temperature
        sign_f          var     byte            ' +/- Sign for fahrenheit temperature
        dummy           var     word            ' Dummy variable for DIV32 function
        x               var     byte            ' Loop counter
        i               var     byte            ' Loop counter
        byte_in         var     byte            ' Temporary holder for byte received from DS18B20
        data_byte       var     byte            ' Byte to perform CRC calculations on
        dq              var     byte[9]         ' 9 Byte Arrray for DS18B20
        crc_calc        var     byte            ' Calculated CRC value
        neg_bit         var     raw_temp.Bit11  ' Sign-Bit for +/- Temp. 1 = Below 0 deg C
        raw_temp        var     word            ' Raw Temperature
        temp_c          var     word            ' Temperature in ºC
        temp_f          var     word            ' Temperature in ºF
        temp_k          var     word
        sign_c_array    var     byte[5]
        sign_f_array    var     byte[5]
        temp_c_array    var     word[5]
        temp_f_array    var     word[5]
        temp_k_array    var     word[5]
        crc_error       var     byte            ' Loop counter for CRC errors
        a               var     byte
        
        clear
    
    main:
        for a = 0 to 4
        temp_c = 0
        temp_f = 0
        temp_k = 0
        dummy = 0
        dq = 0
        gosub read_temp
        sign_c_array[a] = sign_c
        sign_f_array[a] = sign_f
        temp_c_array[a] = temp_c
        temp_f_array[a] = temp_f
        temp_k_array[a] = temp_k
        next a
        gosub display
        goto main
    
    read_temp:
        owout a,1,[$CC,$44]                 ' Send start temperature conversion command
        low portb.5                         ' Turn on transistor between rail and data pin
        pause 750                           ' Allow enough time to process Tconv
        high portb.5                        ' Turn off transistor between rail and data pin
        owout a,1,[$CC,$BE]                 ' Send read scratch pad command
        owin a,0,[STR dq\9]                 ' Read all 9 bytes and store them in dq array
        raw_temp.Byte0 = dq[0]              ' Isolate raw temperature from the rest
        raw_temp.Byte1 = dq[1]
        gosub convert_temp                  ' Convert raw data into real temperatures
        return
        
    convert_temp:
        if neg_bit = 1 then below_zero		' If below 0ºC then goto different subroutine
        sign_c = "+"                        ' Display + symbol for negative temp
        sign_f = "+"                        ' Display + symbol for positive temperature
        dummy = 625 * raw_temp              ' Multiply to load internal register with 32-bit value
        temp_c = DIV32 100                  ' Divide internal register by 10 to calculate precision ºC
        dummy = 0
        dummy = 1125 * raw_temp             ' Multiply to load internal register with 32-bit value
        temp_f = DIV32 100                  ' Make it manageable
        temp_f = temp_f + 3200              ' Make it into ºF
        gosub kelvin
        return
    
    below_zero:
        sign_c = "-"                        ' Display - symbol for negative temperature
        sign_f = "+"                        ' Display + symbol for positive temperature
        dummy = 0
        raw_temp.byte0 = raw_temp.byte0 ^ 255
        raw_temp.Byte1 = raw_temp.byte1 ^ 255
        dummy = 625 * raw_temp + 1         ' Multiply inversion to load internal register with 32-bit value
       	temp_c = DIV32 100                  ' Divide internal register by 100 to calculate precision ºC
        temp_f = (temp_c + 5000) * 900      ' Multiply to load interal register with 32-bit value
        temp_f = DIV32 500                  ' Divide internal register by 500
        if raw_temp >= 285 then             ' Check if temperature is + or - ºF
            temp_f = temp_f - 12200         ' Process if temperature is to be negative
            sign_f = "-"                    ' Display a - symbol for negative temperature
            else				
            temp_f = 12200 - temp_f         ' Process if temperature is to be positive
            sign_f = "+"                    ' Display + symbol for a positive temperature
    	endif
        gosub kelvin
    	return
    
    display:
        hserout [sign_c_array[0],dec3 temp_c_array[0]/100,".",dec2 temp_c_array[0]//100,186,"C  ",sign_f_array[0],dec3 temp_f_array[0]/100,".",dec2 temp_f_array[0]//100,186,"F  +",dec3 temp_k_array[0]/100,".",dec2 temp_k_array[0]//100," K",10]
        hserout [sign_c_array[1],dec3 temp_c_array[1]/100,".",dec2 temp_c_array[0]//100,186,"C  ",sign_f_array[1],dec3 temp_f_array[1]/100,".",dec2 temp_f_array[1]//100,186,"F  +",dec3 temp_k_array[1]/100,".",dec2 temp_k_array[1]//100," K",10]
        hserout [sign_c_array[2],dec3 temp_c_array[2]/100,".",dec2 temp_c_array[0]//100,186,"C  ",sign_f_array[2],dec3 temp_f_array[2]/100,".",dec2 temp_f_array[2]//100,186,"F  +",dec3 temp_k_array[2]/100,".",dec2 temp_k_array[2]//100," K",10]
        hserout [sign_c_array[3],dec3 temp_c_array[3]/100,".",dec2 temp_c_array[0]//100,186,"C  ",sign_f_array[3],dec3 temp_f_array[3]/100,".",dec2 temp_f_array[3]//100,186,"F  +",dec3 temp_k_array[3]/100,".",dec2 temp_k_array[3]//100," K",10]
        hserout [sign_c_array[4],dec3 temp_c_array[4]/100,".",dec2 temp_c_array[0]//100,186,"C  ",sign_f_array[4],dec3 temp_f_array[4]/100,".",dec2 temp_f_array[4]//100,186,"F  +",dec3 temp_k_array[4]/100,".",dec2 temp_k_array[4]//100," K",10,10]
        return    
    
    kelvin:
    	if sign_c = "-" then
        	temp_k = 27315 - temp_C
            else
        	temp_k = temp_c + 27315
      	endif
      	return
    END

  2. #2


    Did you find this post helpful? Yes | No

    Red face Doohhhh!!!!

    It would probably help if I entered the array properly in the serial output line.

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


    Did you find this post helpful? Yes | No

    Default

    Hi CCK,

    I know you probably want to fix what you have. But why re-invent the wheel.

    Temperature Conversion (for Picbasic Pro)
    http://www.pbpgroup.com/modules/wfse...p?articleid=18
    <br>
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Default

    The wheel isn't being re-invented, just made using a different process

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


    Did you find this post helpful? Yes | No

    Default

    Fair enough!

    I'll just keep begging everyone else. Eventually, someone will try my program.
    <br>
    DT

  6. #6
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Lightbulb

    Quote Originally Posted by Darrel Taylor
    Fair enough!

    I'll just keep begging everyone else. Eventually, someone will try my program.
    <br>
    Hi Darrel,

    To link people to your version of the re-invented wheel, why don't you
    put the URL of your site www.pbpgroup.com at the bottom of your posts?
    (Automatic signature).

    Best regards,

    Luciano

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


    Did you find this post helpful? Yes | No

    Default

    And steal all the picbasic.co.uk users for my super impressive, emotionaly moving website that has affected 10's of people.

    Naaaa!
    DT

Similar Threads

  1. RC - connection using RCTime produces strange results
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 8th February 2007, 16:16
  2. Searches
    By bearpawz in forum Forum Requests
    Replies: 11
    Last Post: - 7th November 2005, 18:47
  3. Help! - Very strange results with ShiftIn/ShiftOut
    By khufumen in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd February 2005, 22:21
  4. First test program, but strange results
    By bartman in forum General
    Replies: 12
    Last Post: - 19th November 2004, 03:14
  5. I'm getting strange Results using POT or RCTIME on PortB (or GPIO Port)
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 0
    Last Post: - 15th July 2004, 20:01

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