#define F_CPU and data types

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

Hi all,

I am new to embedded programming and have some bit confusions,

 

1. I made to connect 16 MHz xtal to Atmega16 to operate it at 16 MHz now in some example programs they use,

 

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

and in some other examples they write,

#define F_CPU 16000000UL

only,

 

before including delay.h as,

#include <util/delay.h>

now my question is that it it make some difference if I use the

#ifndef #define #endif

or just use only

#define

before including delay.h, because my LED is blinking the same by using the both.

 

2. What is uint8_t ? I think it is 8 bit integer (unsigned), now what type of data types are supported like this, please give me a list of all such data types supported by Atmel studio, is data type 'byte' is also suupported?

 

3. Please explain me in depth what is F_CPU and why it is used, if I use a xtal of freq. = x Hz then I will write in my program F_CPU = xUL to tell the compiler that mu MCU will be working on x Hz, am I write??

 

and what is UL in F_CPU i think it is Unsigned Long, what are the other options ?? Please tell me

 

Please reply me soon

 

Thanks in advance

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

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

and in some other examples they write,

#define F_CPU 16000000UL

only,

 

before including delay.h as,

#include <util/delay.h>

now my question is that it it make some difference if I use the

#ifndef #define #endif

or just use only

#define

You can just use #define F_CPU 16000000UL

 

Inorder to avoid duplicate definition, they have defined as follows. If F_CPU is not defined previously, define here.

 

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

3. Please explain me in depth what is F_CPU and why it is used, if I use a xtal of freq. = x Hz then I will write in my program F_CPU = xUL to tell the compiler that mu MCU will be working on x Hz, am I write??

http://www.nongnu.org/avr-libc/u...

 

what is UL in F_CPU i think it is Unsigned Long

Yes, It is unsigned long

 

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

ananyaavr wrote:
I am new to embedded programming 

One of the distinctive features of embedded microcontroller programming at this level is that you are working right at the border between hardware and software - so you need at least a working understanding of electronics to understand the context in which your code operates.

 

In particular, you must be able to read and understand the datasheet for the microcontroller in question.

 

The Atmega16 datasheet is here: http://www.atmel.com/Images/doc2...

 

Your 3rd question really needs to be answered first:

 

ananyaavr wrote:
what is F_CPU and why it is used, if I use a xtal of freq. = x Hz then I will write in my program F_CPU = xUL to tell the compiler that mu MCU will be working on x Hz, am I write??

Yes, it is the frequency at which the CPU operates; that is, the speed at which it executes instructions.

 

But it's not just the CPU:  This can give the timing reference for everything else on the chip - including timers, UART Baud rate, etc.

 

This is particularly import for things like the delay functions in delay.h - as they rely upon the time taken to execute each instruction in order to compute their delay duration in second.

 

Quote:
1. I made to connect 16 MHz xtal to Atmega16 to operate it at 16 MHz

The crystal frequency is not necessarily the speed at which the chip operates!

 

Look at Figure 2 on page 3 of the datasheet - note that the oscillator output is connected to the 'MCU CTRL & TIMING' block before it gets passed to anything else on the chip.

 

Figure 11 on page 24 at the start of the chapter "System Clock and Clock Options" goes into the full detail of all the timing options available on the chip; in particular, note:

 

ATmega16(L) Datasheet wrote:
Default Clock Source
The device is shipped with CKSEL = “0001” and SUT = “10”. The default clock source setting is therefore the 1 MHz Internal RC Oscillator ...

So simply connecting a 16 MHz crystal is not enough to get the chip running at 16 MHz!

 

Quote:
now in some example programs they use,

 

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

They are trying to be helpful (sic) by providing a default definition in case you forgot to provide a definition of your particular F_CPU.

 

However, this is unlikely to be really helpful - as the likelihood that the default is correct for your particular system is quite small (for the reasons stated above).

 

It would be more helpful to do something like

 

#ifndef F_CPU
#error "F_CPU must be defined as the CPU Clock frequency (which is not necessarily equal to the crystal frequency)"
#endif

 

Which would give you an explicit & more informative error if you did forget to give your definition for F_CPU.

 

(This was discussed here recently, but I can't seem to find it now)

 

Quote:
2. What is uint8_t ?

This is standard 'C' - it has nothing specifically to do with microcontrollers or embedded programming.

 

These standard typedefs were introduced by C99: http://en.wikibooks.org/wiki/C_P...

 

For background, see: http://c-faq.com/decl/inttypes.html and http://c-faq.com/decl/int16.html 

 

Quote:
what is UL in F_CPU i think it is Unsigned Long

Yes, it is.

 

Again, This is standard 'C' - nothing specifically to do with microcontrollers/embedded.

 

'C' generally assumes that everything is an int unless specifically told otherwise. An int is unlikely to be large enough to hold a big value like 16000000 - so specifying it as UL ensures that it will be treated as Unsigned Long

 

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...
Last Edited: Tue. Apr 28, 2015 - 09:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Just to say that for F_CPU you are far better passing it in project wide as a -D rather than #define'ing it within one .c. I guess it's OK to #define in a .h but probably one that all the .c files #include in turn.

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

Thanks to you all,

 

I just want to know that If I am using an xtal of freq. say x Hz what should I define as the F_CPU to tell the compiler that my MCU (and other things inside the chip) will now be working on x Hz

 

Please reply me soon

 

Thanks in advance

 

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

1.   the fuses should be set for a crystal.

2.   the CLKDIV8 fuse should be cleared.

3.   you read the printed value stamped on the crystal.   This is F_CPU

 

e.g. for a 16MHz crystal:

 

If you use AS6,   add F_CPU=16000000uL to the Project->Toolchain->C_Compiler->Symbols   (All Configurations)

If you use a Makefile,   you add -DF_CPU=16000000uL to the $(CFLAGS)

 

Personally,   I don't like putting F_CPU in the C or H files.    I definitely do not like the #ifndef without #Error approach.

 

David.

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

ananyaavr wrote:
I just want to know that If I am using an xtal of freq. say x Hz what should I define as the F_CPU to tell the compiler that my MCU (and other things inside the chip) will now be working on x Hz

Have you studied the datasheet sections I pointed out? They give you the answers to these questions!

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I just want to know that If I am using an xtal of freq. say x Hz what should I define as the F_CPU to tell the compiler

Why would you tell it anything other than the actual frequency the CPU is running at?

Regards,
Steve A.

The Board helps those that help themselves.

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

Koshchi wrote:
Why would you tell it anything other than the actual frequency the CPU is running at?

As a late update (the thread was woken by a hijack), note that "the actual frequency the CPU is running at" is not necessarily the same as the Crystal frequency ...

 

See post #3.

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...
Last Edited: Wed. Nov 6, 2019 - 02:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's a good question and not really my intention.  It seems to me that these should run at the same frequency, given the code I've seen.

 

Just wanted to be sure ATMEL experience persons would not have their transmitter purposely set to 2X the receiver.

 

Marlon

Marlon E. Robinson

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

Did you mean to post that in the other thread?

 

https://www.avrfreaks.net/forum/...

 

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...