This library allows control of the TLC5940 PWM driver IC. It supports both normal operation and controlling multiplexed displays.
TLC5940.cpp
00001 #include "TLC5940.h" 00002 00003 TLC5940::TLC5940(PinName SCLK, PinName MOSI, PinName GSCLK, PinName BLANK, 00004 PinName XLAT, PinName DCPRG, PinName VPRG, const int number) : number(number), 00005 spi(MOSI, NC, SCLK), 00006 gsclk(GSCLK), 00007 blank(BLANK), 00008 xlat(XLAT), 00009 dcprg(DCPRG), 00010 vprg(VPRG), 00011 newGSData(false), 00012 newDCData(false), 00013 need_xlat(false) 00014 { 00015 // Configure SPI to 12 bits and SPI_SPEED 00016 spi.format(12, 0); 00017 spi.frequency(SPI_SPEED); 00018 00019 // Set output pin states 00020 dcprg = 0; 00021 vprg = 0; 00022 xlat = 0; 00023 blank = 1; 00024 00025 // Call the reset function every 4096 PWM outputs 00026 reset_ticker.attach_us(this, &TLC5940::reset, (1000000.0/GSCLK_SPEED) * 4096.0); 00027 00028 // Configure FastPWM output for GSCLK frequency at 50% duty cycle 00029 gsclk.period_us(1000000.0/(GSCLK_SPEED * 1.05)); 00030 gsclk.write(.5); 00031 } 00032 00033 void TLC5940::setNewGSData(unsigned short* data) 00034 { 00035 gsBuffer = data; 00036 00037 // Tell reset function that new GS data has been given 00038 newGSData = true; 00039 } 00040 00041 void TLC5940::setNewDCData(unsigned char* data) 00042 { 00043 dcBuffer = data; 00044 00045 // Tell reset function that new DC data has been given 00046 newDCData = true; 00047 } 00048 00049 void TLC5940::reset() 00050 { 00051 gsclk.write(0); 00052 // Turn off LEDs 00053 blank = 1; 00054 00055 // Virtual function that allows the next data chunk to be set after every GSCLK cycle 00056 // Useful for setting the next frame when multiplexing (e.g. LED matrices) 00057 setNextData(); 00058 00059 // Latch in data from previous cycle if needed 00060 if (need_xlat) 00061 { 00062 // Latch 00063 xlat = 1; 00064 xlat = 0; 00065 00066 // Don't need to latch again 00067 need_xlat = false; 00068 } 00069 00070 // Reset the screen so that it is updating while data is being sent 00071 blank = 0; 00072 gsclk.write(.5); 00073 00074 // Do we have new DC data to send? 00075 if (newDCData) 00076 { 00077 // Set TLC5940 to accpet DC data 00078 vprg = 1; 00079 00080 // Get DC data from registers instead of EEPROM (since we are sending data to the registers now) 00081 dcprg = 1; 00082 00083 // Send DC data backwards - this makes the DC_buffer[0] index correspond to OUT0 00084 for (int i = (16 * number) - 1; i >= 0; i--) 00085 { 00086 // Assemble a 12 bit packet from two 6 bit chunks 00087 spi.write(((dcBuffer[i] & 0x3F) << 6) | (dcBuffer[i-1] & 0x3F)); 00088 i--; 00089 } 00090 00091 // Latch 00092 xlat = 1; 00093 xlat = 0; 00094 00095 // No new data to send (we just sent it!) 00096 newDCData = false; 00097 } 00098 00099 // Do we have new GS data to send? 00100 if (newGSData) 00101 { 00102 // Set TLC5940 to accept GS data 00103 vprg = 0; 00104 00105 // Send GS data backwards - this makes the GS_buffer[0] index correspond to OUT0 00106 for (int i = (16 * number) - 1; i >= 0; i--) 00107 { 00108 // Get the lower 12 bits of the buffer and send 00109 spi.write(gsBuffer[i] & 0xFFF); 00110 } 00111 00112 // Latch after current GS data is done being displayed 00113 need_xlat = true; 00114 00115 // No new data to send (we just sent it!) 00116 newGSData = false; 00117 } 00118 }
Generated on Tue Jul 12 2022 17:55:54 by 1.7.2