Sunday, June 10, 2012

Implementing Artcfox's TLC5940 code on an Arduino, Part 1

In a previous post I explained how to port Matt Pandina's TLC5940 library to work with an Arduino. While that post would allow you to port the code and flash it to an Arduino I didn't go into detail on getting the code to work in an actual circuit.

+Al McElmon prompted me to get my arse into gear and so:

Over the next couple of posts I'm going to do exactly that by referring to some of the chapters and code from Matt's book and providing code and schematics that should build working examples.

This first post will focus on Chapter 3. Lets kick off with a wiring diagram:



As far as the code goes, I have used the code from Chapter3 in the zip file that can be found on Matt's site
You will need to create:
  • An Arduino project file (.ino for the Arduino IDE 1.0+) with the following contents:
extern "C"
{
  #include "main.h"
}


void setup()
{
}

void loop()
{
  themain();
}
  • Create the following main.h file:
#ifndef __MAIN_H
#define __MAIN_H

#define PB0 PINB0
#define PB1 PINB1
#define PB2 PINB2
#define PB3 PINB3
#define PB5 PINB5


#define PC0 PINC0
#define PC1 PINC1
#define PC2 PINC3

#define PD1 PIND1
#define PD3 PIND3
#define PD4 PIND4
#define PD5 PIND5
#define PD6 PIND6
#define PD7 PIND7

int themain(void);

#endif
  • Copy the main.c from the "/code/ch3" dir in Matt's source archive.
  • Edit the main.c file to add #include "main.h" after the first two includes:
#include <stdint.h>
#include <avr/io.h>
#include "main.h"
  • And finally rename the function "int main(void)" to "int themain(void)"
And you should be good to go. The CH3 code doesn't do too much apart from turning a few channels on (see the gsData array for details). If you connected a single LED as per the diagram then it should light.
In a future post I will look into some more exciting code, re-programming the fuses on the Arduino to enable the clock output and other bits necessary in order to get Matt's full library working.
Until then, happy hacking!
-(e)