Help for optimizing a routine

Hi,

I am experimenting with a LED Matrix. I wonder if someone has a idea how to optimize the following routine.

// Interrupt routine
void display() {
  digitalWrite(cols[col], LOW);  // Turn whole previous column off
  col++;
  if (col == 8) {
    col = 0;
  }
  for (int row = 0; row < 8; row++) {
    if (leds[row][col] == 1) {
      digitalWrite(rows[row], LOW);  // Turn on this led
    }
    else {
      digitalWrite(rows[row], HIGH); // Turn off this led
    }
  }
  digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times)
  delayMicroseconds(800);  // Delay so that on times are longer than off time = brighter leds
}

I call this beast inside an interrupt. Maybe direct port manipulating? Could be easy if the pinout of the matrix would be more “in order” but the rows and columns are wildly spreaded.

Other ideas? :-?

Carsten

You can take out this line for starters. It's not needed.

delayMicroseconds(800);

Other than that why do you think it needs optimizing?

Andrew

I dont know what you’re doing with cols or delay, but …

void display()
{
digitalWrite(cols[col], LOW); // Turn whole previous column off
col = (col >= 7) ? 0 : col+1;
for (int row = 0; row < 8; row++)
digitalWrite(rows[row], (leds[row][col] <> 1));
digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times)
delayMicroseconds(800); // Delay so that on times are longer than off time = brighter leds
}

Oh, that delay was in there when the routine was not in the interrupt I guess.

Why optimizing? Well to have more cpu cycles for the main programm :D

Thanks, Carsten

@Ray: Your code works (after changing <> to != I guess you mixed some other language like I always do).

However, timing the original and your code some thousand times (without interrupt), it seems that both codes are equally fast. Seems the compiler is doing a great job here in optimizing the code.

Carsten

8-) Guilty, Delphi!

Glad to know the compiler is that intelligent (thanx)

I think if you really want it to be faster you need to use direct port manipulation rather than digitalWrite(). I’m not too surprised that Ray’s code was similar in speed - it’s doing the same operations just using different C syntax.

That delayMicroseconds(800) has never been needed (it was always an interrupt routine). I wasn’t thinking clearly when I put it in and someone else pointed out it was unnecessary.

Andrew

Uh, hey Andrew its your code :wink: Did not realize that.

Thanks for it. Working like a charm.

Carsten