Go Down

Topic: Trouble with SPI library (Read 1 time) previous topic - next topic

Divi

Good afternoon everyone,
I'm currently working on a project for school and trying to build and program a 5v5v5 LED Cube.
I already soldered the Cube itself together and designed a circuit board for my shiftregistern and stuff...
I believe I could run my with shiftOut() too but I wanted to learn and understand as much as possible because I'm probably wanna make similar projects later again.
After some research I found out that there is the SPI library which should be exspecially good for my project due to its speed. Unfortunatly I have trouble fitting the SPI in my test projects. (I built up an LED line controlled with an 74hc595 and ULN2803 which I also wanna use in my final project)
I'm using an Arduino Micro to control everything.
Howewer I only used 3 Pins with shiftOut() and it worked fine for me - and left out Output Enable (SCK)
I also tried it with all 4 pins but didn't get the results i was looking for...

To give you an idea here is the simplest version of my testing code:
Code: [Select]
#include <SPI.h>

#define speicherPin 5
#define datenPin 11
#define taktPin 13
#define blank_pin 12

byte shiftZahl=0;
long data;

void setup()
{
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
 
  pinMode(taktPin,OUTPUT);
  pinMode(speicherPin,OUTPUT);
  pinMode(datenPin,OUTPUT);
  pinMode(blank_pin, OUTPUT);
  SPI.begin();
}

void loop()
{
  shiftZahl = B11111111;
//  shiftOut(datenPin,taktPin,LSBFIRST,shiftZahl);
//  digitalWrite(speicherPin,HIGH);
//  digitalWrite(speicherPin,LOW);
 
  PORTD |= 1 << blank_pin;
  SPI.transfer(shiftZahl);
  PORTD |= 1 << speicherPin;
  PORTD &= ~(1 << speicherPin);
  PORTD &= ~(1 << blank_pin);
}


I hope someone can help me and excuse my scholl english please :D
Kenneth

PaulRB

Hi, don't try to use port manipulation, use digitalWrite(). When you can make it work with digitalWrite() then if it still too slow you can try to use port manipulation. But I believe you will find that it is very fast with digitalWrite().

Post your schematic so we can check that for you.

Paul

Paul__B

If it still too slow with digitalWrite() then you have a major failure in your coding.

CrossRoads

I find digitalWrite() too slow.
When I want fast I do this:
Code: [Select]

PORTB = PORTB & 0b11111011; // clear D10 on Uno
SPI.transfer(bits0to7);
SPI.transfer(bits8to15);
SPI.transfer(bits16to23);

SPI.transfer(bits24to31); // only need 1 here for 25 total

PORTB = PORTB | 0b00000100; // set D10 on Uno

No loops, just the 4 SPI transfers to send out the column info for example, then turn on a layer driver.


Also, your code is little messed up, see the notes I added below
Code: [Select]

[color=#222222]#include <SPI.h>[/color][color=#222222][/color]
[color=#222222][/color]
[color=#222222]#define speicherPin 5 [/color]// library also sets up D10 as an output so device can be SPI master - might as well use for data latch (RCLK on '595)[color=#222222][/color]
[color=#222222]// #define datenPin 11  // not needed SPI library sets this up - this is MOSI, goes to Serial In[/color][color=#222222][/color]
[color=#222222]// #define taktPin 13 [/color][color=#222222]  // not needed SPI library sets this up - this is SCK, goes to SRCLK[/color][color=#222222][/color]
[color=#222222]// #define blank_pin 12 [/color][color=#222222]// not needed SPI library sets this up - this is MISO, not used for xfer to '595[/color]
[color=#222222][/color]
[color=#222222]byte shiftZahl=0;[/color][color=#222222][/color]
[color=#222222]long data;[/color][color=#222222][/color]
[color=#222222][/color]
[color=#222222]void setup()[/color][color=#222222][/color]
[color=#222222]{[/color][color=#222222][/color]
[color=#222222]  SPI.setBitOrder(MSBFIRST); // move this after SPI.begin(), also this is a default, so not needed[/color][color=#222222][/color]
[color=#222222]  SPI.setDataMode(SPI_MODE0);[/color][color=#222222]// move this after SPI.begin()[/color][color=#222222], also this is a default, so not needed[/color][color=#222222][/color]
[color=#222222]  SPI.setClockDivider(SPI_CLOCK_DIV2);[/color][color=#222222]// move this after SPI.begin()[/color][color=#222222][/color]
[color=#222222]  [/color][color=#222222][/color]
[color=#222222]  // pinMode(taktPin,OUTPUT); [/color][color=#222222]// not needed SPI library sets this up[/color][color=#222222][/color]
[color=#222222]  pinMode(speicherPin,OUTPUT); [/color][color=#222222][/color]
[color=#222222]  // pinMode(datenPin,OUTPUT); [/color][color=#222222]// not needed SPI library sets this up[/color][color=#222222][/color]
[color=#222222]  // pinMode(blank_pin, OUTPUT); [/color][color=#222222]// not needed SPI library sets this up[/color][color=#222222][/color]
[color=#222222]  SPI.begin();[/color][color=#222222][/color]
[color=#222222]}[/color][color=#222222][/color]
[color=#222222][/color]
[color=#222222]void loop()[/color][color=#222222][/color]
[color=#222222]{[/color][color=#222222][/color]
[color=#222222]  shiftZahl = B11111111;[/color][color=#222222][/color]
[color=#222222]//  shiftOut(datenPin,taktPin,LSBFIRST,shiftZahl);[/color][color=#222222][/color]
[color=#222222]//  digitalWrite(speicherPin,HIGH);[/color][color=#222222][/color]
[color=#222222]//  digitalWrite(speicherPin,LOW);[/color][color=#222222][/color]
[color=#222222]  [/color][color=#222222][/color]
[color=#222222]  PORTD |= 1 << blank_pin; // change to RCLK going low[/color][color=#222222][/color]
[color=#222222]  SPI.transfer(shiftZahl); // SPI transfer does 8 bits at a time - need 4 of them here[/color][color=#222222][/color]
[color=#222222]  PORTD |= 1 << speicherPin;[/color][color=#222222]// change to RCLK going high[/color][color=#222222][/color]
[color=#222222]  PORTD &= ~(1 << speicherPin);[/color][color=#222222]// not needed[/color][color=#222222][/color]
[color=#222222]  PORTD &= ~(1 << blank_pin);[/color][color=#222222]// not needed[/color][color=#222222][/color]
[color=#222222]}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

(I don't know why my copy of your code got all the extra stuff added - do your best to ignore the color tag info that was added).
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Divi

thank you very much for your support...
After taking a look on the Arduino pin mapping and shiftregister datasheet again I decided to reconnect the pins the following way:

Arduio -----> shiftregister
MOSI (13) -----> serial data input
MISO (11) -----> shift register clock input
SCK (12)  -----> storage register clock input

... and left out the SS pin
However my LEDs stay turned off even after randomly trying some other connections :/  :smiley-roll-sweat:
Did I fail connecting them the right way?
Greetings...

CrossRoads

Fix these:
Arduino -----> shiftregister
SCK (13) -----> shift register clock inputs
MOSI (11) -----> serial data input
MISO (12)  -----> not used
SS (10) -----> storage register clock input - or pin 5 I think you had?

Then:
Code: [Select]

digitalWrite (ssPin, LOW);
SPI.transfer(dataByte0); // leave it you to break up your long data type as needed
SPI.transfer(dataByte1);
SPI.transfer(dataByte2);
SPI.transfer(dataByte3);
SPI.transfer(dataByte4);
digitalWrite (ssPin, HIGH);

SRCLR pin to +5
OE (G/) pin to Gnd, or a PWM output for brightness control.
This definitely works. Here are 12 shift registers on a board I offer (only 6 populated here), data is written out the same way, with a '328P providing the Arduino functionality:
http://www.crossroadsfencing.com/BobuinoRev17/



Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Divi

Thank you very much, that pin connection worked nicely!
Unfortunatly I totally failed in choosing the right pins first ... obviously on Arduino Micro the pins have the following pin numbers (just for correctness):

MISO = 14
SCK   = 15
MOSI = 16
SS    = 17

Have a nice day!

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up