A to D on the PIC16F687


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    May 2006
    Posts
    36

    Default A to D on the PIC16F687

    Hello, I am trying to just make a demo program so I can view the Ato D value on my LCD. I am not having good luck. I looked at another post that discussed problems with A to D on the 16F876. I was using this as a guide but something is not right.

    I am trying to read a pot on RA4/AN3. I am reading a value but as I adjust my pot the numbers do not count up in a linear fashion. It will count from 0 to 1 to 2 for 7/8 of the single turn pot and then the numbers jump to 255 very quickly. This is linear pot and I should be gettng a linear increase or decrease.

    Here is my code and the settings that I believe are relavant. The code is supposed to read the voltage from the pot and update a LCD to tell me the output. Thanks for any help.
    ----------------------------------------
    AD_AN3 VAR PORTA.4
    AD_AN3_VALUE VAR byte
    DEFINE ADC_BITS 8 ' set number of bits in result
    DEFINE ADC_CLOCK 3 ' set clock source
    DEFINE ADC_SAMPLEUS 50 ' set sampling time for microseconds

    MENU:
    LCDOUT $FE, 1 ' Clear LCD screen
    A_D_AN3:
    ADCON0 = %00001101
    PAUSEUS 50 ' wait 50 microsec
    ' ADCON0.2 = 1 'start conversion(bit 2= 1)
    ' WHILE ADCON0.2 = 1
    ' WEND ' wait for conversion
    ADCIN 3, AD_AN3_VALUE ' read channel 3 to AD_AN3_VALUE
    LCDOUT $FE, 1 ' Clear LCD screen
    LCDOUT "A to D Value"
    LCDOUT $FE, $C0, #AD_AN3_VALUE
    PAUSe 100
    BUTTON FUNCTION_BUTTON, 1, 255, 0, C2, 1, ON_TIME_FUNC
    GOTO A_D_AN3

  2. #2
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    where to begin. First you need to make port a an input. Second there are two AD registers 1 and 2. Third I would put the register setup before your main program. you only need to do it once. Try this.

    DEFINE ADC_BITS 8 ' set number of bits in result
    DEFINE ADC_CLOCK 3 ' set clock source
    DEFINE ADC_SAMPLEUS 50 ' set sampling time for microseconds
    ADCON0 = %10000000 'I think this formats the result
    ADCON1 = %00000000 'I think this defines if ports are A or D. The first 0 may need to be a 1
    trisa = %11111111 'Port A is input
    AD_AN3 VAR PORTA.4
    AD_AN3_VALUE VAR byte

    pause 500 'LCD's need to initialise before sending data
    LCDOUT $FE, 1 ' Clear LCD screen

    A_D_AN3:
    PAUSEUS 50 ' wait 50 microsec
    ADCIN 3, AD_AN3_VALUE ' read channel 3 to AD_AN3_VALUE
    LCDOUT $FE, 1 ' Clear LCD screen
    LCDOUT "A to D Value"
    LCDOUT $FE, $C0, #AD_AN3_VALUE
    PAUSe 100
    BUTTON FUNCTION_BUTTON, 1, 255, 0, C2, 1, ON_TIME_FUNC
    GOTO A_D_AN3

    If it still doesn't work its all about setting the bits in ADCON0 and 1 correctly. See the datasheet

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


    Did you find this post helpful? Yes | No

    Default

    be sure your pot impedance meet the maximum impedance stated in the datasheet too.

    ADCON1.7=1 ' Right justified results..

    Also depending of your OSC speed, you may need to increase your sampling time, and you may want to add a tiny 0.01-0.1 uF capacitor at the PIC input to smooth the results.
    Last edited by mister_e; - 1st July 2006 at 09:33.
    Steve

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

  4. #4
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    I just realized a mistake I told you. You may want to move your LCD off of port A since you are using that for your A to D. I don't know if it will work after making in input and analog. The book tells how to change it.

  5. #5
    Join Date
    Jun 2005
    Location
    Mumbai,India
    Posts
    43


    Did you find this post helpful? Yes | No

    Exclamation Is it ADC or POT

    Hi,
    have U checked with multimeter that U are getting proper voltage on
    wiper of POT? Unless that is confirmed then no use of banging head like
    Mister_E on program and PIC. ;-)
    It is possible that pot has mechanical problems at ground end and
    after wiper moves few turns the ground end opens and U get full voltage
    on wiper!!
    As U are getting upto say number 10 then this is a probability.

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default Missing ...

    4 Errors found before compiling : ( between star lines ...)



    DEFINE ADC_BITS 8 ' set number of bits in result
    DEFINE ADC_CLOCK 3 ' set clock source
    DEFINE ADC_SAMPLEUS 50 ' set sampling time for microseconds
    ADCON0 = %10000000 'I think this formats the result
    ADCON1 = %00000000 'I think this defines if ports are A or D. The first 0 may need to be a 1
    trisa = %11111111 'Port A is input
    AD_AN3 VAR PORTA.4
    '************************************************* *********************
    FUNCTION_BUTTON VAR PORTB.X ' TO BE DECLARED AS YOUR REQUIREMENTS !!!
    '************************************************* ********************
    AD_AN3_VALUE VAR byte
    '************************************************* *********************
    C2 VAR Byte 'TO BE DECLARED "
    '************************************************* *********************
    pause 500 'LCD's need to initialise before sending data
    LCDOUT $FE, 1 ' Clear LCD screen

    A_D_AN3:
    PAUSEUS 50 ' wait 50 microsec
    ADCIN 3, AD_AN3_VALUE ' read channel 3 to AD_AN3_VALUE
    LCDOUT $FE, 1 ' Clear LCD screen
    LCDOUT "A to D Value"
    LCDOUT $FE, $C0, #AD_AN3_VALUE
    PAUSe 100
    '************************************************* ********************
    C2 = 0 ' TO BE INITIALISED HERE !!!
    '************************************************* ********************
    BUTTON FUNCTION_BUTTON, 1, 255, 0, C2, 1, ON_TIME_FUNC
    GOTO A_D_AN3

    '************************************************* ********************
    ON_TIME_FUNC: ' TO BE SOMEWHERE to be called !!!

    ' What is it supposed to do ???????????????????
    '************************************************* *********************

    END




    NOW still hardware mix ... We continue : your LCD is "standard connected" ... so you MUST pass its RS pin to PORTB ( see defines list) to get RA4 free, as you want it an analog input. You also must declare ALL other PORTA pins as DIGITAL ( see ADCON bytes)

    Sorry, I did not verify if defaults LCD connexions are the same for F687 or F84 ... but there's no reason for a difference in the pin names !!!


    Now ... we will know if real ADC problems still exist ...

    Alain
    Last edited by Acetronics2; - 2nd July 2006 at 16:46.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. LCD with PIC16F687
    By jblackann in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th June 2006, 23:16
  2. PIC16F687 - supported?
    By badcock in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th August 2005, 10:33

Members who have read this thread : 0

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