Society of Robots - Robot Forum

Software => Software => Topic started by: Roboteccan on October 27, 2010, 04:27:17 PM

Title: why 500000ms = 8seg?
Post by: Roboteccan 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;
}

please help, me I think that 8seg=8000ms thanks.
Title: Re: why 500000ms = 8seg?
Post by: TrickyNekro 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 ;-)
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan on October 27, 2010, 06:09:39 PM
but the axon microcontroller use crystal?
Title: Re: why 500000ms = 8seg?
Post by: waltr 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.
Title: Re: why 500000ms = 8seg?
Post by: TrickyNekro 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)
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan on October 28, 2010, 12:14:12 PM
thanks for all the problem is solved :)
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan on October 28, 2010, 01:40:38 PM
I put in the configuration 16000000hz but 100000ms=27sec (_delay_ms(100000)) please help me, thanks.
Title: Re: why 500000ms = 8seg?
Post by: Admin 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);
Title: Re: why 500000ms = 8seg?
Post by: knossos 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)
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan 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.
Title: Re: why 500000ms = 8seg?
Post by: knossos 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.
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan 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;

}

but it dont work because 65000ms give me 16sec (65000ms=16sec), please help me   ???
Title: Re: why 500000ms = 8seg?
Post by: knossos 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.
Title: Re: why 500000ms = 8seg?
Post by: Admin on October 29, 2010, 12:06:32 PM
but it dont work because 65000ms give me 16sec (65000ms=16sec), please help me   ???
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?
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan 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.
Title: Re: why 500000ms = 8seg?
Post by: TrickyNekro 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 ;-)
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan 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.  ;D
Title: Re: why 500000ms = 8seg?
Post by: Admin 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.
Title: Re: why 500000ms = 8seg?
Post by: Roboteccan on October 29, 2010, 06:18:15 PM
Please I want the link to download the ISP Programmer because I need that my robotSumo will be very fast, if my axon microcontroller is more slow to processing that is a big disadvantage, please help me.
Title: Re: why 500000ms = 8seg?
Post by: Admin 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 (http://www.pololu.com/catalog/product/1300)