This library allows control of the TLC5940 PWM driver IC. It supports both normal operation and controlling multiplexed displays.

Dependencies:   FastPWM

Dependents:   TLC5940LEDtreiber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TLC5940.h Source File

TLC5940.h

00001 #ifndef TLC5940_H
00002 #define TLC5940_H
00003 
00004 #include "FastPWM.h"
00005  /*
00006  
00007     ASCII art cat(why not?):
00008          /\_/\
00009     ____/ o o \
00010   /~____  =ø= /
00011  (______)__m_m)
00012  
00013  */
00014  
00015 /**
00016   * SPI speed used by the mbed to communicate with the TLC5940
00017   * The TLC5940 supports up to 30Mhz. This should be kept as high
00018   * as possible to ensure that data has time to be sent each reset cycle.
00019   */
00020 #define SPI_SPEED 30000000
00021 
00022 /**
00023   * The rate at which the GSCLK pin is pulsed
00024   * This also controls how often the reset function is called
00025   * The rate at which the reset function is called can be calculated by: (1/GSCLK_SPEED) * 4096
00026   * The maximum reliable rate is around ~32Mhz. I reccomend keeping this as low as possible because
00027   * a higher rate will use more CPU. Also, this must be low enough to give time for sending new data
00028   * before the completion of a GSCLK cycle (4096 pulses). If you are daisy chaining multiple TLC5940s,
00029   * divide 32Mhz by the number of chips to get a good maximum rate.
00030   */
00031 #define GSCLK_SPEED 2500000
00032 
00033 /**
00034   *  This class controls a TLC5940 PWM driver IC.
00035   *  It supports sending dot correction and grayscale data. However, it does not support error checking or writing the EEPROM.
00036   *  This class uses the FastPWM library by Erik Olieman to continuously pulse the GSLCK pin without CPU intervention. After
00037   *  4096 pulses, the private member funciton reset is called by the ticker. It resets the display by pulsing the BLANK pin. If new
00038   *  data has been set to be sent by the functions setNewGSData or setNewDCData, it is sent here. The definition GSCLK_SPEED in TLC5940.h
00039   *  controls how often this function is called. A higher GSCLK_SPEED will increase the rate at which the screen is updated but also increase
00040   *  CPU time spent in that function. The default value is 1Mhz. The rate at which the reset function is called can be calculated by: 
00041   *  (1/GSCLK_SPEED) * 4096.
00042   *
00043   *  Using the TLC5940 class to control an LED:
00044   *  @code
00045   *  #include "mbed.h"
00046   *  #include "TLC5940.h"
00047   *  
00048   *  // Create the TLC5940 instance
00049   *  TLC5940 tlc(p7, p5, p21, p9, p10, p11, p12, 1);
00050   *  
00051   *  int main()
00052   *  {   
00053   *      // Create a buffer to store the data to be sent
00054   *      unsigned short GSData[16] = { 0x0000 };
00055   *  
00056   *      // Enable the first LED
00057   *      GSData[0] = 0xFFF;
00058   *      
00059   *      // Set the new data
00060   *      tlc.setNewGSData(GSData);
00061   *      
00062   *      while(1)
00063   *      {
00064   *  
00065   *      }
00066   *  }
00067   *  @endcode
00068   */
00069 class TLC5940
00070 {
00071 public:
00072     /**
00073       *  Set up the TLC5940
00074       *  @param SCLK - The SCK pin of the SPI bus
00075       *  @param MOSI - The MOSI pin of the SPI bus
00076       *  @param GSCLK - The GSCLK pin of the TLC5940(s)
00077       *  @param BLANK - The BLANK pin of the TLC5940(s)
00078       *  @param XLAT - The XLAT pin of the TLC5940(s)
00079       *  @param DCPRG - The DCPRG pin of the TLC5940(s)
00080       *  @param VPRG - The VPRG pin of the TLC5940(s)
00081       *  @param number - The number of TLC5940s (if you are daisy chaining)
00082       */
00083     TLC5940(PinName SCLK, PinName MOSI, PinName GSCLK, PinName BLANK,
00084             PinName XLAT, PinName DCPRG, PinName VPRG, const int number = 1);
00085 
00086     /**
00087       *  Set the next chunk of grayscale data to be sent
00088       *  @param data - Array of 16 bit shorts containing 16 12 bit grayscale data chunks per TLC5940
00089       *  @note These must be in intervals of at least (1/GSCLK_SPEED) * 4096 to be sent
00090       */
00091     void setNewGSData(unsigned short* data);
00092 
00093     /**
00094       *  Set the next chunk of dot correction data to be sent
00095       *  @param data - Array of 8 bit chars containing 16 6 bit dot correction data chunks per TLC5940
00096       *  @note These must be in intervals of at least (1/GSCLK_SPEED) * 4096 to be sent. Also, this function is optional. If you do not
00097       *  use it, then the TLC5940 will use the EEPROM, which (by default) conatins the data 0x3F.
00098       */
00099     void setNewDCData(unsigned char* data);
00100 
00101 protected:
00102     /**
00103       *  Set the next chunk of grayscale data to be sent while in the current reset cycle
00104       *  @note This is useful to send the next set of data right after the first is finished being displayed.
00105       *  The primary purpose for this is multiplexing, although it could be used for anything else.
00106       */
00107     virtual void setNextData() {}
00108 
00109 
00110     // Number of TLC5940s in series
00111     const int number;
00112 
00113 private:
00114     // SPI port - only MOSI and SCK are used
00115     SPI spi;
00116 
00117     // PWM output using the FastPWM library by Erik Olieman
00118     FastPWM gsclk;
00119 
00120     // Digital out pins used for the TLC5940
00121     DigitalOut blank;
00122     DigitalOut xlat;
00123     DigitalOut dcprg;
00124     DigitalOut vprg;
00125 
00126     // Call a reset function to manage sending data and GSCLK updating
00127     Ticker reset_ticker;
00128 
00129     // Has new GS/DC data been loaded?
00130     volatile bool newGSData;
00131     volatile bool newDCData;
00132 
00133     // Do we need to send an XLAT pulse? (Was GS data clocked in last reset?)
00134     volatile bool need_xlat;
00135 
00136     // Buffers to store data until it is sent
00137     unsigned short* gsBuffer;
00138     unsigned char* dcBuffer;
00139 
00140     // Function to reset the display and send the next chunks of data
00141     void reset();
00142 };
00143 
00144 
00145 /**
00146   *  This class allows a TLC5940 to be multiplexed.
00147   *  It inherits the TLC5940 class and uses it to control the TLC5940 driver(s). It does not support sending dot corection data.
00148   *  This class sets the new grayscale data every iteration of the GSCLK reset loop. It then updates the current row using the
00149   *  user defined function SetRows. The framerate you will recieve using this function can be calculate by: 1 / (((1/GSCLK_SPEED) * 4096) * rows).
00150   *  I reccomend maintaining a framerate above 30fps. However, keep in mind that as your framerate increases, so does your CPU usage.
00151   *
00152   *  Using the TLC5940Mux class to control an 8x8 LED matrix:
00153   *  @code
00154   *  #include "mbed.h"
00155   *  #include "TLC5940.h"
00156   *  
00157   *  // Bus connecting to the rows of the LED matrix through PNP transistors
00158   *  BusOut rows(p22, p23, p24, p25, p26, p27, p28, p29);
00159   *  
00160   *  // Function to update the rows using the BusOut class
00161   *  void SetRows(int nextRow)
00162   *  {
00163   *      // I am using PNP transistors, so inversion is necessary
00164   *      rows = ~(1 << nextRow);
00165   *  }
00166   *  
00167   *  // Create the TLC5940Mux instance
00168   *  TLC5940Mux tlc(p7, p5, p21, p9, p10, p11, p12, 1, 8, &SetRows);
00169   *  
00170   *  int main()
00171   *  {   
00172   *      tlc[0][0] = 0xFFF; // Turn on the top left LED
00173   *      while(1)
00174   *      {
00175   *  
00176   *      }
00177   *  }
00178   *  @endcode
00179   */
00180 class TLC5940Mux : private TLC5940
00181 {
00182 public:
00183     /**
00184       *  Set up the TLC5940
00185       *  @param SCLK - The SCK pin of the SPI bus
00186       *  @param MOSI - The MOSI pin of the SPI bus
00187       *  @param GSCLK - The GSCLK pin of the TLC5940(s)
00188       *  @param BLANK - The BLANK pin of the TLC5940(s)
00189       *  @param XLAT - The XLAT pin of the TLC5940(s)
00190       *  @param DCPRG - The DCPRG pin of the TLC5940(s)
00191       *  @param VPRG - The VPRG pin of the TLC5940(s)
00192       *  @param number - The number of TLC5940s (if you are daisy chaining)
00193       *  @param rows - The number of rows you are multiplexing
00194       *  @param SetRows - The function pointer to your function that sets the current row. 
00195       *  @note The SetRows function allows you to set exactly how you want your rows 
00196       *  to be updated. The TLC5940Mux class calls this function with an argument of int that contains the number of the row to 
00197       *  be turned on. If the TLC5940Mux class needs the first row to be turned on, the int will be 0.
00198       */
00199     TLC5940Mux(PinName SCLK, PinName MOSI, PinName GSCLK, PinName BLANK,
00200                PinName XLAT, PinName DCPRG, PinName VPRG, const int number,
00201                const int rows, void (*SetRows)(int));
00202     
00203     // Destructor used to delete memory        
00204     ~TLC5940Mux();
00205 
00206     /**
00207       *  Set the contents of the buffer that contains the multiplexed data
00208       *  @param data - The data to set to the buffer containing 16 12 bit grayscale data chunks per TLC5940
00209       *  @returns The data provided
00210       */
00211     unsigned short* operator=(unsigned short* data);
00212     
00213     /**
00214       *  Get a pointer to one of the rows of the multiplexed data
00215       *  @param index - The row that you would like the contents of
00216       *  @returns A pointer to the data containing the requested row containing 16 12 bit grayscale data chunks per TLC5940
00217       *  @note This operator can also be used to change or get the value of an individual LED.
00218       *  For example:
00219       *  @code
00220       *  TLC5940Mux[0][0] = 0xFFF;
00221       *  @endcode
00222       */
00223     unsigned short* operator[](int index);
00224     
00225 private:
00226     // Virtual function overriden from TLC5940 class
00227     virtual void setNextData();
00228     
00229     // Number of rows
00230     const int rows;
00231 
00232     // Function to set the current row
00233     void (*SetRows)(int);
00234 
00235     // The current row
00236     volatile int currentIndex;
00237 
00238     // Buffer containing data to be sent during each frame
00239     unsigned short* dataBuffer;
00240 };
00241 
00242 #endif