#include <Adafruit_TLC5947.h>
#include <SPI.h>
const int slaveSelectPin = 10;
#define clock 13 // 10
#define data 11
#define oe 15 // 12
#define latch 10 // 11
#define NUM_TLC5974 2
unsigned long frame;
Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch, oe);
unsigned long tick, dur;
void setup() {
pinMode(latch, OUTPUT);
digitalWrite(latch, LOW);
pinMode(clock, OUTPUT);
digitalWrite(clock, LOW);
pinMode(data, OUTPUT);
digitalWrite(data, LOW);
SPI.setCS(10);
SPI.setMOSI(11);
SPI.setSCK(13);
SPI.begin();
//tlc.begin();
// Serial.begin(38400);
// delay(1000);
// Serial.println("TLC5974_SPI test");
}
void loop() {
//////
tick = micros();
colorWipe(0, 0, 100); // "Red" (depending on your LED wiring);
Serial.print((micros() - tick));
Serial.print(" red used");
Serial.println(" us");
delay(1500);
//////
tick = micros();
colorWipe(0, 100, 0); // "Green" (depending on your LED wiring);
Serial.print((micros() - tick));
Serial.print(" green used");
Serial.println(" us");
delay(1500);
//////
tick = micros();
colorWipe(100, 0, 0); // "Blue" (depending on your LED wiring);
Serial.print((micros() - tick));
Serial.print(" blue used");
Serial.println(" us");
delay(1500);
//////
tick = micros();
colorWipe(5, 5, 5); // "Blue" (depending on your LED wiring);
Serial.print((micros() - tick));
Serial.print(" white used");
Serial.println(" us");
}
void colorWipe(uint16_t r, uint16_t g, uint16_t b) {
for (uint16_t i = 0; i < 8 * NUM_TLC5974; i++) {
tlc.setLED(i, r, g, b);
}
SPIwrite();
}
void SPIwrite() {
#define TSPEED 1000000 //1Mhz
unsigned int chan1 = 0;
unsigned int chan2 = 0;
byte address1 = 0;
byte address2 = 0;
byte address3 = 0;
SPISettings TLC5947(TSPEED , MSBFIRST, SPI_MODE0);
// packing each 2 channel (12bit*2) to 3 byte (8bit*3) for transfering
SPI.beginTransaction(TLC5947);
// digitalWriteFast(latch, HIGH);
digitalWriteFast(latch, LOW);
for (unsigned int ledpos = 24 * NUM_TLC5974 ; ledpos > 0; ledpos = ledpos - 2) {
chan1 = tlc.getCHAN(ledpos - 1);
chan2 = tlc.getCHAN(ledpos - 2);
address1 = (byte)(chan1 >> 4) ;
address2 = (byte)((chan1 << 4) & (B11110000)) + (byte)((chan2 >> 8) & (B00001111));
address3 = (byte)chan2;
/* address1 = B00000000;
address2 = B00000000;
address3 = B00000000;*/
SPI.transfer(address1);
SPI.transfer(address2);
SPI.transfer(address3);
}
SPI.endTransaction();
// digitalWriteFast(latch, LOW);
digitalWriteFast(latch, HIGH);
digitalWriteFast(latch, LOW);
digitalWriteFast(clock, LOW);
}