This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

USART IN SPI MODE COMMUNICATION BETWEEN TLC5971 and Atmega1284P

Other Parts Discussed in Thread: TLC5971

I have written the codes below for communication between TLC5971 and Atmega1284P microcontroller, but I still have some doubts. 

#include "TLC5971.h"
#include <util/delay.h>



void TLC5971_init(void)
{
	// Function control to set all Outputs off
	fc = TLC5971_BLANK;				// Sets Bit 213 to 1
	// Brightness control: Sets brightness control to full by default
	bc[0]= 127;
	bc[1]= 127;
	bc[2]= 127;

		
}

/************************************ SETTING THE DATA  *******************************************/
// set the LEDs Parameters
//- *fc: array of N euint8 for Function control - {fc0, fc1, ... , fcN }
//- *bc: array of N*3 euint8 for Brightness correction-{bcR0, bcG0, bcB0, bcR1, bcG1,... , bcBN }
//- *gs: array of N*12 euint16 for Grayscale data (PWM)-{gs0.0, gs0.1, gs0.2, gs0.3, gs0.4, ... , gs1.0, gs1.1, ... , gsN.12 }

// Set FC
void setFC(uint8_t f)
{
	fc = f;
}

// Set Grayscale
void setGS(uint16_t *g)
{
	uint8_t N = 22;				// The number of TLC5971 pieces

	
	// do this for all the drivers
	for (int8_t i=N-1; i>=0; i--)
	{
		// Grayscale data - 12x 16-bit words per driver
		gs = g + (12*i);
		// Send the write command
		sendWriteCommand();
		// Function control data
		sendFC();
		// Brightness correction
		sendBC();
		// Grayscale data
		sendGS();
	}

}

/* 
	This is used if the TLC5971 is driving LEDs with same colours 
	It sets the level of brightness of the LEDs from 0 to 127 (128 Levels)
*/
void setBC(uint8_t b)
{
	int8_t i;
	// BC has 128 Steps - i = 0,1,2
	for (i=0; i<3; i++)
	{
		if (b<127)
		{
			bc[i]= b;
		}
		else {
			bc[i]=127;
		}
	}
}

/* 
	This is used if the TLC5971 is driving LEDs with different colours
	It sets the level of brightness of the three different colours
	from 0 to 127 (128 Levels)
*/
/*
void setBC(uint8_t *b)
{
	for (uint8_t i=0; i<3; i++)
	{
		if (b[i] < 128)
		{
			bc[i] = b[i];
		}
		else
		{
			bc[i] = 127;
		}
	}
}
*/

/**************** SENDING THE DATA : Send MSB First *****************/

// sends the LEDs data
//- *Command: 6-bits (0x25 = 0b1001010 is sent, to enable copying of the 218 bit data in the 224-bit register to the 218-bit data latch
//- *fc: 5-bits of Function control of TLC5971 - {OUTMG, EXTGCK, TMGRST , DSPRPT, BLANK }
//- *bc: 21-bits with 7-bits of each of the colours of TLC5971 for OUTRn, OUTGn, OUTBn - {}
//- *gs: 192-bits with 16-bits for each of the 12 channels of TLC5971-{OUTB3,..., OUTB0, OUTG3,...,OUTG0, OUTR3,..., OUTR0}

// Send the write command to the device (6-bits = 0x25 = 0b100101
void sendWriteCommand()
{
	shiftData(0x25,6);
	
}

// Send the Function Control (FC)
void sendFC()
{
	shiftData(fc,5);
}

// Send the Brightness Control (BC)
void sendBC()
{
	uint8_t j = 0;
	// For 7-bits for each of the three colours:j=0,1,2
	for (j=2; j>=2; j--)
	{
		shiftData(bc[j],7);
	}
}

// Send the Grayscale
void sendGS()
{
	
	uint8_t j = 0;
	// For the 12 channels: j = 0, 1, 2 ....
	for (j=11; j>=0; j--)
	{
		// Shifting the bits of a 16-bit integer by 8 places to the right 
		// gives the Most Significant Byte of the 16-bit integer
		uint8_t Hi = (uint8_t)(gs[j]>>8);
		
		// Masking bits to 1 of the 16-bit integer with an 8-bit integer 
		// grabs the Least Significant Byte of the 16-bit integer
		uint8_t Lo = (uint8_t)(gs[j]&0xFF);
		
		// Shifting the Most Significant Byte
		shiftData(Hi,8);
		// Shifting the Least Significant Byte
		shiftData(Lo,8);
	}
	
}

// Sends LED parameters : num is number of bits in data
void shiftData(uint8_t data, uint8_t num)
{
	uint8_t i = 0;
	uint8_t tlcData;
	usartSPI_init();
	
	for (i=num-1; i>=0; i--)
	{
		usartSPI_write(tlcData);
	}
	
	_delay_us(0.64); //640ns wait time  for SCKI speed of 12.5MHz
}

I would appreciate if I can get some feedbacks

Kind regards

SOSO