169 lines
4 KiB
Plaintext
169 lines
4 KiB
Plaintext
//TLC5940NT pin definitions
|
|
#define VPRG 2
|
|
#define SIN 11
|
|
#define SCLK 13
|
|
#define XLAT 4
|
|
#define BLANK 5
|
|
#define DCPRG 6
|
|
#define GSCLK 7
|
|
#define MISO 12
|
|
#define SS 10
|
|
|
|
unsigned long currentMillis, previousMillis;
|
|
int ledState;
|
|
|
|
int level[32] = {
|
|
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095,4095, 4095, 4095, 4095,4095, 4095, 4095, 4095,
|
|
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095,4095, 4095, 4095, 4095,4095, 4095, 4095, 4095};
|
|
|
|
|
|
byte rele[6] = {
|
|
14, 15, 16, 17, 18, 19};
|
|
|
|
|
|
char spi_transfer(volatile byte data)
|
|
{
|
|
SPDR = data; // Start the transmission
|
|
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
|
|
{
|
|
};
|
|
return SPDR; // return the received byte
|
|
}
|
|
|
|
void makemagic(){
|
|
setGreys();
|
|
feedPorts();
|
|
}
|
|
|
|
void feedPorts() {
|
|
// Clock for TLC5940's PWM
|
|
digitalWrite(BLANK, HIGH);
|
|
digitalWrite(BLANK, LOW); //=all outputs ON, start PWM cycle
|
|
for (int i=0; i<4096; i++) {
|
|
pulseGSCLK();
|
|
}
|
|
}
|
|
|
|
void pulseGSCLK() {
|
|
//ultra fast pulse trick, using digitalWrite caused flickering
|
|
PORTD |= 0x80 ; // bring pin 7 high, but don't touch any of the other pins in PORTB
|
|
//16 nanosecs is the min pulse width for the 5940, but no pause seems needed here
|
|
PORTD &= 0x7F; // bring pin 7 low without touching the other pins in PORTB
|
|
}
|
|
|
|
void setGreys() {
|
|
digitalWrite(BLANK, HIGH);
|
|
digitalWrite(XLAT,LOW);
|
|
for(int i = 15; i>=0; i--){
|
|
spi_transfer( (level[2*i+1] & 0x0FF0) >> 4 );
|
|
spi_transfer( ((level[2*i+1] & 0xF) << 4) | ((level[2*i] & 0x0F00) >> 8) );
|
|
spi_transfer( level[2*i] & 0xFF);
|
|
}
|
|
digitalWrite(XLAT,HIGH);
|
|
digitalWrite(XLAT,LOW);
|
|
digitalWrite(BLANK, LOW);
|
|
}
|
|
|
|
|
|
void startup(){
|
|
for(byte x = 0; x < 31; ++x){
|
|
level[x] = 0;
|
|
makemagic();
|
|
}
|
|
|
|
}
|
|
|
|
const byte remap[32] = {
|
|
1,2,3,4,5,6,7,8,9,10,11,12,23,24,25,26,27,28,17,18,19,20,21,22,0,0,0,0,0,0,0,0};
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
pinMode(VPRG, OUTPUT);
|
|
pinMode(SIN, OUTPUT);
|
|
pinMode(SCLK, OUTPUT);
|
|
pinMode(XLAT, OUTPUT);
|
|
pinMode(BLANK, OUTPUT);
|
|
pinMode(DCPRG, OUTPUT);
|
|
pinMode(GSCLK, OUTPUT);
|
|
pinMode(MISO, INPUT);
|
|
pinMode(SS,OUTPUT);
|
|
digitalWrite(SS,HIGH); //disable device
|
|
digitalWrite(SIN, LOW);
|
|
digitalWrite(SCLK, LOW);
|
|
digitalWrite(XLAT, LOW);
|
|
digitalWrite(VPRG, LOW);
|
|
digitalWrite(BLANK, HIGH);
|
|
digitalWrite(GSCLK, HIGH);
|
|
digitalWrite(DCPRG, LOW); // USE EEPROM DC register if LOW
|
|
|
|
//Setup the Hardware SPI registers
|
|
// SPCR = 01010000
|
|
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
|
|
//sample on leading edge of clk,system clock/4 (fastest)
|
|
byte clr;
|
|
SPCR = (1<<SPE)|(1<<MSTR);
|
|
clr=SPSR;
|
|
clr=SPDR;
|
|
delay(10);
|
|
|
|
|
|
for(byte i = 0; i < 6; ++i) pinMode(rele[i], OUTPUT);
|
|
for(byte i = 0; i < 6; ++i) digitalWrite(rele[i], LOW);
|
|
startup();
|
|
|
|
}
|
|
|
|
int ch = 21; // canal base del RGB
|
|
|
|
void loop() {
|
|
|
|
makemagic();
|
|
|
|
unsigned long currentMillis = millis();
|
|
|
|
if(currentMillis - previousMillis > 1000) {
|
|
// save the last time you blinked the LED
|
|
previousMillis = currentMillis;
|
|
|
|
if (ledState == LOW){
|
|
level[remap[ch]] = 4095;
|
|
level[remap[ch+1]] = 0;
|
|
level[remap[ch+2]] = 4095;
|
|
ledState = HIGH;
|
|
}
|
|
else{
|
|
level[remap[ch]] = 4095;
|
|
level[remap[ch+1]] = 4095;
|
|
level[remap[ch+2]] = 0;
|
|
ledState = LOW;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//void rele(int bpm, int step1, int step2, int step3, int step4, int step5, int step6, int step7, int step8, int step9, int step10, int step11, int step12, int step13, int step14, int step15, int step16 )
|
|
void releOn(int tiempo, int estado )
|
|
{
|
|
int ch1 = estado & 0x01; // 0, 1
|
|
int ch2 = (estado>>1) & 0x01; // 2
|
|
int ch3 = (estado>>2) & 0x01; // 4
|
|
int ch4 = (estado>>3) & 0x01; // 8
|
|
int ch5 = (estado>>4) & 0x01; // 16
|
|
int ch6 = (estado>>5) & 0x01; // 32
|
|
digitalWrite(rele[0],ch1);
|
|
digitalWrite(rele[1],ch2);
|
|
digitalWrite(rele[2],ch3);
|
|
digitalWrite(rele[3],ch4);
|
|
digitalWrite(rele[4],ch5);
|
|
digitalWrite(rele[5],ch6);
|
|
|
|
delay(tiempo);
|
|
|
|
}
|
|
|
|
|
|
|