Page 1 of 2

ADC Issues

Posted: Sat Jul 06, 2013 3:43 am
by kochevnik
Just got a couple of Basic Chips - hooked them up, ran some programs all that is working fine.

MY problem is I get very inconsistent readings from the ADC reads I am doing - even with what
should be 0 volts coming from a 9v battery as a power source (thru an LDO down to 3.3v) I get
numbers that vary from 0 to 1024. I have multiple caps on everything, all the ground tied together,
not sure what I am doing wrong ? I have this same setup going into a picaxe chip with it's lower
adc resolution and I get very consistent readings. This chip, not so much.

Any help or things to check, very appreciated.

Thx






Sample code below :

Code: Select all

PRINT "START"	

INPUT (11)
VHI = 0
VLO = 65472

FOR X=1 TO 100000
	VNOW = AD(0)
	IF VNOW > VHI THEN VHI = VNOW
	IF VNOW < VLO THEN VLO = VNOW 
NEXT

PRINT "VLO = " + VLO
PRINT "VHI = " + VHI
PRINT "DELTA = " + (VHI - VLO)

PRINT "END"

Re: ADC Issues

Posted: Mon Jul 08, 2013 2:14 pm
by basicchip
Your INPUT(11) will disable the analog input, making it a digital one.

Remove that from the code and report.

What is the analog source you are measuring?

Re: ADC Issues

Posted: Mon Jul 08, 2013 4:57 pm
by kochevnik
Thx - this was a couple of days ago and I did get all this to work. I'll have to re-try that
input command - I'm sure you're right, it just didnt act that way for me. In any case
during the debugging process I took it out as it didnt seem to affect anything.

I'll have to post my code and test setup here later if it can help anyone else. For initial
testing I hooked up a wire to ground to get 0 ADC readings - that worked. Then
I used a 10k resistor from a voltage divider to test some higher readings. What I
found was that the quantity and the number of capacitors I placed across the ADC signal
to ground affected the variance in the range of ADC values I saw quite a bit - I saw some other posts here
where 100-1000 pf caps were said to be a good idea, but the one that worked the best for
me was a 470 nf (I think). Without that cap I got some really wild swings in adc readings.
I'm sure that's my bad circuit design tho :)

I also had to drop the adc resolution quite a bit - in my code I
just divided the adc value by 256 - and once I did those two things (adding the cap and changing the code) - I got consistent readings
- in 4 million adc reads I would get about 20 readings that were off by +/- 2 points and no
other 'bad' readings outside that range. For what I am trying to do that worked well enough.

Eventually the ADC pins are supposed to read signals from an electret microphone array (+ opamps + peak detector).
I'm very new at all this and while I got the consistent DC readings, I'm finding it a little tougher
to get consistent readings from the array of mics - but I dont think that is the Coridium chip's fault,
just my misunderstanding the signals and the design.

But still plugging away.

Re: ADC Issues

Posted: Mon Jul 08, 2013 7:56 pm
by basicchip
Sorry I didn't answer sooner, and no one else could. Any members very first post get's moderated to prevent the spammers out there.

And this was a big weekend for our small community. Truckee had its annual airshow and I was volunteering or attending over the 3 days. 16,000 plus people came out to visit.

Back to AD(x). For most of the chips we setup the AD inputs as analog inputs at reset. So all you have to do is use the AD(x) keyword. This gives you the best performance. In most things we opt for performance, so that INPUT, OUTPUT, DIR all will configure AD pins as digital, which takes a little longer. But those keywords would normally be used once during configuration. The exception would be IOs that are used bi-directionally. IN and OUT just read or set the pin, they do not control direction. For convenience we do set the pin direction for the IO(x) keyword. If IO(x) is on the left of the =, then it becomes an output, if on the right it becomes an input.

AD, does not convert them back, as that would take more time. If for some reason you want to do both digital and analog functions with a single pin, you would need to write to the PINSEL register. Details on those in the corresponding user manual.

Re: ADC Issues

Posted: Wed Jul 10, 2013 3:35 pm
by kochevnik
No worries - I knew this was a holiday weekend and would probably have to wait - always other stuff to do anyways :)

Here is something I am trying to understand - with the code below - I don't have anything connected to the Basicchip and I am setting both a 'regular' pin and one of the AD pins as inputs - my understanding is INPUT(33) is for AD(2) [pin 10].

When I run the code below I get -1's for reading the digital pin and I get 0's for reading the AD pin. Remember nothing connected - so why the difference ? Is this related to pullups ? Whatever it is, how do I get the AD pins to act as 'normal' digital input pins ?

And another question - this one may be a bit odd, as a newbie :

I have a half wave (top half of sine wave) analog signal - goes from 0 to 3v from the floor to the peak. The frequency is about 1khz. If I were to feed this signal into a pin setup as a digital input and read it, what should I see as I read that digital pin as the signal oscillates ?

And what are the voltage levels for logic zero and logic -1 for the basic chip (are they in the LPC datasheets ?).

And I know you are asking - why would anyone do this - why doesnt he just use the ADC ? Well I have a project where I am monitoring an array of electret mics feeding peak detectors (R + C + diode) to determine if a loud noise has occurred and then using the time delay of arrival (TDOA) to determine the direction of the noise. For TDOA it is vital that I determine the noise peak & timing for each mic. The problem is that the peak detectors vary quite a bit in their behavior - the rate they charge and discharge is not similar enough to get reliable readings from the ADC - i.e. mic array 1 might peak at 235 on the adc while for the same sound mic array 2 shows 215 on the ADC pin. Datapoint - the sounds I am looking at are all in the sub 1khz range.

So since the basicchip is so fast, I skipped the peak detector and fed the ADC pin right from the mic + opamp. This looks like it might work but it is right on the very edge of the ADC capabilities as matched with the 1 khz frequency - e.g. it tke roughly 10 usec to do an ADC read + some misc checking, and with a 1 khz sine wave as an example, each wave is 1 msec, so I get 1 ms / 10 us = 100 reads per wave - Assuming the wave transitions from 0v to 3.3v at peak noise level all in 1/2 of a wave - gives me 50 readings to go from 0v to 3.3v and if the adc is set to go from 0 to 255, that's about 255/50 = 5 ADC points diff with each read - varying as the curve flattens out on top of course.

So it occurred to me that if the digital pin transitions to logic hi at some fixed voltage, and reading the digital pin is about 10 times faster than an ADC read, then if all I was interested in was catching some fixed point in the waveform, if I could feed that analog signal into a digital pin, I could get 10 times as many readings = much better accuracy in determining exactly when the loud noise hit some high threshold. I don't need to capture THE PEAK, I just need to capture the same high point on the waveform of the loud noise.

So long explananation - short question - can I feed an analog signal (0 to 3.3v) into a digital pin and have it reliably transition to logic high (0 on the basic chip as my understanding is logic low is -1) ?

Hope all that makes sense.


Code: Select all

PRINT "START"	

INPUT(33)
INPUT(2)

START = TIMER

FOR X=1 TO 100
	A = IN(33)
	B = IO(33)
	
	PRINT A
	PRINT B
	
	T = IN(2)
	U = IO(2)
	
	PRINT T
	PRINT U

	
NEXT

Re: ADC Issues

Posted: Wed Jul 10, 2013 5:35 pm
by basicchip
I did a couple quick checks and found the INPUT(33) is not properly changing the IO configuration register for digital function. So you will need to do that manually. In your case you may want to enable hystersis, which will give you some noise immunity.

AD conversions on the BASIC chip take about 5.5usec, I checked that by--

x=1000000
while x
x=x-1
a=ad(2)
loop

That program took 6000 msec, and the loop alone takes about 500 msec.

To make AD(2) a digital pin

#include <LPC11xx.bas>
IOCON_R_PIO1_1 = &H81 ' digital IO, no pullup

IOCON_R_PIO1_1 = &HA1 ' digital IO, no pullup, with hysterisis

IOCON_R_PIO1_1 = &H91 ' digital IO, with pullup

Details in the user manual

As for using a digital IO for a peak detector, the problem is that you don't really know where the peak is, though you could measure the time it goes high and the time it goes low and assume the peak is in the middle.

Sounds like you want to measure more than one input, which would best be done by reading the GPIO register directly. To then do that and to find the peak might be some fancy footwork/programming.

Re: ADC Issues

Posted: Thu Jul 11, 2013 1:31 pm
by kochevnik
Appreciate the help.

BTW -I dont necessarily have to use the peak - I can use any consistent point on the waveform - one technique I saw some use was to wait to see if the waveform rose to a peak, then check for the zero crossing point after that peak passed.

Some shorter questions so I dont waste too much of your time :)

1) Is the Basic Chip running at 48 mhz ?

2) When I plugged my analog halfwave signal ( 800 hz 3.3 volts p2p) into digital pin 8 then signal on my scope went from a nice half wave to a flat-line of about 1.3 volts with small peaks (+0.2v) every 800 hz. Unplugging the analog signal wire from the digital gives me my nice half wave back on the scope. Running a loop and checking IO(8) with the analog signal wire plugged in gives me all -1's.

Why did that happen ?

And can I get the digital pin to process that analog signal as a digital signal ? i.e. a 3.3v peak = logic one and 0v on the waveform being a logic zero ?

Re: ADC Issues

Posted: Thu Jul 11, 2013 2:13 pm
by basicchip
The BASICchip is running at 48 MHz +/- 1%

Sounds like you are loading the signal, which can be because the BASICchip is driving it, or the impedance of your signal is quite high. NXP has changed the way they spec the analog inputs, they use to suggest a recommended source impedance and that use to be around 5K. On the LPC1114, they are calling it Rvsi, which is now 40K. So to drive that your source impedance should be below that.

What does the signal source look like? From the symptom, I would say the effective impedance is much higher than 40K.

Re: ADC Issues

Posted: Thu Jul 11, 2013 3:40 pm
by kochevnik
Sorry - I should have put that in my post -

I have a frequency generator (a BK 3001B running at 800 hz 3.3v p2p) and the signal goes thru a diode then thru a 10k resistor then into the digital pin.

The manual (attached - just 15 pages) for the 3011 is attached - pg 2 has the specs and the output impedance says 50 ohms +/- 6%

Re: ADC Issues

Posted: Thu Jul 11, 2013 4:06 pm
by basicchip
A diode resistor would have an impedance of the resistor when the diode is conducting, so 10K in your case.

But it would have near infinite impedance when the diode is turned off (so it's very high).

So the AD input looks like a small cap with some series resistance, so what that ends up being is a peak detector for the signal. And that is what you are seeing.

Add a 10K pulldown resistor and you'll see the half wave rectified signal, divided by the resistor ratio.