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.

Can't make a very simple LED fading work

Status
Not open for further replies.

eebhoi01

Advanced Member level 4
Joined
Feb 22, 2012
Messages
116
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,347
Hi,

I really can't understand why I don't have any output. I'm kinda frustrated.

Okay, I am planning to do a very simple LED fading using atmega328p via Atmel Studio. Below is my code, I used the 8-bit timer using TCCR0A control register. I also set WGM02 in TCCR0B so OCR0A will be the TOP value of the TCNT. Can anyone help me why I am not getting any output? I already defined the DDR but still no luck. Any help is highly appreciated. Thanks in advance.

Code:
#define F_CPU 16000000UL

#include <[COLOR="#000000"]avr/io.h>[/COLOR]
#include <[COLOR="#000000"]util/delay.h>[/COLOR]

void initPWM()
{
	TCCR0A |= 1,,WGM01 | 1,,WGM00 | 1,,COM0A0;
	TCCR0B |= 1,,WGM02;
	DDRB |=	1,,PINB1;	
}

void setPWMOut(int dutycycle)	
{
	OCR0A = dutycycle;
}


int main (void)
{
	int	brightness = 0;
	int fadeAmount = 5;
	initPWM();
		
	while(1)
	{
		 
		 setPWMOut(brightness);
		 brightness = brightness + fadeAmount;
	
		 if (brightness <= 0 || brightness >= 255)
			{
			fadeAmount = -fadeAmount;
			}
		_delay_ms(30);
	}
}
 
Last edited by a moderator:

Hi,

What is the varable defintion for "brightness".

I assume it is "uint8". (corrected: before it was "int8")
If so, then the value range is 0...255.This means it never can be smaller than 0 or bigger than 255.

The same is with "fadeAmount".

--> I recommend to define every variable unambigously.
Not only for us, but for you, too.
And I know that math and the results may be the same ... but not always. Some compilers make difference, sometimes it depends on the used microcontroller (bit width).
Without clear definitions porting code may be cause problems.

***
Generally.
Please add some comments to your code, not only for us, but fo you, too.
I assume when you read the code after some time you don't know what prescaler you set, what counter frequency you exoect and what PWM frequency you expect. (Just as example)
It's hard to guess your ideas ... thus it's hard to verify if the code matches your ideas.

Klaus
 
Last edited:

Hi,

What is the varable defintion for "brightness".

I assume it is "int8".
If so, then the value range is 0...255.This means it never can be smaller than 0 or bigger than 255.

The same is with "fadeAmount".

--> I recommend to define every variable unambigously.
Not only for us, but for you, too.
And I know that math and the results may be the same ... but not always. Some compilers make difference, sometimes it depends on the used microcontroller (bit width).
Without clear definitions porting code may be cause problems.

***
Generally.
Please add some comments to your code, not only for us, but fo you, too.
I assume when you read the code after some time you don't know what prescaler you set, what counter frequency you exoect and what PWM frequency you expect. (Just as example)
It's hard to guess your ideas ... thus it's hard to verify if the code matches your ideas.

Klaus

Hi, thank you for your response.

I first declare brightness and fadeAmount variable as "int" but i changed it to int_8. Still there is no output.
 

Hi,

Sadly I made a mistake, too, in my post above. I corrected it. See post#2.

I don't know how the compiler/microcontroller treats it.
In assembler one would use the "carry" flag.

If you use signed "int8", the range is -128 ...0...+127. It can never be 255 or higher.
If you use "uint8", then the range is 0...255, it can never be <0.

--> Please check the assembler code, how the compiler translates your code.

Klaus
 

Thank you. But I still not able to get any output. I've read the data sheet so many times, sigh... :(
 

I'm not familiar with Atmel Studio, I don't think it runs under Linux but you could try changing the limit check to bracket the individual checks like this:
if ((brightness <= 0) || (brightness >= 255))

If that doesn't work, check that "setPWMOut(0)" really turns the LED off and "setPWMOut(255)" turns it fully on. I assume you have a suitable resistor in series with the LED.

Brian.
 

I can't see what you mean by '..not getting any output'. How are you determining this - using a scope, a DMM or what?
Are you sure that the code is running at all?
What debugging steps have you taken - have you single stepped your way though the code to make sure the values are being updated as required?
What is the circuit you are using? Does the LED work at all? Have you tried a 'flash the LED" style program with the circuit?
Not all problems are with the code....
Susan
 

I strongly recommend to consider using some tool to configure the microcontrollers registers settings, there are a lot of interactions among them for which you may have been overlooked.

Have a look at the ATMEL START (click on the "Start new project" button ), there you can select a device, define clocks, add peripherals and at the end you get the code output in C language, fully commented.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top