16F877 ADC reading annomaly

Dear all,

I'm currently debugging a bit of hardware developed by someone else - I have the code and the hardware in front of me and from my previous PIC work I'm

99% certain it should be working fine. Its coded in C and essentially boils down to something that tests two ADC values and reports them over the serial port. Using pins 3 and 4 (adc channels 1 and 2) the code goes:

set_adc_chennel(1); result=read_adc(); Print some chars, print the result,. set_adc_channel(2); result-read_adc(); Print some chars print result

It does this, waits some time, does it again and then stops.

The strange output is that the ADC reading is somewhere around 17-25 for the first "result" and then 5 or 6 for the second. The actual voltage monitored on the pins is 0.441v and 0.12v respectively, steady to the third decimal place.

Changing the voltage (they are pressure sensors) to 0.580 seems to make zero difference to the first reading but changing the second to a voltage of

0.2v reads around 11. Reducing the voltage on the pins to zero yields no change in the first but 0 on the second. These voltages have been tested at the actual pin-body joint of the processor so are the actual pin voltages.

Any hints?

Can one pin be blown without affecting the others? I've only ever had a whole port blow before. I had considered the switchover time being too short with no delay before read_adc but the same happens before the second reading and that is "correct".

Any help appreciated!

Reply to
james
Loading thread data ...

What do you mean by "around 17-25"? Is this the digital value from the channel? What voltage is the PIC running at? Are you using the internal voltage reference or an external voltage reference? Are your grounds separate for the analog and digital (and tied at only one point)?

Also, you may want to introduce a small delay after setting the channel before reading it (something like a 50 - 100us delay) to allow the channel to stabilize.

Dave

Reply to
starfire

This is my problem really, its varying despite the pin voltage being steady - at ambient pressure (0.4v pin voltage) it gives anything from 17 to 25, seemingly higher if the unit has only just been powered on but that could be a coincidence. Its using the internal voltage ref (setup_comparator(NC_NC_NC_NC)) and the PIC is running at 4.98v. Grounds are not seperate, but scoping out the signals shows they are all rock steady signals with very little noise. The whole unit is fed by shielded cables and inside a metal box.

I was confused by fact that the code to test channel 1 and 2 is essentially identical and run consecutively, yet only one of them seems to be performing "correctly" - my thought process went "if the delay was the issue, both should have the same problem". Clearly my thinking process may be wrong! I'll see about re-programming it with some delays! Cheers!

Reply to
james

And yes, sorry, it is the digital value from the channel.

Reply to
james

Hi James -

One other thing I thought of... I'm not sure what flavor of PIC you're using but the analog inputs on the 18(L)Fxxx devices like to be terminated with a resistance to ground of less than about 2.5K... I usually use 2.2K. This may or may not be a problem with your sensor output. The pressure sensors I have used from Motorola have been buffered so it has not been a problem with them. A lot of sensors outputs have a fairly high impedance and need to be buffered before being input to the PIC. You may just want to try to tie a

2.2K resistor to ground from each analog input channel.

Dave

Reply to
starfire

"starfire" wrote in news: snipped-for-privacy@corp.supernews.com:

James-

I think the best advice I can offer is to try to separate out your problems and take them on one by one. Until you know for a fact that you're accurately and correctly sampling, you shouldn't be doing your debugging with pressure sensors in place. Until you know the pressure sensors are working correctly with stable outputs, you shouldn't be trying to sample them, but looking at them with a scope.

Divide and conquer. Fastest way to go.

--
Scott
Reverse name to reply
Reply to
Scott Seidman

Good advice, Scott. In addition, simulate the analog inputs into the program to verify they are what you think they should be...

Dave

Reply to
starfire

"starfire" wrote in news:13m3pnbc822td31 @corp.supernews.com:

the

at

you're

Now that I think of it, isn't there something in the PIC a/d about whether it returns a 10 or a 12 bit number (or is it 8 or 10??). If its a short word, I can't remember if you need to read the left or right side of it.

--
Scott
Reverse name to reply
Reply to
Scott Seidman

Scott -

Depending on the flavor of PIC, it will return an 8-bit or 10-bit value. The pertinent values are in the correct bit positions (low order bits) but you should AND off all other bits... just to be sure. value = input_adc() & 0x3ff; for the 10-bit input.

Dave

Reply to
starfire

"starfire" wrote in news: snipped-for-privacy@corp.supernews.com:

I'm remembering now with my compiler, all the PICs will return 8 bits unless you specify 10.

--
Scott
Reverse name to reply
Reply to
Scott Seidman

Cheers for the discussion, I've been away with work, back at it now...

I already tried providing a fixed voltage to the PIC with a plain old 5v power source (to take the sensors our of the mix), this didnt help as I only got "200" in return. I then tried testing the sensors and I seem to get a decent variation, as expected, with pressure between 0 and 5v, even when connected to the pic. I tried a few routes of tying the ADC inputs to ground through a 2k, 5k, 22K resistor and making sure the other inputs are tied low just in case I get cross-talk between the other pins on the port. No change. I'm going to re-program it to include the delay after pin-change, I've had a lot of people suggest this as the fault and it makes sense to me too.

The sensors are, as far as I can tell, internally buffered through some sort of an ASIC (according to the manufacturer).

Cheers for your help so far guys!

Reply to
james

james wrote in news:Kp-dnYxV-s_Bl- snipped-for-privacy@bt.com:

That's a constant value, which you might expect. What do you get when you put 2.5V in?? Also, you should probably assume that the A/D's might behave funny near the enpoints. Test at a voltage about 1/2 Vcc, where Vcc is the chip supply.

--
Scott
Reverse name to reply
Reply to
Scott Seidman

I'd have expected ~250ish (full scale, give or take, on an 8bit value?). On the 1/2 Vcc I get a wandering (between tests), yet indicative ~160ish. 1V gives ~90-95 depending on the test, despite the actual voltage being rock steady.

Reply to
james

james wrote in news: snipped-for-privacy@bt.com:

We're going to go back and forth a bunch here, but how do you KNOW the actual voltage is rock steady. If you're looking at this in an oscilloscope, I'd believe it, and if you're not, I don't.

The more info you can give us on how you've got your A/D configured, the faster you might sort this out. What are you using for Vcc?

--
Scott
Reverse name to reply
Reply to
Scott Seidman

I totally agree with Scott.

From your descriptions of the value variations for the input conditions given, the values could very well be exactly what you'd expect to see if the power supply line is not very stable (higher ripple voltage or less than the nominal "5V"). As another note, don't expect you will EVER get the converted values to be rock-steady for a specific voltage input condition... It just won't happen. It is very typical to get variations of +/- 2 values around the nominal value even with very stable input conditions. Remember the PIC itself is causing variable currents and ripple onto the voltage lines, also. A large value cap right at the PIC power line inputs helps. I usually use something like a 0.1uF in parallel with a 10uF to 22uF across the power line inputs.

Again, as Scott has stated, you need some precision equipment to take precision measurements. A scope trace picture is literally worth a thousand e-mails! There are just too many variables to speculate as to a single cause.

Dave

Reply to
starfire

"starfire" wrote in news: snipped-for-privacy@corp.supernews.com:

To add a bit, when dealing with a problematic subsystem, I think its very important to isolate it as completely as possible, and run the code for just that subsystem. Once you have it working, you can add it back in to the rest of your program, and then start figuring out if that process screws you up.

--
Scott
Reverse name to reply
Reply to
Scott Seidman

Also keep in mind there are two delays when taking PIC ADC measurement. An acquisition time and a sample time. Read the spec to make sure you account for both.

Reply to
AlbertWhistle

I'm on this again tonight after the Christmas break, I'm confident about the measurements of voltage as I've been watching it (and the power rails) with a digital scope, including watching for supply dips during valve actuation. It really couldnt be cleaner from what I see. these values are not jumping 1 or 2 values, but 5-10. Let me see how i get on and I'll report back, its not been my #1 priority until tonight!

Cheers!

Reply to
Coyoteboy

"Coyoteboy" wrote in news: snipped-for-privacy@bt.com:

5 LSB's of a zero to 5 10 bit A/D is about 5 mV. Are your values REALLY fluctuating less than 5 mV??
--
Scott
Reverse name to reply
Reply to
Scott Seidman

Im not sure its reporting 10bit, i think its reporting 8 bit, as per your earlier reply. This is another thing I'm out to check though. The IC is capable of both, im not sure how/where it's been set in the CCS C code, if at all, and what is default. I assumed 8 bit default - this may have been a mistake.

Reply to
Coyoteboy

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.