Attached is a 10x faster LedControl library.
I just made LedControl library 10 times faster with a small modification. The problem has always been that ShiftOut is a tired dog which runs on 3 legs. In short... It is SLOW!
This modified library uses a routine internal to LedControl.cpp named ShiftOutFast. It gets its speed from directly controlling the SPI transfer via PORTB bit manipulation commands. This puts in a restriction that data control pins must be 8 or higher so you can only have 2 fast SPI units on a single Uno. Usually one is enough. Also, as written, it is MSBFIRST. It could be changed to LSBFIRST with a slight modification. QED.
This same routine works well for LED Matrix and I will upload that to my web site or here in a little while, with examples for driving 8 of 8x8 LED matrices far faster than you can read it. It is just a total blur at full speed. I expect it could drive more but I only have 8 of them in series for now. And the library as currently written only supports 8.
And, unlike some other code I have seen (MD_Parola), this is understandable as well as being blindingly fast. It is easy to modify to make it do what you want. Of course, MD_Parola does a lot more really neat stuff. But it displays backwards (times 2) on my LED Matrices and will take some time to fix.
Here is the centerpiece of the speedup. The clockLow, dataLow, clockHigh and dataHigh are computed in the code setup routine "LedControl" in the .cpp file where the pin numbers are captured.
void LedControl::shiftOutFast(uint8_t val) // Write a byte by software SPI, MSB first.
{
for (uint8_t i = 8; i; i--) { // Run through the 8 data bits outputting them while clocking.
PORTB &= clockLow; // Set Clock pin low.
PORTB &= dataLow; // Set Data pin low (might change quite quickly!)
if (val & 0x80) PORTB |= dataHigh; // Data proved to be high, set Data pin High now.
PORTB |= clockHigh; // Set Clock pin high. Data now captured in unit.
val <<= 1; // Shift user data byte one left for next time.
}
} // end of shiftOutFast routine.
It is unclear to me why this code, having been known for at least 5 years, is not already in the standard library under ShiftOut or, since it only works on PORTB, call if ShiftOutB or whatever.
Enjoy your new speed,
Mike
LedControl.zip (16.3 KB)