Problem reading multiple ADC ports


Closed Thread
Results 1 to 12 of 12
  1. #1
    jswayze's Avatar
    jswayze Guest

    Default Problem reading multiple ADC ports

    I'm trying to read voltage and current draw from a battery. I'm using an Allegro current IC to provide 0-5v as a representation of current (2.5v being 0 amps.) I now have code working that gives me the proper voltage from RA0, but readings from RA1 (my second ADC source) are not working. I get the same output from the PIC whether there is voltage present or not (the output being about 3.9v)

    After I execute one ADCIN command and get the result, is there something I need to do (reset, change banks, etc.) in order to read another port?

    Here are the relevant parts of my code as it stands tonight:

    Code:
    TRISA = %11111111 ' Set PORTA to all input
    CMCON = %00000111 ' Disable analog comparators
    ANSEL = %00000011 ' Set PORTA.0 = A/D in, rest digital
    ADCON1 = %10000000 ' Right Justify A/D result
    OSCCON = %01110000 ' INTRC = 8MHz
    
    ' ===================================
    ' Get the voltage from RA0
    ' ===================================
        ADCIN 0, ADval ' Read A/D on RA0
        ADval = (ADval*10) */ Quanta  
        ADval1 = (ADval/10) * 2   
     
        SEROUT2 PORTB.1,Baud,["Volts: ",dec ADval1 dig 3,".",dec3 ADval1," Vdc",$0D,$0A]
    
    ' ===================================
    ' Get the voltage from RA1
    ' ===================================
        ADCIN 1, ADval2 ' Read A/D on RA1
        
        SEROUT2 PORTB.1,Baud,["Amps: ",bin ADval2 ," A",$0D,$0A]
    And here's an example of the output:
    Code:
    Volts: 7.854 Vdc
    Amps: 1100100111 A
    
    Volts: 7.862 Vdc
    Amps: 1100101000 A
    
    Volts: 7.854 Vdc
    Amps: 1100101000 A
    
    Volts: 7.854 Vdc
    Amps: 1100101000 A
    Note that the voltage is verified and correct. The problem is that the amperage value is the same whethere voltage is present or not at RA1.

    Any thoughts here?

    Thanks,

    Jeff

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    wich PIC are you using??? Is by any chance Adval, Adval1,Adval2 are WORD or BYTE define.

    did you try with another channel? Did you use a current limit resistor to the input of ADC?

    let me know

    regards
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    jswayze's Avatar
    jswayze Guest


    Did you find this post helpful? Yes | No

    Default

    Sorry, I'll fill in some of the gaps.

    I'm using a PIC16F88

    ADval, ADval1 and ADval2 are defined as WORD.

    I did not try another channel, and I did not use a resistor.

    I can try another channel, but that seems unlikely to be my problem. I'm not using a resistor on my other input even though the battery its connected to is capable of pumping out a lot of current.

    Thanks,

    Jeff

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    what about if you insert these lines
    DEFINE ADC_BITS 10 ; Set number of bits in result
    DEFINE ADC_CLOCK 3 ; Set clock source 3=rc
    DEFINE ADC_SAMPLEUS 50 ; Set sampling time in uS

    and add PAUSE 50 after each ADCIN ???

    is by any chance voltage at channel 1 can exceed 5 volts?

    Last edited by mister_e; - 3rd November 2004 at 07:45.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Are you reading 7.854 Vdc directly on RA0 or measuring the center tap on a voltage divider?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    jswayze's Avatar
    jswayze Guest


    Did you find this post helpful? Yes | No

    Default

    mister_e: Those code lines were in my program, but I didn't include them above.

    I added the pause statement and it helped - a little. I now get different readings from *no* input compared to *some* input. No input to A1 produces a response of essentially 0 from the ADC. When voltage is applied (in this case 2.5v) it goes back to the number it used to give before the PAUSE statement was added... approximately 800 (out of 1024)

    Soooo... it knows whether somethign is there or not, but it can't discriminate the real value. When I get home tonight I'll try varying the voltage that that channel sees and see if the result changes... I'm guessing not.

    BTW, I'm using an Allegro current module that outputs a voltage of 0-5v depending on the input current. 0 A current should output 2.5v from the IC, which is what the ADC should be seeing when I plug it in.

    Bruce - I'm using a voltage divider and reading half of that value. The math you helped me with multiplies the result by 2 to get the real value on the screen.

    -Jeff

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hum... interesting sensors. I have a little assumption here about the noise, but i'm really not sure of this. have an 4.7k between PIC and sensor, one .1uF between PIC ADC input and ground.

    i'm curious to see what goes out of the sensor with a scope when there's no current flow on the TAB sensor.

    maybe pull-down resistor at the sensor output can also do something...

    sensor out=max 10ma

    regards
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    Hi Jeff,

    Looking at your results, it seems like your pic is stuck on channel zero even if you're reading channel one. A redaing of 800 vould represent around 3.9V, just what you should have on channel zero(7.8/2=3.9).

    Either you have a short beetween theese two pins on your board, or it might be a miss/bug in PBP. Looking at the datasheet i see that the 16F88 uses slightly different bits in the registers controlling the A/D converter than most other pics. I only have V2.44 which doesn't support 16F88 so i can't check, but it just might be that they missed this at MeLabs.

    Look for a short and if there is none you should try stuffing the A/D controlling registers manually and see how that goes.

    /Ingvar

  9. #9
    jswayze's Avatar
    jswayze Guest


    Did you find this post helpful? Yes | No

    Default

    Good point, Ingvar, but I think I'm finally in good shape.

    Unfortunately, I believe many of my "mystery" problems were due to faulty connections on my breadboard. Once I re-seated all my components things started working more reliably.

    At any rate I'm now reading both channels successfully, and I only have to work out some math issues to maximize the resolution of the current sensor.

    Next step is to etch a board, put one together, and send it up with my R/C plane!

    Thanks to all for your help. This is a very responsive community, and I hope I can add value to it as my skills improve.

    -Jeff

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    hi Jswayze,

    "Once I re-seated all my components things started working more reliably."

    chuckle does it? It happen to me a lot of time when using too much some of these breadboard... since that time i change them often an trow them to the garbage.

    We often believe the problem are in our program without any regards to the hardware... nothing is perfect

    good luck!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    Good to hear that you're getting on in the right direction.

    In case anyone were alarmed by my statement about it possibly beeing PBP causing the problem, don't worry. I took a look at Melabs site and found 16F87/88 includefiles for PBP2.43/44. Thank you Melabs :-) . I checked the asm code produced by ADCIN and it looks just fine. It fiddles with the correct bits in the correct positions.

    /Ingvar

  12. #12
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    For sure ADCIN will work for any PIC. This is the easy solution to get from them. But it takes a bit more code space than if you access directly to the registers.

    i attach an example from melabs website.
    Code:
    adval  var word					'Create adval to store result
    
    
    	TRISA = %11111111  	' Set PORTA to all input
        ADCON1 = %10000010 	' Set PORTA analog and RIGHT justify result
    	ADCON0 = %11000001	' Configure and turn on A/D Module
        Pause 500       	' Wait .5 second
    
    
    loop: 		ADCON0.2 = 1					'Start Conversion
    
    notdone:	pause 5
    			if ADCON0.2 = 1 Then notdone	'wait for low on bit-2 of ADCON0, conversion finished
    
    			adval.highbyte = ADRESH			'move HIGH byte of result to adval
    			adval.lowbyte = ADRESL			'move LOW byte of result to adval
    
    	    	Lcdout $fe, 1   				'Clear screen
            	Lcdout "Value: ", DEC adval		'Display the decimal value  
    
            	Pause 100       				'Wait .1 second
    
           		Goto loop       				'Do it forever
            	End




    Some programmer prefer to use this way... nobody's working the same way.

    regards
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 03:18
  2. PIC18F2423, ADC problem
    By mistergh in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th March 2009, 02:31
  3. Mutliple ADC problem with PIC16F877
    By andyto in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd October 2007, 18:47
  4. Reading multiple ports
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 20th May 2004, 18:22
  5. Reading multiple ADC channels FAST
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st April 2004, 23:37

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