Сообщение от Kopranych
|
AVR Studio 4 виснет при отладке любого проекта функция _delay_ms()!
|
Подпрограмма _delay_ms использует операции с плавающей арифметикой. Возможно, у вас она (арифметика) не подключена, возможно, вы не указали частоту проца. Вариантов много.
Цитата из avr-libc-user-manual (c.296)
They are meant as convenience functions where actual time values can be specified rather than a number of cycles to wait for. The idea behind is that compile-time constant expressions will be eliminated by compiler optimization so floating-point expressions can be used to calculate the number of delay cycles needed based on the CPU frequency passed by the macro F_CPU.
Ниже приведен исходный текст _delay_ms
#ifndef __OPTIMIZE__
# warning "Compiler optimizations disabled; functions from ‹util/delay.h› won't work as designed"
#endif
/*
Perform a delay of \c __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.
*/
void _delay_ms(double __ms)
{
uint16_t __ticks;
double __tmp = ((F_CPU) / 4e3) * __ms;
if(__tmp ‹ 1.0)
__ticks = 1;
else if(__tmp › 65535)
{
__ticks = (uint16_t) (__ms * 10.0); //__ticks = requested delay in 1/10 ms
while(__ticks)
{
_delay_loop_2(((F_CPU) / 4e3) / 10); // wait 1/10 ms
__ticks --;
}
return;
}
else __ticks = (uint16_t)__tmp;
_delay_loop_2(__ticks);
}
На мой взгляд лучше сразу использовать _delay_loop_2(__ticks); Вот исходник
/*
Delay loop using a 16-bit counter \c __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.
*/
void _delay_loop_2(uint16_t __count)
{
__asm__ volatile
(
"1: sbiw %0,1" "\n\t"
"brne 1b"
: "=w" (__count)
: "0" (__count)
);
}