### Author Topic: why 500000ms = 8seg?  (Read 2584 times)

0 Members and 1 Guest are viewing this topic.

#### Roboteccan

• Jr. Member
• Posts: 20
##### why 500000ms = 8seg?
« on: October 27, 2010, 04:27:17 PM »
hi, this is my firt program and the 500000ms delay give me 8seg (500000ms=8seg)?

#include<avr/IO.h>
#include<avr/delay.h>

int main()

{

DDRC = 0xFF;

PORTC = 1;
_delay_ms(500000);

PORTC = 8;
_delay_ms(500000);
PORTC = 0;
}

#### TrickyNekro

• Contest Winner
• Supreme Robot
• Posts: 1,208
• Hardware and Firmware Designer
##### Re: why 500000ms = 8seg?
« Reply #1 on: October 27, 2010, 05:15:35 PM »
Crystal fail.....
Will also lead to bad if not none UART communication to the IC...

Check again the crystal you are using and the crystal you declared ;-)
You should be using a "faster" crystal....

And it's sec not seg ;-)
For whom the interrupts toll...

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #2 on: October 27, 2010, 06:09:39 PM »
but the axon microcontroller use crystal?

#### waltr

• Supreme Robot
• Posts: 1,944
##### Re: why 500000ms = 8seg?
« Reply #3 on: October 27, 2010, 08:51:10 PM »
Quote
_delay_ms(500000);
is 500000 a valid input value for the _delay_ms function?
Look at the source code and function prototype for the _delay_ms function to see what the variable is defined as.

#### TrickyNekro

• Contest Winner
• Supreme Robot
• Posts: 1,208
• Hardware and Firmware Designer
##### Re: why 500000ms = 8seg?
« Reply #4 on: October 27, 2010, 09:34:18 PM »
I guess 16MHz....

That should look like:

\$crystal = 16000000

If I remember right it's something like this....

The 16 is for crystal values and the zeros are for pointing out that's in the megahertz range....

1000 = kilo (3 zeros)
1000000 = mega (6 zeros)
For whom the interrupts toll...

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #5 on: October 28, 2010, 12:14:12 PM »
thanks for all the problem is solved

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #6 on: October 28, 2010, 01:40:38 PM »

• Supreme Robot
• Posts: 11,702
##### Re: why 500000ms = 8seg?
« Reply #7 on: October 28, 2010, 02:23:05 PM »
You are using AVRLib, right?

Quote
_delay_ms(500000);
is 500000 a valid input value for the _delay_ms function?
Look at the source code and function prototype for the _delay_ms function to see what the variable is defined as.
If I remember right, it only accepts 16 bit values. That means the highest time you can do is 65536, not 500000.

Do this and see if it works:
Code: [Select]
`for(i=0;i<10;i++)     delay_ms(50000);`

#### knossos

• Robot Overlord
• Posts: 278
##### Re: why 500000ms = 8seg?
« Reply #8 on: October 28, 2010, 03:34:33 PM »
If I remember right, it only accepts 16 bit values. That means the highest time you can do is 65536, not 500000.

216 = 65535 (65536 values including 0)
« Last Edit: October 28, 2010, 03:38:34 PM by knossos »
"Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light."

— Oscar Wilde

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #9 on: October 28, 2010, 07:17:44 PM »
I am using AVR Studio 4 and I put in the confuguration 16000000hz but it dont work because it give me 100000=27sec.

#### knossos

• Robot Overlord
• Posts: 278
##### Re: why 500000ms = 8seg?
« Reply #10 on: October 28, 2010, 07:26:44 PM »
I am using AVR Studio 4 and I put in the confuguration 16000000hz but it dont work because it give me 100000=27sec.

It's not about the frequency, its about the size of the variable passed to the function delay_ms().   Delay accepts a 16 bit unsigned integer.  That means it can be any number from 0 to 65535.  If you try to put in a larger number you get unexpected results.  The longest you can delay using delay_ms() is about 65 and a half seconds (65.535 seconds actually).  If you want to delay longer than that you can use multiple calls to delay or you can use a loop like Admin posted.
"Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light."

— Oscar Wilde

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #11 on: October 29, 2010, 10:23:07 AM »
this is my new program

#include<avr/IO.h>
#include<avr/delay.h>

int main()

{

DDRC = 0xFF;

PORTC = 1;
_delay_ms(65000);

PORTC = 0;

}

#### knossos

• Robot Overlord
• Posts: 278
##### Re: why 500000ms = 8seg?
« Reply #12 on: October 29, 2010, 10:42:47 AM »
Quote
Function Documentation
void _delay_ms    (    double     __ms     )

Perform a delay of __ms milliseconds, using _delay_loop_2().

The macro F_CPU is supposed to be defined to a constant defining the CPU clock frequency (in Hertz).

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

When the user request delay which exceed the maximum possible one, _delay_ms() provides a decreased resolution functionality. In this mode _delay_ms() will work with a resolution of 1/10 ms, providing delays up to 6.5535 seconds (independent from CPU frequency). The user will not be informed about decreased resolution.

Also

Quote
void _delay_loop_2    (    uint16_t     __count     )

Delay loop using a 16-bit counter __count, so up to 65536 iterations are possible. (The value 65536 would have to be passed as 0.) The loop executes four CPU cycles per iteration, not including the overhead the compiler requires to setup the counter register pair.

Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 milliseconds can be achieved.

Hope this helps some.
"Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light."

— Oscar Wilde

• Supreme Robot
• Posts: 11,702
##### Re: why 500000ms = 8seg?
« Reply #13 on: October 29, 2010, 12:06:32 PM »
hmmmm, thats exactly 1/8th what it should be.

It sounds like the Divide by 8 fuse is set.

Did you change any fuses? Are you using an ISP programmer or the bootloader? Do you have an ISP programmer?

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #14 on: October 29, 2010, 12:06:55 PM »
I put 64Mhz ATmega640 and all the times work very well 60000ms=1min 8000ms=8sec I dont know why it work, but it work   the axon microcontroller with 64Mhz work very well.

#### TrickyNekro

• Contest Winner
• Supreme Robot
• Posts: 1,208
• Hardware and Firmware Designer
##### Re: why 500000ms = 8seg?
« Reply #15 on: October 29, 2010, 01:05:56 PM »
Seems you messed some number here :-p

Anyhow... It probably happens what admin tells.... You need to set some internal fuses for the Atmega...
Best way you have is to get an Atmel ISP programmer...

Don't say that the Chinese guys won't work but nothing's like mama.... Better get an original ISP programmer ;-)
For whom the interrupts toll...

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #16 on: October 29, 2010, 02:17:31 PM »
I dont know what is the ISP, in the "GETTING STARTED The Bootloader" dont say nothing about ISP, I only download AVR studio 4 and the FBOOT v1.7 EXE and I configure AVR studio 4 with ATmega640 and 16000000hz.

• Supreme Robot
• Posts: 11,702
##### Re: why 500000ms = 8seg?
« Reply #17 on: October 29, 2010, 02:58:37 PM »
Basically there appears to be a fuse on your microcontroller that somehow got corrupted. This fuse causes your microcontroller to run 8x slower.

As a long term solution, you'll need an ISP programmer (between \$16 and \$36) to fix the fuse. Its very easy to fix.

(or it could be a mistake in your code somewhere, but less likely)

Its not actually a big deal so no need to panic, but if you feel you need more processing power in the future, let us know and we can help.

#### Roboteccan

• Jr. Member
• Posts: 20
##### Re: why 500000ms = 8seg?
« Reply #18 on: October 29, 2010, 06:18:15 PM »

• Supreme Robot
• Posts: 11,702
##### Re: why 500000ms = 8seg?
« Reply #19 on: October 29, 2010, 07:41:15 PM »
For a robot sumo, 2MHz is plenty fast already.

And ISP programmer is hardware, not software.

Many people recommend this one, although I've never used it before:
http://www.pololu.com/catalog/product/1300