LedControl icon indicating copy to clipboard operation
LedControl copied to clipboard

Performance improvements with use of SPI API

Open pasquyonline opened this issue 9 years ago • 5 comments

Hi, I have changed just 2 rows of the LedControl.cpp so that the library can use the SPI protocol. I added

  1. #include <SPI.h> as the first row,
  2. SPI.transfer(spidata[i-1]); at row 206 instead of shiftOut(SPI_MOSI,SPI_CLK,MSBFIRST,spidata[i-1]);

With this simple changes now I'm able to show a message with an high scrolling rate (depending also on how you initialize the SPI interface). This is the initialization chunk in the setup() function:

... SPISettings settingsA(8000000, MSBFIRST, SPI_MODE0);

void setup() { // initialize SPI: SPI.begin(); SPI.beginTransaction(settingsA); ... }

dsc_8394 dsc_8401

pasquyonline avatar Sep 20 '15 13:09 pasquyonline

Yes, thanks for the enhancement.

I know that hardware SPI is much faster than my bit-toggle solution. By the time I wrote the LedControl library the arduino IDE did not have a SPI-library and using SPI would frequently clash with other SPI-controlled devices.

I never changed the LedControl implementation because (for me) one of the main features is that you can use any 3 IO-Pins to connect your device. You are not tied to the Hardware-SPI pins.

If you don't mind I'll leave this issue open so others can see how you did it...

Thanks for the contribution Eberhard

wayoda avatar Sep 20 '15 13:09 wayoda

OK, thanks for your answer. I'm agree to leave the issue open. If you are interested, I have also do a porting to the Raspberry Pi (very easy thanks to your clean code, always with the SPI) that use the bcm2835 library.

pasquyonline avatar Sep 20 '15 14:09 pasquyonline

I love the library but would also like to have the option of hardware SPI. Can we not merge the two techniques and give the users the option of hardware vs bit banged?

nkolban avatar Dec 28 '15 06:12 nkolban

I have a created a fork that allows the choice between hardware and software SPI.

gordoste avatar May 03 '19 12:05 gordoste

@pasquyonline thank you very much for sharing your code. I ran in the same issue while trying to create an accurate clock (milliseconds accuracy) using the 8x8 matrix. Finally in addition to what you pointed out I have to add two calls to SPI.beginTransaction and SPI.endTransaction otherwise the code didn't work. So my final working code is as following:

SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0)); for(int i=maxbytes;i>0;i--) //shiftOut(SPI_MOSI,SPI_CLK,MSBFIRST,spidata[i-1]); SPI.transfer(spidata[i-1]); SPI.endTransaction();

BTW, the following https://arduino.stackexchange.com/questions/16348/how-do-you-use-spi-on-an-arduino have a good explanation on the SPI nomenclature (I recommended it because I was having hard time to understand to mapping between DIN, CS, and CLK to MOSI, SCK and SS)

rkachach avatar Sep 12 '19 14:09 rkachach