Celcius to Fahrenheit

Status
Not open for further replies.

AtomSoft

Well-Known Member
Ok while this is simple...

Code:
                    fahrenheitT = centigradeT;
                    fahrenheitT *= 9.0;
                    fahrenheitT /= 5.0;
                    fahrenheitT += 32;

What if the centigrade value is supposed to be NEGATIVE?

So if my "centigradeT" is 5 and my NEG variable is 1

Then i show -5 on LCD

This works well... but how would i show this in fahrenheit? which is 23F

How would i code this?

NOTE:
The data is 14bits long (13 really) but NEG is 14th bit
Im using DOUBLES for my conversions... is that ok?
The PIC is a PIC18F27J53
Im using C18
 
Last edited:
Ok when i get a number like:

+1 = 0000 0000 0000 0011
-1 = 1111 1111 1111 1100

Bit 31 tells us if the number is NEGATIVE
If so I usually just FLIP the bits and save the NEG bit in a variable.

Now i have 2 parts of data

NEG and DATA

NEG = DATA[31]
DATA = DATA >> 1
When i CONVERT DATA to FAHRENHEIT its 33.8F

When in fact -1 = 30.2F

Do you understand my dilemma?
 
Ok when i get a number like:

+1 = 0000 0000 0000 0011
-1 = 1111 1111 1111 1100

Bit 31 tells us if the number is NEGATIVE
If so I usually just FLIP the bits and save the NEG bit in a variable.

Now i have 2 parts of data NEG and DATA

Why not do whatever (signed) math you want and then use the absolute value of the result. You can still capture a 'negative' flag too. Here's an example (using BoostC) that I use for converting the raw Celsius*16 value from a DS18B20 to a Fahrenheit*10 value for display on a 4 digit LED display;

Code:
    /*                                                              *
     *  the 16C term in the formulas below is the raw Celsius * 16  *
     *  temperature data in the 'rawtemp16' var'.  a rounded value  *
     *  of °C*10 or °F*10 is used for display with a decimal point  *
     *  inserted manually before the last (tenths) digit.           *
     *                                                              *
     *  10F = 18C + 320 = 16C + 2C + 320 = 16C + (16C+4)/8 + 320    *
     *                                                              */
         bin = rawtemp16 + 4;   // rounding
         bin /= 8;              // now bin = '2C' (rounded)
         bin += rawtemp16;      // now bin = 16C + 2C
         bin += 320;            // now bin = 18C + 320 = 10F
         if(negflag = bin.15)   // set/clr "negflag", if negative
           bin = -bin;          // twos complement the temperature
Code:
;
;    10F = 18C + 320 = 16C + 2C + 320 = 16C + (16C+4)/8 + 320
;
;                  rawtemp Neg  Output  Display
;     =========================================
;     125.0000°C   h'07D0'  0   002570  257.0°F 
;     100.0000°C   h'0640'  0   002120  212.0°F
;      77.0000°C   h'04D0'  0   001706  170.6°F
;      25.0000°C   h'0190'  0   000770   77.0°F
;      22.0000°C   h'0160'  0   000716   71.6°F
;       0.5000°C   h'0008'  0   000329   32.9°F
;       0.0625°C   h'0001'  0   000321   32.1°F
;       0.0000°C   h'0000'  0   000320   32.0°F
;     - 0.0625°C   h'FFFF'  0   000319   31.9°F
;     - 0.1250°C   h'FFFE'  0   000318   31.8°F
;     - 5.0000°C   h'FFB0'  0   000230   23.0°F
;     -10.0000°C   h'FF60'  0   000140   14.0°F
;     -17.0625°C   h'FEEF'  0   000013    1.3°F
;     -17.6875°C   h'FEE5'  0   000002    0.2°F
;     -17.7500°C   h'FEE4'  0   000001    0.1°F
;     -17.8125°C   h'FEE3'  1   000001  - 0.1°F
;     -25.0000°C   h'FE70'  1   000130  -13.0°F
;     -55.0000°C   h'FC90'  1   000670  -67.0°F
;
 
Last edited:
Just let the compiler worry about negative and positive numbers and how to add and mutliply them, and concentrate on the acutal computation. The compiler will handle the operations correctly, as long as you´re using floating point numbers or singed integer numbers.
Edit: obviously you can only do integer calculations with integer numbers..
 
Last edited:
So simple yet effective Thanks!(MIKE)

Thanks to others as well.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…