ATMEGA168A - F_CPU warning

Bob picture Bob · Dec 19, 2015 · Viewed 9.7k times · Source

I have written the code below in order to make an ATMEGA168A blink a small led:

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

#define F_CPU 1000000UL

int main(void)
{
    DDRB = 0b00000010;
    PORTB = 0b00000000;

    while(1)
    {
        PORTB ^= 1 << 1;
        _delay_ms(1000);
    }
}

The compiler is giving me a warning as follows:

Warning     #warning "F_CPU not defined for <util/delay.h>"

Here is where this warning comes from (delay.h)

#ifndef F_CPU
/* prevent compiler error by supplying a default */
# warning "F_CPU not defined for <util/delay.h>"
# define F_CPU 1000000UL
#endif

What am I doing wrong here? Is my declaration incorrect?

Answer

donjuedo picture donjuedo · Dec 19, 2015

You need to define F_CPU before that inclusion. You could do it on the command line when compiling, or put it in your source. You defining this symbol is you letting the build system know how fast your particular CPU is running. (I.e., you don't get to change the actual speed this way).

For a discussion, see http://www.avrfreaks.net/forum/understanding-fcpu