PDA

View Full Version : Problem reading multiple ADC ports



jswayze
- 3rd November 2004, 05:26
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:



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:


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

mister_e
- 3rd November 2004, 05:57
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

jswayze
- 3rd November 2004, 06:09
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

mister_e
- 3rd November 2004, 06:31
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?

Bruce
- 3rd November 2004, 06:55
Are you reading 7.854 Vdc directly on RA0 or measuring the center tap on a voltage divider?

jswayze
- 3rd November 2004, 17:45
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

mister_e
- 3rd November 2004, 18:02
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

Ingvar
- 4th November 2004, 13:29
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

jswayze
- 4th November 2004, 14:09
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

mister_e
- 4th November 2004, 15:39
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!

Ingvar
- 4th November 2004, 16:29
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

mister_e
- 4th November 2004, 16:46
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.


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