PWM_TLC5940NT WITH ATMEGA32

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

   Good day. I am trying to do interfacing TLC5940NT with Atmega32. I read the TLC5940NT datasheet. But  i didn't understand well. And i tried to do program in atmel studio 6.2. In this i am getting LED is always off. I don't know whether my attempt  is correct or not. And also i don't know my code and circuit are correct. i feel the problem is in my code, bcoz i haven't understand clearly. Can you please give me a start up code. I confused with this how to start and i struggled with this. my Code and circuit is:

 

/*
 * PWM_TLC5940NT.c
 *
 * Created: 12/12/2014 4:53:16 PM
 *  Author: Dev5
 */ 

#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdio.h>


void uart0_init(void)
{
	UCSRA=0x00;
	UCSRB=0x18;
	UCSRC=0x06;
	UBRRH=0x00;
	UBRRL=0x33;
}

void transmitByte( unsigned char data )
{
	while ( !(UCSRA & (1<<UDRE)) )
	; 			                /* Wait for empty transmit buffer */
	UDR = data; 			        /* Start transmition */
}

void transmitString(char* string)
{
	while (*string)
	transmitByte(*string++);
}


int main(void)
{
    unsigned int ipwm;
	
	DDRB = (1<<PB5)|(1<<PB7)|(1<<PB4);
	PORTB |= (1<<PB4);             // CS pin is not active
	
	SPCR = (1<<SPE)|(1<<MSTR);    // Enable SPI, Master, set clock rate fck/2
	SPSR |= (1<<SPI2X);
	_delay_ms(100);
	
	
	DDRD |= (1<<PD4)|(1<<PD5);                    //OC1A, OC1B outputs
	TCCR1A = (2<<COM1A0)|(2<<COM1B0)|(2<<WGM10);  //PWM#14, active-high LEDs
	TCCR1B = (3<<WGM12)|(3<<CS10);                //PWM#14, div64
	ICR1 = F_CPU/64/100 - 1;                      //OVF @ 100Hz
	TIMSK |= (1<<TOV1);
	
	
	uart0_init();
	_delay_ms(500);
	
	transmitString("\n\n\nin main");
	
	while(1)
    {
        //TODO:: Please write your application code 
		
		transmitString("\nin loop");
		
		ipwm=0;
		
		ipwm++;
		OCR0 = ipwm;
		
		if(ipwm == 255)
		{
			transmitString("\nipwm=255");
			PORTD = 0xFF;
			ipwm = 0;
		}
    }
	return 0;            // Standard Return Code
}

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Tue. Dec 16, 2014 - 06:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/*
 * PWM_TLC5940NT.c
 *
 * Created: 12/12/2014 4:53:16 PM
 *  Author: Dev5
 */ 

#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdio.h>


ISR(TIMER1_OVF_vect)
{
	static unsigned int ipwm;
	ipwm=0;
	
	ipwm++;
	OCR1A = ipwm;
	
	if(ipwm == 65535)
	{
		PORTD = 0xFF;
		ipwm = 0;
	}
}

int main(void)
{
	DDRB = (1<<PB5)|(1<<PB7)|(1<<PB4);
	PORTB |= (1<<PB4);             // CS pin is not active
	
	SPCR = (1<<SPE)|(1<<MSTR);    // Enable SPI, Master, set clock rate fck/2
	SPSR |= (1<<SPI2X);
	//_delay_ms(100);

	
	DDRD |= (1<<PD4)|(1<<PD5);                    //OC1A, OC1B outputs
	TCCR1A = (2<<COM1A0)|(2<<COM1B0)|(2<<WGM10);  //PWM#14, active-high LEDs
	TCCR1B = (3<<WGM12)|(3<<CS10);                //PWM#14, div64
	ICR1 = F_CPU/64/100 - 1;                      //OVF @ 100Hz
	TIMSK |= (1<<TOV1);

	
	while(1)
    {
        //TODO:: Please write your application code 
    }
	return 0;            // Standard Return Code
}

Now i changed the code like above, and it is working like led's on then dimming and off. 

 

In my application  if i give voltage value through hyper terminal, brightness of LED is  based on that value. how to achieve this one. 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Tue. Dec 16, 2014 - 06:37 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sasi,

 

You appear to have just copy-pasted sequences of code without understanding them at all.

 

It is always best to start with an idea.    Develop the design with pencil and paper.     Then write down your specification in English.   And draw the basic program flowchart.

 

The subsequent coding is relatively trivial.    And if we see your spec,  we can help you with any steps that you have difficulty with.

 

Both of the "code listings" that you have posted have got serious flaws.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

okay david. Thanks david smiley  Now i will start from design with pencil and paper.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi all,

   good day.  I am struggling to figure out how to and where to  set the bit values for led brightness. In my application i want to setting bits for LED brightness control through hyper-terminal. I don't want to do LED ON in OVF. I read the data sheet in that after low the BLANK GS counter will increment and it reaches 4096 then BLANK is high. And the Brightness control is through 12 bits. How to set these bits and where to set it. Can you please help me where i am getting struck. It is something different and i like to learn.  My piece of code i have tried and circuit is,

 

/*
 * PWM_TLC5940NT.c
 *
 * Created: 12/12/2014 4:53:16 PM
 *  Author: Dev5
 */ 

#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdio.h>


void uart0_init(void)
{
	UCSRA=0x00;
	UCSRB=0x18;
	UCSRC=0x06;
	UBRRH=0x00;
	UBRRL=0x33;
}

unsigned char receiveByte( void )
{
	while(!(UCSRA & (1<<RXC))); 	// Wait for incoming data

	return UDR;
}

void transmitByte( unsigned char data )
{
	while ( !(UCSRA & (1<<UDRE)) )
	; 			                /* Wait for empty transmit buffer */
	UDR = data; 			        /* Start transmition */
}

void transmitString(char* string)
{
	while (*string)
	transmitByte(*string++);
}


int main(void)
{
	unsigned int GS_counter = 0, Data_counter = 0;
	
	DDRB = (1<<PB5)|(1<<PB7)|(1<<PB4);
	PORTB |= (1<<PB4);             // CS pin is not active
	
	SPCR = (1<<SPE)|(1<<MSTR);    // Enable SPI, Master, set clock rate fck/2
	SPSR |= (1<<SPI2X);
	_delay_ms(100);
	
	
	DDRD |= (1<<PD4)|(1<<PD5);                    //OC1A, OC1B outputs
	
	//Timer 1
	TCCR1A = (2<<COM1A0)|(2<<COM1B0)|(2<<WGM10);  //PWM#14, active-high LEDs
	TCCR1B = (3<<WGM12)|(3<<CS10);                //PWM#14, div64
	ICR1 = F_CPU/64/100 - 1;                      //OVF @ 100Hz
	TIMSK |= (1<<TOV1);
	
	
	uart0_init();
	_delay_ms(500);
	
	transmitString("\n\n\nin main");
	
	
	
	while(1)
    {
        //TODO:: Please write your application code 
		
		transmitString("\nin loop");
		
		PORTB &= ~(1<<PB4);    //BLANK low for GS counter increment
		
		GS_counter++;
		
		PORTD = 0x30;
		//OUTn switch ON  AND SET BIT VALUE FOR led BRIGHTNESS
		if(GSCLK_counter>4095)
		{
			PORTB |= (1<<PB4);     //BLANK high for OUTn switch OFF
			GS_counter = 0;	
			PORTD = 0x00;
		}
		
    }
	return 0;            // Standard Return Code
}

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

   good day.  I am struggling to figure out how to and where to  set the bit values for led brightness. In my application i want to setting bits for LED brightness control through hyper-terminal. I don't want to do LED ON in OVF. I read the data sheet in that after low the BLANK GS counter will increment and it reaches 4096 then BLANK is high. And the Brightness control is through 12 bits. How to set these bits and where to set it. Can you please help me where i am getting struck. It is something different and i like to learn.  My piece of code i have tried and circuit is,

From a brief glance at the TLC5940 data sheet,   you can control it via SPI interface.

 

The Timer commands were setting up regular PWM on the AVR pins,

I would just connect an LED (with resistor) to OC1A and OC1B pins.    Then experiment with different PWM  modes and different duty-cycles.

 

Then connect the TLC5940 to the SPI pins.    And send commands to the TLC5940 that control the light form LEDs connected to OUT0 .. OUT15.

 

From a learning point of view,    buying a $12 UNO-clone would mean that you have Arduino libraries and example code for PWM,  TCL5940, ...  in fact pretty well every chip in existence.

 

You can run the examples,  verify the hardware operation.     Then re-invent the wheel by writing your own version in C to run on the bare-metal.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

   Good day.  

I would just connect an LED (with resistor) to OC1A and OC1B pins.    Then experiment with different PWM  modes and different duty-cycles.

 

Then connect the TLC5940 to the SPI pins.    And send commands to the TLC5940 that control the light form LEDs connected to OUT0 .. OUT15.

I connected LED(with resistor) to OC1A and OC1B pins and also i connected LED's to OUT pins also. In this case the  LED(with resistor) connected in OC1A and OC1B pins are less brightness and LED's connected in OUT  pins are very good brightness and it is not getting dim and off. always on. 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

ICR1 = F_CPU/64/100 - 1; 

F_CPU = 8 MHz and ICR1 = 1249 (max. count value). for checking i connected LED (with resistor) in OCR1A pin.  Here i am getting the output what i am expecting like slowly getting up to LED ON and getting down by LED OFF. same like that i want  in OUT  pins(TLC5940NT). But i am not getting it. What i am doing wrong. Can you please help me.  And i changed the code in while(1) is,

 

while(1)
    {
        //TODO:: Please write your application code 
		
		transmitString("\nin loop");
		
		PORTB &= ~(1<<PB4);    //BLANK low for GS counter increment
		
		for(GSCLK_counter=0; GSCLK_counter<1249; GSCLK_counter++)
		{
			OCR1A = GSCLK_counter;
			_delay_ms(15);
			PORTD = OCR1A;    // if i use  PORTB = GSCLK_counter, LED's are blinking continuously
		}
		_delay_ms(2000);
		
		for (GSCLK_counter=1249; GSCLK_counter>0; GSCLK_counter--) 
		{
			OCR1A = GSCLK_counter;
			_delay_ms(15);
			PORTD = OCR1A;    //if i use PORTB = GSCLK_counter, LED's are blinking continuously  
		}
		_delay_ms(1000);
	
    }
	return 0;            // Standard Return Code
}

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Wed. Dec 24, 2014 - 09:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just googled "TLC5940" and one of the first hits was http://www.google.co.uk/url?sa=t...

 

Which seems to be a very comprehensive tutorial.  "Demystifying the TLC5940" by Matthew T. Pandina

 

I really think that your best bet is always to start with an Arduino and then adapt the code for your eventual target.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good day. Yes David i will try in Arduino examples. 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN