Hi,
I have a setup with two shift registers connected via SPI to my microcontroller (teensy).
I am experiencing some strange code behaviour that I hope you can help me sort out.
The principle is simple: send two bytes where the first goes to the last shift register and the second goes to the first one.
Everything seems to work fine if the number is larger than 1,
so
SPI.transfer(B01010000); //last shift register
SPI.transfer(B10100000); //first shift register
works fine.
Also if the first byte sent is 0 things works as expected:
SPI.transfer(B00000000); //last shift register
SPI.transfer(B10100000); //first shift register
This however doesn not work:
SPI.transfer(B01010000); //last shift register
SPI.transfer(B00000000); //first shift register[/code]
When I reverse the byte order the last shift register behaves not as I would expect:
It turns on the LEDs once and then switches them off (after a 1 second delay in my loop).
If I put everything in setup() so it only runs once it does work, so something goes wrong when it is looping.
I am aware that it doesn´t really make sense to loop this code, it is just a simplified version of the sketch.
Here is the full code for the setup version:
#include <SPI.h>
int latchPin = 8;
byte spiModePin = 10;
void setup() {
pinMode(latchPin, OUTPUT);
digitalWrite (latchPin, HIGH);
pinMode (spiModePin, OUTPUT); // needed for device to be SPI master, if an input and it goes low device becomes SPI slave
SPI.begin();
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
digitalWrite(latchPin, LOW);
//this works when only set once:
SPI.transfer(B10100000); //last shift register
SPI.transfer(B00000000); //first shift register
digitalWrite(latchPin, HIGH);
SPI.endTransaction();
}
void loop() {
delay(1000);
}
Here is the non-working version when placed in loop():
#include <SPI.h>
int latchPin = 8;
byte spiModePin = 10;
void setup() {
pinMode(latchPin, OUTPUT);
digitalWrite (latchPin, HIGH);
pinMode (spiModePin, OUTPUT); // needed for device to be SPI master, if an input and it goes low device becomes SPI slave
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
digitalWrite(latchPin, LOW);
//this doesn´t work:
SPI.transfer(B01010000); //last shift register
SPI.transfer(B00000000); //first shift register
//this works:
// SPI.transfer(B00000000); //last shift register
// SPI.transfer(B10100000); //first shift register
digitalWrite(latchPin, HIGH);
SPI.endTransaction();
delay(1000);
}
Thanks for any insight.
HC
