1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Adc Pic16f877a

Discussion in 'Microcontrollers' started by derrick826, Oct 5, 2007.

  1. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    hi guys,

    i have read the datasheet of the PIC16F877A from microchip regarding on the configuration of all the registers needed to program the ADC. i have used a potential resistor to vary different input voltages into my analogue pin RA0 starting from (0v - 4.80V) . my Vref+, voltage reference would be Vdd while Vref- would be Vss. The particular result which i get in ADRES is being displayed on 10 led lights to indicate that 10-bits are the result of the analogue being converted to digital. As i vary my pot , the results are in the following:

    ---------------9876543210
    1v to pin RA0 = 0000000000
    2v to pin RA0 = 0100000001
    3v to pin RA0 = 1000000010
    4v to pin RA0 = 1100000011

    what may have gone wrong? how come the other LEDs of bit 7,6,5,4,3,2 is not lighting up? Must i use any component to fix to the pin RA0 of PIC16F877A?

    here my coding:


    org 0x000
    goto start

    int org 0x004
    goto int


    start
    call initial
    again
    call startconv
    call check
    call result
    goto again

    initial
    BCF STATUS,RP1
    BSF STATUS,RP0
    MOVLW b'00000001'
    MOVWF TRISA
    MOVLW b'00000000'
    MOVWF TRISB
    MOVLW b'00000000'
    MOVWF TRISC
    MOVLW b'00000000'
    MOVWF TRISD
    MOVLW b'10001110'
    MOVWF ADCON1
    BCF STATUS,RP1
    BCF STATUS,RP0
    MOVLW b'10000001'
    MOVWF ADCON0
    RETURN

    startconv
    BSF ADCON0,GO
    RETURN


    check
    BTFSS PIR1,ADIF
    goto check
    MOVLW b'00000001'
    MOVWF PORTC
    BCF PIR1,ADIF
    return

    result
    BCF STATUS,RP1
    BSF STATUS,RP0
    MOVFW ADRESL
    MOVWF PORTB
    BCF STATUS,RP1
    BCF STATUS,RP0
    MOVFW ADRESH
    MOVWF PORTD
    RETURN

    END

    Btw is there a specific calculation to get the digital values of 10bit?

    what i know is this =


    actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)


    I dun really understand how this equation comes abt. I have read nigel-gwin's tutorial but still no improvement. Perhaps one to one attention right here might be a good help. thanks guys
     
    Last edited: Oct 5, 2007
  2. eng1

    eng1 New Member

    Joined:
    Apr 7, 2006
    Messages:
    951
    Likes:
    19
    Location:
    Italy
    A short delay is required before starting a new conversion. Check section 11.1 - "A/D acquistion requirements" in the datasheet. Also the maximum input impedance should be kept below 2.5 kΩ.
     
  3. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    thanks for replying eng1

    yea it mentioned here in the data sheet about time acquisition.. so before i start a conversion i have to delay at least about 20 micro second for the capacitor to charge up? and it says here that the maximum recommended impedance for analog sources is 10k ohm... so i should put a 10 Kohm in series along with the connection of the pot and RA0 pin?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. eng1

    eng1 New Member

    Joined:
    Apr 7, 2006
    Messages:
    951
    Likes:
    19
    Location:
    Italy

    Yes, at least 20 us.

    No, that's the maximum allowed value for the analog source. The input impedance should be as low as possible, so don't add a series resistor.
    From your first post I understand that you're using a pot as a voltage divider, what's its resistance?
     
  6. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,213
    Likes:
    640
    Location:
    Derbyshire, UK
    No, that will make it worse!, you would be adding an extra 10K to the existing impedance. If you're not switching channels it doesn't make that much difference, but what value is the pot you're using?.

    As for the value conversion, 0V in gives a reading of zero, and Vref in (5V?) gives a reading of 1023.
     
  7. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i'm using 10K ohm pot.

    This may sound silly but what's the difference if i put a 10kohm or any impedance below 2.5k ohm in series along with the connection of the pot and RA0 pin? is it important when comes to switching channels?

    so with my code above.. just added with the 20micro second delay before starting the a/d conversion and that's it? this will give me a 10bit digital result from any analogue input i give to the assigned analogue pin?
     
  8. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,213
    Likes:
    640
    Location:
    Derbyshire, UK
    Using a 10K pot it should be fine.

    With a higher value source impedance it takes longer for the capacitor inside to charge, but as long as the input doesn't change very quickly then once it's initially charged there's no real problems about using a single input.

    However, if you switch inputs then you can get instant large changes.

    For example, you are reading 0.1V on one input, and switch to another with 4.8V on it, the capacitor has to charge from 0.1V to 4.8V before you can accurately take a reading from it. The same applies to switching to a lower voltage, the internal capacitor has to discharge.
     
  9. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    oh i see, i think i have this concept right this time. so the equation which i mention is correct?

    actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)

    if i were to use my Vref+, voltage reference= Vdd (which is 5v) while Vref- = Vss (ground)

    Any one more thing, the maximum voltage which can be applied to my AN0 pin
    as analogue pin must not be more than 5v.. am i right?
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,,

    Try this, your original code is wrong.
    Code (text):
     list p=16f877,f=inhX32, x=off
     include <p16f877.inc>
     

    ;free dev/debug regs 20 to 27

     errorlevel -302

     __CONFIG _CP_OFF & _XT_OSC & _PWRTE_ON  & _WDT_OFF & _LVP_OFF


        org 0x000
        goto start

        org 0x004


    start
        call initial
    again
        call startconv
        call check
        call result
        goto again

    initial
        BCF STATUS,RP1
        BSF STATUS,RP0
        MOVLW b'00000001'
        MOVWF TRISA
        MOVLW b'00000000'
        MOVWF TRISB
        MOVLW b'00000000'
        MOVWF TRISC
        MOVLW b'00000000'
        MOVWF TRISD

        MOVLW b'10001110';rj,dddddd,a
        MOVWF ADCON1

        BCF STATUS,RP1
        BCF STATUS,RP0
        MOVLW b'10000001';osc/32,..... adon
        MOVWF ADCON0
        RETURN

    startconv:
        BSF ADCON0,GO
        RETURN


    check:
        BTFSS PIR1,ADIF
        goto check
        ;;MOVLW b'00000001'
        ;;MOVWF PORTC
        BCF PIR1,ADIF
        return

    result:
        BCF STATUS,RP1
        BSF STATUS,RP0
        MOVF ADRESL,W; ****
        BCF STATUS,RP0;*****
        MOVWF PORTB
        BCF STATUS,RP1
        BCF STATUS,RP0
        MOVF ADRESH,W;*****
        MOVWF PORTD
        RETURN

        END
    This now runs OK, in the Simulator.:)
     
    Last edited: Oct 5, 2007
  11. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    oh yea... thanks a bunch eric... bank problems

    MOVF ADRESL,W and MOVFW ADRESL is there any difference in that? can you tell me?
     
  12. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    MOVF ADRESL,W ; moves contents of ADRESL into W

    MOVWF ADRESL; moves contents of W into ADRESL; edited: typo corrected.

    I have now edited the code, it ALL in the code posted, paste and copy it, lets know.
     
    Last edited: Oct 5, 2007
  13. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    i thought
    MOVWF ADRESL ; moves contents of W into ADRESL ??

    OMG i learned the wrong codes all these while..
     
  14. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Woops a typo!.

    Should have read: MOVWF ADRESL
     
  15. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    is there no such mnemonic of movfw ADRESL ? moves contents of ADRESL into W ?
     
  16. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    I have seen MOVFW Reg, but I dont use it.

    The assembler IIRC, MPLAB adds the extra lines of code.

    EDIT:
    Which assembler are you using??
     
    Last edited: Jul 7, 2008
  17. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    lol okay then, i guess i have to test it .....my previous question
    about the equation which i mention is it correct?

    actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)

    if i were to use my Vref+, voltage reference= Vdd (which is 5v) while Vref- = Vss (ground)

    and the maximum voltage which can be applied to my AN0 pin
    as analogue pin must not be more than 5v.... am i right?
     
  18. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Yes dont exceed +5Vdc input to the ADC when the Vdd =+5V

    The equation is correct. The values in the ADC regs are binary:)
     
  19. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    alright thank you eric! more problems to come! just wait for my next thread!
     
  20. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,213
    Likes:
    640
    Location:
    Derbyshire, UK
    It's not a real instruction, the assembler converts it to something else - like all the 'special' instructions in the list.
     
  21. derrick826

    derrick826 New Member

    Joined:
    Oct 5, 2007
    Messages:
    82
    Likes:
    0
    hi again, i have tried the codings and it works fine but it seems that the accuracy of 10bit- digital outputs on the LEDs after conversion when compare using the formula:

    actual analogue voltage = [digital values in 10 bit (decimal)] * (5v/1023)

    is kinda out...

    for example: when i input 4.78v into RA0 it gives me 1111111111 (all leds light up)

    when i input 0.02v into RA0 it gives me 0000000001 (first led light up)

    but according to the formula which i mentioned, the first LED will light up which represent 1 bit if 4.8mv is being applied:

    4.8mv = 1* (5/1023)

    is there any method which I can do to improve its accuracy from my codings?
     

Share This Page