Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC A/D Sampling multiple inputs

Status
Not open for further replies.

NJ Roadmap

New Member
I'm trying to sample a couple of analog inputs by getting the A/D on my PIC18F4520 to toggle between the two pins using the ADCON0, ADCON1 and ADCON2 registers. But somehow the values read don't seem to be the true analog values (I'm reading them off to an LCD), however if the pins are sampled on their own without the PIC toggling between the two inputs the readings are good. I've given a 20ms delay between pin toggles so that should be enough for the holding cap to recharge to the new pin's value. Even then the cap should eventually charge to the correct value. Any ideas? I'm stumpted. I'm using Vss and Vdd are Vref+ and Vref- for both pin measurements.

My ADCON settings are right, the only issue I can think of is that I'm using AN0 and AN6 as inputs as opposed to say AN0 and AN1, and hence I've had to set ADCON1 bits 3:0 so that AN0:AN6 are set as analog inputs but I don't see that as a problem, or is it one?
 
You say you've got a 20mS delay between pin toggles. What you should have is a delay after the pin has been changed but before the conversion starts. So, select the channel by writing to ADCON0 with the Go/Done bit clear, wait a while to allow the capacitor to charge and then set the Go/Done bit.

HTH

Mike.
 
Acquisition times are typically in the 20uS range. We're only charging a 120pF capacitor! He's allowed 1000 times that. Unless he has some very weird hardware then 20mS is more than adequate.

Mike.
 
Pommie said:
Acquisition times are typically in the 20uS range. We're only charging a 120pF capacitor! He's allowed 1000 times that. Unless he has some very weird hardware then 20mS is more than adequate.

Like I said, it depends entirely on the source impedance, you can't blindly assume 20mS is enough when you've no idea what the source impedance might be.
 
For 20mS to be inadequate the source impedance would have to be over 20MΩ. I think it much more likely that he is placing the delay in the wrong place.

Mike.
 
Nj can you post your code? I'm going to agree with Pommie it's likley you're delaying, switching the ADC channel and then immediatly sampling. Make sure that delay is actually occuring between the switching of mux channels and the sample.
 
For that chip you should be able to set an automatic acquisition delay time in ADCON2 bits bit 5-3 based on the clock frequency.
Saves having to manually wait for acquisition before sampling.

You still need to allow a small delay after each sample before starting another acquisition though.
 
Thanks for all the help. I had originally followed all the steps talked about here. I then went back to bare brick and found out that it was working fine. The problem was when switching sampling inputs - the first 3 readings are off and represent the ADC value from the previous input. Hence when I average over 30 readings, the first 3 readings were corrupting the result. I've hence got the PIC to discard the first 3 readings and then continue to average over the next 30 readings, hence making 33 readings in total. Works a charm now!
 
You've never given us any idea what it's been fed from?, taking four samples before it's settled down suggests the source impedance is far too high - and obviously rather than ignore three readings, you could just delay longer for the sample and hold to charge (or discharge).
 
Nigel Goodwin said:
You've never given us any idea what it's been fed from?, taking four samples before it's settled down suggests the source impedance is far too high - and obviously rather than ignore three readings, you could just delay longer for the sample and hold to charge (or discharge).

QFT .
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top