[AVR] delay_ms in ASF wizard of AVR Studio6.2 is giving inaccurate delay

Status
Not open for further replies.

Mandar Joshi

Member level 2
Joined
Mar 4, 2015
Messages
50
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Visit site
Activity points
445
I am using STK600(ATmega2560) and ASF wizard for my project. I have written simple code for LED blinking. But whenever I use delay_ms from ASF it gives inaccurate delay.
As I am using ASF, I haven't defined F_CPU manually and default clock for ATmega2560 is 8MHz as per datasheet. So direct parameter passing to delay_ms() should give appropriate delay but it doesn't.

Fuse values are

Extended 0xFF
High 0x99
Low 0xE2


Is anything wrong with my fuse settings? I have another code for uart but it works fine with same fuse settings and it confirms default clock is 8MHz. So why I am getting inaccurate delays?

My code for LED blink is as follows:
Code:
#include <asf.h>

int main (void)
{
	// Insert system clock initialization code here (sysclk_init()).

	board_init();

	DDRB=0xff;
	
	while(1)
   {
	 PORTB=0x00;
	 
	 delay_ms(2500);
	 
	 PORTB=0xff;
	 
	 delay_ms(2500);  
   }	
	
	// Insert application code here, after the board has been initialized.
}

- - - Updated - - -

How can I find what F_CPU it is taking by default? I don't have any debugger.
 

it seems your fuses are OK (internal 8Mz without DIV8) and I can guess your led takes about 8 times slower to blink (instead of 2.5s, it takes 20s) if so, you're right, the asf routine isn't getting the right value for sysclk_get_cpu_hz or something related...

sadly i can't help you more, but maybe you can try something...

in the old days we used <util/delay.h> to make quick delays, that avr-libc library needed a F_CPU constant, something similar to:

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

void delay_ms(n){
   while(n--) _delay_ms(1); // _delay_ms can't take big arguments...
}

int main (void)
{

        //	board_init();

	DDRB=0xff;
	
	while(1)
   {
	 PORTB=0x00;
	 
	 delay_ms(2500);
	 
	 PORTB=0xff;
	 
	 delay_ms(2500);  
   }	

}

I'm sure this should work but we are not using that ASF thingie that maybe you are further using...
 

Klaus, inaccurate means as Kurenai_ryu said if I use delay_s() and pass 24 to it LED blinks at 3 seconds. But then I tried by changing CKDIV8 fuse then it works proper for few delays and again fails if I increase delay parameters.

There has to be some kind of limit to parameters we pass to delay functions. But unable to figure out what's the problem is?

And as I said earlier when CKDIV8 is not programmed clock is 8MHz, so after programming CKDIV8 it is 1MHz which I have confirmed from another small uart code.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…