Understanding F_CPU

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

Dear Friends,

Its been a month since I started learning AVR. Originally wanted to make a LED Cube, then realizing there are basic lessons I have to understand then finally settled to make a propeller clock misunderstanding myself about the knowledge I had.

Sooner got into the task to "trying" to understand timers and interrupts. Then navigated to external clock sources. Meanwhile today I realized I have not moved anywhere.

I have a very primitive question or rather a stupid question. Revisited the LED blinking example to clarify some of my understandings.

To start with below is the program i wrote :

#include 
#include 

int main(void)
{
	//
	//DataDirection Setting the first pin of Port B to output = 1;
	//by default all the pins are in input mode = 0;
	//DDRB |= 1 << PINB0;
	//PORTB |= 1 << PINB0;
	int i;
	DDRB = 0b00000010;
	PORTB = 0b00000000;
	
	while(1)
	{
		//Setting the first pin of Port B to 1 = 5v;
		PORTB ^= 1 << 1;
		_delay_ms(1000);
	}	
}

As you see I am trying to blink an LED connected to PB1 of my ATmega32 every one second. And my MakeFile was having an F_CPU reflecting "F_CPU = 8000000". When I ran the program I notice the LED was blinking every 8 seconds rather should have been blinking every 1 second. With little common sense I figured this out by looking at the MakeFile where the F_CPU was set to 8 Mhz and changed it to 1MHZ. After this the program worked as expected.

Given I have made a wrong entry to the F_CPU, I simply do not understand how the timing varies between two clocks. Logically speaking 1000 milliseconds is always 1000 milliseconds right? Does the implementation of _delay_ms method has got anything to do with the F_CPU? Please enlighten me.

Thanks and Regards,

#0K Srinivasan.

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

F_CPU is only a way for you to tell _delay_ms() how fast your MCU is running, it doesn't in any way change the speed of the MCU. So your mega32 is actually running at 1MHz (which is the default). Have a look in the data sheet under "System Clock and Clock Options".

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

F_CPU does not make your AVR run at that speed, it only tells the compiler that it is running at that speed. If you lie to the compiler, you will not get the results you want. It is up to you to ensure that the CPU frequency is correct (by setting the proper fuse bits, supplying the proper crystal/clock source, etc.).

Edit: I need to type faster ;)

Regards,
Steve A.

The Board helps those that help themselves.

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

Hello Snigelen and Steve,

Thank you for all your quick replies. It was really quick friends. Good I now get the point. I have a 20 MHZ crystal (the one that comes in a metal canister) and I want to hook it up with my ATmega32 board. Can someone tell me

* Capacitors needed
* Fuse bits.
* It should be connected to XTAl0 and XTAL1 rite?

I am trying to learn this but somehow or the other way round there is not a tutorial aimed at beginners. Please apologize me if there is a rule that beginners should neither touch nor think about clocks before certain things are learned. I am basically trying to learn by the experimenting way.

Thanks and Regards,

#0K Srinivasan.

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

The datasheet for the crystal should recommend capacitors but maybe try 12pF.

How the fuses are presented to you will depend on the programmer and PC based programming software you use. Good software should have easy to select options to pick full swing high speed crystal.

Each leg of the crystal connects to one of either XTAL1 or XTAL2 and each leg then has one of those capacitors connected to ground.

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

Quote:

Good I now get the point. I have a 20 MHZ crystal (the one that comes in a metal canister) and I want to hook it up with my ATmega32 board.

Ummm--Do you expect to get good, reliable results overclocking your AVR by 25% over rated max speed? Especially as an apparent neophyte?
Quote:

Can someone tell me

* Capacitors needed
* Fuse bits.
* It should be connected to XTAl0 and XTAL1 rite?

I am trying to learn this but somehow or the other way round there is not a tutorial aimed at beginners.


??? Have you looked at the datasheet chapter on System Clock and Clock Options, and the specifically the Crystal Oscillator section? What does it say there?

Re capacitors:

Quote:
The optimal value of the capacitors depends on the crystal or resonator in use, the amount of stray capacitance, and the electromagnetic noise of the environment. Some initial guidelines for choosing capacitors for use with crystals are given in...

Re fuse bits:
Table xxx. Crystal Oscillator Operating Modes

Re connection:

Quote:
XTAL1 and XTAL2 are input and output, respectively, of an inverting amplifier which can be configured for use as an On-chip Oscillator, as shown in Figure xxx. Either a quartz crystal or ...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

As everyone has told you, you need to check the datasheet of the microcontroller you are using, if you are using a 20Mhz crystal you have to tell to the compiler

#define F_CPU 20000000UL

Check fuses for the crystal selection and there is also a fuse named CKDIV8 which divides your crystal frequency by 8 so your actual frequency would be the crystal frequency divided by 8, this fuse is on newer devices MEGA32 doesn´t have this fuse.
But if you are trying to blink a LED you don’t need an external crystal you can use the internal RC oscillator but you also need to check the fuses and the datasheet.

Entra a www.mecatronicamexicana.blogspot... y ve lo que los estudiantes Mexicanos de Mecatronica hacen y conoce un poco más acerca de Mecatronica

Last Edited: Fri. Aug 24, 2012 - 08:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lams_007 wrote:
check the datasheet of the microcontroller
Why don't you have a look for your self? You will find that
Quote:
there is also a fuse named CKDIV8
there is no such fuse in the oldies, like mega32. :twisted:

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

Oooo my mistake I´ll fix that thanks :-D

Entra a www.mecatronicamexicana.blogspot... y ve lo que los estudiantes Mexicanos de Mecatronica hacen y conoce un poco más acerca de Mecatronica