Electronic – avr-gcc _delay_ms timings are off; ISP to blame

avravr-gccavrdude

I just built an AVRISP with an old Arduino Nano (ATmega328p), and I tried to flash a simple blink program onto an ATmega328 (non-picopower). The first time I flashed the program, everything worked out great — the 1000ms on and 1000ms off interval were as I expected. However, I changed the off interval to 100ms, and suddenly the period was almost 9 seconds in total — about 8 seconds on and .8 seconds off. Flashing the original 1000ms on and off intervals resulted in an elongated period as well.

I am using the Makefile from a simple blink tutorial; the only thing I've changed is the port designation and MCU. Given that, I believe that the issue lies in my ISP… but what could cause an initial success, then repeated failure? If you need any more information (header or hex dump, etc.) then let me know and I'll be happy to oblige.

Best Answer

The problem is that your definition of F_CPU doesn't match the hardware. On MCUs fresh from Atmel the CKDIV8 fuse (which divides the CPU clock by 8) is programmed, so be sure to unprogram it if needed if you haven't already.