12Bit ADC with PIC18F2523


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2008
    Posts
    25

    Default 12Bit ADC with PIC18F2523

    Hello,
    I have code for 10 bit ADC which is normally working with PIC18F2455 and I want to use this code for 12 bit ADC with PIC18F2523.
    I changed Define ADC_BITS 10 to ADC_BITS 12, but results are sill 10 Bits.
    I using 20 MHz external oscillator.
    How to configure this code for 12 bit ADC with PIC18F2523 ?

    Thank you


    Here is my code:

    Define LOADER_USED 1
    DEFINE OSC 48

    ' Define LCD registers and bits
    Define LCD_DREG PORTB
    Define LCD_DBIT 4
    Define LCD_RSREG PORTC
    Define LCD_RSBIT 7
    Define LCD_EREG PORTC
    Define LCD_EBIT 6
    DEFINE LCD_BITS 4 ' 4-bit data bus
    DEFINE LCD_LINES 2 ' LCD - 2 lines


    ' Define ADCIN parameters

    Define ADC_BITS 12 ' 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

    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %00001010 ' Set PORTA analog...
    ADCON2 = %10000000 ' ...and right justify result

    Pause 500 ' Wait .5 second

    loop: ADCIN 0, adval ' Read channel 0 to adval
    Lcdout $fe, 1 ' Clear LCD
    Lcdout "Value: ", DEC adval ' Display the decimal value
    Pause 100 ' Wait .1 second
    Goto loop ' Do it forever
    End

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    ADCON2 = %10000000 ' ...and right justify result
    how many times do we see this totally wrong statement

    ADCON2 = %10000000 ' ...and right justify result and set the conversion clock to fosc/2
    which is way out of limits for a 48mhz osc and contradicts the define
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    why not just
    ADCON2.7=1
    or
    ADCON2 = ADCON2 | %10000000 ' ...and right justify result


    other than that i have nothing , never tried a 12 bit adc chip.
    you may need to do it manually

  3. #3
    Join Date
    Apr 2008
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    Hello richard,
    I configured my code for 12 Bit AD, now I get 12 Bits results, but it is working only with 4MHz external oscillator.
    When I replace extenal oscillator wit 20MHz and DEFINE OSC 20, it is not working, what is wrong?


    Define LOADER_USED 1
    DEFINE OSC 4

    ' Define LCD registers and bits
    Define LCD_DREG PORTB
    Define LCD_DBIT 4
    Define LCD_RSREG PORTC
    Define LCD_RSBIT 7
    Define LCD_EREG PORTC
    Define LCD_EBIT 6
    DEFINE LCD_BITS 4 ' 4-bit data bus
    DEFINE LCD_LINES 2 ' LCD - 2 lines


    ' Define ADCIN parameters

    Define ADC_BITS 12 ' 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

    advalLow VAR WORD ' make it available to use in PBP
    advalHigh VAR WORD ' make it available to use in PBP
    advalLow = ADRESL
    advalHigh = ADRESH


    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %00001010 ' Set PORTA analog...
    ADCON2.7=1 ' ...and right justify result

    Pause 500 ' Wait .5 second

    loop: ADCIN 0, adval ' Read channel 0 to adval
    adval = ADRESL + (ADRESH * 256)
    Lcdout $fe, 1 ' Clear LCD
    Lcdout "Value: ", DEC adval ' Display the decimal value
    Pause 100 ' Wait .1 second
    Goto loop ' Do it forever
    End

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    in what way does it not work ?
    where and what are your config settings

    advalLow and advalHigh only need to be bytes

    advalLow VAR byte ' make it available to use in PBP
    advalHigh VAR byte ' make it available to use in PBP


    since ADRESH and ADRESHL are sequential in memory for that chip its possible to save a bit of typing by :-
    Code:
    @my_adresult =ADRESHL
    my_adresult var word ext
    
    ;.............
    
    
    ADCIN 0, adval ' Read channel 0 to dummy adval
     adval = my_adresult

  5. #5
    Join Date
    Apr 2008
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    Ok,
    I directly saving the ADRESL and ADRESH register which is used to store the end results of an analog-to-digital conversion (ADC) to results adval and sending LCD. My problem is, the code is working properly only with 4MHz oscillator, when I define 20MHz occillator, the conversion is very slow, waiting few second, when start conversion.
    Whay the conversion is fast for 4Mhz oscillator and very slow for 20MHz oscillator

    adval = ADRESL + (ADRESH * 256)

    and sending LCD

    Lcdout $fe, 1 ' Clear LCD
    Lcdout "Value: ", DEC adval ' Display the decimal value

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    where and what are your config settings ?

    the default osc is 1mhz which may be the problem

  7. #7
    Join Date
    Apr 2008
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    config setting:


    DEFINE OSC 20

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    DEFINE OSC 20 does nothing but tell the compiler to base timed sequences on that assumption

    if you need an external xtaL or anything eles then the config fuses need to set appropriately
    eg
    Code:
    ;----[18F2523 Hardware Configuration]-------------------------------------------
    #CONFIG
      CONFIG  OSC = HS
      CONFIG  FCMEN = OFF
      CONFIG  IESO = OFF
      CONFIG  PWRT = OFF
      CONFIG  BOREN = SBORDIS
      CONFIG  BORV = 3
      CONFIG  WDT = ON
      CONFIG  WDTPS = 512
      CONFIG  CCP2MX = PORTC
      CONFIG  PBADEN = OFF
      CONFIG  LPT1OSC = OFF
      CONFIG  MCLRE = ON
      CONFIG  STVREN = ON
      CONFIG  LVP = OFF
      CONFIG  XINST = OFF
      CONFIG  DEBUG = OFF
      CONFIG  CP0 = OFF
      CONFIG  CP1 = OFF
      CONFIG  CP2 = OFF
      CONFIG  CP3 = OFF
      CONFIG  CPB = OFF
      CONFIG  CPD = OFF
      CONFIG  WRT0 = OFF
      CONFIG  WRT1 = OFF
      CONFIG  WRT2 = OFF
      CONFIG  WRT3 = OFF
      CONFIG  WRTC = OFF
      CONFIG  WRTB = OFF
      CONFIG  WRTD = OFF
      CONFIG  EBTR0 = OFF
      CONFIG  EBTR1 = OFF
      CONFIG  EBTR2 = OFF
      CONFIG  EBTR3 = OFF
      CONFIG  EBTRB = OFF
    #ENDCONFIG

  9. #9
    Join Date
    Apr 2008
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: 12Bit ADC with PIC18F2523

    richard,
    you are true, problem was in config fuses.
    Default configuration is: OSC = XT, after change to: OSC = HS is OK


    richard, thank for your help

Similar Threads

  1. ADC values above 4095 for 12 bit ADC
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st February 2015, 22:24
  2. 12bit Power Supply
    By ztoti in forum Adverts
    Replies: 19
    Last Post: - 15th October 2014, 23:29
  3. 6ch 12bit adc pic16c771
    By pigglet in forum Adverts
    Replies: 0
    Last Post: - 23rd September 2012, 15:37
  4. ADC problem on 18F46K22 device using all 28 ADC pins
    By JimAvanti in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 23rd February 2012, 20:22
  5. PIC18f2423 two 12bit adc input problems
    By Brandon in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th December 2007, 09:47

Members who have read this thread : 3

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