+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Full Member level 1
    Points: 1,859, Level: 10
    Achievements:
    7 years registered

    Join Date
    Feb 2012
    Posts
    116
    Helped
    0 / 0
    Points
    1,859
    Level
    10

    Can't make a very simple LED fading work

    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 <avr/io.h>
    #include <util/delay.h>
    
    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 KlausST; 6th March 2019 at 07:31. Reason: Making include files visible

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 72,643, Level: 65
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,765
    Helped
    3370 / 3370
    Points
    72,643
    Level
    65

    Re: Can't make a very simple LED fading work

    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 by KlausST; 6th March 2019 at 08:03. Reason: Int8 --> uint8
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  3. #3
    Full Member level 1
    Points: 1,859, Level: 10
    Achievements:
    7 years registered

    Join Date
    Feb 2012
    Posts
    116
    Helped
    0 / 0
    Points
    1,859
    Level
    10

    Re: Can't make a very simple LED fading work

    Quote Originally Posted by KlausST View Post
    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.



    •   AltAdvertisment

        
       

  4. #4
    Super Moderator
    Points: 72,643, Level: 65
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,765
    Helped
    3370 / 3370
    Points
    72,643
    Level
    65

    Re: Can't make a very simple LED fading work

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  5. #5
    Full Member level 1
    Points: 1,859, Level: 10
    Achievements:
    7 years registered

    Join Date
    Feb 2012
    Posts
    116
    Helped
    0 / 0
    Points
    1,859
    Level
    10

    Re: Can't make a very simple LED fading work

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



  6. #6
    Super Moderator
    Points: 78,172, Level: 68
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,792
    Helped
    4272 / 4272
    Points
    78,172
    Level
    68

    Re: Can't make a very simple LED fading work

    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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  7. #7
    Advanced Member level 4
    Points: 7,384, Level: 20

    Join Date
    Jan 2015
    Posts
    1,037
    Helped
    326 / 326
    Points
    7,384
    Level
    20

    Re: Can't make a very simple LED fading work

    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



    •   AltAdvertisment

        
       

  8. #8
    Super Moderator
    Points: 28,819, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,588
    Helped
    1087 / 1087
    Points
    28,819
    Level
    41
    Blog Entries
    6

    Re: Can't make a very simple LED fading work

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--