• AVR Freaks

Hot!First ADC-read always zero, Pic18LF67K40

Author
camilla
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/06/04 03:42:34
  • Location: 0
  • Status: offline
2019/12/11 03:44:54 (permalink)
0

First ADC-read always zero, Pic18LF67K40

Hello!
I have an interesting(?) problem... When reading from the ADC, I always get 0 on the first read, whereas the second and third reads give reasonable values.
 
It doesn't matter if I have a long delay in before the first read or do it immediately, so it is not a question of the measured voltage not having time to reach the right levels.
 
The port is initialized as analog in (and would the later reads succeed if that was the issue?), the processor is an Pic18LF67K40 running at 16 MHz.
 
void adconStart(void) {
    ADCON0bits.ADCS = 0; // Fosc / ADCLK
  
    ADCON0bits.ADFM = 1; // right justified
    ADCLK = 0b00000111; // fosc/16  Fosc/(2*(n+1))
   
    ADREF = 0x00;
    ADPCH = channel;
    
    ADCON0bits.ADON = 1;        // enable the A/D
    ADCON0bits.ADGO = 1;          // start conversion
}
 
UINT16 adconRead(UINT8 channel) {
    UUINT16 result;

    adconStart(channel);
    while (ADCON0bits.ADGO);      // wait for completion
    
    result.u8msb = ADRESH;
    result.u8lsb = ADRESL;
    return result.u16;
}

#1

2 Replies Related Threads

    ric
    Super Member
    • Total Posts : 25124
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: First ADC-read always zero, Pic18LF67K40 2019/12/11 12:40:27 (permalink)
    +3 (3)
    Set  up the ADC once at the start of your code, not every reading.
    Read the datasheet ADC chapter again, particularly with regards to "acquisition time".
    You must either wait the required acquisition time AFTER selecting a channel, but BEFORE setting the GO bit, or you must enable the built in acquisition delay via the ADACQ register.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    camilla
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2019/06/04 03:42:34
    • Location: 0
    • Status: offline
    Re: First ADC-read always zero, Pic18LF67K40 2019/12/12 02:13:11 (permalink)
    0
    Thanks for your answer, I will have a look at acquisition time, I had read that part, but not really understood it :).

    The reason for the set up every time is that we read from different channels, and that one is read at a different clock rate and with left-justification instead. I removed this code, since it did't really pertain to the question (I hope) and I wanted to publish as little of the code as possible :D.
     
    Do you think this might be part of the issue?
    #3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5