help switching from multiple analogReads to adc code

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello there

this is my first post here. I have been using the atmega328 (with Arduino IDE). I am trying to transition into using AVRstudio, etc and more direct ways of communicating with analog and digital ports.'

my question is whether or not anyone can point me to a good resource, book, tutorial on ADC code for MULTIPLE channels? i keep hearing from everyone that analogRead is a terribly slow way to read the ADC. but everything I read on the ADC is referring to just using the mux to select one channel

my applications usually require adc ports adc0-adc3
(usually 2 pots and 2 audio input jacks)

thanks for any and all help

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I keep hearing from everyone that analogRead is a terribly slow way to read the ADC
Define "terribly slow". I doubt that it is any slower than reading it directly. An ADC conversion takes between 65µs and 260µs, depending on what you set the ADC clock to.
Quote:
but everything I read on the ADC is referring to just using the mux to select one channel
You can only read one channel at a time since there is only one ADC on the chip. You set a channel, do a conversion, then set the next channel for the next conversion. There are no short cuts to read faster than that. You can, of course, use the ADC interrupt so that you can do something else while waiting for an ADC conversion to complete.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what people have told me is that analogRead makes you wait. whereas direct adc code (using the mux...i am aware that you have to read one channel at a time) lets you do other things while the add conversion is going on. I should have been more specific...none of the tutorials I have read have specified how to read more than one channel during the whole program. I need to be able to read 4 inputs. thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The one thing you want to steer clear of is mixing multi-channel and "free running" mode but other than that handling multi-channel is just a case of setting the ADMUX bits then triggering the conversion with ADSC. A common "gotcha" is not correctly clearing the previous channel bits when setting a new channel. Either use an & mask or always set both the channel number and any ref bits and ADLAR bits that you are also using each time.

The Tutorial Forum has example code for ADC - fleshing it out to multiple channels should be relatively straightforward.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you tell us a little about your program? Are you trying to digitze speech? What are your desired specs? We know the limitations of the internal a/d. If you want 16 bit samples at 44KHz, you need to use an external a/d converter.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
none of the tutorials I have read have specified how to read more than one channel during the whole program
As I said above, simply set a channel, do a conversion, set the next channel, do a conversion. It is not complicated at all.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

none of the tutorials I have read have specified how to read more than one channel during the whole program

I wondered about this. The Tutorial Forum here has:

(1) [CODE] [C] Scanning, Free Running A/D Converter

(1a) [CODE][C]Scanning, "free running" ADC. Version 2

(2) Using AVR ADC

(3) [TUT] [C] Newbie's Guide to the AVR ADC

(4) [TUT][HARD]Differential ADC For Monitoring Voltage Window

(5) [TUT][ASM][CODE] A Moron's Guide to the AVR ADC

Of those (1), (1a), (2), (3) all talk about multi-channel use.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

loss1234 wrote:
...none of the tutorials I have read have specified how to read more than one channel during the whole program.

I could suggest RTFM, but I won't.
The data sheet gives a good explanation of this. Read the section thoroughly, then read it again. Have a coffee and then pull out that data sheet and have another read. Sometimes you have to force info into your brain using multiple servings of the info.
Well, that works for me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

funny i had only seen 1 and 5. (i had printed both of them out but i must have not understood or overlooked what you are reffering to) I had also read the datasheet (i have that printed out too) but the datasheet isnt always the most easy to read. i will check out tutorials. tutorial one didnt make much sense to me as it had an h file and a c file...i wasnt sure how to call it...

so far it seems much more complicated than analogRead which is possibly why I am having a hard time "getting" it...with analogRead you just stick in analogRead wherever you want in the code...with the adc code, I have been hearing that you should set up an interrupt with your timer (say an OVF from timer 0) and then call each adc call from that inside the ISR instead from inside your loop (or main)

just a few details on my projects

i have been making a bunch of synthesizer related programs which are not so much "audio" based as control voltage oriented. meaning they sample control voltages from other synth modules and then spit out CV or audio.

the sampling rate need not be more than what the AVR is capable of.

I was actually playing around with some comb filtering, circular buffer stuff, and many people recommnded i get to know the real avr code as opposed to just Arduino stuff

anyway, sorry if I overlooked some tutorials.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I have been hearing that you should set up an interrupt with your timer (say an OVF from timer 0) and then call each adc call from that inside the ISR instead from inside your loop (or main)
Don't always believe what you're told.
You can either poll the ADC from a place in the code that suits you.
Or collect the values from an ADC ISR.
You could also do it the way you described if you FIRST collect the ADC value from previous conversion and THEN start another conversion (from which you collect the result next time Timer ISR triggers).
You do NOT want to start a conversion inside a Timer ISR and then sit in that ISR doing nothing but waiting for conversion to finish...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

loss1234 wrote:
i have been making a bunch of synthesizer related programs which are not so much "audio" based as control voltage oriented. meaning they sample control voltages from other synth modules and then spit out CV or audio.
Hmnnn .... takes me back to the 70s when I was building analogue synths. You mention control voltages, so figure you're playing around with analogue synths rather than midi controlled synths ?