TLC5940 single chip simple code

newplumber

Senior Member
Hi
I ordered some tlc5940 chips thru hole from ebay and many thanks to gbrusseau and his post found here
http://www.picaxeforum.co.uk/showthread.php?8616-RGB-LED-Control-using-RS232-(Serial)/page3
I was able to make a single tlc5940 work using his code and tweeking it alittle
I used a 20m2 for the GSCLK (over kill but works )
with this code gbrusseau made
Code:
'******************************************************
'*              TLC5940 Demo Program                  *
'****************************************************** 
'*  A free running oscillator for the TLC5940         *
'*  grayscale clock - GSCLK                           *
'*                                                    *
'*  Every 4096 GSCLK pulses (about every 4ms)         *
'*  BLANK is pulsed to reset the TLC5940              *
'*  grayscale counter.                                *
'******************************************************
setfreq M8			'Set PICAXE 8M to 8mhz 
high B.0			'Set BLANK high at startup
low B.1				'Set GSCLK low at startup
				
OSC:				'Start free running GSCLK oscillator
pwmout B.1,0,0				
high B.0			'Pulse pin 1 (BLANK) every 4ms, which is just right
low B.0				'for 4096 GSCLK pulses for every BLANK pulse.	
pwmout B.1,1,1
pause 7			'Note: GSCLK doesn't need to be exactly 4096 pulses
goto OSC			'between BLANK pulses.
Then I used a 20X2 for the main code of course using gbrusseau's code again with a few changes (my changes probably wrong)
Code:
'******************************************************
'*           SINGLE TLC5940 Demo Program              *
'****************************************************** 
'*  Store display data in memory.                     *
'*                                                    *
'*  PICAXE port 0 - TLC5940 SCLK                      *
'*  PICAXE port 1 - TLC5940 XLAT                      *
'*  PICAXE port 2 - TLC5940 SIN                       *
'*  PICAXE port 3 - TLC5940 BLANK                     *
'*                  (from PICAXE 8M GSCLK oscillator) *
'******************************************************
'This program makes 16 leds scrolling 2 leds at a tine from bright to dim...dim to bright..etc ...  
#PICAXE 20X2 
SETFREQ M32                   ' setfreq M64 works too  
low B.2				'SCLK - Set TLC5940 SCLK to initial state 
low B.1				'XLAT - Set TLC5940 XLAT to initial state
low B.3				'SIN - Set TLC5940 SIN to initial state
	
DATA (063,223,223,223,223,223,223,223,063,063,223,223,223,223,223,223,063,063,063,223,223,223,223,223,063,063,063,063,223,223,223,223)
     '  1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8
DATA (063,063,063,063,063,223,223,223,063,063,063,063,063,063,223,223,063,063,063,063,063,063,063,223,063,063,063,063,063,063,063,063)  
     '  1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8
         
DATA (223,063,063,063,063,063,063,063,223,223,063,063,063,063,063,063,223,223,223,063,063,063,063,063,223,223,223,223,063,063,063,063)
     '  1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8
DATA (223,223,223,223,223,063,063,063,223,223,223,223,223,223,063,063,223,223,223,223,223,223,223,063,223,223,223,223,223,223,223,223)
     '  1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8   1   2   3   4   5   6   7   8    
      
     
'***************************************************
'*  Read DATA bytes. Each byte represents          *
'*  two 4 bit datagrams. Each datagram represents  *
'*  a TLC5940 channel grayscale value.             *
'*  A lookup table converts the datagram value     *
'*  to a real 12 bit TLC5940 grayscale value       *
'*  between 0 and 4096. (4096 brightness steps)    *
'*                                                 *               
'*  This scheme is designed to conserve memory.    *
'*  Storing a 12 bit value for each TLC5940        *
'*  channel uses three times as much memory,       *
'*  but the 4 bit datagram scheme limits the       *
'*  grayscale to 15 brightness steps for this demo *
'***************************************************


STRT: 

for B0=1 to 8
READ B1,B2	   		'Read a grayscale 8 bit byte from DATA memory
B3=B2&15			'Split the 8 bit byte into two 4 bit datagrams
B2=b2/16			'Present first 4 bit datagram for lookup table


for B4=1 to 2		'Lookup the 12 bit grayscale value represented by the 4 bit datagram
LOOKUP B2,(0,4,12,27,31,47,63,95,190,213,375,425,511,750,1023),W6

W4=2048
for B5=1 to 12		'Shift the 12 bit grayscale word into the TLC5940s
pinB.3=W6&W4 MAX 1		'SIN
W4=W4/2		
high B.2			'SCLK	
low B.2
next

B2=B3				'Present second 4 bit datagram for lookup table
next
				'Loop back to lookup table
inc B1			'Icreament DATA address pointer
next 
				'After 576 bits have been shifted into the three TLC5940s
do while pinB.0=0		'BLANK - Wait for the TLC5940 BLANK signal  
loop				'from the PICAXE 8M GSCLK oscillator to go high,
high B.1			'XLAT - then load the 576 grayscale bits into the TLC5940
low B.1

if B1<128 then STRT	'Don't read DATA byte beyond stored data limit
B1=0				'Reset DATA address pointer to zero
goto STRT
Like always it helps so much for a start code and thanks again gbrusseau and everyone
I have found if you keep both picaxe chips running at the same speed (setfreq m8) then the leds seem to flicker
but if I run the 20X2 chip at a fast speed then there is no sign of flicker and the TLC5940 is WAY cool :)
your friend mark
 

Attachments

Top