This library creates the interface to operate the TLC5940. This device manages 16 PWM outputs.

tlc5940.cpp

Committer:
Fiuba
Date:
2010-11-27
Revision:
0:64ea4d75027c
Child:
1:e8c8347fa919

File content as of revision 0:64ea4d75027c:

#include "tlc5940.h"
#include "mbed.h"

/** Create a tlc5940 interface object connected to some specifics pins
 *
 * @param DC_data[] Dot Correction values for initialization
 */
tlc5940::tlc5940 (int DC_data[]) : VPROG(p21), SIN(p22), SCLK(p23), XLAT(p24), BLANK(p25), DCPROG(p27), GSCLK(p26), SOUT(p28), XERR(p29) {
    first_cycle_flag = false;

    // Pins in startup state
    GSCLK = 0;
    SCLK = 0;
    VPROG = 1;
    XLAT = 0;
    BLANK = 1;
    DCPROG = 0;
    wait(0.01);

    // DC input cycle starts
    DCPROG = 1;
    VPROG = 1;

    for (int counter=0; counter < (2*96); counter++) {
        SIN = DC_data[counter];
        SCLK = 1;
        wait_us(5);
        SCLK = 0;
        wait_us(5);
    }
    XLAT = 1;
    wait_us(5);
    XLAT = 0;
    wait_us(5);
    DCPROG = 0;
    // DC input cycle ends
}

/** Send the specified set of grayscale values
 *
 * @param data[] Array of 12-bit Grayscale values for transmission
 */
void tlc5940::send_data (int data[]) {
    // Grayscale data input + Grayscale PWM
    data_counter = 0;
    GSCLK_counter = 0;

    if (VPROG == 1) {
        VPROG = 0;
        first_cycle_flag = true;
    }

    // Send the new data
    BLANK = 0;
    for (GSCLK_counter = 0; GSCLK_counter <= 4095; GSCLK_counter++) {
        if (data_counter < 2*192) {
            // Every new led consists of 12 bits
            aux_ind = data_counter % 12;
            if ( aux_ind == 0 ) aux_value = data[data_counter/12];
            // Send the last bit
            SIN = (aux_value >> aux_ind) & 0x01;

            SCLK = 1;
            GSCLK = 1;
            SCLK = 0;
            GSCLK = 0;

            data_counter++;
        } else {
            GSCLK = 1;
            GSCLK = 0;
        }
    }
    BLANK = 1;

    XLAT = 1;
    XLAT = 0;

    if (first_cycle_flag) {
        SCLK = 1;
        SCLK = 0;
        first_cycle_flag = false;
    }
}