wayoda / LedControl

An Arduino library for MAX7219 and MAX7221 Led display drivers
Other
464 stars 243 forks source link

Performance improvements with use of SPI API #8

Open pasquyonline opened 8 years ago

pasquyonline commented 8 years ago

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

wayoda commented 8 years ago

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

pasquyonline commented 8 years ago

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.

nkolban commented 8 years ago

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?

gordoste commented 5 years ago

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

rkachach commented 4 years ago

@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)