Go Down

Topic: funny A/D channel readings with Bobuino-1284 ??? (Read 3 times) previous topic - next topic

oric_dan

Would someone please double check this. I tried reading the A/D channels of the
mega1284 loaded with the Bobuino variant of optiboot, and the channel numbering
seems to be offset by one position.

The complete sketch is given below. First off, it steps through analogRead() using
A0..A7 as channel parameter, and then repeats using 0..7. The results are shown
here, edited to show inputs. IE, with voltages on pins 33&34 [supposed to be ADC
channels 0 and 1, the levels end up on channel positions 1 and 2. Similarly, with
voltage applied at pin 40 [defined as ADC 7], the level shows up on channel position
0. The other readings are just noise, since the pins are not terminated.

What is wrong here?
Quote
1284 Analog Test ...
defines: A0=14, A7=21

pin 33=0V, pin 34=5V
--------------------
A0..A7:  524 0 1023 774 611 524 457 417
 0...7:  437 0 1023 875 747 638 555 492
A0..A7:  349 0 1023 793 622 529 460 426
 0...7:  402 0 1023 880 756 646 566 508

pin 40=0V
---------
A0..A7:  0 131 236 272 296 326 348 340
 0...7:  0 131 234 274 296 319 342 336
A0..A7:  0 129 234 265 284 314 345 349
 0...7:  0 107 202 242 267 293 314 324


Code: [Select]
/**********************************************************
file: analog_test1284
revised: 01/21/13, orig 01/21/13.

Tests Bobuino-1284 ADC channels.
***********************************************************/

/**** Variables ****/
const int ledPin = 13;
int ledState = LOW;        
long interval = 500;  // blink interval (msec)
 
#define INTV   5000   // ADC repeat interval.
 
/*****************************************/
void setup()
{      
 pinMode(ledPin, OUTPUT);
 Serial.begin(57600);
 Serial.println("\n1284 Analog Test ...");

 a_print("defines: A0=",A0);
 a_print(", A7=",A7);
}

/******************************************/
void a_print( char *s, int num )
{
 Serial.print(s);  Serial.print(num);
}

/*****************************************************/
void loop()
{
 schedule_blink();
 disp_adc();
}

/**************************************************/
void disp_adc()
{
 static unsigned long prev=0;   // last time LED was updated
 unsigned long current=millis();
 static int cnt=0;
 int i, val=0;
 
 if( (current - prev) > INTV) {
   prev = current;   // schedule next tick.
   nl();
   Serial.print("A0: ");  
   for( i=0; i<8; i++) {
     val = analogRead(A0+i);
     a_print(" ",val);
     delay(100);
   }
   nl();
   Serial.print(" 0: ");  
   for( i=0; i<8; i++) {
     val = analogRead(i);
     a_print(" ",val);
     delay(100);
   }
 }    
}

/*****************************************************/
void schedule_blink()
{
 static unsigned long prev=0;   // last time LED was updated
 unsigned long current=millis();

 if( (current - prev) > interval) {
   prev=current;       // save last time blinked the LED

   // if the LED is off turn it on and vice-versa:
   if( ledState == LOW)  ledState = HIGH;
   else  ledState = LOW;

   // set the LED with the ledState of the variable:
   digitalWrite(ledPin, ledState);
 }
}

void nl() {  Serial.println("");  }

CrossRoads

Try doing 2 reads in a row, gives the voltage a chance to settle thru the multiplexer:
Code: [Select]

      val = analogRead(A0+i);
      val = analogRead(A0+i);
      a_print(" ",val);
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

oric_dan

#2
Jan 22, 2013, 05:34 am Last Edit: Jan 22, 2013, 05:37 am by oric_dan Reason: 1
Yeah, hi Bob, I had actually tried earlier reading the same channel multiple times,
and got the same result with the channels skewed.

The following is the result with the modified code shown below, and with the 0V and
5V applied to pin 40 only, which s/b A/D channel 7.

BTW, I'm not using your board, but have Bobuino-optiboot burned into a mega1284
chip, as mentioned on those 2 other recent threads.
Quote
all A0:  0 0 0 0 0 0 0 0
A0..A7:  0 134 233 264 283 313 340 352
0...7:  0 107 201 243 272 304 348 351

all A0:  1023 1023 1023 1023 1023 1023 1023 1023
A0..A7:  1023 803 653 528 448 413 403 393
0...7:  1023 887 783 670 575 517 478


Code: [Select]
/**************************************************/
void disp_adc()
{
 static unsigned long prev=0;   // last time LED was updated
 unsigned long current=millis();
 static int cnt=0;
 int i, val=0;
 
 if( (current - prev) > INTV) {
   prev = current;   // schedule next tick.
   
   nl();
   Serial.print("all A0: ");  
   for( i=0; i<8; i++) {
     val = analogRead(A0);
     a_print(" ",val);
     delay(100);
   }    
   nl();
   Serial.print("A0..A7: ");  
   for( i=0; i<8; i++) {
     val = analogRead(A0+i);
     val = analogRead(A0+i);
     a_print(" ",val);
     delay(100);
   }
   nl();
   Serial.print(" 0...7: ");  
   for( i=0; i<8; i++) {
     val = analogRead(i);
     val = analogRead(i);
     a_print(" ",val);
     delay(100);
   }
 }    
}

CrossRoads

I am thinking you are reading floating pins.
Instead of (A0 + i)
try putting the pins in an array, and use the array value as the selector
Code: [Select]

analogRead(analogArray[i]);


with the pins defined at the beginning of the sketch

Code: [Select]

analogArray[] = {0,1,2,3,4,5,6,7};
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

oric_dan

I dug up enough jumpers now to terminate all the unused A/D channels, and got this
with 5V,0V on pin 40. It's not a miswire on my protoboard, as I'm applying V straight
to the pin. I'll try your last idea.
Quote
all A0:  1023 1023 1023 1023 1023 1023 1023 1023
A0..A7:  1023 0 0 0 0 0 0 0
0...7:  1023 0 0 0 0 0 0 0

all A0:  0 0 0 0 0 0 0 0
A0..A7:  0 0 0 0 0 0 0 0
0...7:  0 0 0 0 0 0 0 0

Go Up