Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC24HJ502, TLC5940, C Code

Status
Not open for further replies.

mishkinis

Newbie level 4
Joined
Nov 2, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,383
Hi, I am trying to control individual RGB LEDs with the TLC5940 and PIC24HJ502 microcontroller. I am programming in C (MPLAB). I have googled all possible documentation on internet, datasheet, TI programming flowchart & demystifying TLC5940. Not much progress yet, most of examples are on Arduino. I have to use PIC24 and I haven't found good beginners examples for that. Anyway here is my code:

#include <p24HJ128GP502.h>
#include <libpic30.h>
#include <stdio.h>

#define VPRG LATBbits.LATB2
#define SCLK LATBbits.LATB3
#define SIN LATBbits.LATB4
#define XLAT LATBbits.LATB5
#define BLANK LATBbits.LATB6
#define GSCLK LATBbits.LATB7


unsigned char gsData[192] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 15 //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 12
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 11
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 10
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 9
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 8
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 7
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 6
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 5
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 4
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 3
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 1
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 0
};

int Data_Counter,GSCLK_Counter,x,true,z;
unsigned int bright,brightness,FirstCycleFlag;
//char GS_DATA[



void main()
{


_PLLPRE = 0 ;// then N1 = 2. This yields a
_PLLDIV = 40; // then M = 32. This yields a
_PLLPOST = 0; // then N2 = 2. This provides


AD1PCFGL = 0xFFFF;
_TRISB2 = 0;
_TRISB3 = 0;
_TRISB4 = 0;
_TRISB5 = 0;
_TRISB6 = 0;
_TRISB7 = 0;

FirstCycleFlag=0;
true=1;
GSCLK=0;
SCLK=0;
VPRG=0;
XLAT=0;
BLANK=1;


while(1){


if (VPRG==true){
VPRG=0;
FirstCycleFlag=1;
}
else{
GSCLK_Counter=0;
Data_Counter=0;
BLANK=0;
}


if (GSCLK_Counter>4095){
BLANK = 1;
XLAT = 1;
XLAT = 0;
SCLK=1;
SCLK=0;
FirstCycleFlag = 0;
}


else if (Data_Counter>191){
if (gsData[Data_Counter]){
SIN =1;
}
else{
SIN=0;
SCLK=1;
SCLK=0;
Data_Counter++;
}
}
else{
GSCLK=1;
GSCLK=0;
GSCLK_Counter++;
}
}
}


Does anyone see what I am doing wrong? Or maybe someone has they own SIMPLEST explanation of how to control TLC5940
 

Hey, I have Updated My Code but still no success. Here it is. I have the GSCLK_Counter counting up to 4095 but the Data_Counter is behaving weird. Seems that I cannot shift the data from my array

#include <p24HJ128GP502.h>
#include <libpic30.h>
#include <stdio.h>

#define VPRG LATBbits.LATB2
#define SCLK LATBbits.LATB3
#define SIN LATBbits.LATB4
#define XLAT LATBbits.LATB5
#define BLANK LATBbits.LATB6
#define GSCLK LATBbits.LATB7


unsigned char gsData[192] = {
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 15
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 14
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 13
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 12
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 11
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 10
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 9
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 8
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 15
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 14
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 13
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 12
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 11
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 10
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, // Channel 9
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 // Channel 8
};

int Data_Counter,GSCLK_Counter,x,true,z;
unsigned int bright,brightness,FirstCycleFlag;
//char GS_DATA[



void main()
{


_PLLPRE = 0 ;// then N1 = 2. This yields a
_PLLDIV = 40; // then M = 32. This yields a
_PLLPOST = 0; // then N2 = 2. This provides

AD1PCFGL = 0xFFFF;
_TRISB2 = 0;
_TRISB3 = 0;
_TRISB4 = 0;
_TRISB5 = 0;
_TRISB6 = 0;
_TRISB7 = 0;
_TRISB8 = 0;

FirstCycleFlag=0;
true=1;
GSCLK=0;
SCLK=0;
VPRG=0;
XLAT=0;
BLANK=0;

VPRG=0;
GSCLK_Counter=0;
Data_Counter=0;

while(1){


if (GSCLK_Counter>4095){
BLANK=1;
XLAT=1;
XLAT=0;
SCLK=1;
SCLK=0;
// GSCLK_Counter=0;
}


if (Data_Counter<191){
SIN = gsData[Data_Counter];
SCLK=1;
SCLK=0;
Data_Counter++;

}

else{
// GSCLK=1;
// GSCLK=0;
GSCLK_Counter++;

if (Data_Counter>191){
Data_Counter=0;
GSCLK=1;
GSCLK=0;

}
}
}
}
 

So.. The 3rd version of the code. Now I am able to light up LEDs by changing values in the gsData[] array. But it does it randomly. The problem is that GSCLK_Counter has to reach value 4095 and Data_Counter value 191 the same time. So probably I need two timers for that. Still haven't figured that one out but there is definetely some progress in the work. Hopefully it will be helpful for others too. If you have any advices you are still welcome to post them.

Here is my new code:

#include <p24HJ128GP502.h>
#include <libpic30.h>
#include <stdio.h>

#define VPRG LATBbits.LATB1
#define SIN LATBbits.LATB2
#define SCLK LATBbits.LATB3
#define XLAT LATBbits.LATB4
#define BLANK LATBbits.LATB5
#define GSCLK LATBbits.LATB6


unsigned char gsData[192] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 11
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 11
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 11
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // Channel 14
};

int Data_Counter,GSCLK_Counter,x,true,z;
unsigned int bright,brightness,FirstCycleFlag;
//char GS_DATA[



void main()
{


_PLLPRE = 0 ;// then N1 = 2. This yields a
_PLLDIV = 40; // then M = 32. This yields a
_PLLPOST = 0; // then N2 = 2. This provides

AD1PCFGL = 0xFFFF;
_TRISB1 = 0;
_TRISB2 = 0;
_TRISB3 = 0;
_TRISB4 = 0;
_TRISB5 = 0;
_TRISB6 = 0;
_TRISB7 = 0;

FirstCycleFlag=0;
true=1;
GSCLK=0;
SCLK=0;
VPRG=0;
XLAT=0;
BLANK=0;

VPRG=0;
GSCLK_Counter=0;
Data_Counter=0;

while(1){

if (Data_Counter<191){

Data_Counter++;
SIN=gsData[Data_Counter];
SCLK=1;
SCLK=0;
}
else{
Data_Counter=0;
XLAT=1;
XLAT=0;
}
if (GSCLK_Counter<4095){
GSCLK_Counter++;
GSCLK=1;
GSCLK=0;
}
else {
GSCLK_Counter=0;
BLANK=1;
BLANK=0;
XLAT=1;
XLAT=0;
SCLK=1;
SCLK=0;
}
}
}
 

So guys, I have solved the problems of the code myself. As long as I didn't get replies I will not comment on the code but I will still post it if anybody will have the same problems. If anybody needs explanation for the code just post it here and I will explain it. This is very simple code, by changing values in the array you can manipulate colors of RGB LEDs.

#include <p24HJ128GP502.h>
#include <libpic30.h>
#include <stdio.h>

#define VPRG LATBbits.LATB1
#define SIN LATBbits.LATB2
#define SCLK LATBbits.LATB3
#define XLAT LATBbits.LATB4
#define BLANK LATBbits.LATB5
#define GSCLK LATBbits.LATB6


unsigned char gsData[192] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 16.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 15
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 14
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 13.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 12
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 11
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 10.
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 9
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 8
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 7.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 6
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 5
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 4.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 3
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Channel 2
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // Channel 1.
};

int Data_Counter,GSCLK_Counter,x,true,z;
unsigned int bright,brightness,FirstCycleFlag;
//char GS_DATA[



void main()
{


_PLLPRE = 0 ;// then N1 = 2. This yields a
_PLLDIV = 40; // then M = 32. This yields a
_PLLPOST = 0; // then N2 = 2. This provides

AD1PCFGL = 0xFFFF;
_TRISB1 = 0;
_TRISB2 = 0;
_TRISB3 = 0;
_TRISB4 = 0;
_TRISB5 = 0;
_TRISB6 = 0;
_TRISB7 = 0;

FirstCycleFlag=1;
true=1;
GSCLK=0;
SCLK=0;
VPRG=0;
XLAT=0;
BLANK=0;

VPRG=0;
GSCLK_Counter=0;
Data_Counter=0;
x=4095;
z=191;
FirstCycleFlag=1;

while(1){

GSCLK_Counter=0;
Data_Counter=0;
//FirstCycleFlag=1;
BLANK = 0;
gsData[0];




while (GSCLK_Counter<4095){



if (Data_Counter>191){
GSCLK=1;
GSCLK=0;
GSCLK_Counter++;

}
else
{
SIN=gsData[Data_Counter];
SCLK=1;
SCLK=0;
Data_Counter++;
GSCLK=1;
GSCLK=0;
}
}
BLANK=1;
BLANK=0;
XLAT=1;
XLAT=0;
if (FirstCycleFlag=1){
SCLK=1;
SCLK=0;
FirstCycleFlag=0;
GSCLK_Counter=0;
}
}
}
 

Good job!Now,i driver one 32x256RGB LED matrix display.but i not use PIC MCU and TLC5940.however,i think you are doing very nice project!if you can drive more area,and add communication,PC message editer software,it's will be perfect!
 

Thank you, I haven't found many good codes of TLC5940 for PIC so I decided to post it here. It definitely will be possible to control it both via PC and without it. The main reason I use these TLC5940 is because it gives you possibility to drive lots of RGB LEDs with just few I/O pins from the microcontroller. Now I am trying to write some fading routine of LEDs. :) I'll post my next results soon.
 

Thank you, I haven't found many good codes of TLC5940 for PIC so I decided to post it here. It definitely will be possible to control it both via PC and without it. The main reason I use these TLC5940 is because it gives you possibility to drive lots of RGB LEDs with just few I/O pins from the microcontroller. Now I am trying to write some fading routine of LEDs. :) I'll post my next results soon.

Yes,I not fond any nice codes too,thank you share.but if know it's how to work,and see the datasheet,timing,it's easy to drive,right?I think principle is important than code.
 

hi mishkinis
Can you please post the schematic?
and why did you use PIC24?
Can i use the same code for 18f87k22?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top