Switching ADC channels continuously

Thread Starter

LaurenceR

Joined Feb 7, 2013
107
I guess my question is twofold. I have written a program for a phase and frequency correct timer1 with an ADC input from PA0 pin (attiny 261A). The single input 10 bit value is placed in OCR1C to change the top value which changes the frequency. I would like to then switch to another ADC potentiometer on PA1 and use it to change the OCR1D value which would change the value of the ON time (duty cycle).

Question 1.) in Free running mode do I need to use the ADC interrupt to read the value into the timer. Will the 10 bit value get jumbled up or will it get written before advancing to the next command? Should I put a delay between changing inputs so as to give it time to think....so to speak?

Question 2.) Can I switch from one ADC input to the other by just changing the value in ADMUX?

Thanks in advance for any help you can give.
 

MrChips

Joined Oct 2, 2009
34,698
You should always check the reference manual for the correct procedure.
If sampling rate is not critical you do not need to use interrupts.
It might be simpler to use single conversion mode.

Step 1 - Select the input channel by setting ADMUX
Step 2 - Start conversion
Step 3 - Wait for conversion complete
Step 4 - Read ADCL first then ADCH
 

Thread Starter

LaurenceR

Joined Feb 7, 2013
107
You should always check the reference manual for the correct procedure.
If sampling rate is not critical you do not need to use interrupts.
It might be simpler to use single conversion mode.

Step 1 - Select the input channel by setting ADMUX
Step 2 - Start conversion
Step 3 - Wait for conversion complete
Step 4 - Read ADCL first then ADCH
Thanks for your reply, very helpful. If I understand this correctly, I would have to trigger the ADC (set ADSC in ADSCRB) with another input pin to change the value?
I would prefer to leave it "free running" so it changes values whenever I adjust the two potentiometers.
 

mckenney

Joined Nov 10, 2018
125
Question 1.) in Free running mode do I need to use the ADC interrupt to read the value into the timer.
That would be the usual way. You could poll for ADIF (not ADSC, which will always be on), but then you won't be able to do much else.
Will the 10 bit value get jumbled up or will it get written before advancing to the next command?
No. The ADC uses the usual low/high protocol to assure you can read it atomically.
Should I put a delay between changing inputs so as to give it time to think....so to speak?
No.
Question 2.) Can I switch from one ADC input to the other by just changing the value in ADMUX?
Yes. Keep in mind that the change won't take effect until the ADC is next started. In Free Running mode, the next conversion has already started by the time you see the ADIF, so you need to think off-by-one.
 

Thread Starter

LaurenceR

Joined Feb 7, 2013
107
That would be the usual way. You could poll for ADIF (not ADSC, which will always be on), but then you won't be able to do much else.

No. The ADC uses the usual low/high protocol to assure you can read it atomically.

No.

Yes. Keep in mind that the change won't take effect until the ADC is next started. In Free Running mode, the next conversion has already started by the time you see the ADIF, so you need to think off-by-one.
Very helpful, thanks.
I don't mind being off by one conversion if that's the case. I just want the last conversion number to remain in tact after I switch to the other ADC channel. Would a delay between the switching help anything?

Thanks again,
 

Thread Starter

LaurenceR

Joined Feb 7, 2013
107
If you want to have it in free running mode, take two readings and throw away the first reading.
Thanks again,
Going back to the datasheet as you suggested, It appears that the last conversion is completed before changes are made. It also states that the first next conversion from the changed input may not be accurate. Apparently changing adc inputs takes a bit of time to equilibrate. I'm not concerned with one bad reading, however for the sake of completeness I might try a delay.
If I am understanding the datasheet correctly, when you change adc channels it need time for the reference voltage to equilibrate and about 30 more clock cycles for the first next adc conversion. Either way it looks a time delay is the fix.

Many thanks again for your help.
 

MrChips

Joined Oct 2, 2009
34,698
This is internal to the ADC.
The charge settling time will depend on external source resistance and any external stray capacitance.
 

Thread Starter

LaurenceR

Joined Feb 7, 2013
107
I found a datasheet for one of the at mega ICs. It showed the internal circuit for the adc input. There is was, the RC circuit you referred to. Looks like the ADC input voltage on the nmos gate determines how much of the reference voltage gets through. Interesting how much we can learn when we ask what we think are simple question.
No more questions. I have plenty to work with. Thanks for your help.
 

mckenney

Joined Nov 10, 2018
125
It also states that the first next conversion from the changed input may not be accurate. Apparently changing adc inputs takes a bit of time to equilibrate.
The only statement of this kind I can find in DOC8197 applies to differential channels. From your description, you are using single-ended channels. (I've never used differential channels on an AVR, so I didn't notice it before.)

In general, there is no need to throw away samples when switching channels. The sampling capacitor must settle within the sample/hold period -- otherwise, consecutive samples from the same channel wouldn't work. If your source impedance is too high, you need to lengthen the sample/hold period by slowing down the ADC clock.

I'm actually not a big fan of Free Running mode (I'm a fixed-rate-sampling kind of guy), but I won't tell you not to use it.
 

Thread Starter

LaurenceR

Joined Feb 7, 2013
107
Correct, I am using single ended input. I think I'm missing something regarding "manual" mode. I think I overlooked it as I wanted to be able to turn a potentiometer and have it reflect the change rather instantaneously. I assumed in manual mode I would have to have some external trigger for it to resample. Can this be done with software?
My source impedance from the potentiometer is R 25k, no added capacitance or inductance on the input.
I attached a file that shows the decoupling circuit (pate 21) I was referring to. I mistakenly said the voltage on the gate would control how much reference voltage got through, should have said current.
I'm sure I need to learn more about "manual mode".
Thanks again as your comments have been very informative and helpful.

Regards
 

Attachments

Thread Starter

LaurenceR

Joined Feb 7, 2013
107
I already said it in post #2. It is called single conversion mode.
Your absolutely correct. I overlooked the obvious from your statement. The truth is I am totally unfamiliar with single ended mode as I never really concentrated on it. I am now looking more closely at it and finding it is software driven and doesn't require external triggers as I incorrectly assumed. Essentially I'm back to the drawing board and learning more about it.
Many thanks as well.
 
Top