This is an INCLUDE file that makes it EASY to convert between Celsius, Fahrenheit and Kelvin temperature scales. It's compatible with both MPASM and PM, and is not processor dependant, so it should work on ANY chip that's supported by Picbasic Pro..
Functions: Input Range
* CtoF - Celsius to Fahrenheit -273.0 to +1802.6 * FtoC - Fahrenheit to Celsius -459.0 to +3276.7 * CtoK - Celsius to Kelvin -273.0 to +3000.0 * KtoC - Kelvin to Celsius 0.0 to +3276.7 * FtoK - Fahrenheit to Kelvin -459.0 to +3276.7 * KtoF - Kelvin to Fahrenheit 0.0 to +3276.7
All functions accept a Signed Word variable for the input, it MUST be a variable, and will NOT work with constants. The output is also a Signed Word variable. Resolution is 0.1 deg. In order for the numbers to fit in a word variable, it's multiplied *10
25.0 deg C is stored as 250, -10 is stored as -100
Here's a simple example that converts 25.0 °C to Fahrenheit.
INCLUDE "Temp_Convert.pbp" C VAR WORD ; Celsius F VAR WORD ; Fahrenheit K VAR WORD ; Kelvin C = 250 ; Set to 25.0 deg. C @ CtoF _C, _F ; Convert Celsius to Fahrenheit
F now contains 770, representing 77.0 °F Notice the @ symbol at the beginning of the line, and the Underscore before the variable names.
To convert the previous result (77.0 °F) to Kelvin is just as easy.
@ FtoK _F, _K ; Convert Fahrenheit to Kelvin
K now contains 2980, representing 298.0 °K
And of course, you'll need to be able to display the results. Here's an example for use with an LCD display.
X VAR WORD X = F ; Copy F to X for ShowTemp GOSUB ShowTemp : LCDOUT $DF,"F" ; $DF is deg. symbol X = C GOSUB ShowTemp : LCDOUT " ",$DF,"C" STOP ' --- Display a Signed Word with 1 decimal place --- ShowTemp: if X.15 = 1 then LCDOUT "-" X = ABS(X) LCDOUT dec (X/10),".",dec X//10 Return
For a more complete example program, see the Test_Temp_Convert.pbp file in the .Zip located below.