Go Down

Topic: Mega2560 Read all Pins (Read 4633 times) previous topic - next topic

ripper121

How fast can i read out all pins (Digital or ADC values) and save it to a array with a Arduino Mega2560?

CrossRoads

#1
Sep 12, 2013, 10:10 pm Last Edit: Sep 12, 2013, 10:19 pm by CrossRoads Reason: 1
With direct port manipulation, quite fast:
Code: [Select]

byte inputArray[13]; // How many are there? Check the data sheet.

inputArray[0] = PORTA;
inputArray[1] = PORTB;
inputArray[2] = PORTC;
inputArray[3] = PORTD;
inputArray[4] = PORTE;
inputArray[5] = PORTF;
inputArray[6] = PORTG;
inputArray[7] = PORTH;
inputArray[8] = PORTI;
inputArray[9] = PORTJ;
inputArray[10] = PORTK;
inputArray[11] = PORTL;
inputArray[12] = PORTM;

// ADC would be a little different:
int analogArray[16];  // 16 inputs?

analogArray[0] = analogRead(A0);
analogArray[1] = analogRead(A1);
analogArray[2] = analogRead(A2);
analogArray[3] = analogRead(A3);
analogArray[4] = analogRead(A4);
analogArray[5] = analogRead(A5);
analogArray[6] = analogRead(A6);
analogArray[7] = analogRead(A7);
analogArray[8] = analogRead(A8);
analogArray[9] = analogRead(A9);
analogArray[10] = analogRead(A10);
analogArray[11] = analogRead(A11);
analogArray[12] = analogRead(A12);
analogArray[13] = analogRead(A13);
analogArray[14] = analogRead(A14);
analogArray[15] = analogRead(A15);


Don't put them either of them in a loop, that just adds around 12uS per read.
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.

ripper121

And what if i will check 56 pins if they high and safe it to an array?
All checks must be done in under 20us!

CrossRoads

Try it - you don't need anything connected to do it.

startTime = micros();
// add the  reads above
endTime = micros();
duration = endTime -startTime;
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.

ripper121

I have no Mega2560 but i will know it bevor i buy it for my projekt.

ripper121

Can someone test it for me with a mega2560?

CrossRoads

Do you have any kind of Arduino? Read the same port 10, 12 times, see what you get. Reading multiple ports will yield the same result.
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.

Simpson_Jr

Apparently the 2560 doesn't have a PORTI and PORTL is the last port.

Anyway, this is the code I used. The ADC-pins can... be read at different speeds, I included normal (slowest) and fastest way. (there are 8 speedsettings and faster probably results in less accurate readings)

Code: [Select]

// needed to switch to adc-fast mode later
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  byte inputArray[11]; // How many are there? Check the data sheet.
  int analogArray[16];  // 16 inputs?
  long startTime;

  startTime = micros();
  inputArray[0] = PORTA;
  inputArray[1] = PORTB;
  inputArray[2] = PORTC;
  inputArray[3] = PORTD;
  inputArray[4] = PORTE;
  inputArray[5] = PORTF;
  inputArray[6] = PORTG;
  inputArray[7] = PORTH;
  inputArray[8] = PORTJ;
  inputArray[9] = PORTK;
  inputArray[10] = PORTL;
  startTime = micros()- startTime;
  Serial.print("Digital  = ");
  Serial.print(startTime);
  Serial.println( " microseconds");

  // ADC test normal (slowest)
  startTime = micros();

  analogArray[0] = analogRead(A0);
  analogArray[1] = analogRead(A1);
  analogArray[2] = analogRead(A2);
  analogArray[3] = analogRead(A3);
  analogArray[4] = analogRead(A4);
  analogArray[5] = analogRead(A5);
  analogArray[6] = analogRead(A6);
  analogArray[7] = analogRead(A7);
  analogArray[8] = analogRead(A8);
  analogArray[9] = analogRead(A9);
  analogArray[10] = analogRead(A10);
  analogArray[11] = analogRead(A11);
  analogArray[12] = analogRead(A12);
  analogArray[13] = analogRead(A13);
  analogArray[14] = analogRead(A14);
  analogArray[15] = analogRead(A15);
  startTime = micros()- startTime;
  Serial.print("Analog   = ");
  Serial.print(startTime);
  Serial.println( " microseconds");

  // fastest ADC - mode
  cbi(ADCSRA,ADPS2) ;
  cbi(ADCSRA,ADPS1) ;
  cbi(ADCSRA,ADPS0) ;
  startTime = micros();

  analogArray[0] = analogRead(A0);
  analogArray[1] = analogRead(A1);
  analogArray[2] = analogRead(A2);
  analogArray[3] = analogRead(A3);
  analogArray[4] = analogRead(A4);
  analogArray[5] = analogRead(A5);
  analogArray[6] = analogRead(A6);
  analogArray[7] = analogRead(A7);
  analogArray[8] = analogRead(A8);
  analogArray[9] = analogRead(A9);
  analogArray[10] = analogRead(A10);
  analogArray[11] = analogRead(A11);
  analogArray[12] = analogRead(A12);
  analogArray[13] = analogRead(A13);
  analogArray[14] = analogRead(A14);
  analogArray[15] = analogRead(A15);
  startTime = micros()- startTime;
  Serial.print("Analog fast  = ");
  Serial.print(startTime);
  Serial.println( " microseconds");   
}

void loop() {
  // put your main code here, to run repeatedly:
}



And here's the result using my 2560.

Code: [Select]
Digital  = 4 microseconds
Analog   = 1896 microseconds
Analog fast  = 108 microseconds



Cristian76

Good morning,
could you please suggest me the simplest way to read all 16 analog inputs of Mega2560 ?
Thanks a lot.

Cristiano

Simpson_Jr

Hi, welcome to this forum,

Probably by using an array.

Code: [Select]
int analogArray[16];  // Declare array to store values of all analogue pins
void setup() {
  Serial.begin(115200);

  // Here's the answer to your question
  for (int x = 0; x < 16; x++) {
    analogArray[x] = analogRead(x);
    delay(1); // some settling time for the ADC before next reading is started
  }

  // Show values read
  for (int x = 0; x < 16; x++) {
    Serial.print("Analog value of pin A");
    Serial.print(x);
    Serial.print(" is ");
    Serial.println(analogArray[x]);
  }

}
void loop() {
  // put your main code here, to run repeatedly:

}

amrithmmh

i am doing similar project where all 16 analog pins will have a pressure sensor attached to it and data should be read from all of them fastest way possible with decent accuracy and the sampling frequency of each signal shouild be 2kHz atleast is this possible?

Grumpy_Mike

Start your own thread, this one started in 2013!

Go Up