Go Down

Topic: funny A/D channel readings with Bobuino-1284 ??? (Read 4013 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.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

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.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

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

oric_dan

#5
Jan 22, 2013, 06:06 am Last Edit: Jan 22, 2013, 06:15 am by oric_dan Reason: 1
Same thing with your code, V applied to pin 40.
Quote
0...7:  1023 0 0 0 0 0 0 0
0...7:  0 0 0 0 0 0 0 0


Code: [Select]
int analogArray[] = {0,1,2,3,4,5,6,7};

/**************************************************/
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(" 0...7: ");  
   for( i=0; i<8; i++) {
     val=analogRead(analogArray[i]);
     a_print(" ",val);
     delay(100);
   }
 }    
}


At least I'm not having any problems whatsoever uploading sketches to the chip, like
the guys on the other threads, :-).

CrossRoads

Well, that's what you want now, yes?
0 with jumpers to Gnd, and 1023 when pulled high?
Try the other pins with a pot from 0 to 5V and see that they follow the pot voltage.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

oric_dan

I tried the pot on pins 40 and 39, modifying the channel scanned in the first loop,
and still works the same - ie, channels skewed. The last test, I used analogRead(21)
rather than analogRead(A7).
Quote
all A0, signal on pin 40:  0 0 68 465 733 935 1023 1023
A0..A7:  1023 0 0 0 0 0 0 0
0...7:  1023 0 0 0 0 0 0 0
all A0, signal on pin 40:  1023 1023 922 751 473 316 62 0
A0..A7:  0 0 0 0 0 0 0 0
0...7:  0 0 0 0 0 0 0 0

all A7, signal on pin 39:  0 208 635 899 1023 1023 1023 1023
A0..A7:  0 0 0 0 0 0 0 1023
0...7:  0 0 0 0 0 0 0 1023
all A7, signal on pin 39:  1023 1023 769 437 157 0 0 0
A0..A7:  0 0 0 0 0 0 0 0
0...7:  0 0 0 0 0 0 0 0

all 21, signal on pin 39:  0 196 298 309 591 756 893 1023
A0..A7:  0 0 0 0 0 0 0 1023
0...7:  0 0 0 0 0 0 0 1023
all 21, signal on pin 39:  1023 1023 868 628 342 128 0 0
A0..A7:  0 0 0 0 0 0 0 0
0...7:  0 0 0 0 0 0 0 0

oric_dan

BTW, I sent the following email to maniacbug ....
Quote
In your files from here,
http://maniacbug.wordpress.com/2011/11/27/arduino-on-atmega1284p-4/

could you check in the arduino_pins.h file for the Bobuino variant bootloader if
the following line

#define analogPinToChannel(p)       ( (p) < NUM_ANALOG_INPUTS ? NUM_ANALOG_INPUTS - (p) : -1 )

should read as

#define analogPinToChannel(p)       ( (p) < NUM_ANALOG_INPUTS ? (NUM_ANALOG_INPUTS-1) - (p) : -1 )

Thanks. I'd really appreciate it - been spending days trying to get this chip in line.

oric_dan

I don't know if maniacbug is going to respond, but I'm 99% sure this is the problem.
The bootloader is configured wrong. I changed that line, and reburned the Bobuino
optiboot into my chip, and it works correctly now.

CrossRoads

Is that from the bootloader, or from pins_arduino.h for the 1284?

I don't think I ever tried the analog inputs, I've only been using the digital pins.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

retrolefty


I don't know if maniacbug is going to respond, but I'm 99% sure this is the problem.
The bootloader is configured wrong. I changed that line, and reburned the Bobuino
optiboot into my chip, and it works correctly now.


What does the bootloader have to do with analog pin number assignments?

pins_arduino.h is used when compiling a sketch only.

Lefty

oric_dan

My mistake about this info being burned into the bootloader - I'm still trying to
sort out all the #defines and customization files and avrdudes and IDE folders,
on and on.

However, what I did was modify the file indicated in the previous post,
Quote
arduino_pins.h file for the Bobuino variant bootloader

located in the Arduino sketch folder under
..\Arduino\hardware\mighty-1284p-master\variants\bobuino

Would be nice if someone else could check this.

CrossRoads

Working on college financial aid application the next couple of nights, don't think I can get to it very soon. It is amazing the level they dig into - much more pervasive than doing annual tax returns.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

retrolefty


My mistake about this info being burned into the bootloader - I'm still trying to
sort out all the #defines and customization files and avrdudes and IDE folders,
on and on.

However, what I did was modify the file indicated in the previous post,
Quote
arduino_pins.h file for the Bobuino variant bootloader

located in the Arduino sketch folder under
..\Arduino\hardware\mighty-1284p-master\variants\bobuino

Would be nice if someone else could check this.



Check what? That is the folder (\bobuino) where the 'custom' pins_arduino.h file needs to be located for the bobuino variant board version.

Lefty

Go Up