# MCU frequency

Hi,

I am still on the beginners level and would be happy for an answer to the following:

How do I set the frequency on an AVR?

I read that the max operating frequency depends on the level of the supply voltage. But, of course, I should know or be able to set the frequency it is actually running at, e.g. considering batteries getting empty!?

Or is it not possible to set the frequency for general commands, but only for certain functions like delay functions using F_CPU!?

Hello,

You don’t get software control over the frequency, which is usually set by an external clock source. You have a few options you can set through programmable fuses (which you can’t change at run-time), such as using the internal RC clock or dividing the clock by 8, but otherwise, you have to set the clock speed by hardware.

For the delay functions you mention, they need to know how fast the processor is running, so you have to tell them; you can’t affect the actual speed using them.

- Jan

Just to make sure I get this right:

so if I am not using an external clock source and no voltage regulator, and my battery supply voltage decreases from, say, 5V to under 2.7V, for instance, the frequency the AVR works with starts decreasing as well. Hence, all delay functions I use will not work correctly any more, since F_CPU is a constant that is not correct any more.

Right?

Thanks!!

Wrong, probably. Generally, your clock source ought to be relatively constant independent of the supply voltage. “The frequency the AVR works with starts decreasing” is not a good phrasing; a better way to put it is that as the supply voltage falls, the maximum frequency at which a microcontroller is capable of operating decreases. If you supply a 20 MHz clock, it will typically keep running at 20 MHz, but as you drop the supply voltage on your MCU, it eventually won’t be capable of running at 20 MHz. This does not mean that the speed will magically get reduced to what is possible; rather, the system will begin failing in various ways.

So, usually, if you need operation from 2.7-5V, you need to pick a frequency that will work for the whole range. For most AVRs, that would make 10 MHz the maximum frequency you can use.

- Jan