I'm using a TPIC6B595 to control a 12V RGB LED strip. It's wired with #G to ground, #SRCLR to +5V, and RCK/SRCK/SER IN to the Arduino Nano (on pins 10/13/11 respectively, which should be #CS/SCLK/MOSI in SPI mode).
This program using shiftOut does work (cycles between blue/green/red, as expected):
#define latchPin 10
#define dataPin 11
#define clockPin 13
// BGR
#define valA 0b100
#define valB 0b010
#define valC 0b001
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void go(byte b) {
digitalWrite(latchPin, LOW);
digitalWrite(clockPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, b);
digitalWrite(latchPin, HIGH);
delay(1000);
}
void loop() {
go(valA);
go(valB);
go(valC);
}
This program using SPI, based on the few examples I could find, is meant to be equivalent but doesn't work — it flashes between white and all-dark every second instead:
#include <SPI.h>
#define latchPin 10
// BGR
#define valA 0b100
#define valB 0b010
#define valC 0b001
void setup() {
pinMode(latchPin, OUTPUT);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
}
void go(byte b) {
SPI.transfer(b);
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
delay(1000);
}
void loop() {
go(valA);
go(valB);
go(valC);
}
I was confused about why the digitalWrite(latchPin, [...]); lines are necessary (shouldn't SPI.transfer take pin 10 low at the start and high at the end?), but removing them caused the lights to stay white, and moving the LOW line above SPI.transfer gave white-dark cycling again.
The white-dark cycling occurs even if I send only one value repeatedly (e.g. comment out the second two calls to go).
What's the more-correct way to use SPI here?