adc temperature sensor


Closed Thread
Results 1 to 13 of 13
  1. #1
    rastan's Avatar
    rastan Guest

    Question adc temperature sensor

    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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    PIC16F677a ???? i'm i stupid or it's a real new one PIC that it not listed on the MicroChip website ???
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    rastan's Avatar
    rastan Guest


    Did you find this post helpful? Yes | No

    Smile

    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

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    rastan's Avatar
    rastan Guest


    Did you find this post helpful? Yes | No

    Unhappy

    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

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    rastan's Avatar
    rastan Guest


    Did you find this post helpful? Yes | No

    Red face found the problem

    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

    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

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    I would bet it was just the floating /MCLR pin. The default CMCON register value is %00000111 at power-up on the 16F87xA series.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  10. #10
    Join Date
    Feb 2006
    Location
    johor,Malaysia
    Posts
    57


    Did you find this post helpful? Yes | No

    Default

    hello
    i do same project like u
    can u share your schematic diagram..

    i hope u can help me

  11. #11
    rastan's Avatar
    rastan Guest


    Did you find this post helpful? Yes | No

    Default

    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

  12. #12


    Did you find this post helpful? Yes | No

  13. #13
    Join Date
    Feb 2006
    Location
    johor,Malaysia
    Posts
    57


    Did you find this post helpful? Yes | No

    Default

    thank for help.
    i success build this project.

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

Similar Threads

  1. Need a cheap touch sensor idea.. here it is
    By mister_e in forum Code Examples
    Replies: 20
    Last Post: - 16th April 2016, 22:42
  2. Precision Temperature Sensor
    By Normnet in forum Off Topic
    Replies: 1
    Last Post: - 8th March 2010, 14:48
  3. 18B20 temperature sensor and PIC
    By boban in forum Schematics
    Replies: 2
    Last Post: - 4th June 2007, 13:51
  4. Precise temperature sensor
    By Christopher4187 in forum Off Topic
    Replies: 0
    Last Post: - 7th June 2006, 18:39
  5. read temperature sensor
    By breeno in forum General
    Replies: 5
    Last Post: - 10th January 2005, 06:04

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts