ºC -> ºF Conversion


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

    Default ºC -> ºF Conversion

    I've hit a road block in converting temperature from celcius to fahrenheit. I know:

    ºF = (ºC*18)/10+32

    This works fine for positve temperatures but not for negative. I discovered:

    ºF = 32-((-ºC*18/10+32)-32)

    This formula works fine until I get to the negative numbers in ºF. In ºC there is a bit set indicating that the number presented is a negative.

    Any ideas would be greatly appreciated.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    what is the range of your positive and negative Temperature reading before converting ???

    i mean positive : from x to y
    negative : from a to b

    I don't know what is your hardware setting but you can probably test the borrow bit and do some math with. Maybe the range will tell a better solution in this case.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    I'm using the DS18S20 which is a celcius chip with a range or -50C - 125C.

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


    Did you find this post helpful? Yes | No

    Default

    Hi!

    See page 13 of the PDF.

    From the Nuts and Volts magazine:

    There’s A New Stamp In Town - Part 1
    Column #68, December 2000 by Jon Williams:

    PDF file:
    http://www.parallax.com/dl/docs/cols...2/col/nv68.pdf

    Basic stamp code:
    http://www.parallax.com/html_pages/d...wnloads_V2.asp


    Best regards,

    Luciano


    * * *

    In general using the calculator:


    Convert -15 Celsius to Fahrenheit:

    -15 * 9/5 = -135/5 = -27
    -27 + 32 = 5 F


    Convert -10 Celsius to Fahrenheit:

    -10 * 9/5 = -90/5 = -18
    -18 + 32 = 14F

    Convert -273 Celsius to Fahrenheit:

    -273 * 9/5 = -2457/5 = -491.4
    -491.4 + 32 = -459.4F

  5. #5
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default

    The following Maxim appnote shows some c-code to convert from the sign-extended two's complemented value:
    Code:
    temp_msb = get[1]; // Sign byte + lsbit
    temp_lsb = get[0]; // Temp data plus lsb
    if (temp_msb <= 0x80){temp_lsb = (temp_lsb/2);} // shift to get whole degree
    temp_msb = temp_msb & 0x80; // mask all but the sign bit
    if (temp_msb >= 0x80) {temp_lsb = (~temp_lsb)+1;} // twos complement
    if (temp_msb >= 0x80) {temp_lsb = (temp_lsb/2);}// shift to get whole degree
    if (temp_msb >= 0x80) {temp_lsb = ((-1)*temp_lsb);} // add sign bit
    temp_c = temp_lsb; // ready for conversion to Fahrenheit
    temp_f = (((int)temp_c)* 9)/5 + 32;
    You may want to check this as well, http://www.rentron.com/PicBasic/one-wire2.htm, it's for PBP.
    Last edited by languer; - 20th January 2005 at 05:21.

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


    Did you find this post helpful? Yes | No

    Post

    Since the DS1802 gives you temperature in half degrees, it would be easier to use the formula F = (C*9/10)+32. For negative numbers you need to convert the negative temperature to positive and reverse the formula F = 32-((ABS C)*9/10). Spotting negative numbers is easily done by looking at the highest bit. If it's set the number is negative.

    TempC VAR WORD
    TempF VAR WORD

    IF TempC.15 THEN
    TempF = 32-((ABS TempC)*9/10)
    ELSE
    TempF = (TempC*9/10)+32
    ENDIF


    You could use the ** operator to save the division. This operator makes a multiplication and an "invisible" division by 65536. 0.9 * 65536 = 58982.4, since we can only use integers we round that to 58983. Our calculation will look like......

    IF TempC.15 THEN
    TempF = 32-((ABS TempC)**58983)
    ELSE
    TempF = (TempC**58983)+32
    ENDIF

    /Ingvar

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


    Did you find this post helpful? Yes | No

    Default

    For the EASIEST way to convert temperatures.

    Try my Temperature Conversion routines for PBP.

    It's an INCLUDE file that converts between Celsius, Fahrenheit and Kelvin.

    HTH,
    &nbsp;&nbsp;&nbsp;Darrel

Similar Threads

  1. A/D conversion with PIC18F67J50
    By ScaleRobotics in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th May 2009, 01:48
  2. A/D conversion problem in 18F2520, 2523, 2550 etc.
    By selimkara in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 10th March 2008, 16:26
  3. Multiple if then optimization
    By Mugelpower in forum mel PIC BASIC Pro
    Replies: 35
    Last Post: - 5th March 2008, 12:15
  4. A/D conversion problem on 18F4431
    By ttease in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th April 2007, 23:03
  5. Strange A/D conversion...
    By Christos_K in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th June 2005, 01:35

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