PDA

View Full Version : adc temperature sensor



rastan
- 24th January 2005, 01:42
hi!
ive been trying to get my adc working with my LM35CZ on a 16f677A and got a ref voltage of 2.55V on porta.3


heres the code

' PicBasic Pro program to display result of
' 10-bit A/D conversion on LCD
'
' Connect analog input to channel-0 (RA0)

' Define LCD registers and bits
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1

' Define ADCIN parameters
DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

adval VAR WORD ' Create adval to store result
x VAR BYTE
temp VAR WORD
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000011 ' Set PORTA analog and right justify result
Low PORTE.2 ' LCD R/W line low (W)

Pause 500 ' Wait .5 second

loop:
adval = 0
For x = 1 TO 100
ADCIN 0, temp ' Read channel 0 to adval
adval = adval + temp
Next x
adval = adval/100
LCDOut $fe, 1 ' Clear LCD
LCDOut "Value: ", DEC adval ' Display the decimal value

Pause 100 ' Wait .1 second

GoTo loop ' Do it forever
End

dibt worry about any gramatical mistakes because it does work, but every time the temp sensor goes through a sundden change or i put my hand near any wires the lcd outputs a load of black squares. sometimes it corrects itself and other times it doesnt.
sonetimes to get it working again i need to reset the power.

any ideas?????

cheers,
phil

mister_e
- 24th January 2005, 02:04
PIC16F677a ???? i'm i stupid or it's a real new one PIC that it not listed on the MicroChip website ???

Bruce
- 24th January 2005, 05:08
I'm guessing you meant 16F877A?

Try placing a 0.1uF cap between Vcc & GND, and locate the cap as close to the PIC as possible. That will probably fix it.

The A series are very sensitive to ESD, EMI, etc, and if you don't have a 0.1uF cap strapped across Vcc & GND close to the PIC you'll see this happen. Just getting near one or touching wires will make it go like splat.

If the cap doesn't cure it, look for bad or lose connections. Code looks OK.

rastan
- 24th January 2005, 11:09
yeah sorry was the 16f877A, typo.

bruce's filter capacitor trick has definatly helped, but not entirely got rid of the problem. it still shows black and sometimes gets 'stuck' every now and again. is there some other way of defeating this problem??

is there some way to recognise that it has gone wrong and for the pic to 'reset' itself??

thanks guys

Bruce
- 24th January 2005, 15:04
You can check for reset by inserting a sign-on message above your main program loop.

LCDOut $FE,1,"Start"
PAUSE 2000 ' Display sign-on message to 2S

Loop:
Main program starts here

You only see the Start message on power-up or hardware reset.

If you keep seeing the sign-on message, you know there's a problem.

Disconnect RE2 from your LCD W pin, and connect the LCD's W pin directly to ground. Comment out this line (Low PORTE.2)

Now comment out the A/D part, and just loop through displaying messages on the LCD. Does it work?

rastan
- 24th January 2005, 18:57
ok thanks bruce,
i tried the things you said, namely taking the a/d lines out, and the lcd was just as random. sometimes it would work.

strangly i changed the a/d bits from 8 to 10 as it is supposed to be (according to the datasheet) and then put the right justified bit on. and it worked very well!

i can now put my hands on the wires and the chip doesnt crash.

even more strangly though, when i put my probes on to measure the voltage on the temp sensor, it does the same thing. stops, gets stuck and then a second or so after i have take the probes off, it restarts. very odd.

it was a good idea to put that start text at the start because it turns out that all this time it was not 'stopping' it was continueously resetting. i changed from a crappy power supply to a battery, that made it even better.

now the only thing that stops it is the probs as i mentioned above. altohugh if i leave it running for an extended period of time, it eventually gets stuck.

one more thing, when i turn it on, the lcd screen sometimes just stays as black squares and other times it is fine. why is this? it seems random.

i can remember a while back, when i started using lcd screens they worked great, but recently as ive been playing with serial comms ive had all sorts of problems, with everything. it almost seems like a timing issue, because its happened so many times, and ive never sorted it.

sorry about this being so long

thanks


' PicBasic Pro program to display result of
' 10-bit A/D conversion on LCD
'
' Connect analog input to channel-0 (RA0)

' Define LCD registers and bits
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1

' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

adval VAR WORD ' Create adval to store result
x VAR BYTE
temp VAR WORD
TRISA = %11111111 ' Set PORTA to all input
'TRISD = 0
ADCON1 = %10000011 ' Set PORTA analog and right justify result
Low PORTE.2 ' LCD R/W line low (W)

Pause 500
LCDOut $FE,1,"Start"
Pause 1000 ' Display sign-on message to 2S ' Wait .5 second

loop:
'adval = 0
'For x = 1 TO 100
ADCIN 0, adval ' Read channel 0 to adval
'adval = adval + temp
'Next x
'adval = adval/100
LCDOut $fe, 1 ' Clear LCD
LCDOut "Value: ", DEC adval ' Display the decimal value
'PORTD.1 = 1
Pause 100
'PORTD.1 = 0
Pause 100
GoTo loop ' Do it forever
End

Bruce
- 24th January 2005, 19:47
LCD's vary like the wind, but for the majority, you should insert a small delay time at power-up to allow the LCD to stabilize.

PAUSE 1000 ' in the beginning normally does the trick

Depending on your contrast settings, during this delay period, you'll probably see black squares. That's normal until you initialize the LCD.

You may also want to experiment with command & data delay times by adding the following to your LCD define section.

DEFINE LCD_COMMANDUS 2000 ' Command delay time in uS
DEFINE LCD_DATAUS 50 ' Data delay time in uS

Just tinker with the delay periods until you get the best results with whatever LCD you're using.

As for the hangs, I notice you have all PortA pins set to inputs. Do you have all of these inputs conected to something?

Floating inputs may be causing problems as well.

rastan
- 24th January 2005, 23:42
ok found it, possibly both or one.
firstly no cmcon = 7 so some of the comparators were turned on
and secondly, i forgot to connect mclr to 5V so it was just floating.

works like a dream.

thanks to bruce and everyone else that replied. youll probably be hearing from me again quite soon :D

heres the final code for anyone else stuck with temperature reading.

Thanks

' PicBasic Pro program to display result of a LM35CZ temp
' sensor with a 10-bit A/D conversion on LCD
'
' Connect analog input to channel-0 (RA0)

' Define LCD registers and bits
Pause 1000 ' to let LCD warm up
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
DEFINE LCD_COMMANDUS 2000 ' 2000 Command delay time in uS
DEFINE LCD_DATAUS 50 '50 Data delay time in uS

' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

adval VAR WORD ' Create adval to store result
x VAR WORD
temp VAR WORD
decimal VAR WORD
cmcon = 7
TRISA = %0001001 ' Set PORTA to all input
'TRISD = 0
ADCON1 = %10000101 ' Set PORTA analog and right justify result
Low PORTE.2 ' LCD R/W line low (W)

Pause 500
LCDOut $FE,1,"Start"
Pause 1000 ' Display sign-on message to 2S ' Wait .5 second

loop:
adval = 0
For x = 1 TO 100
ADCIN 0, temp ' Read channel 0 to adval
adval = adval + temp
Next x
adval = adval/100
decimal = adval DIG 0
adval = adval/10
LCDOut $fe, 1 ' Clear LCD
LCDOut "Value: ", DEC adval,".",DEC decimal,"deg" ' Display the decimal value
'PORTD.1 = 1
Pause 100
'PORTD.1 = 0
GoTo loop ' Do it forever
End

Bruce
- 24th January 2005, 23:53
I would bet it was just the floating /MCLR pin. The default CMCON register value is %00000111 at power-up on the 16F87xA series.

PoTeToJB
- 5th May 2006, 12:08
hello
i do same project like u
can u share your schematic diagram..

i hope u can help me

rastan
- 5th May 2006, 19:10
Wow, ive come a long way since then and I had even forgot that I ever did this!

As for the circuit, I think its best if you try and figure it out on your own. If you are not comfotable about designing a circuit for this then you are obviously not ready to try some thing 'this' complicated.

Why not try a flashing light excersise first? And then maybe a A/D conversion from a potentiometer. And then get an LCD going. They are usually the best ways to go. From there It is possible to simply replace the Potentiometer for the LM35Z and vuala.

Phil

bbarney
- 6th May 2006, 14:15
try here for starters
http://www.rentron.com/PicBasic/LM34.htm

PoTeToJB
- 8th May 2006, 12:21
thank for help.
i success build this project.

very very thank for all and rastan...
thank for your code