'device 18f4550 'PIC Utilizado
Define CLOCK_FREQUENCY = 4 'Oscilador de 4MHZ
Hseropen 4800
'===== ALIAS DEFINITIONS =======================================================
Symbol thermistor = PORTA.0 'Thermistor Divider Circuit input
Symbol testled = PORTB.0 'LED Used for various purposes
'===============================================================================
'single_DISPLAY_TYPE = FAST 'More accurate singleing point to decimal routine
'===== VARIABLE DEFINITIONS ====================================================
Dim i As Word 'Create a generic Word size variable
Dim thermistorinput_adval As Word 'Variable to store the thermistor A to D value (0-1023)
Dim steinhart_a As Single 'Thermistor Constant Values
Dim steinhart_b As Single 'Thermistor Constant Values
Dim steinhart_c As Single 'Thermistor Constant Values
Dim x0 As Single 'General Purpose variables used for for storage during calculations
Dim x1 As Single 'General Purpose variables used for for storage during calculations
Dim x2 As Single 'General Purpose variables used for for storage during calculations
Dim x3 As Single 'General Purpose variables used for for storage during calculations
Dim x4 As Single 'General Purpose variables used for for storage during calculations
Dim x5 As Single 'General Purpose variables used for for storage during calculations
Dim mv As Single 'Resolution * Adval
Dim mvolts As Single
Dim temporary As Single 'Temporary variable for the thermistor linearisation
Dim resistance As Single 'Holds the resistance of the thermistor for a given temperature
Dim lnresistance As Single 'Holds the "ln(Resistance)" value
Dim lnresistancecubed As Single 'Holds the "ln(Resistance)³" value
Dim kelvin As Single 'Holds the singleing point temperature in degrees Kelvin
Dim fahrenheit As Single 'Holds the singleing point temperature in degrees Fahrenheit
Dim celsius As Single 'Holds the singleing point temperature in degrees Celsius
'===============================================================================
Dim resolution As Single
'===== CONSTANTS DEFINITIONS ===================================================
'Steinhart-Hart Algorithm Constants
'Constant values used for the Steinhart-Hart thermistor linearisation routine
'T = 1/ ( a + b(lnR) + c(lnR)³)
steinhart_a = 0.00112549 '8166 'Thermistor's a-value: From Data Sheet
steinhart_b = 0.00023467 '71694 'Thermistor's b-value: From Data Sheet
steinhart_c = 0.00000008 '579674698 'Thermistor's c-value: From Data Sheet
Const vref = 3300 'Constant used for Vref
'define resolution = vref / 1024 'Quatasising constant for ADC reading to Voltage calculation
Const resistor_value = 16900 'Value of Resistor in Resistor/Thermistor voltage divider circuit
'===============================================================================
'===== Main Loop ==============================================================
gettemp:
Gosub getadval
Gosub convertadvaltotemp
Gosub displaytemp
WaitMs 5
Goto gettemp 'return
End
'===============================================================================
'define resolution = vref / 1024 'Quatasising constant for ADC reading to Voltage calculation
_resolution:
resolution = vref / 1024
Return
'===== SUBROUTINE ==============================================================
getadval:
'Read the value from the Analogue to Digital Converter AN0
Adcin 0, thermistorinput_adval '= adin 0
Return
'===============================================================================
'===== SUBROUTINE ==============================================================
convertadvaltotemp: '(Donde está el Return de esta subrutina?..)
'Start Steinhart-Hart Linerization Algorithm
'FORMULA relating Thermistor Resistance to Temperature:
'[ T = 1/ ( a + b(lnR) + c(lnR)³) ]
'Where:
'T = Temp in Kelvin
'lnR = Natural Log of Resistance
'a, b & c, are Thermistor constants from data sheet
Gosub _resolution
mv = resolution * thermistorinput_adval
temporary = vref - mv
x0 = resolution * thermistorinput_adval
x1 = x0 * resistor_value
resistance = x1 / temporary
'Return?..
'start Log calculation here
calculate_lnresistance: '...Command not availible for 16F PICs
lnresistance = Log resistance 'Deduce the Natural Logarithm a value
'Continue Steinhart-Hart Algorithm
steinhart_hart_algorithm:
x2 = lnresistance * lnresistance
lnresistancecubed = lnresistance * x2
x3 = steinhart_b * lnresistance
x4 = steinhart_c * lnresistancecubed
x5 = (steinhart_a + x3 + x4)
kelvin = 1 / x5
celsius = kelvin - 273.15
fahrenheit = kelvin * 1.001 - 459.67
'End of steinhart - hart algorithm
Return
'===============================================================================
'===== SUBROUTINE ==============================================================
displaytemp: 'Transmit the result serially
Hserout "Result (In Fahrenheit) = ", #fahrenheit, 13, 10 '13=CR & 10=LF
WaitMs 1 '00
Hserout "Result (In Celsius) = ", #celsius, 13, 10 '13=CR & 10=LF
WaitMs 1 '00
Hserout "Result (In Kelvin) = ", #kelvin, 13, 10, 13, 10 '13=CR & 10=LF
WaitMs 1 '00
Return