how to read 10 bit value of adc in pic 16f877a


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2014
    Location
    Pakistan
    Posts
    19

    Default how to read 10 bit value of adc in pic 16f877a

    hello everyone I have this code and tried it and its working perfectly to read 8 bit value of an0 ,it shows 0 to 255 value on lcd but i am not being able to read 10bit value of channel please help
    To derieve 8 bit value my code is
    device is 16f877a
    Code:
    
    CLEAR
    DEFINE OSC 4 
    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_LINES 2
    DEFINE LCD_BITS 4
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50
    
    
    Low porte.2
    pause 500
    
    
    DEFINE ADC_BTTS 8
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    
    
    TRISA = %11111111
    TRISD = %00000000
    ADCON1 = %00000111   'set port a digital
    
    A2D_VALUE0 VAR byte
    
    
    LCDOUT $FE, 1
    MAINLOOP:
    ADCIN 0, A2D_VALUE0
    
    
    LCDOUT $FE, $80, DEC A2D_VALUE0,
    PAUSE 10
    GOTO MAINLOOP
    END
    For ten bit value my code is below but it still shows 8 bit value ,please guide me to generate 10 bit value of AN0 channel

    Code:
    CLEAR
    DEFINE OSC 4 
    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_LINES 2
    DEFINE LCD_BITS 4
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50
    
    
    Low porte.2
    pause 500
    
    
    DEFINE ADC_BTTS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    
    
    TRISA = %11111111
    TRISD = %00000000
    ADCON1 = %00000111   'set port a digital
    
    A2D_VALUE0 VAR WORD
    
    
    LCDOUT $FE, 1
    MAINLOOP:
    ADCIN 0, A2D_VALUE0
    
    
    LCDOUT $FE, $80, DEC A2D_VALUE0,
    PAUSE 10
    GOTO MAINLOOP
    END
    
    Last edited by Archangel; - 1st November 2014 at 19:12.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default Re: how to read 10 bit value of adc in pic 16f877a

    DEFINE ADC_BTTS 10 should be DEFINE ADC_BITS 10 (the typo is there in the 8-bit version of the code as well.

    If that alone doesn't do it then try changing left/right justification (ADCON1.7). I can never remember when to use what....

    /Henrik.

  3. #3
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default

    As I always do, let me preface by saying I'm a noob with little surety, a modest desire to help, and an immense desire learn. It is my hope that by making a sincere effort to solve your problem, it will be one I do not have to make for myself

    Looking at page 128 of the datasheet, it would appear your ADCON1 register is bunged:

    Your setting: ADCON1 = %000111, appears to me to left justify the two high bits with ADCON1.7 = 0; I should think it would be easier to manipulate an ADC value with all the bits right justified (ADCON1.7 = 1). Also, the right bits (0-3) are A/D port configuration control bits - they set which channels are ADC, which are the Vref+/- channels, and which are digitals. There are two settings which RESEMBLE your configuration:

    '011X', which disables ADC (making all channels digital) and '1111' which sets AN0 as analog, AN2 as vRef- and AN3 as vRef+. This appears to be what you want...

    So, if my evaluation is correct, 001111 is the correct ADCON1 register configuration setting for 1 ADC channel, 10 bit resolution, right justified. Do you read it this way as well?

    Actually, in better looking at the choices (doh) it appears that 001110, sets AN0 analog - without vRefs. Time to edit had expired...
    Last edited by Archangel; - 1st November 2014 at 19:05. Reason: poster ran out of edit time

Similar Threads

  1. ADC and SEEPROM on 16F877A
    By Digitaldood in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 8th January 2013, 17:49
  2. Replies: 2
    Last Post: - 22nd May 2012, 06:45
  3. 12 bit or higher ADC on a PIC that works with PBP?
    By Brandon in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 11th November 2007, 17:19
  4. Flash PIC with 12 bit ADC
    By Steve_88 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th October 2007, 20:35
  5. Read the newbee threads, 16F877A no go.
    By BobbyA in forum mel PIC BASIC Pro
    Replies: 39
    Last Post: - 31st December 2006, 03:09

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