How to calculate negative degrees Celcius from Fahrenheit?

nerdegutta

Senior Member
Hi.

I got this:

Code:
Temp = ADCval * 4
Temp = ADCval * 8 / 10 + Temp
Temp = ADCval * 8 / 100 + Temp

Temp_F = Temp  / 10
To take an ADC reading from a LM34, and make it Fahrenheit.

To convert it to Celsius I use this:

Code:
Temp_C = Temp_F - 32 
Temp_C = Temp_C * 56 / 100
But this only work above zero degrees Celsius. When it's getting below zero, it overflows or something. How to I calculate the minus degrees?

Temp, Temp_F and Temp_C are all word variables.


- nerdegutta
 

westaust55

Moderator
Not sure why a search would not shoiw that thread.

I had the advantage of knowing marks had done some work in that area (memory had not entirely failed yet - even if my fiendishly flailing fingers generate lots of typos ;) )
Using Fahrenheit as the seach term it was about 15 down on a forum search.
 

womai

Senior Member
Or modify your code to treat positive and negative Celsius results separately:

Code:
if Temp_F >= 32 then
   Temp_C = Temp_F - 32
   Sign = " "
else
   Temp_C = 32 - Temp_F
    Sign = "-"
endif

Temp_C = Temp_C * 56 / 100
where Sign is a byte variable
 

womai

Senior Member
Of course all that breaks down when the temperature is negative even when expressed in Fahrenheit.. (the LM34 seems to be capable of operation down to -50 degC). You'd need to somehow encode this (sign flag, sign bit) and then treat the different cases separately, but you can do all this similarly to my code above.
 

marks

Senior Member
Hi nerdegutta,
I guess converting from Fahrenheit to Celsius would be handy for some hope you give this a try
C=F+76*5/9-60
should do the range you require ,using 5/9 is slightly more accurate then 56/100.
I guess others may want Celsius to Fahrenheit just reverse it.
F=C+60*9/5-76
heres some example code you can simulate
Code:
SYMBOL Temperature  = W0  SYMBOL TempMsb  = b1  SYMBOL TempLsb = b0
SYMBOL Sign         = b2
SYMBOL D0           = b3
SYMBOL D1           = b4 
SYMBOL D2           = b5 
SYMBOL D3           = b6 
SYMBOL D4           = b7

Main:
  W0 = -670    'example -67.0 F
' W0 = 3000    'example 300.0 F
 
       
  Convert:                                      
       Sign = " "                                                  '  Display + 
  Temperature = Temperature  +760 *5 /9 -600                       '  Fahrenheit to Celsius
     IF TempMsb > 127 THEN                       
       Sign = "-"                                                  '  Display - 
       Temperature = - Temperature                                     
     ENDIF 
    
     BINTOASCII Temperature,D4,D4,D3,D2,D1  
      IF  D4 = "0" THEN : D4 = " "                                 ' leading zero blanking 
      IF D3 = "0" THEN : D3 = " " : ENDIF                          ' leading zero blanking 
      ENDIF                          
                          
 SERTXD (CR, LF, "Temperature ",Sign,D4,D3,D2,".",D1," degrees Celsius", CR, LF) '  (0.1)resolutuion
   
GOTO Main
 
Top