sshahryiar
Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh
|
TLC5940 16 Channel 12-Bit PWM Driver (Simple) |
Posted: Tue Apr 09, 2013 4:40 am |
|
|
TLC5940.c
Code: | #define VPRG pin_B0
#define SIN pin_B1
#define SCLK pin_B2
#define XLAT pin_B3
#define BLANK pin_B4
#define DCPRG pin_B5
#define GSCLK pin_B6
#byte PORTB = 0xF81
unsigned char dcData[96] =
{
1, 1, 1, 1, 1, 1, // Ch 15
1, 1, 1, 1, 1, 1, // Ch 14
1, 1, 1, 1, 1, 1, // Ch 13
1, 1, 1, 1, 1, 1, // Ch 12
1, 1, 1, 1, 1, 1, // Ch 11
1, 1, 1, 1, 1, 1, // Ch 10
1, 1, 1, 1, 1, 1, // Ch 9
1, 1, 1, 1, 1, 1, // Ch 8
1, 1, 1, 1, 1, 1, // Ch 7
1, 1, 1, 1, 1, 1, // Ch 6
1, 1, 1, 1, 1, 1, // Ch 5
1, 1, 1, 1, 1, 1, // Ch 4
1, 1, 1, 1, 1, 1, // Ch 3
1, 1, 1, 1, 1, 1, // Ch 2
1, 1, 1, 1, 1, 1, // Ch 1
1, 1, 1, 1, 1, 1, // Ch 0
};
unsigned char gsData[192];
void TLC5940_Init();
void TLC5940_ClockInDC();
void TLC5940_SetGS_And_GS_PWM();
void changeGS(unsigned char channel, unsigned long dc);
void TLC5940_Init()
{
output_low(GSCLK);
output_low(SCLK);
output_low(DCPRG);
output_high(VPRG);
output_low(XLAT);
output_high(BLANK);
memset(gsData, 0x00, sizeof(gsData));
}
void TLC5940_ClockInDC()
{
unsigned long cnt = 0;
output_high(DCPRG);
output_high(VPRG);
while(TRUE)
{
if(cnt > 95)
{
output_high(XLAT);
output_low(XLAT);
break;
}
else
{
if(dcData[cnt])
{
output_high(SIN);
}
else
{
output_low(SIN);
}
output_high(SCLK);
output_low(SCLK);
cnt++;
}
}
}
void TLC5940_SetGS_And_GS_PWM()
{
short firstCycle = FALSE;
unsigned char Data_Cnt = 0;
unsigned long GSCLK_Cnt = 0;
if(bit_test(PORTB, 0))
{
output_low(VPRG);
firstCycle = TRUE;
}
output_low(BLANK);
while(TRUE)
{
if(GSCLK_cnt > 4095)
{
output_high(BLANK);
output_high(XLAT);
output_low(XLAT);
if(firstCycle)
{
output_high(SCLK);
output_low(SCLK);
firstCycle = FALSE;
}
break;
}
else
{
if(!(Data_Cnt > 191))
{
if(gsData[Data_cnt])
{
output_high(SIN);
}
else
{
output_low(SIN);
}
output_high(SCLK);
output_low(SCLK);
Data_Cnt++;
}
}
output_high(GSCLK);
output_low(GSCLK);
GSCLK_cnt++;
}
}
void changeGS(unsigned char channel, unsigned long dc)
{
unsigned long start = 0;
unsigned long stop = 0;
unsigned long s = 0;
short tmp = 0;
switch(channel)
{
case 1:
{
start = 168;
stop = 179;
break;
}
case 2:
{
start = 156;
stop = 167;
break;
}
case 3:
{
start = 144;
stop = 155;
break;
}
case 4:
{
start = 132;
stop = 143;
break;
}
case 5:
{
start = 120;
stop = 131;
break;
}
case 6:
{
start = 108;
stop = 119;
break;
}
case 7:
{
start = 96;
stop = 107;
break;
}
case 8:
{
start = 84;
stop = 95;
break;
}
case 9:
{
start = 72;
stop = 83;
break;
}
case 10:
{
start = 60;
stop = 71;
break;
}
case 11:
{
start = 48;
stop = 59;
break;
}
case 12:
{
start = 36;
stop = 47;
break;
}
case 13:
{
start = 24;
stop = 35;
break;
}
case 14:
{
start = 12;
stop = 23;
break;
}
case 15:
{
start = 0;
stop = 11;
break;
}
default:
{
start = 180;
stop = 191;
break;
}
}
for(s = start; s <= stop; s++)
{
tmp = bit_test(dc, (stop - s));
gsData[s] = tmp;
}
} |
Example
Code: | #include <18F252.h>
#device *= 16
#device ADC = 10
#fuses H4, NOWDT, PUT, PROTECT, NOSTVREN
#fuses NOCPB, NOWRT, NOWRTB NOEBTRB, NOEBTR
#fuses BROWNOUT, CPD, NOOSCSEN, BORV20
#fuses NOLVP, NODEBUG, NOWRTC, NOWRTD
#use delay(clock = 40MHz)
#include "TLC5940.c"
#define reload1 63035
#define reload2 40000
short update_flag = 0;
short pol = 1;
signed long d = 0;
void setup();
#int_timer1
void update()
{
set_timer1(reload1);
update_flag = 1;
}
#int_timer0
void update_cycle()
{
set_timer0(reload2);
if(pol)
{
d += 5;
if(d == 4095)
{
pol = 0;
d = 4095;
}
}
else
{
d -= 5;
if(d <= 0)
{
pol = 1;
d = 0;
}
}
}
void main()
{
setup();
while(TRUE)
{
if(update_flag)
{
setup_timer_1(T1_disabled);
TLC5940_SetGS_And_GS_PWM();
set_timer1(reload1);
setup_timer_1(T1_internal);
update_flag = 0;
}
changeGS(0, d);
changeGS(2, d);
changeGS(4, d);
changeGS(6, d);
changeGS(1, (4095 - d));
changeGS(3, (4095 - d));
changeGS(5, (4095 - d));
changeGS(7, (4095 - d));
changeGS(8, d);
changeGS(10, d);
changeGS(12, d);
changeGS(14, d);
changeGS(9, (4095 - d));
changeGS(11, (4095 - d));
changeGS(13, (4095 - d));
changeGS(15, (4095 - d));
};
}
void setup()
{
disable_interrupts(global);
setup_WDT(WDT_off);
setup_adc_ports(no_analogs);
setup_adc(ADC_off);
setup_timer_0(T0_internal);
setup_timer_1(T1_internal);
setup_timer_2(T2_DISABLED,255,1);
setup_timer_3(T3_DISABLED);
setup_CCP1(CCP_off);
setup_CCP2(CCP_off);
setup_SPI(SPI_DISABLED);
set_timer0(reload2);
set_timer1(reload1);
set_timer2(0);
set_timer3(0);
delay_ms(100);
enable_interrupts(global);
enable_interrupts(int_timer1);
enable_interrupts(int_timer0);
TLC5940_Init();
TLC5940_ClockInDC();
} |
_________________ https://www.facebook.com/MicroArena
SShahryiar |
|