16F LM35DZ Example
The
LM35DZ
is an accurate temperature sensor that will provide an
analogue output of 10mV per degree C within 0.01% Its not hard to
interface one with a PIC, and the following example allows for a
temperature range of -55 to 150 deg C.
The
datasheet shows many ways to use the
LM35DZ, but I'm using the
example on page 7 with the -55 to +150 degree C application. It
requires 3 additional components, 2 *
1N914 (Or 1N4148), and 1 * 18K
resistor.
Here's
the circuit

Note the PIC's power supply/oscillator are not shown
ISIS doesn't have a model for the
LM35DZ, so I put a
signal of 0-2.05V on the output of the
LM35DZ to simulate the
voltage swing. This is not how the device behaves in real life, and
rather has two output references that need to be summed to generate
the correct temperature value.
Now its a matter of sampling the voltage, and displaying it on the
screen, I also have a custom character made so that it displays the
degree symbol on the
LCD.
Device 16F877A
Declare XTAL 4
DECLARE ADIN_RES 10 ' 10-bit result required
DECLARE ADIN_TAD 8_FOSC ' Set the ADC's clock source
DECLARE ADIN_STIME 50 ' Allow 50us sample time
Declare LCD_TYPE 0 ' Type of LCD Used is Alpha
Declare LCD_DTPIN PORTB.4 ' The control bits B4,B5,B6,B7
Declare LCD_RSPIN PORTB.2 ' RS pin on B2
Declare LCD_ENPIN PORTB.3 ' E pin on B3
Declare LCD_INTERFACE 4 ' Interface method is 4 bit
Dim ADC_Result As Float
Dim ADC_Total As Float
Dim Temp_Float as Float
Dim ADC_Channel as Byte
Dim ADC_Loops as Word
Dim Temp as Word
Dim Last_Result1 As Float
Dim Last_Result2 As Float
ADCON1 = %10000000 ' Set all to analogue inputs (PORTA)
TRISA = $FF ' Declare porta as all inputs
Delayms 150
Cls
Print $FE,$40,$07,$05,$07,$00,$00,$00,$00,$00 ' Custom character for Degree
ADC_Loops = 200
Main:
ADC_Channel = 1 ' ADC on first reference
Gosub ADC_Average ' Perform an averaging to enhance accuracy
Temp_Float = ADC_Result ' Store the result
ADC_Channel = 0 ' ADC on second reference
Gosub ADC_Average ' Perform an averaging to enhance accuracy
' Convert values into Volts (with a scale of 1000)
ADC_Result = ADC_Result * 5000 / 1023
Temp_Float = Temp_Float * 5000 / 1023 ' to reduce decimal error
ADC_Result = ADC_Result - Temp_Float ' And calculate difference
ADC_Result = ADC_Result / 10 ' Scale back down remembering 10mV = 1 Deg C
If ADC_Result <> Last_Result1 Then ' Check if the data has changed
' and only update display if it has
Print At 1,1, Dec1 ADC_Result, 0, "C "
Last_Result1 = ADC_Result ' Store new data
Endif
Goto Main ' Loop for ever
ADC_Average: ' Perform an averaging on ADC conversions to reduce errors
ADC_Total = 0 ' Clear summing register
' Loop for a pre-determined number of times
For Temp = 1 To ADC_Loops
ADC_Result = ADIN ADC_Channel ' Grab a new ADC value
ADC_Total = ADC_Total + ADC_Result ' Sum it to the total register
Delayus 1 ' Allow internal capacitors to discharge
Next Temp
' Determin the average of all the equations
ADC_Result = ADC_Total / ADC_Loops
Return
Where you can get the components;

 | Site Tutorial Index |
|  | 16F PIC Examples |
| |  | Motors |
| |  | Thermometers |
| |  | Code Snippets |
|  | 18F PIC Examples |
|  | Handy Tips |