user16165: Is it possible to directly set the registers that control the ADC in an Arduino Yun (micro-controller side is essentially a Arduino Leonardo) from a sketch or is there a specific file somewhere you must edit?
I am working on a project that collects a signal digitizes it and sends it through a digital filter. I looked up the Datasheet for the micro-controller on the Yun and wrote the code setting the registers associated with the ADC portion of the micro controller. The code setting the registers was something like REGNAME = 0x01;. Yet when tested several of the associated registers were unaffected. Pointers on the process to set the ADC manually without using the analogRead() function would be appreciated.
Edit:
My original code looked something like this:
void setup(){
    // register to turn on ADC
    REG1 = REG1 | 0x01;

    //register to set sample rate and set ADC to Free Running Mode
    REG2 = REG2 & 0x30;

    // register to set other settings
    REG3 = REG3 | 0xF1;
}

void loop(){
    // code to take samples from ADC
    // and send them through a digital filter here.
}


user18770: Since your project is about acquiring signals I guess that the sample frequency is important for you. In fact you need to know exactly how much time passes from one sample to another. This cannot be achieved by simple reading the ADC in your loop(). The easiest implementation is to use an interrupt that occurs every n seconds (e.g. every overflow of timerX).
I'm going to write a pseudo-code for this: 
void SetTimer()
{  TISREG |= 1<<Prescaler_bit   //set the prescaler
   TICNT = 0   //sdt the tarting value
   TIINT |= 1<<OVF_Interrupt_enable
   sei() //enable global interrupt
}

ISR(TIM1_OVF_vect)   //happens every [prescaler*2^bit_timer/F_CPU] seconds
{
   while( !(ADCSREG &= (1<<bit_is_ready)) ) ; //usually there is a bit that is 1 while the ADC has read the data, and 0 while it is doing the conversions

//imagine the ADC stores the 3 MSBits in the ADCHI
//and the LSBits in the  ADCLO

value_high = (ADCHI & 0b00000111);  
value_low = ADCLO;

value = (value_high << 7) | value_low;

}


user7508: I can see no problem with your pseudo-code. You should probably post a
complete testable code in order to get better feedback.
That being said, here are a few suggestions:

You should completely set the ADC registers to the values that suit
your application, instead of just changing a few bits here and there.
This is because you should not care about how the Arduino core
library initializes those registers, especially if this is a
prototype for a final product that will not use Arduino core. For
example, instead of

REG1 = REG1 | 0x01;

you can write something like this:
REG1 = _BV(FOO)   // use foo mode...
     | _BV(BAR);  // ...in the bar setting

where _BV(x) is a macro from avr-libc that expands to (1<<(x)),
   and the bit names are those of the datasheet, for better readability.

You may want to run the digital filter in the ADC interrupt service
routine. If you can split your processing into a fast filter
(processing every sample) and a slow filter (running on decimated
samples), then do only the fast one in the ISR and leave the slow one
to the main loop. I say this from experience: when I tried to do
all the processing in loop() I always lost samples, which did not
happen when I moved the filter to the ISR.
But then, make sure your filter is optimized to be fast. Avoid
floating point operations in the ISR if at all possible.

I have written a program that looks very similar to what you are trying
to do: it samples an analog input at 9.6 kS/s and runs an
homodyne detection
filter in order to extract the signal power in a very narrow band around
1 kHz. The ADC is set to free running mode. In the ADC ISR, I
retrieve the sample, run it through the filter, and store the result
into volatile variables. In loop(), I read these back from time to
time. Here is how I set the ADC:
static void configure_adc()
{
    ADMUX  = _BV(REFS0)  // ref = AVCC
           | _BV(ADLAR)  // left adjust result
           | analog_in;  // input channel
    ADCSRB = 0;          // free running mode
    ADCSRA = _BV(ADEN)   // enable
           | _BV(ADSC)   // start conversion
           | _BV(ADATE)  // auto trigger enable
           | _BV(ADIF)   // clear interrupt flag
           | _BV(ADIE)   // interrupt enable
           | 7;          // prescaler = 128
}

The complete program is available here:
homodyne.ino.
It was initially intended for an Arduino Uno, but it runs unmodified on
a Leonardo clone.

16165: <p>Is it possible to directly set the registers that control the ADC in an Arduino Yun (micro-controller side is essentially a Arduino Leonardo) from a sketch or is there a specific file somewhere you must edit?</p>

<p>I am working on a project that collects a signal digitizes it and sends it through a digital filter. I looked up the Datasheet for the micro-controller on the Yun and wrote the code setting the registers associated with the ADC portion of the micro controller. The code setting the registers was something like <code>REGNAME = 0x01;</code>. Yet when tested several of the associated registers were unaffected. Pointers on the process to set the ADC manually without using the <code>analogRead()</code> function would be appreciated.</p>

<p>Edit:
My original code looked something like this:</p>

<pre class="lang-c++ prettyprint-override"><code>void setup(){
    // register to turn on ADC
    REG1 = REG1 | 0x01;

    //register to set sample rate and set ADC to Free Running Mode
    REG2 = REG2 &amp; 0x30;

    // register to set other settings
    REG3 = REG3 | 0xF1;
}

void loop(){
    // code to take samples from ADC
    // and send them through a digital filter here.
}
</code></pre>
 18770: <p>Since your project is about acquiring signals I guess that the sample frequency is important for you. In fact you need to know exactly how much time passes from one sample to another. This cannot be achieved by simple reading the ADC in your loop(). The easiest implementation is to use an interrupt that occurs every n seconds (e.g. every overflow of timerX).</p>

<p>I'm going to write a pseudo-code for this: </p>

<pre><code>void SetTimer()
{  TISREG |= 1&lt;&lt;Prescaler_bit   //set the prescaler
   TICNT = 0   //sdt the tarting value
   TIINT |= 1&lt;&lt;OVF_Interrupt_enable
   sei() //enable global interrupt
}

ISR(TIM1_OVF_vect)   //happens every [prescaler*2^bit_timer/F_CPU] seconds
{
   while( !(ADCSREG &amp;= (1&lt;&lt;bit_is_ready)) ) ; //usually there is a bit that is 1 while the ADC has read the data, and 0 while it is doing the conversions

//imagine the ADC stores the 3 MSBits in the ADCHI
//and the LSBits in the  ADCLO

value_high = (ADCHI &amp; 0b00000111);  
value_low = ADCLO;

value = (value_high &lt;&lt; 7) | value_low;

}
</code></pre>
 7508: <p>I can see no problem with your pseudo-code. You should probably post a
complete testable code in order to get better feedback.</p>

<p>That being said, here are a few suggestions:</p>

<ol>
<li>You should <em>completely set</em> the ADC registers to the values that suit
your application, instead of just changing a few bits here and there.
This is because you should not care about how the Arduino core
library initializes those registers, especially if this is a
prototype for a final product that will not use Arduino core. For
example, instead of</li>
</ol>



<pre class="lang-c++ prettyprint-override"><code>REG1 = REG1 | 0x01;
</code></pre>

<p>you can write something like this:</p>

<pre class="lang-c++ prettyprint-override"><code>REG1 = _BV(FOO)   // use foo mode...
     | _BV(BAR);  // ...in the bar setting
</code></pre>

<p>where <code>_BV(x)</code> is a macro from avr-libc that expands to <code>(1&lt;&lt;(x))</code>,
   and the bit names are those of the datasheet, for better readability.</p>

<ol start="2">
<li><p>You may want to run the digital filter in the ADC interrupt service
routine. If you can split your processing into a fast filter
(processing every sample) and a slow filter (running on decimated
samples), then do only the fast one in the ISR and leave the slow one
to the main loop. I say this from experience: when I tried to do
all the processing in <code>loop()</code> I always lost samples, which did not
happen when I moved the filter to the ISR.</p></li>
<li><p>But then, make sure your filter is optimized to be <em>fast</em>. Avoid
floating point operations in the ISR if at all possible.</p></li>
</ol>

<p>I have written a program that looks very similar to what you are trying
to do: it samples an analog input at 9.6&nbsp;kS/s and runs an
<a href="https://en.wikipedia.org/wiki/Homodyne_detection" rel="nofollow">homodyne detection</a>
filter in order to extract the signal power in a very narrow band around
1&nbsp;kHz. The ADC is set to free running mode. In the ADC ISR, I
retrieve the sample, run it through the filter, and store the result
into volatile variables. In <code>loop()</code>, I read these back from time to
time. Here is how I set the ADC:</p>

<pre class="lang-c++ prettyprint-override"><code>static void configure_adc()
{
    ADMUX  = _BV(REFS0)  // ref = AVCC
           | _BV(ADLAR)  // left adjust result
           | analog_in;  // input channel
    ADCSRB = 0;          // free running mode
    ADCSRA = _BV(ADEN)   // enable
           | _BV(ADSC)   // start conversion
           | _BV(ADATE)  // auto trigger enable
           | _BV(ADIF)   // clear interrupt flag
           | _BV(ADIE)   // interrupt enable
           | 7;          // prescaler = 128
}
</code></pre>

<p>The complete program is available here:
<a href="https://gist.github.com/edgar-bonet/0b03735d70366bc05fc6" rel="nofollow">homodyne.ino</a>.
It was initially intended for an Arduino Uno, but it runs unmodified on
a Leonardo clone.</p>
