PDA

View Full Version : Temp sensor problem



Christopher4187
- 14th June 2006, 01:43
OK, I certainly don't understand what's going on. I am using a LM34CZ (if someone has a more precise temp sensor please let me know) and I am simply trying to read the voltage from it. It wasn't working correctly so I put a potentiometer on the analog port instead of it and that doesn't work either.

Below is the code.......someone please tell me what I am doing wrong. This code was actually taken from Bruce at Rentron.com. It is erratic but also the difference is +13 volts at some points. I have 2.55 on porta.3 and the input from the pot is on porta.1. I am using a 16F876.



DEFINE osc 20 ' We're using a 20 MHz oscillator
DEFINE ADC_BITS 8 ' Set A/D for 8-bit operation
DEFINE ADC_CLOCK 1 ' Set A/D clock Fosc/8
DEFINE ADC_SAMPLEUS 50 ' Set A/D sampling time @ 50 uS
samples VAR WORD ' Multiple A/D sample accumulator
sample VAR BYTE ' Holds number of samples to take
temp VAR BYTE ' Temperature storage
samples = 0 ' Clear samples accumulator on power-up

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000011 ' Set PORTA.0,1,2,5 = A/D, PortA.3 = +Vref
PAUSE 500 ' Wait .5 second

loop:
FOR sample = 1 TO 20 ' Take 20 samples
ADCIN 1, temp ' Read channel 0 into temp variable
samples = samples + temp ' Accumulate 20 samples
PAUSE 250 ' Wait approximately 1/4 seconds per loop
NEXT sample
temp = samples/20
DEBUG "Temperature is: ",DEC temp," Deg F",10,13
samples = 0 ' Clear old sample accumulator
GOTO loop ' Do it forever

END

Bruce
- 14th June 2006, 04:34
Minimum Tad is 1.6uS.

If you're using a 20MHz osc, then you'll want to change the A/D conversion
clock setting. Change DEFINE ADC_CLOCK 1 to DEFINE ADC_CLOCK 2.

My example uses a 4MHz osc, so DEFINE ADC_CLOCK 1 works. This selects
8Tosc which is fine up to 5MHz. 8/5MHz = 1.6uS which is right at the cut
off point of 1.6uS min Tad.

Above 5MHz you want 32Tosc. Anything below this, and your A/D clock is
too fast. For example: 8/20MHz = 400nS = 4 times too fast for min Tad.

Also make sure you're never applying > Vref to any A/D input. If you have
+Vref set to 2.55V, you should never exceed 2.55V input on any A/D pin.

Christopher4187
- 14th June 2006, 14:09
Thanks for the reply. It is a lot better but I measure the voltage into porta.3 as .67 but the display says .72. I have checked it up and down and it's linear but the PIC isn't displaying the correct voltage. Is there any better method to measuring an analog voltage? I was thinking of using an ADC0831 but I wanted to stay away from adding more chips. Maybe a current loop temp senor? In my project, I wanted to have data like 72.3, 72.4 instead of just 72 or 73. Are there any sensors that will display three digits to have better clarity?

Bruce
- 14th June 2006, 14:49
See this thread http://www.picbasic.co.uk/forum/showthread.php?t=3533