1

im very new to Atmegas ant AVR Programming, so i hope you would forgive me this noob question: I have got this code, which runs perfectly at 1MHZ, but if i fuse the Atmega to internal 8 MHZ while changing the F_CPU Frequency to 8 MHZ as well, the LED is flashin way to fast, it seems that the C Compiler is ignoring my new Frequency. Could someone help me ;)?

#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/io.h>

int main(void)
{
  while(1)
  {
    //TODO:: Please write your application code
    /* set PC0 on PORTC (digital high) and delay for 500mS */
    PORTD &= ~_BV(PD5);
    _delay_ms(500);

    /*  PC0 on PORTC (digital 0) and delay for 500mS */
    PORTD |= _BV(PD5);
    _delay_ms(500);
  }
}

it is compiles with these commands:

avr-gcc -g -Os -mmcu=atmega8  -c test.c 
avr-gcc -g -mmcu=atmega8 -dF_CPU=8000000UL -o flash.elf test.o
avr-objcopy -j .text -j .data -O ihex flash.elf flash.hex

A BIG Thank YOU for your time and help ;) Delay.h: http://pastebin.com/wzppfma3

  • You've changed 2 things: CPU frequency and `F_CPU`, and you're assuming that the compiler is ignoring `F_CPU`. But it could also be that your CPU frequency is not what you think it is. – indiv Aug 13 '15 at 20:38
  • The compiler itself does certainly ignore the macro. This because it has not idea about clocks and the underlying system. If your delay-functions do not honour that define, you should check those. – too honest for this site Aug 13 '15 at 20:46
  • The CPU Frequency has to be 8MHZ because the LED is flashing approx. 8 times to fast, therefore i think the compiler is still in a 1MHZ Mode. The Fuses i used are LOW: 0xe4 High:0xd9 , so RC 8Mhz 6ck +64ms Startup time is selected. The Delay Function uses the F_CPU Constant: http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__util__delay_1gad22e7a36b80e2f917324dc43a425e9d3.html – user3321030 Aug 13 '15 at 20:48
  • @Olaf `_delay_ms()` is an inline function defined in the `` header, and its behavior is dependent on the value of the `F_CPU` macro. –  Aug 13 '15 at 20:56
  • @user3321030: Please repeat: "The compiler does not care about the system frequency!" – too honest for this site Aug 13 '15 at 20:57
  • @duskwuff: Thanks for the clarification. However, OP might check this, as that would explain the problems. – too honest for this site Aug 13 '15 at 20:58
  • What do you expect `-dF_CPU=8000000UL` to do? Didn't you confuse with `-DF_CPU=8000000UL` which would have to be used when compiling the source, not for linking. – too honest for this site Aug 13 '15 at 21:03
  • Jup it seems that i confused that, but also no change, the LED is still flashing at 8Hz :( "The compiler does not care about the system frequency!" "The compiler does not care about the system frequency!" :D – user3321030 Aug 13 '15 at 21:09
  • @user3321030 Apparently `_delay_ms()` is defined in ``. It would help if you added that definition to the question. – user3386109 Aug 13 '15 at 21:14
  • http://pastebin.com/wzppfma3 this is my delay.h Header file – user3321030 Aug 13 '15 at 21:22
  • 2
    You're exceeding the maximum delay `_delay_ms()` can give, at either clock frequency. – Dmitri Aug 13 '15 at 21:27
  • @Dmitri ok thanks, what is my max. Delay at 8 MHZ? – user3321030 Aug 13 '15 at 21:32
  • 2
    At 8MHz, max delay is about 32.768 ms – Dmitri Aug 13 '15 at 21:34
  • 1
    @user3386109 `` is part of the AVR toolchain headers. It's not part of this user's project. –  Aug 14 '15 at 04:12

2 Answers2

3

The problem is _delay_ms has a maximum ms value depending on your F_CPU frequency. According to the docs this maximum is:

262.14 ms / F_CPU in MHz

which works out to 32 ms max for your 8 MHz. So instead, write a loop like this to get a 500 ms delay:

for (uint8_t i=0; i<50; i++) _delay_ms(10);
bobasaurus
  • 129
  • 1
  • 9
  • 1
    Thank you :) That is the Solution ;) – user3321030 Aug 25 '15 at 21:45
  • 1
    According to the documentation `_delay_ms` switches to a lower resolution if you request delays bigger than the maximum and then supports delays up to 6. something seconds independent of CPU clock frequency... https://www.microchip.com/webdoc/AVRLibcReferenceManual/group__util__delay_1gad22e7a36b80e2f917324dc43a425e9d3.html – Julian F. Weinert Jul 17 '19 at 12:47
  • @Julian That must be a new feature since I posted this 4 years ago, though it looks like it's limited to 6.5535 sec max. – bobasaurus Jul 18 '19 at 16:01
-1

You should change internal fuse of AVR to work with 8MHz, writing it on the code only is not enough, set lfuse to 0xE4 to make AVR work at 8MHz

Nasr
  • 2,482
  • 4
  • 26
  • 31