Combining 10kHz Interrupt with ADC polling

I have a loop which polls half a dozen sensors but that loop will also be interrupted at a frequency of 10kHz (using a timer with interrupt at the end) to toggle a bit and change a register value. My concern is, I heard that ADC polling takes 100 uS. If I am interrupting my code at 10kHz (100uS also), doesn’t this essentially mean that none of my analogRead() statements will ever finish without being interrupted? Isn’t that going to cause all sorts of problems for reading sensor values?

I heard you can change the ADC prescalar, say from 128 to 64. I also heard something about that decreasing resolution. Does that mean I can’t get 10 bits unless I use 128 as a prescalar? If it’s not the case, what is the bare minimum code to change the prescalar to 64? I saw the register table but people’s examples have more than just 3 lines of code to change 3 registers and I don’t understand why.

Why not add an external ADC with a fast SPI interface?
Can achieve higher resolution and at faster speeds.
Do some filtering here for the # bits and a package you can work with
http://www.digikey.com/products/en/integrated-circuits-ics/data-acquisition-analog-to-digital-converters-adc/700?k=adc&k=&pkeyword=adc&pv1989=0&pv243=78&FV=fff40027%2Cfff80183&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=250

doesn't this essentially mean that none of my analogRead() statements will ever finish without being interrupted

So what! Look at the code for analogRead()!

Mark

Are you saying that interrupting analogRead() won't have any detrimental effects?

CrossRoads:
Why not add an external ADC with a fast SPI interface?
Can achieve higher resolution and at faster speeds.
Do some filtering here for the # bits and a package you can work with
http://www.digikey.com/products/en/integrated-circuits-ics/data-acquisition-analog-to-digital-converters-adc/700?k=adc&k=&pkeyword=adc&pv1989=0&pv243=78&FV=fff40027%2Cfff80183&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=250

I may yet end up doing so... however given the scope of the larger project, I have so much of the Nano's hardware in use and my code is spiraling out of control... I am reluctant to further extend the complexity of the system with more hardware. But that's my gut responding to stepping out of my comfort zone. I'm sure it's still possible to manage 1 extra chip.

CrossRoads:

http://www.digikey.com/products/en/integrated-circuits-ics/data-acquisition-analog-to-digital-converters-adc/700?k=adc&k=&pkeyword=adc&pv667=60&FV=fff40027%2Cfff80183%2C3cc004e%2C5680002%2C56c0007%2C142c0442%2C142c04b7%2C142c0d25%2C142c0323%2C142c0341%2C142c035e%2C142c0398%2C142c03d9%2Cffe002bc%2C264000a%2C2640016%2C2640005&mnonly=0&newproducts=0&ColumnSort=-348&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=250

Just for the sake of pursuing all avenues, can I still decrease the ADC prescalar and retain 10 bit resolution?

Don't know, I've never tried that. I've only for faster external parts with more resolution.

It's ok I managed to stumble upon the answer in the spec sheet:

By default, the successive approximation circuitry
requires an input clock frequency between 50kHz and
200kHz to get maximum resolution. If a lower resolution
than 10 bits is needed, the input clock frequency to the
ADC can be higher than 200kHz to get a higher sample rate.

Basically the next lower prescalar of 64 puts the sample rate higher than 200 kHz so you would lose precision.

On the plus side, I found 2 things that might help. The first (kinda unrelated) is that you can flip 2 registers at any time and change your ref voltage on the ADC to 1.1V internally. This gives you almost 5x the precision for sensors with very little gain.

Second, rather than use an interrupt based system triggered by my 10kHz signal, what I could do instead is connect the timer pins directly to the gate of a logic level MOSFET and use that to switch my signal in hardware. This way I don't need to toggle the signal itself, since this creates the aforementioned conflict between the ADC sampling and the interrupts. Put another way, instead of switching my signal on and off at 10 kHz from the source, I can produce the signal constantly and switch it on/off with electronic hardware, thus leaving the ADC uninterrupted. The timer functions on its own anyway so it won't conflict.

Isn't it amazing what you can learn by reading the data sheet?

since this creates the aforementioned conflict between the ADC sampling and the interrupts.

There is no conflict!

Mark

Well ok then but you can see why I might think there would be one. The ADC sampling process is clearly very sensitive to noise and time consuming with multiple operations involved in muxing and loading capacitors and running stuff through comparitors, etc. Sounds a bit too much like clockwork for me to be interrupting that every 2 seconds without questioning the integrity of the data.

jremington:
Isn't it amazing what you can learn by reading the data sheet?

Indeed you are quite right! Lazy people like me don't always do all the homework before asking the question. However this does not appear to be a unique characteristic, since people dumber than me are still asking about how to toggle LEDs and people smarter than me are still failing to answer my "apparently" elementary questions until I answer them myself.

It's my bad, to be sure but, be kind. Tis human to err :slight_smile:

I have a loop which polls half a dozen sensors but that loop will also be interrupted at a frequency of 10kHz (using a timer with interrupt at the end) to toggle a bit and change a register value.

people smarter than me are still failing to answer my "apparently" elementary questions until I answer them myself.

Second, rather than use an interrupt based system triggered by my 10kHz signal, what I could do instead is connect the timer pins directly to the gate of a logic level MOSFET and use that to switch my signal in hardware.

How were we going to know from the first post that a timer hardware output pin was involved? You can always avoid interrupt latency by using the hardware outputs.

You wouldn't have known because neither did I. It was not part of my initial design, but an idea that came into my head. What I wanted to do was avoid using hardware to chop the signal in question. I figured I could use an interrupt based system to turn the primary signal on and off at 10kHz... which I can do but then I have a loop polling sensors, full of interrupts. It only recently occurred to me that I could remove all the interrupts by using the timer hardware pin to drive a physical switch. This has implications too however. More hardware to fail, more crowding on my board and an incomplete switch because of semiconductor voltage drops. I won't get full voltage to 0 but full voltage to 0.7 or something. I can live with that though.