This library allows control of the TLC5940 PWM driver IC. It supports both normal operation and controlling multiplexed displays.
TLC5940.h@2:69e40ccf506f, 2013-05-26 (annotated)
- Committer:
- Spencer
- Date:
- Sun May 26 03:08:58 2013 +0000
- Revision:
- 2:69e40ccf506f
- Parent:
- 1:013a9737441d
- Child:
- 3:e5ed5650eb15
Various updates and bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Spencer | 0:be9399a34b15 | 1 | #ifndef TLC5940_H |
Spencer | 0:be9399a34b15 | 2 | #define TLC5940_H |
Spencer | 0:be9399a34b15 | 3 | |
Spencer | 0:be9399a34b15 | 4 | #include "FastPWM.h" |
Spencer | 0:be9399a34b15 | 5 | /* |
Spencer | 0:be9399a34b15 | 6 | |
Spencer | 0:be9399a34b15 | 7 | ASCII art cat(why not?): |
Spencer | 0:be9399a34b15 | 8 | /\_/\ |
Spencer | 0:be9399a34b15 | 9 | ____/ o o \ |
Spencer | 0:be9399a34b15 | 10 | /~____ =ø= / |
Spencer | 0:be9399a34b15 | 11 | (______)__m_m) |
Spencer | 0:be9399a34b15 | 12 | |
Spencer | 0:be9399a34b15 | 13 | */ |
Spencer | 0:be9399a34b15 | 14 | |
Spencer | 0:be9399a34b15 | 15 | /** |
Spencer | 0:be9399a34b15 | 16 | * SPI speed used by the mbed to communicate with the TLC5940 |
Spencer | 0:be9399a34b15 | 17 | * The TLC5940 supports up to 30Mhz. This should be kept as high |
Spencer | 0:be9399a34b15 | 18 | * as possible to ensure that data has time to be sent each reset cycle. |
Spencer | 0:be9399a34b15 | 19 | */ |
Spencer | 0:be9399a34b15 | 20 | #define SPI_SPEED 30000000 |
Spencer | 0:be9399a34b15 | 21 | |
Spencer | 0:be9399a34b15 | 22 | /** |
Spencer | 0:be9399a34b15 | 23 | * The rate at which the GSCLK pin is pulsed |
Spencer | 0:be9399a34b15 | 24 | * This also controls how often the reset function is called |
Spencer | 0:be9399a34b15 | 25 | * The rate at which the reset function is called can be calculated by: (1/GSCLK_SPEED) * 4096 |
Spencer | 0:be9399a34b15 | 26 | * The maximum reliable rate is around ~32Mhz. I reccomend keeping this as low as possible because |
Spencer | 0:be9399a34b15 | 27 | * a higher rate will use more CPU. Also, this must be low enough to give time for sending new data |
Spencer | 0:be9399a34b15 | 28 | * before the completion of a GSCLK cycle (4096 pulses). If you are daisy chaining multiple TLC5940s, |
Spencer | 0:be9399a34b15 | 29 | * divide 32Mhz by the number of chips to get a good maximum rate. |
Spencer | 0:be9399a34b15 | 30 | */ |
Spencer | 2:69e40ccf506f | 31 | #define GSCLK_SPEED 1000000 |
Spencer | 0:be9399a34b15 | 32 | |
Spencer | 0:be9399a34b15 | 33 | /** |
Spencer | 0:be9399a34b15 | 34 | * This class controls a TLC5940 PWM driver IC. |
Spencer | 0:be9399a34b15 | 35 | * It supports sending dot correction and grayscale data. However, it does not support error checking or writing the EEPROM. |
Spencer | 0:be9399a34b15 | 36 | * This class uses the FastPWM library by Erik Olieman to continuously pulse the GSLCK pin without CPU intervention. After |
Spencer | 0:be9399a34b15 | 37 | * 4096 pulses, the private member funciton reset is called by the ticker. It resets the display by pulsing the BLANK pin. If new |
Spencer | 0:be9399a34b15 | 38 | * data has been set to be sent by the functions setNewGSData or setNewDCData, it is sent here. The definition GSCLK_SPEED in TLC5940.h |
Spencer | 0:be9399a34b15 | 39 | * controls how often this function is called. A higher GSCLK_SPEED will increase the rate at which the screen is updated but also increase |
Spencer | 0:be9399a34b15 | 40 | * CPU time spent in that function. The default value is 1Mhz. The rate at which the reset function is called can be calculated by: |
Spencer | 0:be9399a34b15 | 41 | * (1/GSCLK_SPEED) * 4096. |
Spencer | 0:be9399a34b15 | 42 | * |
Spencer | 1:013a9737441d | 43 | * Using the TLC5940 class to control an LED: |
Spencer | 0:be9399a34b15 | 44 | * @code |
Spencer | 0:be9399a34b15 | 45 | * #include "mbed.h" |
Spencer | 0:be9399a34b15 | 46 | * #include "TLC5940.h" |
Spencer | 0:be9399a34b15 | 47 | * |
Spencer | 0:be9399a34b15 | 48 | * // Create the TLC5940 instance |
Spencer | 0:be9399a34b15 | 49 | * TLC5940 tlc(p7, p5, p21, p9, p10, p11, p12, 1); |
Spencer | 0:be9399a34b15 | 50 | * |
Spencer | 0:be9399a34b15 | 51 | * int main() |
Spencer | 0:be9399a34b15 | 52 | * { |
Spencer | 0:be9399a34b15 | 53 | * // Create a buffer to store the data to be sent |
Spencer | 0:be9399a34b15 | 54 | * unsigned short GSData[16] = { 0x0000 }; |
Spencer | 0:be9399a34b15 | 55 | * |
Spencer | 0:be9399a34b15 | 56 | * // Enable the first LED |
Spencer | 0:be9399a34b15 | 57 | * GSData[0] = 0xFFF; |
Spencer | 0:be9399a34b15 | 58 | * |
Spencer | 0:be9399a34b15 | 59 | * // Set the new data |
Spencer | 0:be9399a34b15 | 60 | * tlc.setNewGSData(GSData); |
Spencer | 0:be9399a34b15 | 61 | * |
Spencer | 0:be9399a34b15 | 62 | * while(1) |
Spencer | 0:be9399a34b15 | 63 | * { |
Spencer | 0:be9399a34b15 | 64 | * |
Spencer | 0:be9399a34b15 | 65 | * } |
Spencer | 0:be9399a34b15 | 66 | * } |
Spencer | 0:be9399a34b15 | 67 | * @endcode |
Spencer | 0:be9399a34b15 | 68 | */ |
Spencer | 0:be9399a34b15 | 69 | class TLC5940 |
Spencer | 0:be9399a34b15 | 70 | { |
Spencer | 0:be9399a34b15 | 71 | public: |
Spencer | 0:be9399a34b15 | 72 | /** |
Spencer | 0:be9399a34b15 | 73 | * Set up the TLC5940 |
Spencer | 0:be9399a34b15 | 74 | * @param SCLK - The SCK pin of the SPI bus |
Spencer | 0:be9399a34b15 | 75 | * @param MOSI - The MOSI pin of the SPI bus |
Spencer | 0:be9399a34b15 | 76 | * @param GSCLK - The GSCLK pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 77 | * @param BLANK - The BLANK pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 78 | * @param XLAT - The XLAT pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 79 | * @param DCPRG - The DCPRG pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 80 | * @param VPRG - The VPRG pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 81 | * @param number - The number of TLC5940s (if you are daisy chaining) |
Spencer | 0:be9399a34b15 | 82 | */ |
Spencer | 0:be9399a34b15 | 83 | TLC5940(PinName SCLK, PinName MOSI, PinName GSCLK, PinName BLANK, |
Spencer | 0:be9399a34b15 | 84 | PinName XLAT, PinName DCPRG, PinName VPRG, const int number = 1); |
Spencer | 0:be9399a34b15 | 85 | |
Spencer | 0:be9399a34b15 | 86 | /** |
Spencer | 0:be9399a34b15 | 87 | * Set the next chunk of grayscale data to be sent |
Spencer | 0:be9399a34b15 | 88 | * @param data - Array of 16 bit shorts containing 16 12 bit grayscale data chunks per TLC5940 |
Spencer | 0:be9399a34b15 | 89 | * @note These must be in intervals of at least (1/GSCLK_SPEED) * 4096 to be sent |
Spencer | 0:be9399a34b15 | 90 | */ |
Spencer | 0:be9399a34b15 | 91 | void setNewGSData(unsigned short* data); |
Spencer | 0:be9399a34b15 | 92 | |
Spencer | 0:be9399a34b15 | 93 | /** |
Spencer | 0:be9399a34b15 | 94 | * Set the next chunk of dot correction data to be sent |
Spencer | 0:be9399a34b15 | 95 | * @param data - Array of 8 bit chars containing 16 6 bit dot correction data chunks per TLC5940 |
Spencer | 0:be9399a34b15 | 96 | * @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 |
Spencer | 0:be9399a34b15 | 97 | * use it, then the TLC5940 will use the EEPROM, which (by default) conatins the data 0x3F. |
Spencer | 0:be9399a34b15 | 98 | */ |
Spencer | 0:be9399a34b15 | 99 | void setNewDCData(unsigned char* data); |
Spencer | 0:be9399a34b15 | 100 | |
Spencer | 0:be9399a34b15 | 101 | protected: |
Spencer | 0:be9399a34b15 | 102 | /** |
Spencer | 0:be9399a34b15 | 103 | * Set the next chunk of grayscale data to be sent while in the current reset cycle |
Spencer | 0:be9399a34b15 | 104 | * @note This is useful to send the next set of data right after the first is finished being displayed. |
Spencer | 0:be9399a34b15 | 105 | * The primary purpose for this is multiplexing, although it could be used for anything else. |
Spencer | 0:be9399a34b15 | 106 | */ |
Spencer | 0:be9399a34b15 | 107 | virtual void setNextData() {} |
Spencer | 0:be9399a34b15 | 108 | |
Spencer | 0:be9399a34b15 | 109 | |
Spencer | 0:be9399a34b15 | 110 | // Number of TLC5940s in series |
Spencer | 0:be9399a34b15 | 111 | const int number; |
Spencer | 0:be9399a34b15 | 112 | |
Spencer | 0:be9399a34b15 | 113 | private: |
Spencer | 0:be9399a34b15 | 114 | // SPI port - only MOSI and SCK are used |
Spencer | 0:be9399a34b15 | 115 | SPI spi; |
Spencer | 0:be9399a34b15 | 116 | |
Spencer | 0:be9399a34b15 | 117 | // PWM output using the FastPWM library by Erik Olieman |
Spencer | 0:be9399a34b15 | 118 | FastPWM gsclk; |
Spencer | 0:be9399a34b15 | 119 | |
Spencer | 0:be9399a34b15 | 120 | // Digital out pins used for the TLC5940 |
Spencer | 0:be9399a34b15 | 121 | DigitalOut blank; |
Spencer | 0:be9399a34b15 | 122 | DigitalOut xlat; |
Spencer | 0:be9399a34b15 | 123 | DigitalOut dcprg; |
Spencer | 0:be9399a34b15 | 124 | DigitalOut vprg; |
Spencer | 0:be9399a34b15 | 125 | |
Spencer | 0:be9399a34b15 | 126 | // Call a reset function to manage sending data and GSCLK updating |
Spencer | 0:be9399a34b15 | 127 | Ticker reset_ticker; |
Spencer | 0:be9399a34b15 | 128 | |
Spencer | 0:be9399a34b15 | 129 | // Has new GS/DC data been loaded? |
Spencer | 0:be9399a34b15 | 130 | volatile bool newGSData; |
Spencer | 0:be9399a34b15 | 131 | volatile bool newDCData; |
Spencer | 0:be9399a34b15 | 132 | |
Spencer | 0:be9399a34b15 | 133 | // Do we need to send an XLAT pulse? (Was GS data clocked in last reset?) |
Spencer | 0:be9399a34b15 | 134 | volatile bool need_xlat; |
Spencer | 0:be9399a34b15 | 135 | |
Spencer | 0:be9399a34b15 | 136 | // Buffers to store data until it is sent |
Spencer | 0:be9399a34b15 | 137 | unsigned short* gsBuffer; |
Spencer | 0:be9399a34b15 | 138 | unsigned char* dcBuffer; |
Spencer | 0:be9399a34b15 | 139 | |
Spencer | 0:be9399a34b15 | 140 | // Function to reset the display and send the next chunks of data |
Spencer | 0:be9399a34b15 | 141 | void reset(); |
Spencer | 0:be9399a34b15 | 142 | }; |
Spencer | 0:be9399a34b15 | 143 | |
Spencer | 0:be9399a34b15 | 144 | |
Spencer | 0:be9399a34b15 | 145 | /** |
Spencer | 0:be9399a34b15 | 146 | * This class allows a TLC5940 to be multiplexed. |
Spencer | 0:be9399a34b15 | 147 | * It inherits the TLC5940 class and uses it to control the TLC5940 driver(s). It does not support sending dot corection data. |
Spencer | 0:be9399a34b15 | 148 | * This class sets the new grayscale data every iteration of the GSCLK reset loop. It then updates the current row using the |
Spencer | 0:be9399a34b15 | 149 | * user defined function SetRows. The framerate you will recieve using this function can be calculate by: 1 / (((1/GSCLK_SPEED) * 4096) * rows). |
Spencer | 0:be9399a34b15 | 150 | * I reccomend maintaining a framerate above 30fps. However, keep in mind that as your framerate increases, so does your CPU usage. |
Spencer | 0:be9399a34b15 | 151 | * |
Spencer | 0:be9399a34b15 | 152 | * Using the TLC5940Mux class to control an 8x8 LED matrix: |
Spencer | 0:be9399a34b15 | 153 | * @code |
Spencer | 0:be9399a34b15 | 154 | * #include "mbed.h" |
Spencer | 0:be9399a34b15 | 155 | * #include "TLC5940.h" |
Spencer | 0:be9399a34b15 | 156 | * |
Spencer | 0:be9399a34b15 | 157 | * // Bus connecting to the rows of the LED matrix through PNP transistors |
Spencer | 0:be9399a34b15 | 158 | * BusOut rows(p22, p23, p24, p25, p26, p27, p28, p29); |
Spencer | 0:be9399a34b15 | 159 | * |
Spencer | 0:be9399a34b15 | 160 | * // Function to update the rows using the BusOut class |
Spencer | 0:be9399a34b15 | 161 | * void SetRows(int nextRow) |
Spencer | 0:be9399a34b15 | 162 | * { |
Spencer | 0:be9399a34b15 | 163 | * // I am using PNP transistors, so inversion is necessary |
Spencer | 0:be9399a34b15 | 164 | * rows = ~(1 << nextRow); |
Spencer | 0:be9399a34b15 | 165 | * } |
Spencer | 0:be9399a34b15 | 166 | * |
Spencer | 0:be9399a34b15 | 167 | * // Create the TLC5940Mux instance |
Spencer | 0:be9399a34b15 | 168 | * TLC5940Mux tlc(p7, p5, p21, p9, p10, p11, p12, 1, 8, &SetRows); |
Spencer | 0:be9399a34b15 | 169 | * |
Spencer | 0:be9399a34b15 | 170 | * int main() |
Spencer | 0:be9399a34b15 | 171 | * { |
Spencer | 0:be9399a34b15 | 172 | * tlc[0][0] = 0xFFF; // Turn on the top left LED |
Spencer | 0:be9399a34b15 | 173 | * while(1) |
Spencer | 0:be9399a34b15 | 174 | * { |
Spencer | 0:be9399a34b15 | 175 | * |
Spencer | 0:be9399a34b15 | 176 | * } |
Spencer | 0:be9399a34b15 | 177 | * } |
Spencer | 0:be9399a34b15 | 178 | * @endcode |
Spencer | 0:be9399a34b15 | 179 | */ |
Spencer | 0:be9399a34b15 | 180 | class TLC5940Mux : private TLC5940 |
Spencer | 0:be9399a34b15 | 181 | { |
Spencer | 0:be9399a34b15 | 182 | public: |
Spencer | 0:be9399a34b15 | 183 | /** |
Spencer | 0:be9399a34b15 | 184 | * Set up the TLC5940 |
Spencer | 0:be9399a34b15 | 185 | * @param SCLK - The SCK pin of the SPI bus |
Spencer | 0:be9399a34b15 | 186 | * @param MOSI - The MOSI pin of the SPI bus |
Spencer | 0:be9399a34b15 | 187 | * @param GSCLK - The GSCLK pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 188 | * @param BLANK - The BLANK pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 189 | * @param XLAT - The XLAT pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 190 | * @param DCPRG - The DCPRG pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 191 | * @param VPRG - The VPRG pin of the TLC5940(s) |
Spencer | 0:be9399a34b15 | 192 | * @param number - The number of TLC5940s (if you are daisy chaining) |
Spencer | 0:be9399a34b15 | 193 | * @param rows - The number of rows you are multiplexing |
Spencer | 0:be9399a34b15 | 194 | * @param SetRows - The function pointer to your function that sets the current row. |
Spencer | 0:be9399a34b15 | 195 | * @note The SetRows function allows you to set exactly how you want your rows |
Spencer | 0:be9399a34b15 | 196 | * to be updated. The TLC5940Mux class calls this function with an argument of int that contains the number of the row to |
Spencer | 0:be9399a34b15 | 197 | * be turned on. If the TLC5940Mux class needs the first row to be turned on, the int will be 0. |
Spencer | 0:be9399a34b15 | 198 | */ |
Spencer | 0:be9399a34b15 | 199 | TLC5940Mux(PinName SCLK, PinName MOSI, PinName GSCLK, PinName BLANK, |
Spencer | 0:be9399a34b15 | 200 | PinName XLAT, PinName DCPRG, PinName VPRG, const int number, |
Spencer | 0:be9399a34b15 | 201 | const int rows, void (*SetRows)(int)); |
Spencer | 0:be9399a34b15 | 202 | |
Spencer | 0:be9399a34b15 | 203 | // Destructor used to delete memory |
Spencer | 0:be9399a34b15 | 204 | ~TLC5940Mux(); |
Spencer | 0:be9399a34b15 | 205 | |
Spencer | 0:be9399a34b15 | 206 | /** |
Spencer | 0:be9399a34b15 | 207 | * Set the contents of the buffer that contains the multiplexed data |
Spencer | 0:be9399a34b15 | 208 | * @param data - The data to set to the buffer containing 16 12 bit grayscale data chunks per TLC5940 |
Spencer | 0:be9399a34b15 | 209 | * @returns The data provided |
Spencer | 0:be9399a34b15 | 210 | */ |
Spencer | 0:be9399a34b15 | 211 | unsigned short* operator=(unsigned short* data); |
Spencer | 0:be9399a34b15 | 212 | |
Spencer | 0:be9399a34b15 | 213 | /** |
Spencer | 0:be9399a34b15 | 214 | * Get a pointer to one of the rows of the multiplexed data |
Spencer | 0:be9399a34b15 | 215 | * @param index - The row that you would like the contents of |
Spencer | 0:be9399a34b15 | 216 | * @returns A pointer to the data containing the requested row containing 16 12 bit grayscale data chunks per TLC5940 |
Spencer | 0:be9399a34b15 | 217 | * @note This operator can also be used to change or get the value of an individual LED. |
Spencer | 0:be9399a34b15 | 218 | * For example: |
Spencer | 0:be9399a34b15 | 219 | * @code |
Spencer | 0:be9399a34b15 | 220 | * TLC5940Mux[0][0] = 0xFFF; |
Spencer | 0:be9399a34b15 | 221 | * @endcode |
Spencer | 0:be9399a34b15 | 222 | */ |
Spencer | 0:be9399a34b15 | 223 | unsigned short* operator[](int index); |
Spencer | 0:be9399a34b15 | 224 | |
Spencer | 0:be9399a34b15 | 225 | private: |
Spencer | 0:be9399a34b15 | 226 | // Virtual function overriden from TLC5940 class |
Spencer | 0:be9399a34b15 | 227 | virtual void setNextData(); |
Spencer | 0:be9399a34b15 | 228 | |
Spencer | 0:be9399a34b15 | 229 | // Number of rows |
Spencer | 0:be9399a34b15 | 230 | const int rows; |
Spencer | 0:be9399a34b15 | 231 | |
Spencer | 0:be9399a34b15 | 232 | // Function to set the current row |
Spencer | 0:be9399a34b15 | 233 | void (*SetRows)(int); |
Spencer | 0:be9399a34b15 | 234 | |
Spencer | 0:be9399a34b15 | 235 | // The current row |
Spencer | 0:be9399a34b15 | 236 | int index; |
Spencer | 0:be9399a34b15 | 237 | |
Spencer | 0:be9399a34b15 | 238 | // Buffer containing data to be sent during each frame |
Spencer | 0:be9399a34b15 | 239 | unsigned short* dataBuffer; |
Spencer | 0:be9399a34b15 | 240 | }; |
Spencer | 0:be9399a34b15 | 241 | |
Spencer | 0:be9399a34b15 | 242 | #endif |