Help to setup ADC for 16F1826


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    50

    Default Help to setup ADC for 16F1826

    I am pulling what hair is left out of my head.
    For PIC16F1826, I am trying to get the ADC setup.
    I have a voltage INPUT on RA2 (pin1).
    This is where I want to ADC to be controlled (AN2).
    I referred to the datasheet (page 139), but cannot get this thing to work.
    I also have INPUTS on RA3 (pin2) and RA5 (pin4)
    Here is my code:

    Code:
    '= PIC16F1826                                           =
    '========================================================
    'Initialize variable
    #CONFIG
     __config _CONFIG1, _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
     __config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _STVREN_OFF
    #ENDCONFIG
    
    '_MCLRE_OFF = MCLR pin functions as INPUT RA5, MCLR internally tied to VDD
    
    DEFINE OSC 4
    DEFINE ADC_BITS 8       ' 8 bit A/D Conversion
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50   ' 50 uS A/D sample time
    
    PORTA = 0
    PORTB = 0
    TRISA = %00101100
    TRISB = %00000000
    ADCON0 =%00001001
    ADCON1 =%11000000      'Right justified
    
    ANSELA =%00000100      ' AN2 on PortA.2 analog
    ANSELB =%00000000      ' Digital inputs
    
    FVRCON = 0
    DACCON0 = 0
    DACCON1 = 0
    INTCON = 0
    CM1CON1 = 0
    
    adval var word
    
    mainloop:
    adcin 2, adval
    if adval<18 then PortB.3 = 1
    if adval>28 then PortB.3 = 0
    goto mainloop
    end
    Help please!!
    Thx!

  2. #2
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    166


    Did you find this post helpful? Yes | No

    Default Re: Help to setup ADC for 16F1826

    Two things I have noticed in your code:

    1/. The 16F1826 has a 10 bit ADC whereas you have defined an 8 bit setup
    2/. ADCCON0 = %00001001 sets the ADC channel to be AN9 where you are trying to use AN2

    Cheers
    Barry
    VK2XBP

  3. #3
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: Help to setup ADC for 16F1826

    Hey Barry!
    Thanks so much!!!
    This did it!

    DEFINE OSC 4
    DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
    DEFINE ADC_CLOCK 4
    DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time

    PORTA = 0
    PORTB = 0
    TRISA = %00101100
    TRISB = %00000000
    ADCON1 =%11000000 'Right justified

    ANSELA =%00000010 ' AN2 on PortA.2 analog
    ANSELB =%00000000 ' Digital inputs
    I actually thought you could tell a 10-bit ADC to work as an 8-bit. But no worries! All is perfect once again!!

  4. #4
    Join Date
    Oct 2011
    Posts
    54


    Did you find this post helpful? Yes | No

    Default Re: Help to setup ADC for 16F1826

    Hi SOTATOSA
    10 bit A/D converters do work in 8 bit mode. Just set LEFT justified and the 8 MSBs are held in the ADRESH register and the 2 LSBs are held in the ADRESL register bits 6 and 7 which you ignore. ADRESH register contains the 8 bit result. I think this is how DEFINE ADC_BITS 8 works. Setting 8 bit conversion AND RIGHT justified would cause some confusion. Hope this helps.
    Phil

  5. #5
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Help to setup ADC for 16F1826

    this tutorial helped me understand A/D a little better...

    http://www.microcontrollerboard.com/...converter.html

    and here is another good tutorial...

    http://www.edaboard.com/nextoldesttoentry1570.html
    Last edited by Heckler; - 11th February 2014 at 22:39.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  6. #6
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: Help to setup ADC for 16F1826

    Thanks for all the replies. Making some kind of sense now! :-)

Similar Threads

  1. ADC problems with a 16F1826 chip
    By alec in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 1st September 2012, 22:09
  2. 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
  3. 16F913 setup
    By Andre_Pretorius in forum General
    Replies: 31
    Last Post: - 17th April 2010, 23:45
  4. ADC setup for 16F716
    By droptail in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th November 2009, 02:39
  5. LCD setup
    By ruijc in forum General
    Replies: 11
    Last Post: - 19th November 2007, 09: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