Hot!Setting adc bits

Author
majorkuso
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2013/02/23 09:10:56
  • Location: 0
  • Status: offline
2018/06/04 07:12:01 (permalink)
0

Setting adc bits

I'm trying to setup the account on a pic16lf1455. When I try to set the bits I have to use adcon1 and adcon0 in order to set all the adc parameters, but I don't see the reference voltage bits, am using the datasheet right? I will upload my code when I get home.
#1

17 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 1893
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Setting adc bits 2018/06/04 07:42:08 (permalink)
    0
    Please enlighten us: what do you mean by "don't see the reference voltage bits" ?
    What will certainly help: which compiler version are you using?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    KTrenholm
    Super Member
    • Total Posts : 410
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: Setting adc bits 2018/06/04 08:15:17 (permalink)
    +1 (1)
    Sounds like you didn't look at the datasheet carefully enough.
    Page 156:
    ADCON1 <1:0>
    00: VREF+ == VDD
    01: Reserved
    10: VREF+ == External VREF Pin
    11: VREF+ Connected to FVR Module
     

    du00000001
    Please enlighten us: what do you mean by "don't see the reference voltage bits" ?

    ^^Also this^^
    post edited by KTrenholm - 2018/06/04 08:18:39
    #3
    majorkuso
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2013/02/23 09:10:56
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/07 06:46:29 (permalink)
    0
    I am using xc8 latest install here is what I have so far
     
    #include <xc.h>
    #define _XTAL_FREQ 500000
    #define led LATAbits.LATA5
    //int result = ADRES;
    void main(void)
    {
        //Initialisation
        
        TRISAbits.TRISA5 = 0; // set RA5 as output
        LATAbits.LATA5 = 0;   // set RA5 in low state
        ADCON0bits.ADON = 1; // turn on ADC
        ADCON0bits.CHS = 3; // select pin 3 RA4 AN3
        ADCON1bits.ADPREF = 0b00  // set vref to power supply voltage
        
    // main loop
        
        while (1){
            while (ADCON0bits.GO_nDONE == 1) // wait for AD conversion to complete
            {
            // perform action
            }
            
                     
        }
        return;
    }
    #4
    jack@kksound
    code tags!
    • Total Posts : 2590
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/07 09:07:20 (permalink)
    +2 (2)
    A few observations:
    You never return from a main() function, no place to return to so this causes a reset of the program if it executes,  remove the return. It is outside the while() loop but really should not be there.
    You should always completely configure a peripheral before you enable it, do the ADON = 1 after you set the other options.
    Your code never starts the A/D conversion, you wait for it to complete but you must start it. Enabling the A/D does not start the conversion: ADCON0bits.GO_nDONE = 1; this will start the conversion, do this after you enable the A/D (and have waited long enough for the required acquisition time).
    #5
    du00000001
    Just Some Member
    • Total Posts : 1893
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Setting adc bits 2018/06/07 12:31:54 (permalink)
    +1 (1)
    The procedure to start a conversion is described quite detailled in chapter 16.2 of the data sheet - occupying less than a quarter of a page.
    May I ask "where is the problem"?
    Contrasting to quite a number of members here, I assume that you're a native English speaker.
    Just reading some lines and writing the corresponding code really shouldn't be too difficult.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #6
    majorkuso
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2013/02/23 09:10:56
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/11 13:12:06 (permalink)
    0
    here is the change I just want to make sure I am using the correct syntax and order of operations. for instance I wanted to make sure I set the ADCON1bits.ADPREF correctly. could I have set it = to 0 or is how I have it also correct?
     
    #include <xc.h>
    //#include <xlcd.h>
    #define _XTAL_FREQ 500000
    #define led LATAbits.LATA5
    //int result = ADRES;
    void main(void)
    {
        //Initialisation
        
        TRISAbits.TRISA5 = 0; // set RA5 as output
        LATAbits.LATA5 = 0;   // set RA5 in low state
        ADCON0bits.CHS = 3; // select pin 3 RA4 AN3
        ADCON1bits.ADPREF = 0b00;  // set vref to power supply voltage
        ADCON0bits.ADON = 1; // turn on ADC

    // main loop
        
        while (1){
            ADCON0bits.GO = 1; //start ADC conversion
            while (ADCON0bits.GO_nDONE == 1) // wait for AD conversion to complete
            {
           
            }
        
            // perform action after conversion      
        }
      }
    #7
    du00000001
    Just Some Member
    • Total Posts : 1893
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Setting adc bits 2018/06/11 16:10:25 (permalink)
    0
    Looks promising. Are you reasonable about the 500 kHz XTAL frequency?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #8
    majorkuso
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2013/02/23 09:10:56
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/14 07:04:06 (permalink)
    0
    I want the system to be in a low power.
    #9
    coffee critic
    Super Member
    • Total Posts : 183
    • Reward points : 0
    • Status: offline
    Re: Setting adc bits 2018/06/14 08:56:56 (permalink)
    0
    Set ADCS to FRC and add 5us of delay between the ADON and GO. 

    n_*$
    #10
    majorkuso
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2013/02/23 09:10:56
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/14 09:12:28 (permalink)
    0
    How cabn i read the value of two different sensors using ADC, do I create a routine called read sensors and put the value of the two readings in two different variables and  and perform the logic on the two and send the result back?
    #11
    du00000001
    Just Some Member
    • Total Posts : 1893
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Setting adc bits 2018/06/14 09:25:21 (permalink)
    +1 (1)
    How can I read ...

    • Basically speaking, you have to assign each sensor an analog input pin.
    • Then select the respective pin as the ADC input and have a conversion take place.
    • Whether you have a single routine scanning/converting all inputs or whether you have several routines that can be called independently depends on your system requirements:
    I just recently created a single conversion routine (on an Arduino, but this doesn't matter in this context) that converts 3 analog inputs, massively filters two of them and passes the results to one variable per input. And the whole crap is in an ISR with 1 kHz repetition rate wink
    For the task given this can be considered nearly perfect (a bit of an overkill for one of the channels), and I can be sure that the conversions do not interfere with each other. This is clearly a pro for a single routine in an interrupt-driven system.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #12
    majorkuso
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2013/02/23 09:10:56
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/18 07:20:51 (permalink)
    0
    for some reason I get error 0x3021 when I try to get the chip id to program it, but other chips read fine like the pic12f683 i had on hand. i am using a pickit 3 with latest mplab
    #13
    jack@kksound
    code tags!
    • Total Posts : 2590
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/18 07:59:16 (permalink)
    +1 (1)
     I get error 0x3021

    Which program gives you this error code? And when?
    #14
    majorkuso
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2013/02/23 09:10:56
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/18 08:08:10 (permalink)
    0
    both the ide and ipe  when I supply power from tool.
    #15
    du00000001
    Just Some Member
    • Total Posts : 1893
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Setting adc bits 2018/06/18 08:18:19 (permalink)
    +1 (1)
    error or id 0x3021 ?
    Could it - eventually - be that your current project setting is PC16LF1455, but your controller is a PC16F1455 ?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #16
    jack@kksound
    code tags!
    • Total Posts : 2590
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/18 08:37:19 (permalink)
    +2 (2)
    Never seen error codes in hex.....0x3021 is the device id for a 16F1455; a 16LF1455 would be 0x3025. 
    #17
    majorkuso
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2013/02/23 09:10:56
    • Location: 0
    • Status: offline
    Re: Setting adc bits 2018/06/19 05:25:40 (permalink)
    +1 (1)
    yep that is what it was
    #18
    Jump to:
    © 2018 APG vNext Commercial Version 4.5