timing question on ATTiny13 / ATTiny2313

Newbie here. I have been playing with the ATTiny13 and ATTiny2313 and have some questions and comments on using the _delay_ms function.

I am using the code from the Ghetto Programming posting on the Instructables web site. The “Hello World” program contains this line:

#define F_CPU 100000UL // Sets up the default speed for delay.h

I’ve looked around a bit but I haven’t found a good explanation of why this is one-hundred-thousand and not one-million. I’m using the internal oscillator.

Using the code from the Instructables site, I tried to change the ‘led off’ portion to three seconds by changing the _delay_ms() value to 3000, but the led turned back on faster than it should have. This behavior made me wonder if the value was too high based on something I had read that the _delay_ms code would act funny if the value used was too high

“The maximal possible delay is 262.14 ms / F_CPU in MHz.”

If I change the F_CPU value to one million, then I can use _delay_MS(3000) and it seems to stay off for 3 seconds. This is just a casual observation - no test equipment was used to confirm the actual length of time.

In fact, if I use the original _delay_ms() values with the F_CPU value of one million, both ‘led on’ and ‘led off’ occur for much longer times.

Is the F_CPU value supposed to represent the speed of the AVR chip or is it the oscillator frequency?

I’m editing the code and programming the chips with Programmer’s Notepad and I don’t get any errors.

Thank you.

As you have discovered, F_CPU doesn’t change the actual oscillator speed. The chip, by default, uses the 8MHz internal oscillator with the prescaler set to divide that by 8.

F_CPU only tells the libraries and various functions in your code how to calculate the proper delay time based on the physical clock frequency. The physical clock frequency can be changed by reprogramming the fuses. Be aware though - setting the fuses incorrectly can leave your chip in a non-running state.

Thank you for the reply.

I checked the data sheets for both the ATTiny2313 and the ATTiny13. The 2313’s internal oscillator defaults to 8 MHz and the ATTiny13’s internal oscillator defaults to 9.6 MHz. Both chips default to the prescaler set to divide by 8. So I guess if I want to keep things simple and just use the internal oscillator and leave the fuses alone, So for the ATTiny13, I should change F_CPU value to 1,200,000 to get the _delay_ms() as close as I can.