Solved: Blinking Led

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

Hi!

It's my first forum. I'll try to write very clear, because I don't speak english, I speak Spanish.

 

I am beginning with AVR microcontrollers, specially with Atmega8. I am using Eclipse Neon to do it. And I was making a program to blink a Led, the typicall program.

But I have a problem: In my program I made a delay of 1000 ms (1 s), so the led should blink every 1 second. But in fact it  doesn't every 1 second. The Led blink more than 1 second.

I think, maybe is about the frecuency of work of the atmega8 and my computer? I don't Know exactly.

 

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

int main()
{
    DDRD = 0b00000010; //PD0 as input, PD1 as output
    //DDRD = 0x02;

    while (1)
    {
        PORTD = 0x00; //Led OFF
        _delay_ms(1000);
        PORTD = 0x02; //Led ON
        _delay_ms(1000);

    }
    return 0;
}

 

 

 

Thanks. Maybe It's a silly question, but I want to learn.

 

Thanks!

This topic has a solution.

Angela SanMer
Beginner Electronic Research

Last Edited: Thu. Apr 19, 2018 - 07:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Your code turns the LED off for 1 second and on for 1 second.

The LED should blink once every 2 seconds.

 

Also, you should   #define F_CPU  before #include <util/delay.h> 

 

For example:

#define F_CPU 16000000UL

 

The UL on the end of the number specifies unsigned long.

 

 

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

The _delay_ms() function should be fairly accurate, but the AVR has no inherent concept of "time". It can only compare your 1 second to something else, usually the clock frequency of the AVR. That is what F_CPU is used for. (Simplified) It tells the AVR that is must count to 16 million to delay one second.

 

In the background in the _delay_ms() function there are some calculations going on. To have the correct delay, these calculations have to be done when the program is being compiled, and not while the program is running on your AVR. If the AVR has to do the counting and the calculations at the same time, it takes much to long.

Therefore it is neccasarry to turn on optimisaton during compilation of your program.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

As others have implied this:

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

should be something like this:

#include <avr/io.h>
#define F_CPU 123456UL
#include <util/delay.h>

replace the 123456 in that with the speed in Hz you know the CPU to be running at. So if it is 8MHz use:

#define F_CPU 8000000UL

or if it were 3.6864MHz then use:

#define F_CPU 3686400UL

etc.

 

The point here is that the F_CPU definition must be made BEFORE the line that includes util/delay.h

 

By default most AVRs are supplied running at (roughly) 1MHz but if you have changed fuses to alter the clock source only you now know how fast it is running. The C code (for things like delay.h) needs to be told and that's what you use F_CPU for.

 

BTW don't be mistaken (as some are) in thinking that simply changing F_CPU in the code changes the speed of the AVR. All F_CPU exists for is to give you the opportunity to let the C know what speed you have arranged for the AVR to run at - you can't change that just by editing a line of C.

Last Edited: Mon. Apr 16, 2018 - 08:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks to all for the Help!

Yeah! I understood the meaning of MCU Frecuency and the use of  F_CPU.

My program work ok.

Thanks!!

Angela SanMer
Beginner Electronic Research

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

Excellent!

 

Now please mark the solution - see Tip #5

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...