understanding and using the tlc5940 library

Hello all,

I have a very basic knowledge of electronics and maybe even less of programming. For school projects and for fun, I have been working with my arduino for a while now. I can write some simple functions and make my arduino do what I want if I want to. But I am not so familiar with more complex programming and for example with shift registers and other IC's.

Right now I am building a very large 3 digit 7 segment myself. It is a very large wooden box with a single red LED for each segment of each digit in it. So it consists of 21 LED's. I have got my hands on 2 tlc5940's yesterday. From my research on the arduino forums and the wide capability of these chips. I thought it would be a nice idea to start using them with a simple setup like this one.

What I would like to do is to use these digits for the time being as a counter. So I would like to send the TLC's a number and want the number to be displayed using the corresponding LED's.

Now of course, as I explained before, I am good in building stuff, but not so good in understanding and applying the C language. After taking a long&long look at the TLC5940 library I am now totally confused and really don't know where I should start with the programming.

I do have some basic questions for example, - How do I address each LED in my sketch while using the TLC library? - How does this whole system of libraries work? What do I have to change in which document inside the library folder in order to set my chip up correctly.

I know already that I can actually use simpler chips such as the 74HC595 but i want to take on the challenge to learn to use the TLC for possible future projects.

I would be more than happy to hear if somebody could at least point out a direction for me to start from. My main problem is that I have no idea as to how I should work with this library and for example which commands I can use and where they are listed.

Thanks! Argun

The library comes with lots of example programs. All you need to do is to look at them and run one or two to see how the commands are used.
Basically one commands sets an LED number in memory, and the other command transfers tat memory to the TLC chip itself so you can see the results.

Hi Grumpy_Mike,

thanks for your reply, I will try it out as soon as I can.(when I get home)
but for the time being, for example I checked the BasicUse example in the library,

it looks a little like this

void setup()
{
  /* Call Tlc.init() to setup the tlc.
     You can optionally pass an initial PWM value (0 - 4095) for all channels.*/
  Tlc.init();
}

/* This loop will create a Knight Rider-like effect if you have LEDs plugged
   into all the TLC outputs.  NUM_TLCS is defined in "tlc_config.h" in the
   library folder.  After editing tlc_config.h for your setup, delete the
   Tlc5940.o file to save the changes. */

void loop()
{
  int direction = 1;
  for (int channel = 0; channel < NUM_TLCS * 16; channel += direction) {

    /* Tlc.clear() sets all the grayscale values to zero, but does not send
       them to the TLCs.  To actually send the data, call Tlc.update() */
    Tlc.clear();

    /* Tlc.set(channel (0-15), value (0-4095)) sets the grayscale value for
       one channel (15 is OUT15 on the first TLC, if multiple TLCs are daisy-
       chained, then channel = 16 would be OUT0 of the second TLC, etc.).

       value goes from off (0) to always on (4095).

       Like Tlc.clear(), this function only sets up the data, Tlc.update()
       will send the data. */
    if (channel == 0) {
      direction = 1;
    } else {
      Tlc.set(channel - 1, 1000);
    }
    Tlc.set(channel, 4095);
    if (channel != NUM_TLCS * 16 - 1) {
      Tlc.set(channel + 1, 1000);
    } else {
      direction = -1;
    }

    /* Tlc.update() sends the data to the TLCs.  This is when the LEDs will
       actually change. */
    Tlc.update();

    delay(75);
  }

}

commands such as “direction”, “channel”, and “clear” are mentioned and I wonder where i can find a list of all the available commands?

also, in the setup ,it calls something like tlc.init(), which I assume is for initializing the library(or something?? sorry for my knowledge on this subject, i just try to do my best)
but I can’t seem to find it anywhere else in the other documents in the library folder.

could I possibly use these “channel” and “clear” commands to light up/down the LED’s I want?

then I could add something like a switch case for each number to be displayed.
for example if the counter is at 123, it would switch to the case that the first digit is 3 and light up the 5 LED’s for that, second digit is 2 and light up the 5 LED’s for that and for the third digit, it would light up the 2 LED’s that would form the number 1.

does this make sense?

direction and channel are not commands, but variables defined in that sketch.

The methods (what you are calling commands) provided by that library are all prefixed with 'Tlc.'

The Tlc library only provides six methods to use. It's Reference doc is http://students.washington.edu/acleone/codes/tlc5940arduino/html_r012/

You can also read the sources of the library itself to see exactly what each method does.

All that being said, by the sound of it, where you really should be starting is with a basic C/C++ language tutorial. Google will provide an abundant resource of beginner, getting started, and tutorial sites for C/C++.

argh!! beat me to it. :)

jraskell,
thanks for your advice,

I know for sure that I need some basic training in C.

so, these variables that are defined in the sketch should normally come from somewhere right?
normally I would define every variable at the beginning of the sketch. But I assume that in this case, they are defined in the tlc5940.h library. Unfortunately I was unable to locate it in there.( so I actually spent a bit of time reading the source of the libraries but that actually got me more confused in the first place). I am indeed really interested to see what happens in the background with these variables and how they are defined.

i guess the six methods are these by the way,

Core Functions (see the BasicUse Example and Tlc5940):
_ * Tlc.init(int initialValue (0-4095)) - Call this is to setup the timers before using any other Tlc functions. initialValue defaults to zero (all channels off)._
_ * Tlc.clear() - Turns off all channels (Needs Tlc.update())_
* Tlc.set(uint8_t channel (0-(NUM_TLCS * 16 - 1)), int value (0-4095)) - sets the grayscale data for channel. (Needs Tlc.update())
_ * Tlc.setAll(int value(0-4095)) - sets all channels to value. (Needs Tlc.update())_
* uint16_t Tlc.get(uint8_t channel) - returns the grayscale data for channel (see set).
_ * Tlc.update() - Sends the changes from any Tlc.clear’s, Tlc.set’s, or Tlc.setAll’s._

am I right?

so I can better stick to trying to get my LED’s working with these functions to get me started?

so, these variables that are defined in the sketch should normally come from somewhere right?
normally I would define every variable at the beginning of the sketch. But I assume that in this case, they are defined in the tlc5940.h library.

direction is defined at the first line of the loop() function:

  int direction = 1; //direction variable defined as an int

It is also initialized to a value of 1.

channel is defined inside the for() loop, again as an int.

for (int channel = 0; channel < NUM_TLCS * 16; channel += direction) {

channel is also initialized to a value of 0.

am I right?

Yes those functions are all there is. Everything else are friendly named variables to give you a clue as to what they do.

so I can better stick to trying to get my LED's working with these functions to get me started?

Yes see the examples for how they are used and woven into a sketch.

so I actually spent a bit of time reading the source of the libraries but that actually got me more confused in the first place

That is doing stuff like talking to the TLC chip, feeding it with the buffer memory and sorting out where to put brightness information in the buffer memory from a simple LED number. Thia is made more difficult by the fact that each LED takes up 12 bits which is one and a half bytes so there is no simple way of working out what byte address corresponds to what LED. It is also doing stuff like setting up timers and interrupts using the direct registers in the chip.