title: ATmega2560 ADC channels
body: <p>I have been using ATmega128  and I am now looking a bit into ATmega2560. I have tried ADC in 128 already. When I tried it in 2560 I don't have problem except when I am changing it. I wrote this code in codevision. When using only single channel it works fine but when changing channels there is erratic behaviour. What is the problem ?</p>
  
  <p>Here the code</p>
  
  <pre><code>void ADC_init(void)
  {
       PINF=0;
       PORTF=0;
       PINK=0;
       PORTK=0;
       ADMUX  = 0b01000000;
       ADCSRA =0x87; //ADEN=1,ADIE=0
       ADCSRB=0x00;
  }
  
  void ADC1_read(void)// current - ADC1
  {
      ADMUX=0x41;
      ADCSRA|=0x40; //ASSC=1 (ad_start)
      ADCSRB=0x00;
      while((ADCSRA&amp;(1&lt;&lt;ADSC)));    //while((ADCSRA&amp;0x10)==0);
      ADCSRA |= (1&lt;&lt;ADIF);
      cur_l=ADCL;
      cur_h=ADCH;
      cur_buf=cur_h;//0x03;
      cur_buf=(cur_buf&lt;&lt;8)|cur_l;
      cur_vol = (unsigned long)(((unsigned long)cur_buf*500)/1023);                      
  }
  
  void ADC3_read(void)  //temp sensor - ADC3
  {
      unsigned long temp_volt;
      ADMUX = 0xC3;
      ADCSRA|=0x40; //ASSC=1 (ad_start)
      ADCSRB=0x00;
      while((ADCSRA&amp;0x10)==0);
      ADCSRA |= (1&lt;&lt;ADIF);
      temp_l=ADCL;
      temp_h=ADCH;
      temp_buf=temp_h;//0x03;
      temp_buf&lt;&lt;=8;
      temp_buf|=temp_l;
      temp_volt = (unsigned long)(((unsigned long)temp_buf*256)/1023);    
      temp_analog = (unsigned long)(((unsigned long)temp_buf*256)/1023)-50;
  }
  </code></pre>
  
  <p>The process function inside Main is like this :</p>
  
  <pre><code>void process(void)
  {
      static unsigned char tick_2sec=0, tick_3sec=0;
      unsigned char rx2;
  
      if (tick_1sec){
              tick_2sec++;
              tick_3sec++;
              tick_1sec = 0;            
      }
      if (tick_2sec == 2){
          tick_2sec = 0;
          if (t2 == 1){
              t2 = 0;
              ADC3_read(); //temp            
          }
      }
      if (tick_3sec == 3){
          tick_3sec = 0;
          if (t3 == 1){
              t3 = 0;
              ADC1_read(); //current           
          }
      }                          
  }
  </code></pre>
  
  <p>I used temperature sensor TMP36 to input ADC3 and at 20 C , output voltage is about .7 V. When only ADC 3 is used with 2.56 V reference voltage, the value acquired from ADCH and ADCL is 0x118. Likewise, I connected ADC1 to voltage source that outputs 2.48 volt at normal condition. When I used only ADC1 with reference voltage 5 volt, the output from ADCH and ADCL is 509. And with change in voltage, the ADCH and ADCL registers also change correspondingly. When I called the functions to read ADC1 and ADC3 at intervals of 2 and 3 seconds, as shown in code, at same voltage the ADCH and ADCL contents were different from above. ADC3  had ADCH,L as 137-140 and ADC1 had ADCH,L as 340-352. </p>