How to Control a Ton of RGB LEDs with Arduino & TLC5940

ton of rgb leds dxci o

This video explains how to use the 16 channel PWM controller TLC5940

The CODE:

//Texas Instruments TLC 5940 with Arduino
//www.kevindarrah.com

#include <SPI.h>//Serial Peripheral Interface Library

  byte ch=0, chbit=0, spibit=0, spibyte=0;// variables used by tlc sub routine
  int SINData;//variable used to shift data to the TLC
  byte transferbyte[48];// bytes that are sent out to the tlc5940 via SPI
  // 48 because 16 channels @ 12bits gives 384bits, 384/8 = 48 bytes, 12 bit to 8 bit conversion
  byte DCvalue[32];//0-63, 6 bit DOT Correction Bytes
  int i, j, k, l,m,n; //misc variables
  int green, oldgreen= 2048;//for random animation
  int red, oldred = 2048;//for random animation
  int blue, oldblue = 2048;//for random animation
  int comeback=0, pick_color=0;//for random animation
  //*******************************************************************************************
  //*******************************************************************************************
void setup(){//   MAIN SETUP   MAIN SETUP   MAIN SETUP   MAIN SETUP   MAIN SETUP

  pinMode(2, OUTPUT);//XLAT
  pinMode(3, OUTPUT);//OSC2B GSCLK
  pinMode(4, OUTPUT);//VPRG
  pinMode(11, OUTPUT);//MOSI DATA
  pinMode(13, OUTPUT);//SPI Clock

  //Set up the SPI
  SPI.setBitOrder(MSBFIRST);//Most Significant Bit First
  SPI.setDataMode(SPI_MODE0);// Mode 0 Rising edge of data, keep clock low
  SPI.setClockDivider(SPI_CLOCK_DIV4);//Run the data in at 16MHz/4 - 4MHz

  for(i=0; i<48; i++)//clear out Gray Scale Data
  transferbyte[i]=0; 

  for(i=0; i<32; i++)//set Dot Correction data to max (63 decimal for 6 bit)
  DCvalue[i]=63;  

  Serial.begin(115200);//debugging?

  //set up DOT Correction
  DotCorrection();// sub routine helps 

  noInterrupts();// set up the counters, so don't go into interrupts
  TCCR2A=B00010010;//Timer 2 set to Compare Mode Toggling pin 5 @ 8MHz, Arduino Digital 3
  // TIMER 2 IS GSCLCK
  //Timer 2 prescaler set to 1, 16/1=16 MHz, but toggles pin 5 every other cycle, 8MHz
  TCCR2B=B00000001;

 

Read more: How to Control a Ton of RGB LEDs with Arduino & TLC5940

Leave a Comment

Your email address will not be published. Required fields are marked *