ssquared
Junior Member level 3
I am working on a Project to control 22 TLC5971 LED derivers using Atmega1284p. I have written the code and tested for controlling each of the drivers in the chain and it is working well.
Now I want to modify the code so that I can control a group of drivers using different values of n. I have an idea to put the the Drivers in three groups and use a switch statement to change from one group to another.
I would appreciate if you can help me correct my code. My code is found below.
Another idea is to use a for loop to turn on the Data pin ON and OFF. I have tried with this loop, but it is still not changing the pattern
Below is the complete code with the added loop
Now I want to modify the code so that I can control a group of drivers using different values of n. I have an idea to put the the Drivers in three groups and use a switch statement to change from one group to another.
I would appreciate if you can help me correct my code. My code is found below.
Code:
int main(void)
{
usartSPI_init();
static struct TLC5971 tlcarray[22]; /* Declare an array of 22 structure TLC5971 */
struct TLC5971 *ptlc; /* ptlc is a pointer to a structure */
uint8_t i = 0;
uint8_t n = 2;
// For Turning on 0*n 1*n 2*n 3*n drivers where number of drivers should be less than 22
for (i=0; i<22; i+=n)
{
ptlc = &tlcarray[i];
ptlc->BCR = 3;
ptlc->BCG = 3;
ptlc->BCB = 3;
ptlc->TMGRST = 1;
ptlc->EXTGCK = 1;
ptlc->cmd = 0x25;
ptlc->GS[0][BLUE] = 0x1000; // OUTB0
ptlc->GS[0][GREEN] = 0x1000; // OUTG0
ptlc->GS[0][RED] = 0x1000; // OUTR0
ptlc->GS[1][BLUE] = 0x1000; // OUTB1
ptlc->GS[1][GREEN] = 0x1000; // OUTG1
ptlc->GS[1][RED] = 0x1000; // OUTR1
ptlc->GS[2][BLUE] = 0x1000; // OUTB2
ptlc->GS[2][GREEN] = 0x1000; // OUTG2
ptlc->GS[2][RED] = 0x1000; // OUTR2
ptlc->GS[3][BLUE] = 0x1000; // OUTB3
ptlc->GS[3][GREEN] = 0x1000; // OUTG3
ptlc->GS[3][RED] = 0x1000; // OUTR3
}
for (uint8_t i = 0; i < 22; i++)
{
CS_lo();
sendBits(&tlcarray[i]);
CS_hi();
}
while (1)
{
for (uint8_t i = 0; i < 22; i++)
{
CS_lo();
sendBits(&tlcarray[i]);
CS_hi();
}
// Stop SCKI at high or Low for 8 times the period between 224*N
PORTD &= ~(1 << PD4);
_delay_us(5); // Atleast: 8*Period T + 1.34us
// Send one SCKI clock with SDTI Low after 1.34us
PORTD |= (1 << PD4);
PORTD &= ~(1 << PD3);
// OUTXn are Turned ON
tlc5971.BLANK = 0;
}
}
Another idea is to use a for loop to turn on the Data pin ON and OFF. I have tried with this loop, but it is still not changing the pattern
Code:
for(uint8_t j = 0; j < 65535; j++)
{
//This turns Pin D3 ON and OFF : Turns D3 HIGH
PORTD |=(1<<PD3);
//PAUSE 1 Second
_delay_ms(1000);
//turns D3 LOW
PORTD &= ~(1 << PD3);
//PAUSE 1 Second
_delay_ms(1000);
}
Below is the complete code with the added loop
Code:
#include "usartSPI.h"
#include "tlc5971_poecie.h"
#include <avr/io.h>
void tlc5971_init()
{
//tlc5971.BLANK = 1;
//tlc5971.DSPRPT = 0;
//tlc5971.TMGRST = 1;
//tlc5971.EXTGCK = 1;
//tlc5971.OUTTMG = 0;
}
// The sendBits() is used to send bits to address of the particular struct
void sendBits(struct TLC5971 *ptlc) /* ptlc points to the struct TLC5971 */
{
uint8_t n, *p = (uint8_t*)ptlc; /* copy the structure pointed to by ptlc to the structure pointed to by p*/
for (n = 28; n-- > 0; ) /* Looping 28 times to send the 28 bytes for one LED driver */
{
usartSPI_transfer(p[n]); /* Using SPI to send each of the 28 bytes */
}
}
int main(void)
{
usartSPI_init();
static struct TLC5971 tlcarray[22]; /* Declare an array of 22 structure TLC5971 */
struct TLC5971 *ptlc; /* ptlc is a pointer to a structure */
uint8_t i = 0;
uint8_t n = 2;
// For Turning on 0*n 1*n 2*n 3*n drivers where number of drivers should be less than 22
for (i=0; i<22; i+=n)
{
ptlc = &tlcarray[i];
ptlc->BCR = 3;
ptlc->BCG = 3;
ptlc->BCB = 3;
ptlc->TMGRST = 1;
ptlc->EXTGCK = 1;
ptlc->cmd = 0x25;
ptlc->GS[0][BLUE] = 0x1000; // OUTB0
ptlc->GS[0][GREEN] = 0x1000; // OUTG0
ptlc->GS[0][RED] = 0x1000; // OUTR0
ptlc->GS[1][BLUE] = 0x1000; // OUTB1
ptlc->GS[1][GREEN] = 0x1000; // OUTG1
ptlc->GS[1][RED] = 0x1000; // OUTR1
ptlc->GS[2][BLUE] = 0x1000; // OUTB2
ptlc->GS[2][GREEN] = 0x1000; // OUTG2
ptlc->GS[2][RED] = 0x1000; // OUTR2
ptlc->GS[3][BLUE] = 0x1000; // OUTB3
ptlc->GS[3][GREEN] = 0x1000; // OUTG3
ptlc->GS[3][RED] = 0x1000; // OUTR3
}
for (uint8_t i = 0; i < 22; i++)
{
CS_lo();
sendBits(&tlcarray[i]);
CS_hi();
}
for(uint8_t j = 0; j < 65535; j++)
{
//This turns Pin D3 ON and OFF : Turns D3 HIGH
PORTD |=(1<<PD3);
//PAUSE 1 Second
_delay_ms(1000);
//turns D3 LOW
PORTD &= ~(1 << PD3);
//PAUSE 1 Second
_delay_ms(1000);
}
while (1)
{
for (uint8_t i = 0; i < 22; i++)
{
CS_lo();
sendBits(&tlcarray[i]);
CS_hi();
}
// Stop SCKI at high or Low for 8 times the period between 224*N
PORTD &= ~(1 << PD4);
_delay_us(5); // Atleast: 8*Period T + 1.34us
// Send one SCKI clock with SDTI Low after 1.34us
PORTD |= (1 << PD4);
PORTD &= ~(1 << PD3);
// OUTXn are Turned ON
tlc5971.BLANK = 0;
}
}