Home » Adjusting TLC5940 libraries to run different frequencies/values

Adjusting TLC5940 libraries to run different frequencies/values

To run the TLC5940 with any other values apart from the DEFAULT values, the tlc_config.h file requires to be changed. While it is easy to do so, it may cause issues if more than one configurations are often required. The solution around this is to create more than one libraries. Main advantage of doing this is to have different libraries for different parameter values (number of TLCs, frequencies) instead of having to change the tlc_config.h file every time a change is required. This should avoid confusion and potential dangers of constantly changing the config file.
I have emailed the creator of the TLC5940 my new libraries and he shall hopefully upload them next to the original library. However, the way to make changes is covered as follows:

1. In the Arduino libraries folder, copy-paste the ‘TLC5940’ folder in to the same location. The folder will paste as something like ‘TLC5940 (2)’.

2. Rename the folder to the name of the new library. E.g. I required my library to run at 244Hz, so I renamed the folder to TLC5940_244. This name will be the name you use to call up the library in your source code.

3. Rename the following files to reflect the new library name, e.g. :

tlc_config.h to tlc244_config.h 
tlc5940.h to tlc5940_244.h 
tlc5940.cpp to tlc5940_244.cpp 

I have renamed them such that they are easy to remember.

4. Next we need to change the names of these files in the above three, as well as change some parameters in the ‘tlc_config.h’ file. While any editing program will do, I would recommend not to use NOTEPAD, as it makes the file look clustered.

clip_image002

I have used MPLAB IDE to edit this, mainly because I already had that on my system. It is Microchip’s IDE to program their chips. It is a free download. Note that Arduino IDE 0.22 does not open these file.

5. For ease, we shall start off with the easiest, Tlc_5940_244.h. Once again I have made changes according to my own requirements. Following changes are required on the following lines:

a. 19. Change TLC5940_H to TLC5940_244_H
b. 20. Change TLC5940_H to TLC5940_244_H
c. 26. Change “tlc_config.h” to “tlc244_config.h”
d. 57. Change TLC5940 to TLC5940_244
e. 84. Change TLC5940 to TLC5940_244

Once you have made these changes, save the file.

6. Next we make similar changes to ‘Tlc_5940_244.cpp’. This file is a bit more complicated than the last file, but once again I will mention the changes required with line numbers so hopefully it should not be too difficult.

a. 25. Change “tlc_config.h” to “tlc244_config.h”
b. 26. Change “Tlc5940.h” to “Tlc5940_244.h”
c. 92. Change Tlc5940 to Tlc5940_244
d. 159. Change Tlc5940 to Tlc5940_244
e. 173. Change Tlc5940 to Tlc5940_244
f. 202. Change Tlc5940 to Tlc5940_244
g. 224. Change Tlc5940 to Tlc5940_244
h. 239. Change Tlc5940 to Tlc5940_244
i. 267. Change Tlc5940 to Tlc5940_244
j. 293. Change Tlc5940 to Tlc5940_244
k. 372. Change Tlc5940 to Tlc5940_244

Once you have made these changes, save the file.

7. The actual changes required are made in the Tlc244_config file. These configure number of TLCs connected, and the frequency output of the PWM operated by these TLCs. Following are the changes required for different frequencies:

a. 19. Change TLC_CONFIG_H to TLC244_CONFIG_H
b. 20. Change TLC_CONFIG_H to TLC244_CONFIG_H
c. 55. #define NUM_TLCS 1: This defines the number of TLCs cascaded together. The default value is 1. It can be any number between 1 and 16. Numbers greater require adjustment of the TLC_CHANNEL_TYPE variable on line 85. I will not be covering that, but it should be easy enough to make the required change.
d. 94. #define TLC_PWM_PERIOD 8192: This defines the frequency according to the formula:

clip_image003

Where fosc is 16MHz for Arduino, and this integer depends on TLC_GSCLK_PERIOD.
e. 101. #define TLC_GSCLK_PERIOD 3: This value is determined by TLC_PWM_PERIOD, and has to be an integer. This restricts the values we can have for TLC_PWM_PERIOD. It is given by the following formula:

clip_image004

f. Using formulae, some example values are:
i. GSCLK: 0, PWM: 2048 for frequency: 3.9kHz
ii. GSCLK: 1, PWM: 4096 for frequency: 1.95kHz
iii. GSCLK: 3, PWM: 8192 for frequency: 973 Hz (DEFAULT VALUE)
iv. GSCLK: 15, PWM: 32768 for frequency : 244 Hz
Since our current example uses 244Hz, we shall change
v. the values in lines 94 and 101 to 32768 and 15 respectively.
g. Once the changes are made, save this file.

8. Now the new library is ready to be used. To call this library, use #include , instead of . Functional use of the chip in the Arduino Sketch is exactly the same as normal.

9. It should be noted that these line numbers/changes apply to R014 of this library, which was released on May 30, 2011 (http://code.google.com/p/tlc5940arduino/downloads/list)
The example libraries I have done may be uploaded to that address as well.

Related Posts

Leave a Reply

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