Reading multiple ADC channels FAST


Closed Thread
Results 1 to 5 of 5
  1. #1
    lwindridge's Avatar
    lwindridge Guest

    Question Reading multiple ADC channels FAST

    I'm working on reading two ADC ports (RA0 and RA1) for two seperate sensor inputs.
    I can read the value of one input without any problems, but when checking the second input it seems to nearly duplicate the first - even when the sensor isn't attached!

    Can anyone shed any light on this? I need the sensor check to not slow down the code if possible as it is also reading rpm pulses and needs to be able to make rpm outputs matching the input but time shifting in 20uS steps (ignition retard on an engine basing retard values on air flow on RA0 and air temp on RA1).

    I've attached my current code for reference.
    I am using a 16F877 @ 4Mhz.

    Cheers

    Leigh Windridge
    Attached Files Attached Files

  2. #2
    Calco's Avatar
    Calco Guest


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Not quite sure about this but im guessing its something to do with your ADCON statement.
    ADCON1 = %00000010 sets the a/d to LEFT justify the reult
    whereas ADCON1 = %10000010 will right justify the result.
    I always use right justify.
    also you may need to turn the A/D on seperately with ADCON0 = %11000001.

    just basic thoughts, Ive not used the ADCIN command before I usually access the registers directly.

    Hope this helps


    Darryl

  3. #3
    lwindridge's Avatar
    lwindridge Guest


    Did you find this post helpful? Yes | No

    Default

    I was wondering myself if it was the ACDIN command getting overlap between the registers hence them both coming out identical.

    Do you have any code snippets I could browse through to see if I could get it working with direct access to the registers?

    Leigh

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Here is an example reading the Registers directly... just six simple lines of code...

    MyByte var Byte

    ADCON1=%00000000
    ' Set LEFT Justification
    ' Enable ALL ADC's (using PIC16F87x as example)
    ADCON0=%01000001
    ' Set Fosc/8
    ' Set ADC Channel 0 (RA0/AN0)
    ' Enable ADC Module
    PauseUS 50
    ' 50uS Pause to allow sampling Capacitor to charge
    ADCON0.1=1
    ' Start Conversion
    While ADCON0.1=1:Wend
    ' Wait for conversion to complete
    MyByte=ADRESH
    ' Read 8-bit Result

    Justification is controlled by bit 7 of ADCON1. Use LEFT justification (ADCON1.7=0) for 8-bit ADC usage (byte result) and you read the result from the ADRESH register (as per the example above). Use RIGHT justification (ADCON1.7=1) for 10-bit ADC usage (word result), then ADRESH becomes the highbyte of the word, and ADRESL becomes the lowbyte... as per the example below (which also switches to RA1/AN1)...

    MyWord var Word

    ADCON1=%10000000
    ' Set LEFT Justification
    ' Enable ALL ADC's (using PIC16F87x as example)
    ADCON0=%01001001
    ' Set Fosc/8
    ' Set ADC Channel 1 (RA1/AN1)
    ' Enable ADC Module
    PauseUS 50
    ' 50uS Pause to allow sampling Capacitor to charge
    ADCON0.1=1
    ' Start Conversion
    While ADCON0.1=1:Wend
    ' Wait for conversion to complete
    MyWord.Highbyte=ADRESH
    MyWord.Lowbyte=ADRESL
    ' Read 16-bit Result

    Braindamagingly simple (every PIC that has ADC's gives explicit step-by-step instructions in it's Datasheet - you gotta read those Datasheets!!).... now you know how simple it is, hands up anyone that can give me a valid reason to continue using ADCIN?

    Melanie

  5. #5
    lwindridge's Avatar
    lwindridge Guest


    Did you find this post helpful? Yes | No

    Default

    You're right - once I sussed the how the code works it was very simple

    Only problem is I couldn't figure it out from the data sheet - I guess I need more time working with PIC's to be able to think in a way that will allow me to understand it's register settings properly.

    Now my next problem is going to be reading the two values, getting an RPM reading and working out the degrees per second of a crank shaft and duplicating the incoming pulse on an output pin but delayed by x uS according to a lookup table...

    All good fun so expect some more odd questions to be coming out soon!!

    Thanks again!

    Leigh
    Last edited by lwindridge; - 21st April 2004 at 23:12.

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 02:18
  2. multiple ADC using DSpic 30f3010
    By angeline in forum mel PIC BASIC
    Replies: 6
    Last Post: - 22nd February 2008, 10:03
  3. ADC - 2 channels but not same voltage reference - how to?
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th November 2007, 19:03
  4. 18F1320 ADC multiple channel select
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th November 2005, 21:40
  5. Problem reading multiple ADC ports
    By jswayze in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 4th November 2004, 16:46

Members who have read this thread : 2

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