Here is my attempt to demo a TLC5940 PWM LED driver connected to the Launchpad. This code does not use interrupts and follows the basic sample from Demystifying TLC5940: http://sites.google....ing-the-tlc5940
Made using CCS 4.
/*
* TLC5940 Demo for MSP430
* Only PWM functionality is demonstrated
* Dot correction function is not used
* Rotates a pattern of decreasing light intensity on LEDs - OUT0..OUT15
* To pause, press and hold the S2 button on the launchpad
* Launchpad LED1 stays on, LeED2 flashes on every PWM cycle
*/
#include <msp430g2231.h>
// Pin mapping
#define GSCLK_PIN BIT7 // to TLC5940 pin 18
#define SIN_PIN BIT1 // to TLC5940 pin 26
#define SCLK_PIN BIT2 // to TLC5940 pin 25
#define BLANK_PIN BIT4 // to TLC5940 pin 23
#define XLAT_PIN BIT5 // to TLC5940 pin 24
#define LED1_PIN BIT0 // Launchpad LED1 (Red)
#define LED2_PIN BIT6 // Launchpad LED2 (Green)
#define S2_PIN BIT3 // Launchpad S2 button
// Number of daisy-chained TLC5940 ICs
#define TLC5940_N 1
// Useful macros
#define setHigh(n) ( P1OUT |= n )
#define setLow(n) ( P1OUT &= ~n )
#define pulse(n) { setHigh(n); setLow(n); }
// PWM cycles to wait before rotating gsData
#define DELAY 3
// Grayscale values for each channel in reverse order
unsigned int gsData[] = {
0, // Channel 15
0, // Channel 14
0, // Channel 13
0, // Channel 12
BIT1, // Channel 11
BIT2, // Channel 10
BIT3, // Channel 9
BIT4, // Channel 8
BIT5, // Channel 7
BIT6, // Channel 6
BIT7, // Channel 5
BIT8, // Channel 4
BIT9, // Channel 3
BITA, // Channel 2
BITB, // Channel 1
BITB | BITA | BIT9 | BIT8 | BIT7 | BIT6 | BIT5 | BIT4 | BIT3 | BIT2 | BIT1 | BIT0 // Channel 0
} ;
// Update gs during PWM cycle?
unsigned char gsUpdateFlag=1;
void init(void)
{
P1DIR |= GSCLK_PIN | SIN_PIN | SCLK_PIN | BLANK_PIN | XLAT_PIN; // Set output
// Init output pins
setLow(GSCLK_PIN);
setLow(SCLK_PIN);
setLow(XLAT_PIN);
setLow(BLANK_PIN);
}
void GS(void)
{
unsigned int Data_Counter = 0, GSCLK_Counter;
setLow(BLANK_PIN);
for (GSCLK_Counter = 0; GSCLK_Counter < 4096; GSCLK_Counter++)
{
if (gsUpdateFlag && (! (Data_Counter > TLC5940_N * 192 -1)))
{
// Check and load next data bit, MSB first, 12 bits per value
if (gsData[Data_Counter / 12] & (BITB >> ( Data_Counter % 12)))
setHigh(SIN_PIN);
else
setLow(SIN_PIN);
pulse(SCLK_PIN);
Data_Counter++;
P1OUT |= LED2_PIN; // LED2 on
}
else
P1OUT &= ~LED2_PIN; // LED2 off
pulse(GSCLK_PIN);
}
setHigh(BLANK_PIN);
pulse(XLAT_PIN);
gsUpdateFlag = 0; // PWM update finished, reset gsUpdate flag
}
void main(void)
{
int i, t;
WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
// Set DCO to ~15.25 MHz as per datasheet
// Does not use calibration values
BCSCTL1 = RSEL0 | RSEL1 | RSEL2 | RSEL3; // RSELx = 15
DCOCTL = DCO0 | DCO1; // DCOx = 3, MODx = 0
init();
P1DIR |= LED1_PIN | LED2_PIN; // Enable Launchpad LEDs for output
P1OUT |= LED1_PIN; // LED1 on
P1OUT &= ~LED2_PIN; // LED2 off
for (;<img src='http://forum.43oh.com/public/style_emoticons/<#EMO_DIR#>/icon_e_wink.gif' class='bbc_emoticon' alt=';)' /> // Loop forever
{
for (i=0; i < DELAY; i++)
GS();
if (P1IN & S2_PIN) // If S2 not pressed
{
// Rotate gsData
t = gsData[0];
for (i = 0; i < 15; i++)
gsData[i] = gsData[i+1];
gsData[15] = t;
gsUpdateFlag = 1; // Update gs on next PWM cycle
}
}
}












