Arduino mega2560 read 16bit input

hello guys
i need help to debug my code for reading 16 pins in arduino mega2560
i need to modify the code so that it takes the minimum time to finish the task.
here is my code:

int k;
byte value[2048][2];
void setup() {
** // put your setup code here, to run once:**
** DDRL = 0xC0; //43(PL6) is enable, 42(PL7) is clk out**
** DDRA = 0x00; //input of HOB 22 - 29**
** PORTA = 0xFF; //pull up enabled for port A**
** DDRC = 0x00; //input og LOB 37 - 30**
** PORTC = 0xFF; //pull up enabled for port C**
** Serial.begin(9600);**
}
void loop() {
** // put your main code here, to run repeatedly:**
** read_data(0);**
}
void read_data(int enable)
{
** if (enable == 0)**
** {**
** int time = micros();**
** Serial.println("start");**
** for (k = 0; k < 4096; i++)**
** {**
** PORTL = 0x80;**
** PORTL = 0x00;**
** value[k][0] = PINA;**
** value[k][1] = PINC;**
** }**
** time = micros() - time;**
** Serial.println(time);**
** Serial.println("done");**
** }**
}

plse help me :slight_smile: :slight_smile:

You need to clear the serial buffer before doing the timing test:

int k;
byte value[2048][2];

void setup() {
  DDRL = 0xC0;  //43(PL6) is enable, 42(PL7) is clk out
  DDRA = 0x00;   //input of HOB  22 - 29
  PORTA = 0xFF;   //pull up enabled for port A
  DDRC = 0x00;  //input og LOB   37 - 30
  PORTC = 0xFF;   //pull up enabled for port C
  Serial.begin(9600);
}

void loop() {
  read_data(0);
}

void read_data(int enable)
{
  if (enable == 0)
  {
    Serial.println("start");
    Serial.flush();
    unsigned long time = micros();
    for (k = 0; k < 4096; k++){
      PORTL = 0x80;
      PORTL = 0x00;
      value[k][0] = PINA;
      value[k][1] = PINC;
    }
    time = micros() - time;
    Serial.println(time);
    Serial.println("done");
  }
}

Seems to go a bit faster if you change the if statement around a bit:

int k;
byte value[2048][2];

void setup() {
  DDRL = 0xC0;  //43(PL6) is enable, 42(PL7) is clk out
  DDRA = 0x00;   //input of HOB  22 - 29
  PORTA = 0xFF;   //pull up enabled for port A
  DDRC = 0x00;  //input og LOB   37 - 30
  PORTC = 0xFF;   //pull up enabled for port C
  Serial.begin(9600);
}

void loop() {
  read_data(0);
}

void read_data(int enable)
{
  if (enable == 0)
  {
    Serial.println("start");
    Serial.flush();
    unsigned long time = micros();
    for (k = 4096; k != 0 ; k--){
      PORTL = 0x80;
      PORTL = 0x00;
      value[k][0] = PINA;
      value[k][1] = PINC;
    }
    time = micros() - time;
    Serial.println(time);
    Serial.println("done");
  }
}

Ok, about all I can think of. Did notice your array was too small (or is your intent to actually read in 4096 bytes, instead of 8192???)

unsigned int k;
byte value[4096][2];

void setup() {
  DDRL = 0xC0;  //43(PL6) is enable, 42(PL7) is clk out
  DDRA = 0x00;   //input of HOB  22 - 29
  PORTA = 0xFF;   //pull up enabled for port A
  DDRC = 0x00;  //input og LOB   37 - 30
  PORTC = 0xFF;   //pull up enabled for port C
  Serial.begin(9600);
}

void loop() {
  read_data(0);
}

void read_data(int enable)
{
  if (enable == 0)
  {
    Serial.println("start");
    Serial.flush();
    k=4096;
    unsigned long time = micros();
    do{
      k--;
      PORTL = 0x80;
      PORTL = 0x00;
      value[4095-k][0] = PINA;
      value[4095-k][1] = PINC;
    } while (k!= 0);
    time = micros() - time;
    Serial.println(time);
    Serial.println("done");
  }
}